From 43a201662a931a16027d4a088b675246df03c330 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 6 Apr 2014 12:57:20 +1000 Subject: Guarded Alloc: use UNLIKELY for debug memset --- intern/guardedalloc/intern/mallocn_guarded_impl.c | 14 +++++++++++--- intern/guardedalloc/intern/mallocn_lockfree_impl.c | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) (limited to 'intern') diff --git a/intern/guardedalloc/intern/mallocn_guarded_impl.c b/intern/guardedalloc/intern/mallocn_guarded_impl.c index 724e7f95845..d86adcf2a7c 100644 --- a/intern/guardedalloc/intern/mallocn_guarded_impl.c +++ b/intern/guardedalloc/intern/mallocn_guarded_impl.c @@ -221,6 +221,14 @@ static bool malloc_debug_memset = false; /* implementation */ /* --------------------------------------------------------------------- */ +#ifdef __GNUC__ +# define LIKELY(x) __builtin_expect(!!(x), 1) +# define UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +# define LIKELY(x) (x) +# define UNLIKELY(x) (x) +#endif + #ifdef __GNUC__ __attribute__ ((format(printf, 1, 2))) #endif @@ -497,9 +505,9 @@ void *MEM_guarded_mallocN(size_t len, const char *str) memh = (MemHead *)malloc(len + sizeof(MemHead) + sizeof(MemTail)); - if (memh) { + if (LIKELY(memh)) { make_memhead_header(memh, len, str); - if (malloc_debug_memset && len) + if (UNLIKELY(malloc_debug_memset && len)) memset(memh + 1, 255, len); #ifdef DEBUG_MEMCOUNTER @@ -951,7 +959,7 @@ static void rem_memblock(MemHead *memh) #endif } else { - if (malloc_debug_memset && memh->len) + if (UNLIKELY(malloc_debug_memset && memh->len)) memset(memh + 1, 255, memh->len); free(memh); } diff --git a/intern/guardedalloc/intern/mallocn_lockfree_impl.c b/intern/guardedalloc/intern/mallocn_lockfree_impl.c index 1d131ed339a..ccda439e29a 100644 --- a/intern/guardedalloc/intern/mallocn_lockfree_impl.c +++ b/intern/guardedalloc/intern/mallocn_lockfree_impl.c @@ -63,6 +63,14 @@ static void (*thread_unlock_callback)(void) = NULL; #define PTR_FROM_MEMHEAD(memhead) (memhead + 1) #define MEMHEAD_IS_MMAP(memhead) ((memhead)->len & (size_t) 1) +#ifdef __GNUC__ +# define LIKELY(x) __builtin_expect(!!(x), 1) +# define UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +# define LIKELY(x) (x) +# define UNLIKELY(x) (x) +#endif + #ifdef __GNUC__ __attribute__ ((format(printf, 1, 2))) #endif @@ -126,7 +134,7 @@ void MEM_lockfree_freeN(void *vmemh) #endif } else { - if (malloc_debug_memset && len) { + if (UNLIKELY(malloc_debug_memset && len)) { memset(memh + 1, 255, len); } free(memh); @@ -219,7 +227,7 @@ void *MEM_lockfree_callocN(size_t len, const char *str) memh = (MemHead *)calloc(1, len + sizeof(MemHead)); - if (memh) { + if (LIKELY(memh)) { memh->len = len; atomic_add_u(&totblock, 1); atomic_add_z(&mem_in_use, len); @@ -242,8 +250,8 @@ void *MEM_lockfree_mallocN(size_t len, const char *str) memh = (MemHead *)malloc(len + sizeof(MemHead)); - if (memh) { - if (malloc_debug_memset && len) { + if (LIKELY(memh)) { + if (UNLIKELY(malloc_debug_memset && len)) { memset(memh + 1, 255, len); } -- cgit v1.2.3