Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -13,14 +13,16 @@ gcc | *-gcc | gcc-* | *-gcc-*) OBJCFLAGS="$OBJSFLAGS -Wall -Werror -pipe -g" ;; esac -OBJCFLAGS="$OBJCFLAGS -fconstant-string-class=OFConstString -fobjc-exceptions" +OBJCFLAGS="$OBJCFLAGS -fconstant-string-class=OFConstCString -fobjc-exceptions" BUILDSYS_SHARED_LIB +AC_CHECK_HEADER(objc/runtime.h, + [AC_DEFINE(HAVE_OBJC_RUNTIME_H, 1, [Whether we have objc/runtime.h])]) AC_CHECK_LIB(objc, sel_get_name, [AC_DEFINE(HAVE_SEL_GET_NAME, 1, [Whether we have sel_get_name])]) AC_CHECK_LIB(objc, sel_getName, [AC_DEFINE(HAVE_SEL_GETNAME, 1, [Whether we have sel_getName])]) Index: src/OFCString.h ================================================================== --- src/OFCString.h +++ src/OFCString.h @@ -24,6 +24,7 @@ - (char*)cString; - (size_t)length; - (OFString*)clone; - (int)compareTo: (OFString*)str; - (OFString*)append: (OFString*)str; +- (OFString*)appendCString: (const char*)str; @end Index: src/OFCString.m ================================================================== --- src/OFCString.m +++ src/OFCString.m @@ -53,25 +53,30 @@ return strcmp(string, [str cString]); } - (OFString*)append: (OFString*)str { + return [self appendCString: [str cString]]; +} + +- (OFString*)appendCString: (const char*)str +{ char *newstr; size_t newlen, strlength; - if (str == NULL) - return [self setTo: str]; + if (string == NULL) + return [self setTo: [OFString newWithCString: (char*)str]]; - strlength = [str length]; + strlength = strlen(str); newlen = length + strlength; newstr = [self resizeMem: string toSize: newlen + 1]; - memcpy(newstr + length, [str cString], strlength + 1); + memcpy(newstr + length, str, strlength + 1); length = newlen; string = newstr; return self; } @end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -9,14 +9,15 @@ * the packaging of this file. */ #import "config.h" -#import - +#define _GNU_SOURCE #import #import + +#import #import "OFExceptions.h" #if defined HAVE_SEL_GET_NAME #define SEL_NAME(x) sel_get_name(x) Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -16,16 +16,14 @@ struct __ofobject_allocated_mem *prev; struct __ofobject_allocated_mem *next; }; @interface OFObject: Object -{ - struct __ofobject_allocated_mem *__mem_pool; -} ++ alloc; - init; - (void*)getMemWithSize: (size_t)size; - (void*)resizeMem: (void*)ptr toSize: (size_t)size; - (void)freeMem: (void*)ptr; - free; @end Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -10,21 +10,74 @@ */ #import "config.h" #import +#import + +#import +#ifdef HAVE_OBJC_RUNTIME_H +#import +#endif #import "OFObject.h" #import "OFExceptions.h" + +#ifdef HAVE_OBJC_RUNTIME_H +#define MEM_POOL (*(struct __ofobject_allocated_mem**)((char*)self + \ + class_getInstanceSize([self class]))) +#else +#define MEM_POOL (*(struct __ofobject_allocated_mem**)((char*)self + \ + ([self class])->instance_size)) +#endif @implementation OFObject ++ alloc +{ + Class class = [self class]; + id inst = nil; + +#ifdef HAVE_OBJC_RUNTIME_H + if ((inst = (id)malloc(class_getInstanceSize(class) + + sizeof(struct __ofobject_allocated_mem*))) != nil) { + memset(inst, 0, class_getInstanceSize(class) + + sizeof(struct __ofobject_allocated_mem*)); + inst->isa = class; + } +#else + if ((inst = (id)malloc(class->instance_size) + + sizeof(struct __ofobject_allocated_mem*)) != nil) { + memset(inst, 0, class->instance_size + + sizeof(struct __ofobject_allocated_mem*)); + inst->class_pointer = class; + } +#endif + + return inst; +} + - init { if ((self = [super init]) != nil) - __mem_pool = NULL; + MEM_POOL = NULL; return self; } + +- free +{ + struct __ofobject_allocated_mem *iter, *iter2; + + for (iter = MEM_POOL; iter != NULL; iter = iter2) { + iter2 = iter->prev; + free(iter->ptr); + free(iter); + } + + free(self); + + return nil; +} - (void*)getMemWithSize: (size_t)size { struct __ofobject_allocated_mem *iter; @@ -42,26 +95,26 @@ andSize: size] raise]; return NULL; } iter->next = NULL; - iter->prev = __mem_pool; + iter->prev = MEM_POOL; - if (__mem_pool != NULL) - __mem_pool->next = iter; + if (MEM_POOL != NULL) + MEM_POOL->next = iter; - __mem_pool = iter; + MEM_POOL = iter; return iter->ptr; } - (void*)resizeMem: (void*)ptr toSize: (size_t)size { struct __ofobject_allocated_mem *iter; - for (iter = __mem_pool; iter != NULL; iter = iter->prev) { + for (iter = MEM_POOL; iter != NULL; iter = iter->prev) { if (iter->ptr == ptr) { if ((ptr = realloc(iter->ptr, size)) == NULL) { [[OFNoMemException newWithObject: self andSize: size] raise]; return NULL; @@ -79,18 +132,18 @@ - (void)freeMem: (void*)ptr; { struct __ofobject_allocated_mem *iter; - for (iter = __mem_pool; iter != NULL; iter = iter->prev) { + for (iter = MEM_POOL; iter != NULL; iter = iter->prev) { if (iter->ptr == ptr) { if (iter->prev != NULL) iter->prev->next = iter->next; if (iter->next != NULL) iter->next->prev = iter->prev; - if (__mem_pool == iter) - __mem_pool = NULL; + if (MEM_POOL == iter) + MEM_POOL = NULL; free(iter); free(ptr); return; @@ -98,19 +151,6 @@ } [[OFMemNotPartOfObjException newWithObject: self andPointer: ptr] raise]; } - -- free -{ - struct __ofobject_allocated_mem *iter, *iter2; - - for (iter = __mem_pool; iter != NULL; iter = iter2) { - iter2 = iter->prev; - free(iter->ptr); - free(iter); - } - - return [super free]; -} @end Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -24,6 +24,8 @@ - (size_t)length; - (OFString*)setTo: (OFString*)str; - (OFString*)clone; - (int)compareTo: (OFString*)str; - (OFString*)append: (OFString*)str; +- (OFString*)appendCString: (const char*)str; +- (OFString*)appendWideCString: (const char*)str; @end Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -89,6 +89,23 @@ { [[OFNotImplementedException newWithObject: self andSelector: @selector(append:)] raise]; return nil; } + +- (OFString*)appendCString: (const char*)str +{ + [[OFNotImplementedException newWithObject: self + andSelector: @selector(appendCString:)] + raise]; + return nil; +} + +- (OFString*)appendWideCString: (const char*)str +{ + [[OFNotImplementedException newWithObject: self + andSelector: @selector( + appendWideCString:)] + raise]; + return nil; +} @end Index: src/OFWideCString.h ================================================================== --- src/OFWideCString.h +++ src/OFWideCString.h @@ -24,6 +24,7 @@ - (wchar_t*)wcString; - (size_t)length; - (OFString*)clone; - (int)compareTo: (OFString*)str; - (OFString*)append: (OFString*)str; +- (OFString*)appendWideCString: (const wchar_t*)str; @end Index: src/OFWideCString.m ================================================================== --- src/OFWideCString.m +++ src/OFWideCString.m @@ -55,25 +55,31 @@ return wcscmp(string, [str wcString]); } - (OFString*)append: (OFString*)str { + return [self appendWideCString: [str wcString]]; +} + +- (OFString*)appendWideCString: (const wchar_t*)str +{ wchar_t *newstr; size_t newlen, strlength; - if ([str wcString] == NULL) - return [self setTo: str]; + if (string == NULL) + return [self setTo: [OFString + newWithWideCString: (wchar_t*)str]]; - strlength = [str length]; + strlength = wcslen(str); newlen = length + strlength; newstr = [self resizeMem: string toSize: (newlen + 1) * sizeof(wchar_t)]; - wmemcpy(newstr + length, [str wcString], strlength + 1); + wmemcpy(newstr + length, str, strlength + 1); length = newlen; string = newstr; return self; } @end