Index: src/OFMacros.h ================================================================== --- src/OFMacros.h +++ src/OFMacros.h @@ -26,5 +26,13 @@ #define OF_BSWAP_V(buf, len) #endif #define OF_ROL(val, bits) \ (((val) << (bits)) | ((val) >> (32 - (bits)))) + +#ifdef __GNUC__ +#define OF_LIKELY(cond) __builtin_expect(!!(cond), 1) +#define OF_UNLIKELY(cond) __builtin_expect(!!(cond), 0) +#else +#define OF_LIKELY(cond) cond +#define OF_UNLIKELY(cond) cond +#endif Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -19,10 +19,11 @@ #import #endif #import "OFObject.h" #import "OFExceptions.h" +#import "OFMacros.h" @implementation OFObject - init { if ((self = [super init]) != nil) { @@ -108,12 +109,12 @@ } iter = __memchunks + __memchunks_size; while (iter-- > __memchunks) { - if (*iter == ptr) { - if ((ptr = realloc(ptr, size)) == NULL) + if (OF_UNLIKELY(*iter == ptr)) { + if (OF_UNLIKELY((ptr = realloc(ptr, size)) == NULL)) [[OFNoMemException newWithObject: self andSize: size] raise]; *iter = ptr; return ptr; @@ -157,31 +158,31 @@ i = __memchunks_size; while (iter-- > __memchunks) { i--; - if (*iter == ptr) { + if (OF_UNLIKELY(*iter == ptr)) { memchunks_size = __memchunks_size - 1; last = __memchunks[memchunks_size]; - if (__memchunks_size == 0 || - memchunks_size > SIZE_MAX / sizeof(void*)) + if (OF_UNLIKELY(__memchunks_size == 0 || + memchunks_size > SIZE_MAX / sizeof(void*))) [[OFOutOfRangeException newWithObject: self] raise]; - if (memchunks_size == 0) { + if (OF_UNLIKELY(memchunks_size == 0)) { free(ptr); free(__memchunks); __memchunks = NULL; __memchunks_size = 0; return self; } - if ((memchunks = realloc(__memchunks, - memchunks_size * sizeof(void*))) == NULL) + if (OF_UNLIKELY((memchunks = realloc(__memchunks, + memchunks_size * sizeof(void*))) == NULL)) [[OFNoMemException newWithObject: self andSize: memchunks_size] raise]; @@ -194,8 +195,8 @@ } } [[OFMemNotPartOfObjException newWithObject: self andPointer: ptr] raise]; - return self /* never reached, but makes gcc happy */; + return self; /* never reached, but makes gcc happy */ } @end Index: src/OFXMLFactory.m ================================================================== --- src/OFXMLFactory.m +++ src/OFXMLFactory.m @@ -14,10 +14,11 @@ #import #import #import "OFXMLFactory.h" #import "OFExceptions.h" +#import "OFMacros.h" /* * We don't use OFString in this file for performance reasons! * * We already have a clue about how big the resulting string will get, so we @@ -116,35 +117,38 @@ andSize: len + 1] raise]; for (i = j = 0; i < len; i++) { switch (s[i]) { case '<': - if (!xf_add2chars(&ret, &nlen, &j, "<")) + if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, "<"))) [[OFNoMemException newWithObject: nil andSize: nlen + 4] raise]; break; case '>': - if (!xf_add2chars(&ret, &nlen, &j, ">")) + if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, ">"))) [[OFNoMemException newWithObject: nil andSize: nlen + 4] raise]; break; case '"': - if (!xf_add2chars(&ret, &nlen, &j, """)) + if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, + """))) [[OFNoMemException newWithObject: nil andSize: nlen + 6] raise]; break; case '\'': - if (!xf_add2chars(&ret, &nlen, &j, "'")) + if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, + "'"))) [[OFNoMemException newWithObject: nil andSize: nlen + 6] raise]; break; case '&': - if (!xf_add2chars(&ret, &nlen, &j, "&")) + if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, + "&"))) [[OFNoMemException newWithObject: nil andSize: nlen + 5] raise]; break; default: @@ -171,43 +175,48 @@ raise]; for (i = j = 0; i < len; i++) { switch (s[i]) { case L'<': - if (!xf_add2wchars(&ret, &nlen, &j, L"<")) + if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j, + L"<"))) [[OFNoMemException newWithObject: nil andSize: (nlen + 4) * sizeof( wchar_t)] raise]; break; case L'>': - if (!xf_add2wchars(&ret, &nlen, &j, L">")) + if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j, + L">"))) [[OFNoMemException newWithObject: nil andSize: (nlen + 4) * sizeof( wchar_t)] raise]; break; case L'"': - if (!xf_add2wchars(&ret, &nlen, &j, L""")) + if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j, + L"""))) [[OFNoMemException newWithObject: nil andSize: (nlen + 6) * sizeof( wchar_t)] raise]; break; case L'\'': - if (!xf_add2wchars(&ret, &nlen, &j, L"'")) + if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j, + L"'"))) [[OFNoMemException newWithObject: nil andSize: (nlen + 6) * sizeof( wchar_t)] raise]; break; case L'&': - if (!xf_add2wchars(&ret, &nlen, &j, L"&")) + if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j, + L"&"))) [[OFNoMemException newWithObject: nil andSize: (nlen + 5) * sizeof( wchar_t)] raise]; @@ -245,21 +254,22 @@ va_start(args, data); while ((arg = va_arg(args, char*)) != NULL && (val = va_arg(args, char*)) != NULL) { char *esc_val; - if ((esc_val = [OFXMLFactory escapeCString: val]) == NULL) { + if (OF_UNLIKELY((esc_val = + [OFXMLFactory escapeCString: val]) == NULL)) { /* * escapeCString already throws an exception, * no need to throw a second one here. */ free(xml); return NULL; } - if (!xf_resize_chars(&xml, &len, 1 + strlen(arg) + 2 + - strlen(esc_val) + 1)) { + if (OF_UNLIKELY(!xf_resize_chars(&xml, &len, 1 + strlen(arg) + + 2 + strlen(esc_val) + 1))) { free(esc_val); [[OFNoMemException newWithObject: nil andSize: len + 1 + strlen(arg) + 2 + strlen(esc_val) + 1] @@ -340,21 +350,22 @@ va_start(args, data); while ((arg = va_arg(args, wchar_t*)) != NULL && (val = va_arg(args, wchar_t*)) != NULL) { wchar_t *esc_val; - if ((esc_val = [OFXMLFactory escapeWideCString: val]) == NULL) { + if (OF_UNLIKELY((esc_val = + [OFXMLFactory escapeWideCString: val]) == NULL)) { /* * escapeWideCString already throws an exception, * no need to throw a second one here. */ free(xml); return NULL; } - if (!xf_resize_wchars(&xml, &len, 1 + wcslen(arg) + 2 + - wcslen(esc_val) + 1)) { + if (OF_UNLIKELY(!xf_resize_wchars(&xml, &len, 1 + wcslen(arg) + + 2 + wcslen(esc_val) + 1))) { free(esc_val); [[OFNoMemException newWithObject: nil andSize: (len + 1 + wcslen(arg) + 2 + wcslen(esc_val) + 1) * @@ -432,11 +443,11 @@ memcpy(ret, strs[0], len - 1); pos = len - 1; for (i = 1; strs[i] != NULL; i++) { - if (!xf_add2chars(&ret, &len, &pos, strs[i])) { + if (OF_UNLIKELY(!xf_add2chars(&ret, &len, &pos, strs[i]))) { free(ret); [[OFNoMemException newWithObject: nil andSize: len + strlen(strs[i])] raise]; }