src/hash_map.c

changeset 1587
7156d6699410
parent 1499
d0a0a41405bb
equal deleted inserted replaced
1586:7f1cadc3ebc1 1587:7156d6699410
76 76
77 // free the map structure 77 // free the map structure
78 cxFree(map->collection.allocator, map); 78 cxFree(map->collection.allocator, map);
79 } 79 }
80 80
81 static void *cx_hash_map_put( 81 static CxMapEntry cx_hash_map_put(
82 CxMap *map, 82 CxMap *map,
83 CxHashKey key, 83 CxHashKey key,
84 void *value 84 void *value
85 ) { 85 ) {
86 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map; 86 struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
115 // allocate new element 115 // allocate new element
116 struct cx_hash_map_element_s *e = cxMalloc( 116 struct cx_hash_map_element_s *e = cxMalloc(
117 allocator, 117 allocator,
118 sizeof(struct cx_hash_map_element_s) + map->collection.elem_size 118 sizeof(struct cx_hash_map_element_s) + map->collection.elem_size
119 ); 119 );
120 if (e == NULL) return NULL; // LCOV_EXCL_LINE 120 if (e == NULL) return (CxMapEntry){NULL, NULL}; // LCOV_EXCL_LINE
121 121
122 // write the value 122 // write the value
123 if (value == NULL) { 123 if (value == NULL) {
124 memset(e->data, 0, map->collection.elem_size); 124 memset(e->data, 0, map->collection.elem_size);
125 } else if (map->collection.store_pointer) { 125 } else if (map->collection.store_pointer) {
130 130
131 // copy the key 131 // copy the key
132 void *kd = cxMalloc(allocator, key.len); 132 void *kd = cxMalloc(allocator, key.len);
133 if (kd == NULL) { // LCOV_EXCL_START 133 if (kd == NULL) { // LCOV_EXCL_START
134 cxFree(allocator, e); 134 cxFree(allocator, e);
135 return NULL; 135 return (CxMapEntry){NULL, NULL};
136 } // LCOV_EXCL_STOP 136 } // LCOV_EXCL_STOP
137 memcpy(kd, key.data, key.len); 137 memcpy(kd, key.data, key.len);
138 e->key.data = kd; 138 e->key.data = kd;
139 e->key.len = key.len; 139 e->key.len = key.len;
140 e->key.hash = hash; 140 e->key.hash = hash;
150 150
151 // increase the size 151 // increase the size
152 map->collection.size++; 152 map->collection.size++;
153 } 153 }
154 154
155 // return pointer to the element 155 // return the entry
156 return elm->data; 156 return (CxMapEntry){&elm->key, elm->data};
157 } 157 }
158 158
159 static void cx_hash_map_unlink( 159 static void cx_hash_map_unlink(
160 struct cx_hash_map_s *hash_map, 160 struct cx_hash_map_s *hash_map,
161 size_t slot, 161 size_t slot,

mercurial