193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
-
+
-
-
+
+
-
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
-
-
+
+
-
+
-
+
+
-
-
+
+
-
-
+
+
-
-
-
+
+
+
-
-
+
+
-
+
-
-
+
+
-
+
|
- (uint32_t)hash
{
/* Classes containing data should reimplement this! */
return (uint32_t)(intptr_t)self;
}
- addItemToMemoryPool: (void*)ptr
- addMemoryToPool: (void*)ptr
{
void **memchunks;
size_t memchunks_size;
memchunks_size = PRE_IVAR->memchunks_size + 1;
if (SIZE_MAX - PRE_IVAR->memchunks_size < 1 ||
memchunks_size > SIZE_MAX / sizeof(void*))
@throw [OFOutOfRangeException newWithClass: isa];
if ((memchunks = realloc(PRE_IVAR->memchunks,
memchunks_size * sizeof(void*))) == NULL)
@throw [OFNoMemException newWithClass: isa
andSize: memchunks_size];
@throw [OFOutOfMemoryException newWithClass: isa
andSize: memchunks_size];
PRE_IVAR->memchunks = memchunks;
PRE_IVAR->memchunks[PRE_IVAR->memchunks_size] = ptr;
PRE_IVAR->memchunks_size = memchunks_size;
return self;
}
- (void*)allocWithSize: (size_t)size
- (void*)allocMemoryWithSize: (size_t)size
{
void *ptr, **memchunks;
size_t memchunks_size;
if (size == 0)
return NULL;
memchunks_size = PRE_IVAR->memchunks_size + 1;
if (SIZE_MAX - PRE_IVAR->memchunks_size == 0 ||
memchunks_size > SIZE_MAX / sizeof(void*))
@throw [OFOutOfRangeException newWithClass: isa];
if ((ptr = malloc(size)) == NULL)
@throw [OFNoMemException newWithClass: isa
andSize: size];
@throw [OFOutOfMemoryException newWithClass: isa
andSize: size];
if ((memchunks = realloc(PRE_IVAR->memchunks,
memchunks_size * sizeof(void*))) == NULL) {
free(ptr);
@throw [OFNoMemException newWithClass: isa
andSize: memchunks_size];
@throw [OFOutOfMemoryException newWithClass: isa
andSize: memchunks_size];
}
PRE_IVAR->memchunks = memchunks;
PRE_IVAR->memchunks[PRE_IVAR->memchunks_size] = ptr;
PRE_IVAR->memchunks_size = memchunks_size;
return ptr;
}
- (void*)allocNItems: (size_t)nitems
withSize: (size_t)size
- (void*)allocMemoryForNItems: (size_t)nitems
withSize: (size_t)size
{
if (nitems == 0 || size == 0)
return NULL;
if (nitems > SIZE_MAX / size)
@throw [OFOutOfRangeException newWithClass: isa];
return [self allocWithSize: nitems * size];
return [self allocMemoryWithSize: nitems * size];
}
- (void*)resizeMem: (void*)ptr
toSize: (size_t)size
- (void*)resizeMemory: (void*)ptr
toSize: (size_t)size
{
void **iter;
if (ptr == NULL)
return [self allocWithSize: size];
return [self allocMemoryWithSize: size];
if (size == 0) {
[self freeMem: ptr];
[self freeMemory: ptr];
return NULL;
}
iter = PRE_IVAR->memchunks + PRE_IVAR->memchunks_size;
while (iter-- > PRE_IVAR->memchunks) {
if (OF_UNLIKELY(*iter == ptr)) {
if (OF_UNLIKELY((ptr = realloc(ptr, size)) == NULL))
@throw [OFOutOfMemoryException
@throw [OFNoMemException newWithClass: isa
andSize: size];
newWithClass: isa
andSize: size];
*iter = ptr;
return ptr;
}
}
@throw [OFMemNotPartOfObjException newWithClass: isa
andPointer: ptr];
@throw [OFMemoryNotPartOfObjectException newWithClass: isa
andPointer: ptr];
}
- (void*)resizeMem: (void*)ptr
toNItems: (size_t)nitems
withSize: (size_t)size
- (void*)resizeMemory: (void*)ptr
toNItems: (size_t)nitems
withSize: (size_t)size
{
if (ptr == NULL)
return [self allocNItems: nitems
withSize: size];
return [self allocMemoryForNItems: nitems
withSize: size];
if (nitems == 0 || size == 0) {
[self freeMem: ptr];
[self freeMemory: ptr];
return NULL;
}
if (nitems > SIZE_MAX / size)
@throw [OFOutOfRangeException newWithClass: isa];
return [self resizeMem: ptr
toSize: nitems * size];
return [self resizeMemory: ptr
toSize: nitems * size];
}
- freeMem: (void*)ptr;
- freeMemory: (void*)ptr;
{
void **iter, *last, **memchunks;
size_t i, memchunks_size;
iter = PRE_IVAR->memchunks + PRE_IVAR->memchunks_size;
i = PRE_IVAR->memchunks_size;
|
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
-
-
+
+
|
PRE_IVAR->memchunks[i] = last;
PRE_IVAR->memchunks_size = memchunks_size;
return self;
}
}
@throw [OFMemNotPartOfObjException newWithClass: isa
andPointer: ptr];
@throw [OFMemoryNotPartOfObjectException newWithClass: isa
andPointer: ptr];
}
- retain
{
PRE_IVAR->retain_count++;
return self;
|