@@ -176,21 +176,19 @@ if OF_UNLIKELY (extraAlignment > 1) extraAlignment = ((instanceSize + extraAlignment - 1) & ~(extraAlignment - 1)) - extraAlignment; - instance = malloc(PRE_IVARS_ALIGN + instanceSize + + instance = calloc(1, PRE_IVARS_ALIGN + instanceSize + extraAlignment + extraSize); if OF_UNLIKELY (instance == nil) { allocFailedException.isa = [OFAllocFailedException class]; @throw (id)&allocFailedException; } ((struct pre_ivar *)instance)->retainCount = 1; - ((struct pre_ivar *)instance)->firstMem = NULL; - ((struct pre_ivar *)instance)->lastMem = NULL; #if !defined(OF_HAVE_ATOMIC_OPS) && defined(OF_HAVE_THREADS) if OF_UNLIKELY (!of_spinlock_new( &((struct pre_ivar *)instance)->retainCountSpinlock)) { free(instance); @@ -199,12 +197,10 @@ } #endif instance = (OFObject *)(void *)((char *)instance + PRE_IVARS_ALIGN); - memset(instance, 0, instanceSize); - if (!objc_constructInstance(class, instance)) { free((char *)instance - PRE_IVARS_ALIGN); @throw [OFInitializationFailedException exceptionWithClass: class]; } @@ -1036,21 +1032,22 @@ @throw [OFOutOfRangeException exception]; if OF_UNLIKELY ((pointer = malloc(PRE_MEM_ALIGN + size)) == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: size]; + preMem = pointer; - preMem->owner = self; preMem->prev = PRE_IVARS->lastMem; preMem->next = NULL; if OF_LIKELY (PRE_IVARS->lastMem != NULL) PRE_IVARS->lastMem->next = preMem; if OF_UNLIKELY (PRE_IVARS->firstMem == NULL) PRE_IVARS->firstMem = preMem; + PRE_IVARS->lastMem = preMem; return (char *)pointer + PRE_MEM_ALIGN; } @@ -1060,10 +1057,49 @@ if OF_UNLIKELY (count > SIZE_MAX / size) @throw [OFOutOfRangeException exception]; return [self allocMemoryWithSize: size * count]; } + +- (void *)allocZeroedMemoryWithSize: (size_t)size +{ + void *pointer; + struct pre_mem *preMem; + + if OF_UNLIKELY (size == 0) + return NULL; + + if OF_UNLIKELY (size > SIZE_MAX - PRE_IVARS_ALIGN) + @throw [OFOutOfRangeException exception]; + + if OF_UNLIKELY ((pointer = calloc(1, PRE_MEM_ALIGN + size)) == NULL) + @throw [OFOutOfMemoryException + exceptionWithRequestedSize: size]; + + preMem = pointer; + preMem->owner = self; + preMem->prev = PRE_IVARS->lastMem; + + if OF_LIKELY (PRE_IVARS->lastMem != NULL) + PRE_IVARS->lastMem->next = preMem; + + if OF_UNLIKELY (PRE_IVARS->firstMem == NULL) + PRE_IVARS->firstMem = preMem; + + PRE_IVARS->lastMem = preMem; + + return (char *)pointer + PRE_MEM_ALIGN; +} + +- (void *)allocZeroedMemoryWithSize: (size_t)size + count: (size_t)count +{ + if OF_UNLIKELY (count > SIZE_MAX / size) + @throw [OFOutOfRangeException exception]; + + return [self allocZeroedMemoryWithSize: size * count]; +} - (void *)resizeMemory: (void *)pointer size: (size_t)size { void *new;