1
2
3
4
5
6
7
8
9
10
11
12 |
/* Explicitly override malloc/free etc when using tcmalloc. */ #if defined(USE_TCMALLOC) #define malloc(size) tc_malloc(size) #define calloc(count,size) tc_calloc(count,size) #define realloc(ptr,size) tc_realloc(ptr,size) #define free(ptr) tc_free(ptr) #elif defined(USE_JEMALLOC) #define malloc(size) je_malloc(size) #define calloc(count,size) je_calloc(count,size) #define realloc(ptr,size) je_realloc(ptr,size) #define free(ptr) je_free(ptr) #endif |
1
2
3
4
5
6
7
8
9
10
11
12 |
#define PREFIX_SIZE (sizeof(size_t)) #ifndef HAVE_MALLOC_SIZE size_t zmalloc_size( void
*ptr) { void
*realptr = ( char *)ptr-PREFIX_SIZE; size_t size = *((size_t*)realptr); /* Assume at least that all the allocations are padded at sizeof(long) by * the underlying allocator. */ if
(size&( sizeof ( long )-1)) size += sizeof ( long )-(size&( sizeof ( long )-1)); return
size+PREFIX_SIZE; } #endif |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
sds sdsnewlen( const
void *init, size_t initlen) { struct
sdshdr *sh; sh = malloc( sizeof ( struct
sdshdr)+initlen+1); if (sh == NULL) return
NULL; sh->len = ( int )initlen; sh->free = 0; if (initlen) { if (init) memcpy(sh->buf, init, initlen); else
memset(sh->buf,0,initlen); } sh->buf[initlen] = ‘\0‘ ; return
( char *)sh->buf; } |
1
2
3
4
5
6
7
8
9
10
11 |
static
unsigned int
dictGenHashFunction( const
unsigned char
*buf, int
len) { unsigned int
hash = 5381; while
(len--) hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */ return
hash; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 |
dictEntry *he, *nextHe; if (ht->table[i] == NULL) continue ; /* For each hash entry on this slot... */ he = ht->table[i]; while (he) { unsigned int
h; nextHe = he->next; /* Get the new element index */ h = dictHashKey(ht, he->key) & n.sizemask; he->next = n.table[h]; n.table[h] = he; ht->used--; /* Pass to the next element */ he = nextHe; } 最后释放旧表指向的哈希字典,将n指向的地址复制给旧表指针。 free(ht->table); /* Remap the new hashtable in the old */ *ht = n; return
DICT_OK; |
原文地址:http://www.cnblogs.com/itdef/p/3774522.html