# define GMQCC_NORETURN
#endif /*! (defined(__GNUC__) && __GNUC__ >= 2) || defined (__CLANG__) */
+#if (defined(__GNUC__)) || defined(__CLANG__)
+# define GMQCC_LIKELY(X) __builtin_expect((X), 1)
+# define GMQCC_UNLIKELY(X) __builtin_expect((X), 0)
+#else
+# define GMQCC_LIKELY(X) (X)
+# define GMQCC_UNLIKELY(X) (X)
+#endif
+
#ifndef _MSC_VER
# include <stdint.h>
#else
stat_mem_block_t *info = (stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size);
void *data = (void*)(info + 1);
- if(!info)
+ if(GMQCC_UNLIKELY(!info))
return NULL;
info->line = line;
info->prev = NULL;
info->next = stat_mem_block_root;
- if (stat_mem_block_root)
+ /* unlikely since it only happens once */
+ if (GMQCC_UNLIKELY(stat_mem_block_root != NULL))
stat_mem_block_root->prev = info;
stat_mem_block_root = info;
void stat_mem_deallocate(void *ptr) {
stat_mem_block_t *info = NULL;
- if (!ptr)
+ if (GMQCC_UNLIKELY(!ptr))
return;
info = ((stat_mem_block_t*)ptr - 1);
stat_mem_block_t *oldinfo = NULL;
stat_mem_block_t *newinfo;
- if (!ptr)
+ if (GMQCC_UNLIKELY(!ptr))
return stat_mem_allocate(size, line, file);
- /* stay consistent with glic */
- if (!size) {
+ /* stay consistent with glibc */
+ if (GMQCC_UNLIKELY(!size)) {
stat_mem_deallocate(ptr);
return NULL;
}
oldinfo = ((stat_mem_block_t*)ptr - 1);
newinfo = ((stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size));
- if (!newinfo) {
+ if (GMQCC_UNLIKELY(!newinfo)) {
stat_mem_deallocate(ptr);
return NULL;
}
newinfo->prev = NULL;
newinfo->next = stat_mem_block_root;
- if (stat_mem_block_root)
+ /*
+ * likely since the only time there is no root is when it's
+ * being initialized first.
+ */
+ if (GMQCC_LIKELY(stat_mem_block_root != NULL))
stat_mem_block_root->prev = newinfo;
stat_mem_block_root = newinfo;