1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
/*
* Copyright (c) 2008 - 2009
* Jonathan Schleifer <js@webkeks.org>
*
* All rights reserved.
*
* This file is part of libobjfw. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE included in
* the packaging of this file.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#import "OFObject.h"
#import "OFExceptions.h"
#define CATCH_EXCEPTION(code, exception) \
@try { \
code; \
\
puts("NOT CAUGHT!"); \
return 1; \
} @catch (exception *e) { \
puts("CAUGHT! Error string was:"); \
puts([[e string] cString]); \
puts("Resuming..."); \
}
int
main()
{
OFObject *obj = [[OFObject alloc] init];
void *p, *q, *r;
/* Test freeing memory not allocated by obj */
puts("Freeing memory not allocated by object (should throw an "
"exception)...");
CATCH_EXCEPTION([obj freeMemory: NULL],
OFMemoryNotPartOfObjectException)
/* Test allocating memory */
puts("Allocating memory through object...");
p = [obj allocMemoryWithSize: 4096];
puts("Allocated 4096 bytes.");
/* Test freeing the just allocated memory */
puts("Freeing just allocated memory...");
[obj freeMemory: p];
puts("Free'd.");
/* It shouldn't be recognized as part of our obj anymore */
puts("Trying to free it again (should throw an exception)...");
CATCH_EXCEPTION([obj freeMemory: p], OFMemoryNotPartOfObjectException)
/* Test multiple memory chunks */
puts("Allocating 3 chunks of memory...");
p = [obj allocMemoryWithSize: 4096];
q = [obj allocMemoryWithSize: 4096];
r = [obj allocMemoryWithSize: 4096];
puts("Allocated 3 * 4096 bytes.");
/* Free them */
puts("Now freeing them...");
[obj freeMemory: p];
[obj freeMemory: q];
[obj freeMemory: r];
puts("Freed them all.");
/* Try to free again */
puts("Now trying to free them again...");
CATCH_EXCEPTION([obj freeMemory: p], OFMemoryNotPartOfObjectException)
CATCH_EXCEPTION([obj freeMemory: q], OFMemoryNotPartOfObjectException)
CATCH_EXCEPTION([obj freeMemory: r], OFMemoryNotPartOfObjectException)
puts("Got all 3!");
puts("Trying to allocate more memory than possible...");
CATCH_EXCEPTION(p = [obj allocMemoryWithSize: SIZE_MAX],
OFOutOfMemoryException)
puts("Allocating 1 byte...");
p = [obj allocMemoryWithSize: 1];
puts("Trying to resize that 1 byte to more than possible...");
CATCH_EXCEPTION(p = [obj resizeMemory: p
toSize: SIZE_MAX],
OFOutOfMemoryException)
puts("Trying to resize NULL to 1024 bytes...");
p = [obj resizeMemory: NULL
toSize: 1024];
[obj freeMemory: p];
puts("Trying to resize memory that is not part of object...");
CATCH_EXCEPTION(p = [obj resizeMemory: (void*)1
toSize: 1024],
OFMemoryNotPartOfObjectException)
/* TODO: Test if freeing object frees all memory */
return 0;
}
|