@@ -7,14 +7,14 @@ * 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. */ -#import #import #import "OFObject.h" +#import "OFExceptions.h" @implementation OFObject - init { if ((self = [super init]) != nil) @@ -24,15 +24,21 @@ - (void*)getMem: (size_t)size { struct __ofobject_allocated_mem *iter; - if ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) + if ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) { + @throw [OFNoMemException new: self + withSize: sizeof( + struct __ofobject_allocated_mem)]; return NULL; + } if ((iter->ptr = malloc(size)) == NULL) { free(iter); + @throw [OFNoMemException new: self + withSize: size]; return NULL; } iter->next = NULL; iter->prev = __mem_pool; @@ -50,19 +56,23 @@ { struct __ofobject_allocated_mem *iter; for (iter = __mem_pool; iter != NULL; iter = iter->prev) { if (iter->ptr == ptr) { - if ((ptr = realloc(iter->ptr, size)) == NULL) + if ((ptr = realloc(iter->ptr, size)) == NULL) { + @throw [OFNoMemException new: self + withSize: size]; return NULL; + } iter->ptr = ptr; return ptr; } } - @throw [OFMemNotPartOfObjException new: ptr fromObject: self]; + @throw [OFMemNotPartOfObjException new: self + withPtr: ptr]; return NULL; } - (void)freeMem: (void*)ptr; { @@ -82,11 +92,12 @@ return; } } - @throw [OFMemNotPartOfObjException new: ptr fromObject: self]; + @throw [OFMemNotPartOfObjException new: self + withPtr: ptr]; } - free { struct __ofobject_allocated_mem *iter, *iter2; @@ -97,25 +108,6 @@ free(iter); } return [super free]; } -@end - -@implementation OFMemNotPartOfObjException -+ new: (void*)ptr fromObject: (id)obj -{ - return [[OFMemNotPartOfObjException alloc] init: ptr - fromObject: obj]; -} - -- init: (void*)ptr fromObject: (id)obj -{ - fprintf(stderr, "ERROR: Memory at %p was not allocated as part of " - "object %s!\n" - "ERROR: -> Not changing memory allocation!\n" - "ERROR: (Hint: It is possible that you tried to free the same " - "memory twice!)\n", ptr, [obj name]); - - return [super init]; -} @end