30 #include <string.h> |
30 #include <string.h> |
31 |
31 |
32 #include "map.h" |
32 #include "map.h" |
33 |
33 |
34 UcxMap *ucx_map_new(size_t size) { |
34 UcxMap *ucx_map_new(size_t size) { |
|
35 return ucx_map_new_allocator(size, NULL); |
|
36 } |
|
37 |
|
38 UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator) { |
35 if(size == 0) { |
39 if(size == 0) { |
36 size = 16; |
40 size = 16; |
37 } |
41 } |
38 |
42 |
39 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap)); |
43 if(!allocator) { |
|
44 allocator = ucx_default_allocator(); |
|
45 } |
|
46 |
|
47 UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap)); |
40 if(map == NULL) { |
48 if(map == NULL) { |
41 return NULL; |
49 return NULL; |
42 } |
50 } |
43 |
51 |
44 map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*)); |
52 map->allocator = allocator; |
|
53 map->map = (UcxMapElement**)allocator->calloc( |
|
54 allocator->pool, |
|
55 size, |
|
56 sizeof(UcxMapElement*)); |
45 if(map->map == NULL) { |
57 if(map->map == NULL) { |
46 free(map); |
58 allocator->free(allocator->pool, map); |
47 return NULL; |
59 return NULL; |
48 } |
60 } |
49 map->size = size; |
61 map->size = size; |
50 map->count = 0; |
62 map->count = 0; |
51 |
63 |
56 for (size_t n = 0 ; n < map->size ; n++) { |
68 for (size_t n = 0 ; n < map->size ; n++) { |
57 UcxMapElement *elem = map->map[n]; |
69 UcxMapElement *elem = map->map[n]; |
58 if (elem != NULL) { |
70 if (elem != NULL) { |
59 do { |
71 do { |
60 UcxMapElement *next = elem->next; |
72 UcxMapElement *next = elem->next; |
61 free(elem->key.data); |
73 map->allocator->free(map->allocator->pool, elem->key.data); |
62 free(elem); |
74 free(elem); |
63 elem = next; |
75 elem = next; |
64 } while (elem != NULL); |
76 } while (elem != NULL); |
65 } |
77 } |
66 } |
78 } |
67 free(map->map); |
79 map->allocator->free(map->allocator->pool, map->map); |
68 } |
80 } |
69 |
81 |
70 void ucx_map_free(UcxMap *map) { |
82 void ucx_map_free(UcxMap *map) { |
71 ucx_map_free_elmlist(map); |
83 ucx_map_free_elmlist(map); |
72 free(map); |
84 map->allocator->free(map->allocator->pool, map); |
73 } |
85 } |
74 |
86 |
75 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, |
87 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, |
76 copy_func fnc, void *data) { |
88 copy_func fnc, void *data) { |
77 UcxMapIterator i = ucx_map_iterator(from); |
89 UcxMapIterator i = ucx_map_iterator(from); |
100 if (map->count > load) { |
112 if (map->count > load) { |
101 UcxMap oldmap; |
113 UcxMap oldmap; |
102 oldmap.map = map->map; |
114 oldmap.map = map->map; |
103 oldmap.size = map->size; |
115 oldmap.size = map->size; |
104 oldmap.count = map->count; |
116 oldmap.count = map->count; |
|
117 oldmap.allocator = map->allocator; |
105 |
118 |
106 map->size = (map->count * 5) >> 1; |
119 map->size = (map->count * 5) >> 1; |
107 map->map = (UcxMapElement**)calloc(map->size, sizeof(UcxMapElement*)); |
120 map->map = (UcxMapElement**)map->allocator->calloc( |
|
121 map->allocator->pool, |
|
122 map->size, |
|
123 sizeof(UcxMapElement*)); |
108 if(map->map == NULL) { |
124 if(map->map == NULL) { |
109 *map = oldmap; |
125 *map = oldmap; |
110 return 1; |
126 return 1; |
111 } |
127 } |
112 map->count = 0; |
128 map->count = 0; |
117 } |
133 } |
118 return 0; |
134 return 0; |
119 } |
135 } |
120 |
136 |
121 int ucx_map_put(UcxMap *map, UcxKey key, void *data) { |
137 int ucx_map_put(UcxMap *map, UcxKey key, void *data) { |
|
138 UcxAllocator *allocator = map->allocator; |
|
139 |
122 if(key.hash == 0) { |
140 if(key.hash == 0) { |
123 key.hash = ucx_hash((char*)key.data, key.len); |
141 key.hash = ucx_hash((char*)key.data, key.len); |
124 } |
142 } |
125 |
143 |
126 size_t slot = key.hash%map->size; |
144 size_t slot = key.hash%map->size; |