Updates with latest tommyds library.

This commit is contained in:
Andrea Mazzoleni 2014-09-26 23:50:01 +02:00
parent eeea58b42a
commit d84babb6bd
16 changed files with 189 additions and 118 deletions

View file

@ -1505,9 +1505,27 @@ void* malloc_nofail(size_t size)
return ptr; return ptr;
} }
void* cmalloc_nofail(size_t count, size_t size) void* calloc_nofail(size_t count, size_t size)
{ {
return malloc_nofail(count * size); void* ptr;
size *= count;
/* see the note in malloc_nofail() of why we don't use calloc() */
ptr = malloc(size);
if (!ptr) {
/* LCOV_EXCL_START */
malloc_fail(size);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
memset(ptr, 0, size);
mcounter += size;
return ptr;
} }
char* strdup_nofail(const char* str) char* strdup_nofail(const char* str)

View file

@ -468,7 +468,7 @@ void* malloc_nofail(size_t size);
* Safe cmalloc. * Safe cmalloc.
* If no memory is available, it aborts. * If no memory is available, it aborts.
*/ */
void* cmalloc_nofail(size_t count, size_t size); void* calloc_nofail(size_t count, size_t size);
/** /**
* Safe aligned malloc. * Safe aligned malloc.

View file

@ -30,6 +30,7 @@
/* redefine the malloc for tommy use */ /* redefine the malloc for tommy use */
#define tommy_malloc malloc_nofail #define tommy_malloc malloc_nofail
#define tommy_calloc calloc_nofail
#define tommy_free free #define tommy_free free
#include "tommyhash.c" #include "tommyhash.c"

View file

@ -27,48 +27,54 @@
#include "tommyarray.h" #include "tommyarray.h"
#include <string.h> /* for memset */
/******************************************************************************/ /******************************************************************************/
/* array */ /* array */
void tommy_array_init(tommy_array* array) void tommy_array_init(tommy_array* array)
{ {
unsigned i;
/* fixed initial size */ /* fixed initial size */
array->bucket_bit = TOMMY_ARRAY_BIT; array->bucket_bit = TOMMY_ARRAY_BIT;
array->bucket_max = 1 << array->bucket_bit; array->bucket_max = 1 << array->bucket_bit;
array->bucket[0] = tommy_cast(void**, tommy_malloc(array->bucket_max * sizeof(void*))); array->bucket[0] = tommy_cast(void**, tommy_calloc(array->bucket_max, sizeof(void*)));
for (i = 1; i < TOMMY_ARRAY_BIT; ++i)
array->bucket[i] = array->bucket[0];
/* initializes it with zeros */ array->bucket_mac = TOMMY_ARRAY_BIT;
memset(array->bucket[0], 0, array->bucket_max * sizeof(void*));
array->bucket_mac = 1;
array->size = 0; array->size = 0;
} }
void tommy_array_done(tommy_array* array) void tommy_array_done(tommy_array* array)
{ {
unsigned i; unsigned i;
for(i=0;i<array->bucket_mac;++i)
tommy_free(array->bucket[i]); tommy_free(array->bucket[0]);
for (i = TOMMY_ARRAY_BIT; i < array->bucket_mac; ++i) {
void** segment = array->bucket[i];
tommy_free(&segment[1 << i]);
}
} }
void tommy_array_grow(tommy_array* array, unsigned size) void tommy_array_grow(tommy_array* array, unsigned size)
{ {
while (size > array->bucket_max) { if (array->size >= size)
/* allocate one more bucket */ return;
array->bucket[array->bucket_mac] = tommy_cast(void**, tommy_malloc(array->bucket_max * sizeof(void*))); array->size = size;
/* initializes it with zeros */ while (size > array->bucket_max) {
memset(array->bucket[array->bucket_mac], 0, array->bucket_max * sizeof(void*)); void** segment;
/* allocate one more segment */
segment = tommy_cast(void**, tommy_calloc(array->bucket_max, sizeof(void*)));
/* store it adjusting the offset */
array->bucket[array->bucket_mac] = &segment[-array->bucket_max];
++array->bucket_mac; ++array->bucket_mac;
++array->bucket_bit; ++array->bucket_bit;
array->bucket_max = 1 << array->bucket_bit; array->bucket_max = 1 << array->bucket_bit;
} }
if (array->size < size)
array->size = size;
} }
tommy_size_t tommy_array_memory_usage(tommy_array* array) tommy_size_t tommy_array_memory_usage(tommy_array* array)

View file

@ -96,18 +96,10 @@ tommy_inline void** tommy_array_ref(tommy_array* array, unsigned pos)
assert(pos < array->size); assert(pos < array->size);
/* special case for the first bucket */ /* get the highest bit set, in case of all 0, return 0 */
if (pos < (1 << TOMMY_ARRAY_BIT)) { bsr = tommy_ilog2_u32(pos | 1);
return &array->bucket[0][pos];
}
/* get the highest bit set */ return &array->bucket[bsr][pos];
bsr = tommy_ilog2_u32(pos);
/* clear the highest bit */
pos -= 1 << bsr;
return &array->bucket[bsr - TOMMY_ARRAY_BIT + 1][pos];
} }
/** /**

View file

@ -27,8 +27,6 @@
#include "tommyarrayblk.h" #include "tommyarrayblk.h"
#include <string.h> /* for memset */
/******************************************************************************/ /******************************************************************************/
/* array */ /* array */
@ -43,7 +41,7 @@ void tommy_arrayblk_done(tommy_arrayblk* array)
{ {
unsigned i; unsigned i;
for(i=0;i<tommy_array_size(&array->block);++i) for (i = 0; i < tommy_array_size(&array->block); ++i)
tommy_free(tommy_array_get(&array->block, i)); tommy_free(tommy_array_get(&array->block, i));
tommy_array_done(&array->block); tommy_array_done(&array->block);
@ -51,8 +49,15 @@ void tommy_arrayblk_done(tommy_arrayblk* array)
void tommy_arrayblk_grow(tommy_arrayblk* array, unsigned size) void tommy_arrayblk_grow(tommy_arrayblk* array, unsigned size)
{ {
unsigned block_max = (size + TOMMY_ARRAYBLK_SIZE - 1) / TOMMY_ARRAYBLK_SIZE; unsigned block_max;
unsigned block_mac = tommy_array_size(&array->block); unsigned block_mac;
if (array->size >= size)
return;
array->size = size;
block_max = (size + TOMMY_ARRAYBLK_SIZE - 1) / TOMMY_ARRAYBLK_SIZE;
block_mac = tommy_array_size(&array->block);
if (block_mac < block_max) { if (block_mac < block_max) {
/* grow the block array */ /* grow the block array */
@ -60,10 +65,7 @@ void tommy_arrayblk_grow(tommy_arrayblk* array, unsigned size)
/* allocate new blocks */ /* allocate new blocks */
while (block_mac < block_max) { while (block_mac < block_max) {
void* ptr = tommy_malloc(TOMMY_ARRAYBLK_SIZE * sizeof(void*)); void* ptr = tommy_calloc(TOMMY_ARRAYBLK_SIZE, sizeof(void*));
/* initializes it with zeros */
memset(ptr, 0, TOMMY_ARRAYBLK_SIZE * sizeof(void*));
/* set the new block */ /* set the new block */
tommy_array_set(&array->block, block_mac, ptr); tommy_array_set(&array->block, block_mac, ptr);
@ -71,9 +73,6 @@ void tommy_arrayblk_grow(tommy_arrayblk* array, unsigned size)
++block_mac; ++block_mac;
} }
} }
if (array->size < size)
array->size = size;
} }
tommy_size_t tommy_arrayblk_memory_usage(tommy_arrayblk* array) tommy_size_t tommy_arrayblk_memory_usage(tommy_arrayblk* array)

View file

@ -52,7 +52,7 @@
/** /**
* Elements for each block. * Elements for each block.
*/ */
#define TOMMY_ARRAYBLK_SIZE (64 * 1024) #define TOMMY_ARRAYBLK_SIZE (4 * 1024)
/** /**
* Array. * Array.

View file

@ -27,8 +27,6 @@
#include "tommyarrayblkof.h" #include "tommyarrayblkof.h"
#include <string.h> /* for memset */
/******************************************************************************/ /******************************************************************************/
/* array */ /* array */
@ -43,7 +41,7 @@ void tommy_arrayblkof_done(tommy_arrayblkof* array)
{ {
unsigned i; unsigned i;
for(i=0;i<tommy_array_size(&array->block);++i) for (i = 0; i < tommy_array_size(&array->block); ++i)
tommy_free(tommy_array_get(&array->block, i)); tommy_free(tommy_array_get(&array->block, i));
tommy_array_done(&array->block); tommy_array_done(&array->block);
@ -51,8 +49,15 @@ void tommy_arrayblkof_done(tommy_arrayblkof* array)
void tommy_arrayblkof_grow(tommy_arrayblkof* array, unsigned size) void tommy_arrayblkof_grow(tommy_arrayblkof* array, unsigned size)
{ {
unsigned block_max = (size + TOMMY_ARRAYBLK_SIZE - 1) / TOMMY_ARRAYBLK_SIZE; unsigned block_max;
unsigned block_mac = tommy_array_size(&array->block); unsigned block_mac;
if (array->size >= size)
return;
array->size = size;
block_max = (size + TOMMY_ARRAYBLK_SIZE - 1) / TOMMY_ARRAYBLK_SIZE;
block_mac = tommy_array_size(&array->block);
if (block_mac < block_max) { if (block_mac < block_max) {
/* grow the block array */ /* grow the block array */
@ -60,10 +65,7 @@ void tommy_arrayblkof_grow(tommy_arrayblkof* array, unsigned size)
/* allocate new blocks */ /* allocate new blocks */
while (block_mac < block_max) { while (block_mac < block_max) {
void* ptr = tommy_malloc(TOMMY_ARRAYBLK_SIZE * array->element_size); void* ptr = tommy_calloc(TOMMY_ARRAYBLK_SIZE, array->element_size);
/* initializes it with zeros */
memset(ptr, 0, TOMMY_ARRAYBLK_SIZE * array->element_size);
/* set the new block */ /* set the new block */
tommy_array_set(&array->block, block_mac, ptr); tommy_array_set(&array->block, block_mac, ptr);
@ -71,9 +73,6 @@ void tommy_arrayblkof_grow(tommy_arrayblkof* array, unsigned size)
++block_mac; ++block_mac;
} }
} }
if (array->size < size)
array->size = size;
} }
tommy_size_t tommy_arrayblkof_memory_usage(tommy_arrayblkof* array) tommy_size_t tommy_arrayblkof_memory_usage(tommy_arrayblkof* array)

View file

@ -51,7 +51,7 @@
/** /**
* Elements for each block. * Elements for each block.
*/ */
#define TOMMY_ARRAYBLKOF_SIZE (64 * 1024) #define TOMMY_ARRAYBLKOF_SIZE (4 * 1024)
/** /**
* Array. * Array.

View file

@ -211,9 +211,9 @@ tommy_inline void tommy_chain_mergesort(tommy_chain* chain, tommy_compare_func*
while (mask != 1) { while (mask != 1) {
mask >>= 1; mask >>= 1;
if (mask & 1) { if (mask & 1) {
tommy_chain_merge_degenerated(&bit[i+1], &bit[i], cmp); tommy_chain_merge_degenerated(&bit[i + 1], &bit[i], cmp);
} else { } else {
bit[i+1] = bit[i]; bit[i + 1] = bit[i];
} }
++i; ++i;
} }

View file

@ -42,33 +42,33 @@ tommy_inline tommy_uint32_t tommy_le_uint32_read(const void* ptr)
#endif #endif
} }
#define tommy_rot(x,k) \ #define tommy_rot(x, k) \
(((x)<<(k)) | ((x)>>(32-(k)))) (((x) << (k)) | ((x) >> (32 - (k))))
#define tommy_mix(a,b,c) \ #define tommy_mix(a, b, c) \
do { \ do { \
a -= c; a ^= tommy_rot(c, 4); c += b; \ a -= c; a ^= tommy_rot(c, 4); c += b; \
b -= a; b ^= tommy_rot(a, 6); a += c; \ b -= a; b ^= tommy_rot(a, 6); a += c; \
c -= b; c ^= tommy_rot(b, 8); b += a; \ c -= b; c ^= tommy_rot(b, 8); b += a; \
a -= c; a ^= tommy_rot(c,16); c += b; \ a -= c; a ^= tommy_rot(c, 16); c += b; \
b -= a; b ^= tommy_rot(a,19); a += c; \ b -= a; b ^= tommy_rot(a, 19); a += c; \
c -= b; c ^= tommy_rot(b, 4); b += a; \ c -= b; c ^= tommy_rot(b, 4); b += a; \
} while (0) } while (0)
#define tommy_final(a,b,c) \ #define tommy_final(a, b, c) \
do { \ do { \
c ^= b; c -= tommy_rot(b,14); \ c ^= b; c -= tommy_rot(b, 14); \
a ^= c; a -= tommy_rot(c,11); \ a ^= c; a -= tommy_rot(c, 11); \
b ^= a; b -= tommy_rot(a,25); \ b ^= a; b -= tommy_rot(a, 25); \
c ^= b; c -= tommy_rot(b,16); \ c ^= b; c -= tommy_rot(b, 16); \
a ^= c; a -= tommy_rot(c,4); \ a ^= c; a -= tommy_rot(c, 4); \
b ^= a; b -= tommy_rot(a,14); \ b ^= a; b -= tommy_rot(a, 14); \
c ^= b; c -= tommy_rot(b,24); \ c ^= b; c -= tommy_rot(b, 24); \
} while (0) } while (0)
tommy_uint32_t tommy_hash_u32(tommy_uint32_t init_val, const void* void_key, tommy_size_t key_len) tommy_uint32_t tommy_hash_u32(tommy_uint32_t init_val, const void* void_key, tommy_size_t key_len)
{ {
const unsigned char* key = tommy_cast(const unsigned char*,void_key); const unsigned char* key = tommy_cast(const unsigned char*, void_key);
tommy_uint32_t a, b, c; tommy_uint32_t a, b, c;
a = b = c = 0xdeadbeef + ((tommy_uint32_t)key_len) + init_val; a = b = c = 0xdeadbeef + ((tommy_uint32_t)key_len) + init_val;
@ -78,7 +78,7 @@ tommy_uint32_t tommy_hash_u32(tommy_uint32_t init_val, const void* void_key, tom
b += tommy_le_uint32_read(key + 4); b += tommy_le_uint32_read(key + 4);
c += tommy_le_uint32_read(key + 8); c += tommy_le_uint32_read(key + 8);
tommy_mix(a,b,c); tommy_mix(a, b, c);
key_len -= 12; key_len -= 12;
key += 12; key += 12;
@ -110,14 +110,14 @@ tommy_uint32_t tommy_hash_u32(tommy_uint32_t init_val, const void* void_key, tom
case 1 : a += key[0]; case 1 : a += key[0];
} }
tommy_final(a,b,c); tommy_final(a, b, c);
return c; return c;
} }
tommy_uint64_t tommy_hash_u64(tommy_uint64_t init_val, const void* void_key, tommy_size_t key_len) tommy_uint64_t tommy_hash_u64(tommy_uint64_t init_val, const void* void_key, tommy_size_t key_len)
{ {
const unsigned char* key = tommy_cast(const unsigned char*,void_key); const unsigned char* key = tommy_cast(const unsigned char*, void_key);
tommy_uint32_t a, b, c; tommy_uint32_t a, b, c;
a = b = c = 0xdeadbeef + ((tommy_uint32_t)key_len) + (init_val & 0xffffffff); a = b = c = 0xdeadbeef + ((tommy_uint32_t)key_len) + (init_val & 0xffffffff);
@ -128,7 +128,7 @@ tommy_uint64_t tommy_hash_u64(tommy_uint64_t init_val, const void* void_key, tom
b += tommy_le_uint32_read(key + 4); b += tommy_le_uint32_read(key + 4);
c += tommy_le_uint32_read(key + 8); c += tommy_le_uint32_read(key + 8);
tommy_mix(a,b,c); tommy_mix(a, b, c);
key_len -= 12; key_len -= 12;
key += 12; key += 12;
@ -160,7 +160,7 @@ tommy_uint64_t tommy_hash_u64(tommy_uint64_t init_val, const void* void_key, tom
case 1 : a += key[0]; case 1 : a += key[0];
} }
tommy_final(a,b,c); tommy_final(a, b, c);
return c + ((tommy_uint64_t)b << 32); return c + ((tommy_uint64_t)b << 32);
} }

View file

@ -28,8 +28,6 @@
#include "tommyhashdyn.h" #include "tommyhashdyn.h"
#include "tommylist.h" #include "tommylist.h"
#include <string.h> /* for memset */
/******************************************************************************/ /******************************************************************************/
/* hashdyn */ /* hashdyn */
@ -39,8 +37,7 @@ void tommy_hashdyn_init(tommy_hashdyn* hashdyn)
hashdyn->bucket_bit = TOMMY_HASHDYN_BIT; hashdyn->bucket_bit = TOMMY_HASHDYN_BIT;
hashdyn->bucket_max = 1 << hashdyn->bucket_bit; hashdyn->bucket_max = 1 << hashdyn->bucket_bit;
hashdyn->bucket_mask = hashdyn->bucket_max - 1; hashdyn->bucket_mask = hashdyn->bucket_max - 1;
hashdyn->bucket = tommy_cast(tommy_hashdyn_node**, tommy_malloc(hashdyn->bucket_max * sizeof(tommy_hashdyn_node*))); hashdyn->bucket = tommy_cast(tommy_hashdyn_node**, tommy_calloc(hashdyn->bucket_max, sizeof(tommy_hashdyn_node*)));
memset(hashdyn->bucket, 0, hashdyn->bucket_max * sizeof(tommy_hashdyn_node*));
hashdyn->count = 0; hashdyn->count = 0;
} }
@ -66,6 +63,9 @@ static void tommy_hashdyn_resize(tommy_hashdyn* hashdyn, unsigned new_bucket_bit
new_bucket_max = 1 << new_bucket_bit; new_bucket_max = 1 << new_bucket_bit;
new_bucket_mask = new_bucket_max - 1; new_bucket_mask = new_bucket_max - 1;
/* allocate the new vector using malloc() and not calloc() */
/* because data is fully initialized in the update process */
new_bucket = tommy_cast(tommy_hashdyn_node**, tommy_malloc(new_bucket_max * sizeof(tommy_hashdyn_node*))); new_bucket = tommy_cast(tommy_hashdyn_node**, tommy_malloc(new_bucket_max * sizeof(tommy_hashdyn_node*)));
/* reinsert all the elements */ /* reinsert all the elements */
@ -73,7 +73,7 @@ static void tommy_hashdyn_resize(tommy_hashdyn* hashdyn, unsigned new_bucket_bit
unsigned i; unsigned i;
/* grow */ /* grow */
for(i=0;i<bucket_max;++i) { for (i = 0; i < bucket_max; ++i) {
tommy_hashdyn_node* j; tommy_hashdyn_node* j;
/* setup the new two buckets */ /* setup the new two buckets */
@ -96,7 +96,7 @@ static void tommy_hashdyn_resize(tommy_hashdyn* hashdyn, unsigned new_bucket_bit
unsigned i; unsigned i;
/* shrink */ /* shrink */
for(i=0;i<new_bucket_max;++i) { for (i = 0; i < new_bucket_max; ++i) {
/* setup the new bucket with the lower bucket*/ /* setup the new bucket with the lower bucket*/
new_bucket[i] = hashdyn->bucket[i]; new_bucket[i] = hashdyn->bucket[i];
@ -165,28 +165,62 @@ void* tommy_hashdyn_remove_existing(tommy_hashdyn* hashdyn, tommy_hashdyn_node*
void* tommy_hashdyn_remove(tommy_hashdyn* hashdyn, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash) void* tommy_hashdyn_remove(tommy_hashdyn* hashdyn, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash)
{ {
unsigned pos = hash % hashdyn->bucket_max; unsigned pos = hash % hashdyn->bucket_max;
tommy_hashdyn_node* i = hashdyn->bucket[pos]; tommy_hashdyn_node* node = hashdyn->bucket[pos];
while (i) { while (node) {
/* we first check if the hash matches, as in the same bucket we may have multiples hash values */ /* we first check if the hash matches, as in the same bucket we may have multiples hash values */
if (i->key == hash && cmp(cmp_arg, i->data) == 0) { if (node->key == hash && cmp(cmp_arg, node->data) == 0) {
tommy_list_remove_existing(&hashdyn->bucket[pos], i); tommy_list_remove_existing(&hashdyn->bucket[pos], node);
--hashdyn->count; --hashdyn->count;
hashdyn_shrink_step(hashdyn); hashdyn_shrink_step(hashdyn);
return i->data; return node->data;
} }
i = i->next; node = node->next;
} }
return 0; return 0;
} }
void tommy_hashdyn_foreach(tommy_hashdyn* hashdyn, tommy_foreach_func* func)
{
unsigned bucket_max = hashdyn->bucket_max;
tommy_hashdyn_node** bucket = hashdyn->bucket;
unsigned pos;
for (pos = 0; pos < bucket_max; ++pos) {
tommy_hashdyn_node* node = bucket[pos];
while (node) {
void* data = node->data;
node = node->next;
func(data);
}
}
}
void tommy_hashdyn_foreach_arg(tommy_hashdyn* hashdyn, tommy_foreach_arg_func* func, void* arg)
{
unsigned bucket_max = hashdyn->bucket_max;
tommy_hashdyn_node** bucket = hashdyn->bucket;
unsigned pos;
for (pos = 0; pos < bucket_max; ++pos) {
tommy_hashdyn_node* node = bucket[pos];
while (node) {
void* data = node->data;
node = node->next;
func(arg, data);
}
}
}
tommy_size_t tommy_hashdyn_memory_usage(tommy_hashdyn* hashdyn) tommy_size_t tommy_hashdyn_memory_usage(tommy_hashdyn* hashdyn)
{ {
return hashdyn->bucket_max * (tommy_size_t)sizeof(hashdyn->bucket[0]) return hashdyn->bucket_max * (tommy_size_t)sizeof(hashdyn->bucket[0])
+ tommy_hashdyn_count(hashdyn) * (tommy_size_t)sizeof(tommy_hashdyn_node); + tommy_hashdyn_count(hashdyn) * (tommy_size_t)sizeof(tommy_hashdyn_node);
} }

View file

@ -221,6 +221,7 @@ tommy_inline tommy_hashdyn_node* tommy_hashdyn_bucket(tommy_hashdyn* hashdyn, to
tommy_inline void* tommy_hashdyn_search(tommy_hashdyn* hashdyn, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash) tommy_inline void* tommy_hashdyn_search(tommy_hashdyn* hashdyn, tommy_search_func* cmp, const void* cmp_arg, tommy_hash_t hash)
{ {
tommy_hashdyn_node* i = tommy_hashdyn_bucket(hashdyn, hash); tommy_hashdyn_node* i = tommy_hashdyn_bucket(hashdyn, hash);
while (i) { while (i) {
/* we first check if the hash matches, as in the same bucket we may have multiples hash values */ /* we first check if the hash matches, as in the same bucket we may have multiples hash values */
if (i->key == hash && cmp(cmp_arg, i->data) == 0) if (i->key == hash && cmp(cmp_arg, i->data) == 0)
@ -237,6 +238,16 @@ tommy_inline void* tommy_hashdyn_search(tommy_hashdyn* hashdyn, tommy_search_fun
*/ */
void* tommy_hashdyn_remove_existing(tommy_hashdyn* hashdyn, tommy_hashdyn_node* node); void* tommy_hashdyn_remove_existing(tommy_hashdyn* hashdyn, tommy_hashdyn_node* node);
/**
* Calls the specified function for each element in the hashtable.
*/
void tommy_hashdyn_foreach(tommy_hashdyn* hashdyn, tommy_foreach_func* func);
/**
* Calls the specified function with argument for each element in the hashtable.
*/
void tommy_hashdyn_foreach_arg(tommy_hashdyn* hashdyn, tommy_foreach_arg_func* func, void* arg);
/** /**
* Gets the number of elements. * Gets the number of elements.
*/ */

View file

@ -313,6 +313,7 @@ tommy_inline tommy_bool_t tommy_list_empty(tommy_list* list)
tommy_inline void tommy_list_foreach(tommy_list* list, tommy_foreach_func* func) tommy_inline void tommy_list_foreach(tommy_list* list, tommy_foreach_func* func)
{ {
tommy_node* node = tommy_list_head(list); tommy_node* node = tommy_list_head(list);
while (node) { while (node) {
void* data = node->data; void* data = node->data;
node = node->next; node = node->next;
@ -326,6 +327,7 @@ tommy_inline void tommy_list_foreach(tommy_list* list, tommy_foreach_func* func)
tommy_inline void tommy_list_foreach_arg(tommy_list* list, tommy_foreach_arg_func* func, void* arg) tommy_inline void tommy_list_foreach_arg(tommy_list* list, tommy_foreach_arg_func* func, void* arg)
{ {
tommy_node* node = tommy_list_head(list); tommy_node* node = tommy_list_head(list);
while (node) { while (node) {
void* data = node->data; void* data = node->data;
node = node->next; node = node->next;

View file

@ -64,16 +64,25 @@ typedef int tommy_bool_t; /**< Generic boolean type. */
/******************************************************************************/ /******************************************************************************/
/* heap */ /* heap */
/* by default uses malloc/realloc/free */ /* by default uses malloc/calloc/realloc/free */
/** /**
* Generic malloc(), realloc() and free() functions. * Generic malloc(), calloc(), realloc() and free() functions.
* Redefine them to what you need. By default they map to the C malloc(), realloc() and free(). * Redefine them to what you need. By default they map to the C malloc(), calloc(), realloc() and free().
*/ */
#if !defined(tommy_malloc) && !defined(tommy_realloc) && !defined(tommy_free) #if !defined(tommy_malloc) || !defined(tommy_calloc) || !defined(tommy_realloc) || !defined(tommy_free)
#include <stdlib.h> #include <stdlib.h>
#endif
#if !defined(tommy_malloc)
#define tommy_malloc malloc #define tommy_malloc malloc
#endif
#if !defined(tommy_calloc)
#define tommy_calloc calloc
#endif
#if !defined(tommy_realloc)
#define tommy_realloc realloc #define tommy_realloc realloc
#endif
#if !defined(tommy_free)
#define tommy_free free #define tommy_free free
#endif #endif
@ -109,7 +118,7 @@ typedef int tommy_bool_t; /**< Generic boolean type. */
*/ */
#if !defined(tommy_likely) #if !defined(tommy_likely)
#if defined(__GNUC__) #if defined(__GNUC__)
#define tommy_likely(x) __builtin_expect(!!(x),1) #define tommy_likely(x) __builtin_expect(!!(x), 1)
#else #else
#define tommy_likely(x) (x) #define tommy_likely(x) (x)
#endif #endif
@ -120,7 +129,7 @@ typedef int tommy_bool_t; /**< Generic boolean type. */
*/ */
#if !defined(tommy_unlikely) #if !defined(tommy_unlikely)
#if defined(__GNUC__) #if defined(__GNUC__)
#define tommy_unlikely(x) __builtin_expect(!!(x),0) #define tommy_unlikely(x) __builtin_expect(!!(x), 0)
#else #else
#define tommy_unlikely(x) (x) #define tommy_unlikely(x) (x)
#endif #endif
@ -278,7 +287,7 @@ typedef void tommy_foreach_arg_func(void* arg, void* obj);
* Integer log2 for constants. * Integer log2 for constants.
* You can use it only for exact power of 2 up to 256. * You can use it only for exact power of 2 up to 256.
*/ */
#define TOMMY_ILOG2(value) ((value) == 256 ? 8 : (value) == 128 ? 7 :(value) == 64 ? 6 : (value) == 32 ? 5 : (value) == 16 ? 4 : (value) == 8 ? 3 : (value) == 4 ? 2 : (value) == 2 ? 1 : 0) #define TOMMY_ILOG2(value) ((value) == 256 ? 8 : (value) == 128 ? 7 : (value) == 64 ? 6 : (value) == 32 ? 5 : (value) == 16 ? 4 : (value) == 8 ? 3 : (value) == 4 ? 2 : (value) == 2 ? 1 : 0)
/** /**
* Bit scan reverse or integer log2. * Bit scan reverse or integer log2.
@ -356,7 +365,7 @@ tommy_inline unsigned tommy_ctz_u32(tommy_uint32_t value)
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
}; };
return TOMMY_DE_BRUIJN_INDEX_CTZ[(tommy_uint32_t)(((value & -value) * 0x077CB531U)) >> 27]; return TOMMY_DE_BRUIJN_INDEX_CTZ[(tommy_uint32_t)(((value & - value) * 0x077CB531U)) >> 27];
#endif #endif
} }