[Bf-blender-cvs] [43a2016] master: Guarded Alloc: use UNLIKELY for debug memset
Campbell Barton
noreply at git.blender.org
Sun Apr 6 05:10:36 CEST 2014
Commit: 43a201662a931a16027d4a088b675246df03c330
Author: Campbell Barton
Date: Sun Apr 6 12:57:20 2014 +1000
https://developer.blender.org/rB43a201662a931a16027d4a088b675246df03c330
Guarded Alloc: use UNLIKELY for debug memset
===================================================================
M intern/guardedalloc/intern/mallocn_guarded_impl.c
M intern/guardedalloc/intern/mallocn_lockfree_impl.c
===================================================================
diff --git a/intern/guardedalloc/intern/mallocn_guarded_impl.c b/intern/guardedalloc/intern/mallocn_guarded_impl.c
index 724e7f9..d86adcf 100644
--- a/intern/guardedalloc/intern/mallocn_guarded_impl.c
+++ b/intern/guardedalloc/intern/mallocn_guarded_impl.c
@@ -222,6 +222,14 @@ static bool malloc_debug_memset = false;
/* --------------------------------------------------------------------- */
#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
static void print_error(const char *str, ...)
@@ -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 1d131ed..ccda439 100644
--- a/intern/guardedalloc/intern/mallocn_lockfree_impl.c
+++ b/intern/guardedalloc/intern/mallocn_lockfree_impl.c
@@ -64,6 +64,14 @@ static void (*thread_unlock_callback)(void) = NULL;
#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
static void print_error(const char *str, ...)
@@ -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);
}
More information about the Bf-blender-cvs
mailing list