Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -2,20 +2,20 @@ LIB_MAJOR = 1 LIB_MINOR = 0 SRCS = OFConstString.m \ OFConstWideString.m \ - OFException.m \ + OFExceptions.m \ OFList.m \ OFListObject.m \ OFObject.m \ OFString.m \ OFWideString.m INCLUDES = OFConstString.h \ OFConstWideString.h \ - OFException.h \ + OFExceptions.h \ OFList.h \ OFListObject.h \ OFObject.h \ OFString.h \ OFWideString.h DELETED src/OFException.h Index: src/OFException.h ================================================================== --- src/OFException.h +++ src/OFException.h @@ -1,16 +0,0 @@ -/* - * Copyright (c) 2008 - * Jonathan Schleifer - * - * All rights reserved. - * - * 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. - */ - -#define __NO_OFEXCEPTION -#import "OFObject.h" - -@interface OFException: OFObject -@end DELETED src/OFException.m Index: src/OFException.m ================================================================== --- src/OFException.m +++ src/OFException.m @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2008 - * Jonathan Schleifer - * - * All rights reserved. - * - * 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 "OFException.h" - -@implementation OFException -@end ADDED src/OFExceptions.h Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * 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 "OFObject.h" + +@interface OFException: OFObject ++ new: (id)obj; +- init: (id)obj; +@end + +@interface OFNoMemException: OFException ++ new: (id)obj + withSize: (size_t)size; +- init: (id)obj + withSize: (size_t)size; +@end + +@interface OFMemNotPartOfObjException: OFException ++ new: (id)obj + withPtr: (void*)ptr; +- init: (id)obj + withPtr: (void*)ptr; +@end ADDED src/OFExceptions.m Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * 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 "OFExceptions.h" + +@implementation OFException ++ new: (id)obj +{ + return [[OFException alloc] init: obj]; +} + +- init: (id)obj +{ + return [super init]; +} +@end + +@implementation OFNoMemException ++ new: (id)obj + withSize: (size_t)size +{ + return [[OFNoMemException alloc] init: obj + withSize: size]; +} + +- init: (id)obj + withSize: (size_t)size +{ + fprintf(stderr, "ERROR: Could not allocate %zd byte for object %s!\n ", + size, [obj name]); + + return [super init]; +} +@end + +@implementation OFMemNotPartOfObjException ++ new: (id)obj + withPtr: (void*)ptr +{ + return [[OFMemNotPartOfObjException alloc] init: obj + withPtr: ptr]; +} + +- init: (id)obj + withPtr: (void*)ptr +{ + 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 Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -27,16 +27,5 @@ - (void*)resizeMem: (void*)ptr toSize: (size_t)size; - (void)freeMem: (void*)ptr; - free; @end - -#ifndef __NO_OFEXCEPTION -#import "OFException.h" - -@interface OFMemNotPartOfObjException: OFException -+ new: (void*)p - fromObject: (id)obj; -- init: (void*)p - fromObject: (id)obj; -@end -#endif Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -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 Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -10,10 +10,11 @@ */ #import #import #import "OFString.h" +#import "OFExceptions.h" @implementation OFString + new: (const char*)str { return [[OFString alloc] init: str]; @@ -92,18 +93,16 @@ return [self setTo:str]; strlength = strlen(str); newlen = length + strlength; - /* FIXME: Add error handling */ - if ((newstr = [self resizeMem: string - toSize: newlen + 1]) == NULL) - return nil; + newstr = [self resizeMem: string + toSize: newlen + 1]; memcpy(newstr + length, str, strlength + 1); length = newlen; string = newstr; return self; } @end Index: src/OFWideString.m ================================================================== --- src/OFWideString.m +++ src/OFWideString.m @@ -11,10 +11,11 @@ #import #import #import #import "OFWideString.h" +#import "OFExceptions.h" @implementation OFWideString + new: (const wchar_t*)wstr { return [[OFWideString alloc] init: wstr]; @@ -94,18 +95,16 @@ return [self setTo: wstr]; strlength = wcslen(wstr); newlen = length + strlength; - /* FIXME: Add error handling */ - if ((newstr = [self resizeMem: wstring - toSize: (newlen + 1) * sizeof(wchar_t)]) == NULL) - return nil; + newstr = [self resizeMem: wstring + toSize: (newlen + 1) * sizeof(wchar_t)]; memcpy(newstr + length, wstr, (strlength + 1) * sizeof(wchar_t)); length = newlen; wstring = newstr; return self; } @end Index: tests/OFObject/OFObject.m ================================================================== --- tests/OFObject/OFObject.m +++ tests/OFObject/OFObject.m @@ -12,10 +12,11 @@ #import #import #import #import "OFObject.h" +#import "OFExceptions.h" int main() { OFObject *obj = [OFObject new];