Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -34,15 +34,15 @@ return [[[OFBigArray alloc] initWithItemSize: is] autorelease]; } - initWithItemSize: (size_t)is { - if ((self = [super init])) { - data = NULL; - itemsize = is; - items = 0; - } + self = [super init]; + + data = NULL; + itemsize = is; + items = 0; return self; } - (size_t)items @@ -181,15 +181,15 @@ @end @implementation OFBigArray - initWithItemSize: (size_t)is { + self = [super initWithItemSize: is]; + if (lastpagebyte == 0) lastpagebyte = getpagesize() - 1; - - if ((self = [super initWithItemSize: is])) - size = 0; + size = 0; return self; } - add: (void*)item Index: src/OFAutoreleasePool.m ================================================================== --- src/OFAutoreleasePool.m +++ src/OFAutoreleasePool.m @@ -84,23 +84,21 @@ - init { OFList *pool_list; - if ((self = [super init])) { - objects = nil; - - pool_list = get_tls(pool_list_key); - - if (pool_list == nil) { - pool_list = [[OFList alloc] - initWithoutRetainAndRelease]; - set_tls(pool_list_key, pool_list); - } - - listobj = [pool_list append: self]; - } + self = [super init]; + + objects = nil; + pool_list = get_tls(pool_list_key); + + if (pool_list == nil) { + pool_list = [[OFList alloc] initWithoutRetainAndRelease]; + set_tls(pool_list_key, pool_list); + } + + listobj = [pool_list append: self]; return self; } - free Index: src/OFDictionary.m ================================================================== --- src/OFDictionary.m +++ src/OFDictionary.m @@ -27,48 +27,48 @@ return [[[OFDictionary alloc] initWithHashSize: hashsize] autorelease]; } - init { - if ((self = [super init])) { - size = 4096; - - @try { - data = [self getMemForNItems: size - ofSize: sizeof(OFList*)]; - } @catch (OFException *e) { - [self free]; - @throw e; - } - memset(data, 0, size); - } + self = [super init]; + + size = 4096; + + @try { + data = [self getMemForNItems: size + ofSize: sizeof(OFList*)]; + } @catch (OFException *e) { + [self free]; + @throw e; + } + memset(data, 0, size); return self; } - initWithHashSize: (int)hashsize { - if ((self = [super init])) { - if (hashsize < 8 || hashsize > 31) { - Class c = isa; - [self free]; - @throw [OFInvalidArgumentException - newWithClass: c - andSelector: _cmd]; - } - - size = (size_t)1 << hashsize; - - @try { - data = [self getMemForNItems: size - ofSize: sizeof(OFList*)]; - } @catch (OFException *e) { - [self free]; - @throw e; - } - memset(data, 0, size); - } + self = [super init]; + + if (hashsize < 8 || hashsize > 31) { + Class c = isa; + [self free]; + @throw [OFInvalidArgumentException + newWithClass: c + andSelector: _cmd]; + } + + size = (size_t)1 << hashsize; + + @try { + data = [self getMemForNItems: size + ofSize: sizeof(OFList*)]; + } @catch (OFException *e) { + [self free]; + @throw e; + } + memset(data, 0, size); return self; } - free Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -62,14 +62,14 @@ return [[self alloc] initWithClass: class_]; } - initWithClass: (Class)class_ { - if ((self = [super init])) { - class = class_; - string = NULL; - } + self = [super init]; + + class = class_; + string = NULL; return self; } - free @@ -100,12 +100,13 @@ } - initWithClass: (Class)class_ andSize: (size_t)size { - if ((self = [super initWithClass: class_])) - req_size = size; + self = [super initWithClass: class_]; + + req_size = size; return self; } - (const char*)cString @@ -134,12 +135,13 @@ } - initWithClass: (Class)class_ andPointer: (void*)ptr { - if ((self = [super initWithClass: class_])) - pointer = ptr; + self = [super initWithClass: class_]; + + pointer = ptr; return self; } - (const char*)cString @@ -170,12 +172,13 @@ } - initWithClass: (Class)class_ andSelector: (SEL)selector_ { - if ((self = [super initWithClass: class_])) - selector = selector_; + self = [super initWithClass: class_]; + + selector = selector_; return self; } - (const char*)cString @@ -211,12 +214,13 @@ } - initWithClass: (Class)class_ andSelector: (SEL)selector_ { - if ((self = [super initWithClass: class_])) - selector = selector_; + self = [super initWithClass: class_]; + + selector = selector_; return self; } - (const char*)cString @@ -280,15 +284,15 @@ - initWithClass: (Class)class_ andPath: (const char*)path_ andMode: (const char*)mode_ { - if ((self = [super initWithClass: class_])) { - path = (path_ != NULL ? strdup(path_) : NULL); - mode = (mode_ != NULL ? strdup(mode_) : NULL); - err = GET_ERR; - } + self = [super initWithClass: class_]; + + path = (path_ != NULL ? strdup(path_) : NULL); + mode = (mode_ != NULL ? strdup(mode_) : NULL); + err = GET_ERR; return self; } - free @@ -347,29 +351,29 @@ - initWithClass: (Class)class_ andSize: (size_t)size andNItems: (size_t)nitems { - if ((self = [super initWithClass: class_])) { - req_size = size; - req_items = nitems; - has_items = YES; - err = GET_ERR; - } + self = [super initWithClass: class_]; + + req_size = size; + req_items = nitems; + has_items = YES; + err = GET_ERR; return self; } - initWithClass: (Class)class_ andSize: (size_t)size { - if ((self = [super initWithClass: class_])) { - req_size = size; - req_items = 0; - has_items = NO; - err = GET_ERR; - } + self = [super initWithClass: class_]; + + req_size = size; + req_items = 0; + has_items = NO; + err = GET_ERR; return self; } - (int)errNo @@ -494,15 +498,15 @@ - initWithClass: (Class)class_ andNode: (const char*)node_ andService: (const char*)service_ { - if ((self = [super initWithClass: class_])) { - node = (node_ != NULL ? strdup(node_) : NULL); - service = (service_ != NULL ? strdup(service_) : NULL); - err = GET_SOCK_ERR; - } + self = [super initWithClass: class_]; + + node = (node_ != NULL ? strdup(node_) : NULL); + service = (service_ != NULL ? strdup(service_) : NULL); + err = GET_SOCK_ERR; return self; } - free @@ -558,15 +562,15 @@ - initWithClass: (Class)class_ andHost: (const char*)host_ andPort: (uint16_t)port_ { - if ((self = [super initWithClass: class_])) { - host = (host_ != NULL ? strdup(host_) : NULL); - port = port_; - err = GET_SOCK_ERR; - } + self = [super initWithClass: class_]; + + host = (host_ != NULL ? strdup(host_) : NULL); + port = port_; + err = GET_SOCK_ERR; return self; } - free @@ -619,16 +623,16 @@ - initWithClass: (Class)class_ andHost: (const char*)host_ andPort: (uint16_t)port_ andFamily: (int)family_ { - if ((self = [super initWithClass: class_])) { - host = (host_ != NULL ? strdup(host_) : NULL); - port = port_; - family = family_; - err = GET_SOCK_ERR; - } + self = [super initWithClass: class_]; + + host = (host_ != NULL ? strdup(host_) : NULL); + port = port_; + family = family_; + err = GET_SOCK_ERR; return self; } - free @@ -680,14 +684,14 @@ } - initWithClass: (Class)class_ andBackLog: (int)backlog_ { - if ((self = [super initWithClass: class_])) { - backlog = backlog_; - err = GET_SOCK_ERR; - } + self = [super initWithClass: class_]; + + backlog = backlog_; + err = GET_SOCK_ERR; return self; } - (const char*)cString @@ -713,12 +717,13 @@ @end @implementation OFAcceptFailedException - initWithClass: (Class)class_ { - if ((self = [super initWithClass: class_])) - err = GET_SOCK_ERR; + self = [super initWithClass: class_]; + + err = GET_SOCK_ERR; return self; } - (const char*)cString Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -80,19 +80,20 @@ - initWithPath: (const char*)path andMode: (const char*)mode { Class c; - if ((self = [super init])) { - if ((fp = fopen(path, mode)) == NULL) { - c = isa; - [super free]; - @throw [OFOpenFileFailedException newWithClass: c - andPath: path - andMode: mode]; - } - } + self = [super init]; + + if ((fp = fopen(path, mode)) == NULL) { + c = isa; + [super free]; + @throw [OFOpenFileFailedException newWithClass: c + andPath: path + andMode: mode]; + } + return self; } - free { Index: src/OFHashes.m ================================================================== --- src/OFHashes.m +++ src/OFHashes.m @@ -120,21 +120,21 @@ return [[[OFMD5Hash alloc] init] autorelease]; } - init { - if ((self = [super init])) { - buf[0] = 0x67452301; - buf[1] = 0xEFCDAB89; - buf[2] = 0x98BADCFE; - buf[3] = 0x10325476; - - bits[0] = 0; - bits[1] = 0; - - calculated = NO; - } + self = [super init]; + + buf[0] = 0x67452301; + buf[1] = 0xEFCDAB89; + buf[2] = 0x98BADCFE; + buf[3] = 0x10325476; + + bits[0] = 0; + bits[1] = 0; + + calculated = NO; return self; } - updateWithBuffer: (const char*)buffer @@ -363,18 +363,18 @@ return [[[OFSHA1Hash alloc] init] autorelease]; } - init { - if ((self = [super init])) { - count = 0; - state[0] = 0x67452301; - state[1] = 0xEFCDAB89; - state[2] = 0x98BADCFE; - state[3] = 0x10325476; - state[4] = 0xC3D2E1F0; - } + self = [super init]; + + count = 0; + state[0] = 0x67452301; + state[1] = 0xEFCDAB89; + state[2] = 0x98BADCFE; + state[3] = 0x10325476; + state[4] = 0xC3D2E1F0; return self; } - updateWithBuffer: (const char*)buf Index: src/OFList.m ================================================================== --- src/OFList.m +++ src/OFList.m @@ -19,26 +19,26 @@ return [[[OFList alloc] init] autorelease]; } - init { - if ((self = [super init])) { - first = NULL; - last = NULL; - retain_and_release = YES; - } + self = [super init]; + + first = NULL; + last = NULL; + retain_and_release = YES; return self; } - initWithoutRetainAndRelease { - if ((self = [super init])) { - first = NULL; - last = NULL; - retain_and_release = NO; - } + self = [super init]; + + first = NULL; + last = NULL; + retain_and_release = NO; return self; } - free Index: src/OFNumber.m ================================================================== --- src/OFNumber.m +++ src/OFNumber.m @@ -298,244 +298,244 @@ return [[[OFNumber alloc] initWithDouble: double_] autorelease]; } - initWithChar: (char)char_ { - if ((self = [super init])) { - value.char_ = char_; - type = OF_NUMBER_CHAR; - } + self = [super init]; + + value.char_ = char_; + type = OF_NUMBER_CHAR; return self; } - initWithShort: (short)short_ { - if ((self = [super init])) { - value.short_ = short_; - type = OF_NUMBER_SHORT; - } + self = [super init]; + + value.short_ = short_; + type = OF_NUMBER_SHORT; return self; } - initWithInt: (int)int_ { - if ((self = [super init])) { - value.int_ = int_; - type = OF_NUMBER_INT; - } + self = [super init]; + + value.int_ = int_; + type = OF_NUMBER_INT; return self; } - initWithLong: (long)long_ { - if ((self = [super init])) { - value.long_ = long_; - type = OF_NUMBER_LONG; - } + self = [super init]; + + value.long_ = long_; + type = OF_NUMBER_LONG; return self; } - initWithUChar: (unsigned char)uchar { - if ((self = [super init])) { - value.uchar = uchar; - type = OF_NUMBER_UCHAR; - } + self = [super init]; + + value.uchar = uchar; + type = OF_NUMBER_UCHAR; return self; } - initWithUShort: (unsigned short)ushort { - if ((self = [super init])) { - value.ushort = ushort; - type = OF_NUMBER_USHORT; - } + self = [super init]; + + value.ushort = ushort; + type = OF_NUMBER_USHORT; return self; } - initWithUInt: (unsigned int)uint { - if ((self = [super init])) { - value.uint = uint; - type = OF_NUMBER_UINT; - } + self = [super init]; + + value.uint = uint; + type = OF_NUMBER_UINT; return self; } - initWithULong: (unsigned long)ulong { - if ((self = [super init])) { - value.ulong = ulong; - type = OF_NUMBER_ULONG; - } + self = [super init]; + + value.ulong = ulong; + type = OF_NUMBER_ULONG; return self; } - initWithInt8: (int8_t)int8 { - if ((self = [super init])) { - value.int8 = int8; - type = OF_NUMBER_INT8; - } + self = [super init]; + + value.int8 = int8; + type = OF_NUMBER_INT8; return self; } - initWithInt16: (int16_t)int16 { - if ((self = [super init])) { - value.int16 = int16; - type = OF_NUMBER_INT16; - } + self = [super init]; + + value.int16 = int16; + type = OF_NUMBER_INT16; return self; } - initWithInt32: (int32_t)int32 { - if ((self = [super init])) { - value.int32 = int32; - type = OF_NUMBER_INT32; - } + self = [super init]; + + value.int32 = int32; + type = OF_NUMBER_INT32; return self; } - initWithInt64: (int64_t)int64 { - if ((self = [super init])) { - value.int64 = int64; - type = OF_NUMBER_INT64; - } + self = [super init]; + + value.int64 = int64; + type = OF_NUMBER_INT64; return self; } - initWithUInt8: (uint8_t)uint8 { - if ((self = [super init])) { - value.uint8 = uint8; - type = OF_NUMBER_UINT8; - } + self = [super init]; + + value.uint8 = uint8; + type = OF_NUMBER_UINT8; return self; } - initWithUInt16: (uint16_t)uint16 { - if ((self = [super init])) { - value.uint16 = uint16; - type = OF_NUMBER_UINT16; - } + self = [super init]; + + value.uint16 = uint16; + type = OF_NUMBER_UINT16; return self; } - initWithUInt32: (uint32_t)uint32 { - if ((self = [super init])) { - value.uint32 = uint32; - type = OF_NUMBER_UINT32; - } + self = [super init]; + + value.uint32 = uint32; + type = OF_NUMBER_UINT32; return self; } - initWithUInt64: (uint64_t)uint64 { - if ((self = [super init])) { - value.uint64 = uint64; - type = OF_NUMBER_UINT64; - } + self = [super init]; + + value.uint64 = uint64; + type = OF_NUMBER_UINT64; return self; } - initWithSize: (size_t)size { - if ((self = [super init])) { - value.size = size; - type = OF_NUMBER_SIZE; - } + self = [super init]; + + value.size = size; + type = OF_NUMBER_SIZE; return self; } - initWithSSize: (ssize_t)ssize { - if ((self = [super init])) { - value.ssize = ssize; - type = OF_NUMBER_SSIZE; - } + self = [super init]; + + value.ssize = ssize; + type = OF_NUMBER_SSIZE; return self; } - initWithIntMax: (intmax_t)intmax { - if ((self = [super init])) { - value.intmax = intmax; - type = OF_NUMBER_INTMAX; - } + self = [super init]; + + value.intmax = intmax; + type = OF_NUMBER_INTMAX; return self; } - initWithUIntMax: (uintmax_t)uintmax { - if ((self = [super init])) { - value.uintmax = uintmax; - type = OF_NUMBER_UINTMAX; - } + self = [super init]; + + value.uintmax = uintmax; + type = OF_NUMBER_UINTMAX; return self; } - initWithPtrDiff: (ptrdiff_t)ptrdiff { - if ((self = [super init])) { - value.ptrdiff = ptrdiff; - type = OF_NUMBER_PTRDIFF; - } + self = [super init]; + + value.ptrdiff = ptrdiff; + type = OF_NUMBER_PTRDIFF; return self; } - initWithIntPtr: (intptr_t)intptr { - if ((self = [super init])) { - value.intptr = intptr; - type = OF_NUMBER_INTPTR; - } + self = [super init]; + + value.intptr = intptr; + type = OF_NUMBER_INTPTR; return self; } - initWithFloat: (float)float_ { - if ((self = [super init])) { - value.float_ = float_; - type = OF_NUMBER_FLOAT; - } + self = [super init]; + + value.float_ = float_; + type = OF_NUMBER_FLOAT; return self; } - initWithDouble: (double)double_ { - if ((self = [super init])) { - value.double_ = double_; - type = OF_NUMBER_DOUBLE; - } + self = [super init]; + + value.double_ = double_; + type = OF_NUMBER_DOUBLE; return self; } - (enum of_number_type)type Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -33,10 +33,15 @@ */ + initialize; /** * Allocates memory for an instance of the class. + * + * alloc will never return nil, instead, it will throw an + * OFAllocFailedException. + * + * \return The allocated object. */ + alloc; /** * Allocated memory for an instance of the class and initializes the instance. @@ -64,10 +69,14 @@ withMethodFromClass: (Class)class; /** * Initialize the already allocated object. * Also sets up the memory pool for the object. + * + * Derived classes may override this, but need to do self = [super init] before + * they do any initialization themselves. init may never return nil, instead + * an exception (for example OFInitializationFailed) should be thrown. * * \return An initialized object */ - init; Index: src/OFPlugin.m ================================================================== --- src/OFPlugin.m +++ src/OFPlugin.m @@ -25,46 +25,40 @@ size_t pathlen, suffixlen; void *handle; OFPlugin *(*init_plugin)(); OFPlugin *plugin; - if ((self = [super init])) { - pathlen = strlen(path); - suffixlen = strlen(PLUGIN_SUFFIX); - - if ((file = malloc(pathlen + suffixlen + 1)) == NULL) { - @throw [OFNoMemException newWithClass: self - andSize: pathlen + - suffixlen + 1]; - } - memcpy(file, path, pathlen); - memcpy(file + pathlen, PLUGIN_SUFFIX, suffixlen); - file[pathlen + suffixlen] = 0; - - if ((handle = dlopen(file, RTLD_NOW)) == NULL) { - free(file); - @throw [OFInitializationFailedException - newWithClass: self]; - } - free(file); - - if ((init_plugin = dlsym(handle, "init_plugin")) == NULL || - (plugin = init_plugin()) == nil) { - dlclose(handle); - @throw [OFInitializationFailedException - newWithClass: self]; - } - - plugin->handle = handle; - return plugin; - } - - return self; + pathlen = strlen(path); + suffixlen = strlen(PLUGIN_SUFFIX); + + if ((file = malloc(pathlen + suffixlen + 1)) == NULL) { + @throw [OFNoMemException newWithClass: self + andSize: pathlen + + suffixlen + 1]; + } + memcpy(file, path, pathlen); + memcpy(file + pathlen, PLUGIN_SUFFIX, suffixlen); + file[pathlen + suffixlen] = 0; + + if ((handle = dlopen(file, RTLD_NOW)) == NULL) { + free(file); + @throw [OFInitializationFailedException newWithClass: self]; + } + free(file); + + if ((init_plugin = dlsym(handle, "init_plugin")) == NULL || + (plugin = init_plugin()) == nil) { + dlclose(handle); + @throw [OFInitializationFailedException newWithClass: self]; + } + + plugin->handle = handle; + return plugin; } - free { dlclose(handle); return [super free]; } @end Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -134,46 +134,46 @@ andArguments: args] autorelease]; } - init { - if ((self = [super init])) { - length = 0; - string = NULL; - is_utf8 = NO; - } + [super init]; + + length = 0; + string = NULL; + is_utf8 = NO; return self; } - initWithCString: (const char*)str { Class c; - if ((self = [super init])) { - if (str != NULL) { - length = strlen(str); + self = [super init]; - switch (check_utf8(str, length)) { + if (str != NULL) { + length = strlen(str); + + switch (check_utf8(str, length)) { case 1: is_utf8 = YES; break; case -1: c = isa; [super free]; @throw [OFInvalidEncodingException - newWithClass: c]; - } - - @try { - string = [self getMemWithSize: length + 1]; - } @catch (OFException *e) { - [self free]; - @throw e; - } - memcpy(string, str, length + 1); - } + newWithClass: c]; + } + + @try { + string = [self getMemWithSize: length + 1]; + } @catch (OFException *e) { + [self free]; + @throw e; + } + memcpy(string, str, length + 1); } return self; } @@ -194,42 +194,41 @@ andArguments: (va_list)args { int t; Class c; - if ((self = [super init])) { - if (fmt == NULL) { - c = isa; - [super free]; - @throw [OFInvalidFormatException newWithClass: c]; - } - - if ((t = vasprintf(&string, fmt, args)) == -1) { - c = isa; - [super free]; - @throw [OFInitializationFailedException - newWithClass: c]; - } - length = t; - - switch (check_utf8(string, length)) { + self = [super init]; + + if (fmt == NULL) { + c = isa; + [super free]; + @throw [OFInvalidFormatException newWithClass: c]; + } + + if ((t = vasprintf(&string, fmt, args)) == -1) { + c = isa; + [super free]; + @throw [OFInitializationFailedException newWithClass: c]; + } + length = t; + + switch (check_utf8(string, length)) { case 1: is_utf8 = YES; break; case -1: free(string); c = isa; [super free]; @throw [OFInvalidEncodingException newWithClass: c]; - } - - @try { - [self addToMemoryPool: string]; - } @catch (OFException *e) { - free(string); - @throw e; - } + } + + @try { + [self addToMemoryPool: string]; + } @catch (OFException *e) { + free(string); + @throw e; } return self; } Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -42,15 +42,15 @@ } #endif - init { - if ((self = [super init])) { - sock = INVALID_SOCKET; - saddr = NULL; - saddr_len = 0; - } + self = [super init]; + + sock = INVALID_SOCKET; + saddr = NULL; + saddr_len = 0; return self; } - free