︙ | | |
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
-
+
-
+
|
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: [self class]];
@throw [OFOutOfRangeException newWithClass: isa];
if ((memchunks = realloc(PRE_IVAR->memchunks,
memchunks_size * sizeof(void*))) == NULL)
@throw [OFNoMemException newWithClass: [self class]
@throw [OFNoMemException 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;
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
|
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: [self class]];
@throw [OFOutOfRangeException newWithClass: isa];
if ((ptr = malloc(size)) == NULL)
@throw [OFNoMemException newWithClass: [self class]
@throw [OFNoMemException newWithClass: isa
andSize: size];
if ((memchunks = realloc(PRE_IVAR->memchunks,
memchunks_size * sizeof(void*))) == NULL) {
free(ptr);
@throw [OFNoMemException newWithClass: [self class]
@throw [OFNoMemException 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*)getMemForNItems: (size_t)nitems
ofSize: (size_t)size
{
if (nitems == 0 || size == 0)
return NULL;
if (nitems > SIZE_MAX / size)
@throw [OFOutOfRangeException newWithClass: [self class]];
@throw [OFOutOfRangeException newWithClass: isa];
return [self getMemWithSize: nitems * size];
}
- (void*)resizeMem: (void*)ptr
toSize: (size_t)size
{
|
︙ | | |
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
|
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
|
-
+
-
-
+
-
+
-
+
|
}
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 [OFNoMemException
@throw [OFNoMemException newWithClass: isa
newWithClass: [self class]
andSize: size];
andSize: size];
*iter = ptr;
return ptr;
}
}
@throw [OFMemNotPartOfObjException newWithClass: [self class]
@throw [OFMemNotPartOfObjException newWithClass: isa
andPointer: ptr];
return NULL; /* never reached, but makes gcc happy */
}
- (void*)resizeMem: (void*)ptr
toNItems: (size_t)nitems
ofSize: (size_t)size
{
size_t memsize;
if (ptr == NULL)
return [self getMemForNItems: nitems
ofSize: size];
if (nitems == 0 || size == 0) {
[self freeMem: ptr];
return NULL;
}
if (nitems > SIZE_MAX / size)
@throw [OFOutOfRangeException newWithClass: [self class]];
@throw [OFOutOfRangeException newWithClass: isa];
memsize = nitems * size;
return [self resizeMem: ptr
toSize: memsize];
}
- freeMem: (void*)ptr;
|
︙ | | |
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
-
+
-
+
-
+
|
if (OF_UNLIKELY(*iter == ptr)) {
memchunks_size = PRE_IVAR->memchunks_size - 1;
last = PRE_IVAR->memchunks[memchunks_size];
if (OF_UNLIKELY(PRE_IVAR->memchunks_size == 0 ||
memchunks_size > SIZE_MAX / sizeof(void*)))
@throw [OFOutOfRangeException
newWithClass: [self class]];
newWithClass: isa];
if (OF_UNLIKELY(memchunks_size == 0)) {
free(ptr);
free(PRE_IVAR->memchunks);
PRE_IVAR->memchunks = NULL;
PRE_IVAR->memchunks_size = 0;
return self;
}
if (OF_UNLIKELY((memchunks = realloc(
PRE_IVAR->memchunks, memchunks_size *
sizeof(void*))) == NULL))
@throw [OFNoMemException
newWithClass: [self class]
newWithClass: isa
andSize: memchunks_size];
free(ptr);
PRE_IVAR->memchunks = memchunks;
PRE_IVAR->memchunks[i] = last;
PRE_IVAR->memchunks_size = memchunks_size;
return self;
}
}
@throw [OFMemNotPartOfObjException newWithClass: [self class]
@throw [OFMemNotPartOfObjException newWithClass: isa
andPointer: ptr];
return self; /* never reached, but makes gcc happy */
}
- retain
{
PRE_IVAR->retain_count++;
|
︙ | | |