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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
#import "OFObject.h"
#import "OFExceptions.h"
@implementation OFObject
- init
{
if ((self = [super init]) != nil)
__mem_pool = NULL;
return self;
}
- free
{
struct __ofobject_allocated_mem *iter, *iter2;
for (iter = __mem_pool; iter != NULL; iter = iter2) {
iter2 = iter->prev;
free(iter->ptr);
free(iter);
}
free(self);
return nil;
}
- (void*)getMemWithSize: (size_t)size
{
struct __ofobject_allocated_mem *iter;
if (size == 0)
return NULL;
if ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) {
[[OFNoMemException newWithObject: self
andSize: sizeof(struct
__ofobject_allocated_mem)
] raise];
}
if ((iter->ptr = malloc(size)) == NULL) {
free(iter);
[[OFNoMemException newWithObject: self
andSize: size] raise];
}
iter->next = NULL;
iter->prev = __mem_pool;
if (__mem_pool != NULL)
__mem_pool->next = iter;
__mem_pool = iter;
return iter->ptr;
}
- (void*)getMemForNItems: (size_t)nitems
ofSize: (size_t)size
{
size_t memsize;
if (nitems == 0 || size == 0)
return NULL;
if (nitems > SIZE_MAX / size)
[[OFOutOfRangeException newWithObject: self] raise];
memsize = nitems * size;
return [self getMemWithSize: memsize];
}
- (void*)resizeMem: (void*)ptr
toSize: (size_t)size
{
struct __ofobject_allocated_mem *iter;
if (ptr == NULL)
return [self getMemWithSize: size];
if (size == 0) {
[self freeMem: ptr];
return NULL;
}
for (iter = __mem_pool; iter != NULL; iter = iter->prev) {
if (iter->ptr == ptr) {
if ((ptr = realloc(iter->ptr, size)) == NULL)
[[OFNoMemException newWithObject: self
andSize: size] raise];
iter->ptr = ptr;
return ptr;
}
}
[[OFMemNotPartOfObjException newWithObject: self
andPointer: ptr] raise];
return NULL; /* never reached, but makes gcc happy */
|
|
|
>
>
|
|
<
<
|
|
>
>
|
>
>
|
>
>
|
<
<
<
|
>
>
>
>
|
|
|
<
|
|
<
<
|
<
|
|
|
|
|
|
|
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#import "OFObject.h"
#import "OFExceptions.h"
@implementation OFObject
- init
{
if ((self = [super init]) != nil) {
__memchunks = NULL;
__memchunks_size = 0;
}
return self;
}
- free
{
size_t i;
for (i = 0; i < __memchunks_size; i++)
free(__memchunks[i]);
if (__memchunks != NULL)
free(__memchunks);
free(self);
return nil;
}
- (void*)getMemWithSize: (size_t)size
{
void *ptr, **memchunks;
size_t memchunks_size;
if (size == 0)
return NULL;
memchunks_size = __memchunks_size + 1;
if (SIZE_MAX - __memchunks_size == 0 ||
memchunks_size > SIZE_MAX / sizeof(void*))
[[OFOutOfRangeException newWithObject: self] raise];
if ((memchunks = realloc(__memchunks,
memchunks_size * sizeof(void*))) == NULL)
[[OFNoMemException newWithObject: self
andSize: memchunks_size] raise];
if ((ptr = malloc(size)) == NULL) {
free(memchunks);
[[OFNoMemException newWithObject: self
andSize: size] raise];
}
__memchunks = memchunks;
__memchunks[__memchunks_size] = ptr;
__memchunks_size = memchunks_size;
return ptr;
}
- (void*)getMemForNItems: (size_t)nitems
ofSize: (size_t)size
{
size_t memsize;
if (nitems == 0 || size == 0)
return NULL;
if (nitems > SIZE_MAX / size)
[[OFOutOfRangeException newWithObject: self] raise];
memsize = nitems * size;
return [self getMemWithSize: memsize];
}
- (void*)resizeMem: (void*)ptr
toSize: (size_t)size
{
size_t i;
if (ptr == NULL)
return [self getMemWithSize: size];
if (size == 0) {
[self freeMem: ptr];
return NULL;
}
for (i = 0; i < __memchunks_size; i++) {
if (__memchunks[i] == ptr) {
if ((ptr = realloc(ptr, size)) == NULL)
[[OFNoMemException newWithObject: self
andSize: size] raise];
__memchunks[i] = ptr;
return ptr;
}
}
[[OFMemNotPartOfObjException newWithObject: self
andPointer: ptr] raise];
return NULL; /* never reached, but makes gcc happy */
|
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
memsize = nitems * size;
return [self resizeMem: ptr
toSize: memsize];
}
- freeMem: (void*)ptr;
{
struct __ofobject_allocated_mem *iter;
for (iter = __mem_pool; iter != NULL; iter = iter->prev) {
if (iter->ptr == ptr) {
if (iter->prev != NULL)
iter->prev->next = iter->next;
if (iter->next != NULL)
iter->next->prev = iter->prev;
if (__mem_pool == iter)
__mem_pool = iter->prev;
free(iter);
free(ptr);
return self;
}
}
[[OFMemNotPartOfObjException newWithObject: self
andPointer: ptr] raise];
return self /* never reached, but makes gcc happy */;
}
@end
|
|
>
|
|
>
>
|
>
>
>
>
|
|
>
>
|
|
>
|
>
|
|
>
>
>
>
>
>
>
>
>
>
|
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
memsize = nitems * size;
return [self resizeMem: ptr
toSize: memsize];
}
- freeMem: (void*)ptr;
{
void *last, **memchunks;
size_t i, memchunks_size;
for (i = 0; i < __memchunks_size; i++) {
if (__memchunks[i] == ptr) {
memchunks_size = __memchunks_size - 1;
last = __memchunks[memchunks_size];
if (__memchunks_size == 0 ||
memchunks_size > SIZE_MAX / sizeof(void*))
[[OFOutOfRangeException newWithObject: self]
raise];
if (memchunks_size == 0) {
free(ptr);
free(__memchunks);
__memchunks = NULL;
__memchunks_size = 0;
return self;
}
if ((memchunks = realloc(__memchunks,
memchunks_size * sizeof(void*))) == NULL)
[[OFNoMemException newWithObject: self
andSize:
memchunks_size]
raise];
free(ptr);
__memchunks = memchunks;
__memchunks[i] = last;
__memchunks_size = memchunks_size;
return self;
}
}
[[OFMemNotPartOfObjException newWithObject: self
andPointer: ptr] raise];
return self /* never reached, but makes gcc happy */;
}
@end
|