Index: src/OFMutableString_UTF8.m ================================================================== --- src/OFMutableString_UTF8.m +++ src/OFMutableString_UTF8.m @@ -425,58 +425,58 @@ if (!s->UTF8) return; for (i = 0; i < s->cStringLength; i++) { /* ASCII */ - if (OF_LIKELY(!(s->cString[i] & 0x80))) + if OF_LIKELY (!(s->cString[i] & 0x80)) continue; /* A start byte can't happen first as we reversed everything */ - if (OF_UNLIKELY(s->cString[i] & 0x40)) + if OF_UNLIKELY (s->cString[i] & 0x40) @throw [OFInvalidEncodingException exceptionWithClass: [self class]]; /* Next byte must not be ASCII */ - if (OF_UNLIKELY(s->cStringLength < i + 1 || - !(s->cString[i + 1] & 0x80))) + if OF_UNLIKELY (s->cStringLength < i + 1 || + !(s->cString[i + 1] & 0x80)) @throw [OFInvalidEncodingException exceptionWithClass: [self class]]; /* Next byte is the start byte */ - if (OF_LIKELY(s->cString[i + 1] & 0x40)) { + if OF_LIKELY (s->cString[i + 1] & 0x40) { s->cString[i] ^= s->cString[i + 1]; s->cString[i + 1] ^= s->cString[i]; s->cString[i] ^= s->cString[i + 1]; i++; continue; } /* Second next byte must not be ASCII */ - if (OF_UNLIKELY(s->cStringLength < i + 2 || - !(s->cString[i + 2] & 0x80))) + if OF_UNLIKELY (s->cStringLength < i + 2 || + !(s->cString[i + 2] & 0x80)) @throw [OFInvalidEncodingException exceptionWithClass: [self class]]; /* Second next byte is the start byte */ - if (OF_LIKELY(s->cString[i + 2] & 0x40)) { + if OF_LIKELY (s->cString[i + 2] & 0x40) { s->cString[i] ^= s->cString[i + 2]; s->cString[i + 2] ^= s->cString[i]; s->cString[i] ^= s->cString[i + 2]; i += 2; continue; } /* Third next byte must not be ASCII */ - if (OF_UNLIKELY(s->cStringLength < i + 3 || - !(s->cString[i + 3] & 0x80))) + if OF_UNLIKELY (s->cStringLength < i + 3 || + !(s->cString[i + 3] & 0x80)) @throw [OFInvalidEncodingException exceptionWithClass: [self class]]; /* Third next byte is the start byte */ - if (OF_LIKELY(s->cString[i + 3] & 0x40)) { + if OF_LIKELY (s->cString[i + 3] & 0x40) { s->cString[i] ^= s->cString[i + 3]; s->cString[i + 3] ^= s->cString[i]; s->cString[i] ^= s->cString[i + 3]; s->cString[i + 1] ^= s->cString[i + 2]; Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -131,18 +131,18 @@ OFObject *instance; size_t instanceSize; instanceSize = class_getInstanceSize(class); - if (OF_UNLIKELY(extraAlignment > 0)) + if OF_UNLIKELY (extraAlignment > 0) extraAlignment = ((instanceSize + extraAlignment - 1) & ~(extraAlignment - 1)) - extraAlignment; instance = malloc(PRE_IVAR_ALIGN + instanceSize + extraAlignment + extraSize); - if (OF_UNLIKELY(instance == nil)) { + if OF_UNLIKELY (instance == nil) { object_setClass((id)&alloc_failed_exception, [OFAllocFailedException class]); @throw (id)&alloc_failed_exception; } @@ -149,12 +149,12 @@ ((struct pre_ivar*)instance)->retainCount = 1; ((struct pre_ivar*)instance)->firstMem = NULL; ((struct pre_ivar*)instance)->lastMem = NULL; #if !defined(OF_ATOMIC_OPS) && defined(OF_THREADS) - if (OF_UNLIKELY(!of_spinlock_new( - &((struct pre_ivar*)instance)->retainCountSpinlock))) { + if OF_UNLIKELY (!of_spinlock_new( + &((struct pre_ivar*)instance)->retainCountSpinlock)) { free(instance); @throw [OFInitializationFailedException exceptionWithClass: class]; } #endif @@ -162,11 +162,11 @@ instance = (OFObject*)((char*)instance + PRE_IVAR_ALIGN); memset(instance, 0, instanceSize); object_setClass(instance, class); - if (OF_UNLIKELY(extra != NULL)) + if OF_UNLIKELY (extra != NULL) *extra = (char*)instance + instanceSize + extraAlignment; return instance; } @@ -602,39 +602,39 @@ - (void*)allocMemoryWithSize: (size_t)size { void *pointer; struct pre_mem *preMem; - if (size > SIZE_MAX - PRE_IVAR_ALIGN) + if OF_UNLIKELY (size > SIZE_MAX - PRE_IVAR_ALIGN) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; - if ((pointer = malloc(PRE_MEM_ALIGN + size)) == NULL) + if OF_UNLIKELY ((pointer = malloc(PRE_MEM_ALIGN + size)) == NULL) @throw [OFOutOfMemoryException exceptionWithClass: [self class] requestedSize: size]; preMem = pointer; preMem->owner = self; preMem->prev = PRE_IVAR->lastMem; preMem->next = NULL; - if (PRE_IVAR->lastMem != NULL) + if OF_LIKELY (PRE_IVAR->lastMem != NULL) PRE_IVAR->lastMem->next = preMem; - if (PRE_IVAR->firstMem == NULL) + if OF_UNLIKELY (PRE_IVAR->firstMem == NULL) PRE_IVAR->firstMem = preMem; PRE_IVAR->lastMem = preMem; return (char*)pointer + PRE_MEM_ALIGN; } - (void*)allocMemoryWithSize: (size_t)size count: (size_t)count { - if (size == 0 || count == 0) + if OF_UNLIKELY (size == 0 || count == 0) return NULL; - if (count > SIZE_MAX / size) + if OF_UNLIKELY (count > SIZE_MAX / size) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; return [self allocMemoryWithSize: size * count]; } @@ -642,37 +642,38 @@ size: (size_t)size { void *new; struct pre_mem *preMem; - if (pointer == NULL) + if OF_UNLIKELY (pointer == NULL) return [self allocMemoryWithSize: size]; - if (size == 0) { + if OF_UNLIKELY (size == 0) { [self freeMemory: pointer]; return NULL; } - if (PRE_MEM(pointer)->owner != self) + if OF_UNLIKELY (PRE_MEM(pointer)->owner != self) @throw [OFMemoryNotPartOfObjectException exceptionWithClass: [self class] pointer: pointer]; - if ((new = realloc(PRE_MEM(pointer), PRE_MEM_ALIGN + size)) == NULL) + if OF_UNLIKELY ((new = realloc(PRE_MEM(pointer), + PRE_MEM_ALIGN + size)) == NULL) @throw [OFOutOfMemoryException exceptionWithClass: [self class] requestedSize: size]; preMem = new; - if (preMem != PRE_MEM(pointer)) { - if (preMem->prev != NULL) + if OF_UNLIKELY (preMem != PRE_MEM(pointer)) { + if OF_LIKELY (preMem->prev != NULL) preMem->prev->next = preMem; - if (preMem->next != NULL) + if OF_LIKELY (preMem->next != NULL) preMem->next->prev = preMem; - if (PRE_IVAR->firstMem == PRE_MEM(pointer)) + if OF_UNLIKELY (PRE_IVAR->firstMem == PRE_MEM(pointer)) PRE_IVAR->firstMem = preMem; - if (PRE_IVAR->lastMem == PRE_MEM(pointer)) + if OF_UNLIKELY (PRE_IVAR->lastMem == PRE_MEM(pointer)) PRE_IVAR->lastMem = preMem; } return (char*)new + PRE_MEM_ALIGN; } @@ -679,44 +680,44 @@ - (void*)resizeMemory: (void*)pointer size: (size_t)size count: (size_t)count { - if (pointer == NULL) + if OF_UNLIKELY (pointer == NULL) return [self allocMemoryWithSize: size count: count]; - if (size == 0 || count == 0) { + if OF_UNLIKELY (size == 0 || count == 0) { [self freeMemory: pointer]; return NULL; } - if (count > SIZE_MAX / size) + if OF_UNLIKELY (count > SIZE_MAX / size) @throw [OFOutOfRangeException exceptionWithClass: [self class]]; return [self resizeMemory: pointer size: size * count]; } - (void)freeMemory: (void*)pointer { - if (pointer == NULL) + if OF_UNLIKELY (pointer == NULL) return; - if (PRE_MEM(pointer)->owner != self) + if OF_UNLIKELY (PRE_MEM(pointer)->owner != self) @throw [OFMemoryNotPartOfObjectException exceptionWithClass: [self class] pointer: pointer]; - if (PRE_MEM(pointer)->prev != NULL) + if OF_LIKELY (PRE_MEM(pointer)->prev != NULL) PRE_MEM(pointer)->prev->next = PRE_MEM(pointer)->next; - if (PRE_MEM(pointer)->next != NULL) + if OF_LIKELY (PRE_MEM(pointer)->next != NULL) PRE_MEM(pointer)->next->prev = PRE_MEM(pointer)->prev; - if (PRE_IVAR->firstMem == PRE_MEM(pointer)) + if OF_UNLIKELY (PRE_IVAR->firstMem == PRE_MEM(pointer)) PRE_IVAR->firstMem = PRE_MEM(pointer)->next; - if (PRE_IVAR->lastMem == PRE_MEM(pointer)) + if OF_UNLIKELY (PRE_IVAR->lastMem == PRE_MEM(pointer)) PRE_IVAR->lastMem = PRE_MEM(pointer)->prev; /* To detect double-free */ PRE_MEM(pointer)->owner = nil; @@ -766,11 +767,11 @@ - autorelease { /* * Cache OFAutoreleasePool since class lookups are expensive with the - * GNU ABI. + * GNU ABI used by GCC. */ if (autoreleasePool == Nil) autoreleasePool = [OFAutoreleasePool class]; return [autoreleasePool addObject: self]; @@ -812,11 +813,11 @@ /* * We can use owner as a sentinel to prevent exploitation in * case there is a buffer underflow somewhere. */ - if (iter->owner != self) + if OF_UNLIKELY (iter->owner != self) abort(); free(iter); iter = next; @@ -826,21 +827,21 @@ } /* Required to use properties with the Apple runtime */ - copyWithZone: (void*)zone { - if (zone != NULL) + if OF_UNLIKELY (zone != NULL) @throw [OFNotImplementedException exceptionWithClass: [self class] selector: _cmd]; return [(id)self copy]; } - mutableCopyWithZone: (void*)zone { - if (zone != NULL) + if OF_UNLIKELY (zone != NULL) @throw [OFNotImplementedException exceptionWithClass: [self class] selector: _cmd]; return [(id)self mutableCopy]; Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -519,12 +519,11 @@ OFString *ret; /* Look if there's a line or \0 in our cache */ if (!waitingForDelimiter && cache != NULL) { for (i = 0; i < cacheLength; i++) { - if (OF_UNLIKELY(cache[i] == '\n' || - cache[i] == '\0')) { + if OF_UNLIKELY (cache[i] == '\n' || cache[i] == '\0') { retLength = i; if (i > 0 && cache[i - 1] == '\r') retLength--; @@ -578,12 +577,12 @@ bufferLength = [self _readIntoBuffer: buffer length: of_pagesize]; /* Look if there's a newline or \0 */ for (i = 0; i < bufferLength; i++) { - if (OF_UNLIKELY(buffer[i] == '\n' || - buffer[i] == '\0')) { + if OF_UNLIKELY (buffer[i] == '\n' || + buffer[i] == '\0') { retLength = cacheLength + i; retCString = [self allocMemoryWithSize: retLength]; if (cache != NULL) Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -69,57 +69,57 @@ size_t i, tmpLength = cStringLength; int UTF8 = 0; for (i = 0; i < cStringLength; i++) { /* No sign of UTF-8 here */ - if (OF_LIKELY(!(cString[i] & 0x80))) + if OF_LIKELY (!(cString[i] & 0x80)) continue; UTF8 = 1; /* We're missing a start byte here */ - if (OF_UNLIKELY(!(cString[i] & 0x40))) + if OF_UNLIKELY (!(cString[i] & 0x40)) return -1; /* 2 byte sequences for code points 0 - 127 are forbidden */ - if (OF_UNLIKELY((cString[i] & 0x7E) == 0x40)) + if OF_UNLIKELY ((cString[i] & 0x7E) == 0x40) return -1; /* We have at minimum a 2 byte character -> check next byte */ - if (OF_UNLIKELY(cStringLength <= i + 1 || - (cString[i + 1] & 0xC0) != 0x80)) + if OF_UNLIKELY (cStringLength <= i + 1 || + (cString[i + 1] & 0xC0) != 0x80) return -1; /* Check if we have at minimum a 3 byte character */ - if (OF_LIKELY(!(cString[i] & 0x20))) { + if OF_LIKELY (!(cString[i] & 0x20)) { i++; tmpLength--; continue; } /* We have at minimum a 3 byte char -> check second next byte */ - if (OF_UNLIKELY(cStringLength <= i + 2 || - (cString[i + 2] & 0xC0) != 0x80)) + if OF_UNLIKELY (cStringLength <= i + 2 || + (cString[i + 2] & 0xC0) != 0x80) return -1; /* Check if we have a 4 byte character */ - if (OF_LIKELY(!(cString[i] & 0x10))) { + if OF_LIKELY (!(cString[i] & 0x10)) { i += 2; tmpLength -= 2; continue; } /* We have a 4 byte character -> check third next byte */ - if (OF_UNLIKELY(cStringLength <= i + 3 || - (cString[i + 3] & 0xC0) != 0x80)) + if OF_UNLIKELY (cStringLength <= i + 3 || + (cString[i + 3] & 0xC0) != 0x80) return -1; /* * Just in case, check if there's a 5th character, which is * forbidden by UTF-8 */ - if (OF_UNLIKELY(cString[i] & 0x08)) + if OF_UNLIKELY (cString[i] & 0x08) return -1; i += 3; tmpLength -= 3; } @@ -170,28 +170,28 @@ *ret = buffer[0]; return 1; } if ((*buffer & 0xE0) == 0xC0) { - if (OF_UNLIKELY(length < 2)) + if OF_UNLIKELY (length < 2) return 0; *ret = ((buffer[0] & 0x1F) << 6) | (buffer[1] & 0x3F); return 2; } if ((*buffer & 0xF0) == 0xE0) { - if (OF_UNLIKELY(length < 3)) + if OF_UNLIKELY (length < 3) return 0; *ret = ((buffer[0] & 0x0F) << 12) | ((buffer[1] & 0x3F) << 6) | (buffer[2] & 0x3F); return 3; } if ((*buffer & 0xF8) == 0xF0) { - if (OF_UNLIKELY(length < 4)) + if OF_UNLIKELY (length < 4) return 0; *ret = ((buffer[0] & 0x07) << 18) | ((buffer[1] & 0x3F) << 12) | ((buffer[2] & 0x3F) << 6) | (buffer[3] & 0x3F); return 4; @@ -204,11 +204,11 @@ of_string_position_to_index(const char *string, size_t position) { size_t i, index = position; for (i = 0; i < position; i++) - if (OF_UNLIKELY((string[i] & 0xC0) == 0x80)) + if OF_UNLIKELY ((string[i] & 0xC0) == 0x80) index--; return index; } @@ -216,11 +216,11 @@ of_string_index_to_position(const char *string, size_t index, size_t length) { size_t i; for (i = 0; i <= index; i++) - if (OF_UNLIKELY((string[i] & 0xC0) == 0x80)) + if OF_UNLIKELY ((string[i] & 0xC0) == 0x80) if (++index > length) return OF_INVALID_INDEX; return index; } Index: src/macros.h ================================================================== --- src/macros.h +++ src/macros.h @@ -27,12 +27,12 @@ # define INTMAX_MAX LONG_LONG_MAX #endif #ifdef __GNUC__ # define OF_INLINE inline __attribute__((always_inline)) -# define OF_LIKELY(cond) __builtin_expect(!!(cond), 1) -# define OF_UNLIKELY(cond) __builtin_expect(!!(cond), 0) +# define OF_LIKELY(cond) (__builtin_expect(!!(cond), 1)) +# define OF_UNLIKELY(cond) (__builtin_expect(!!(cond), 0)) # define OF_CONST_FUNC __attribute__((const)) #else # define OF_INLINE inline # define OF_LIKELY(cond) cond # define OF_UNLIKELY(cond) cond