Mon, 14 Oct 2013 13:36:28 +0200
minor fixes in snprintf and build system
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
5 | * |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
8 | * |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
11 | * |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
15 | * |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include <stdlib.h> |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | #include <string.h> |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include "map.h" |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | UcxMap *ucx_map_new(size_t size) { |
88 | 35 | return ucx_map_new_allocator(size, NULL); |
36 | } | |
37 | ||
38 | UcxMap *ucx_map_new_allocator(size_t size, UcxAllocator *allocator) { | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
39 | if(size == 0) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
40 | size = 16; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
41 | } |
88 | 42 | |
43 | if(!allocator) { | |
44 | allocator = ucx_default_allocator(); | |
45 | } | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
46 | |
88 | 47 | UcxMap *map = (UcxMap*)allocator->malloc(allocator->pool, sizeof(UcxMap)); |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | if(map == NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | return NULL; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | } |
88 | 51 | |
52 | map->allocator = allocator; | |
53 | map->map = (UcxMapElement**)allocator->calloc( | |
54 | allocator->pool, | |
55 | size, | |
56 | sizeof(UcxMapElement*)); | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | if(map->map == NULL) { |
88 | 58 | allocator->free(allocator->pool, map); |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | return NULL; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | map->size = size; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
62 | map->count = 0; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | return map; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
67 | void ucx_map_free_elmlist(UcxMap *map) { |
31 | 68 | for (size_t n = 0 ; n < map->size ; n++) { |
69 | UcxMapElement *elem = map->map[n]; | |
70 | if (elem != NULL) { | |
71 | do { | |
72 | UcxMapElement *next = elem->next; | |
88 | 73 | map->allocator->free(map->allocator->pool, elem->key.data); |
74 | map->allocator->free(map->allocator->pool, elem); | |
31 | 75 | elem = next; |
76 | } while (elem != NULL); | |
77 | } | |
78 | } | |
88 | 79 | map->allocator->free(map->allocator->pool, map->map); |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
80 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
81 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
82 | void ucx_map_free(UcxMap *map) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
83 | ucx_map_free_elmlist(map); |
88 | 84 | map->allocator->free(map->allocator->pool, map); |
31 | 85 | } |
86 | ||
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
87 | int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to, |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
88 | copy_func fnc, void *data) { |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
89 | UcxMapIterator i = ucx_map_iterator(from); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
90 | void *value; |
88 | 91 | UCX_MAP_FOREACH(key, value, i) { |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
92 | int ret = ucx_map_put(to, i.cur->key, fnc ? fnc(value, data) : value); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
93 | if(ret != 0) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
94 | return 1; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
95 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
96 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
97 | return 0; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
98 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
99 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
100 | UcxMap *ucx_map_clone(UcxMap *map, copy_func fnc, void *data) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
101 | size_t bs = (map->count * 5) >> 1; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
102 | UcxMap *newmap = ucx_map_new(bs > map->size ? bs : map->size); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
103 | if(newmap == NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
104 | return NULL; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
105 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
106 | ucx_map_copy(map, newmap, fnc, data); |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
107 | return newmap; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
108 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
109 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
110 | int ucx_map_rehash(UcxMap *map) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
111 | size_t load = (map->size * 3) >> 2; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
112 | if (map->count > load) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
113 | UcxMap oldmap; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
114 | oldmap.map = map->map; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
115 | oldmap.size = map->size; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
116 | oldmap.count = map->count; |
88 | 117 | oldmap.allocator = map->allocator; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
118 | |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
119 | map->size = (map->count * 5) >> 1; |
88 | 120 | map->map = (UcxMapElement**)map->allocator->calloc( |
121 | map->allocator->pool, | |
122 | map->size, | |
123 | sizeof(UcxMapElement*)); | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
124 | if(map->map == NULL) { |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
125 | *map = oldmap; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
126 | return 1; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
127 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
128 | map->count = 0; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
129 | ucx_map_copy(&oldmap, map, NULL, NULL); |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
130 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
131 | /* free the UcxMapElement list of oldmap */ |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
132 | ucx_map_free_elmlist(&oldmap); |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
133 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
134 | return 0; |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
135 | } |
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
136 | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137 | int ucx_map_put(UcxMap *map, UcxKey key, void *data) { |
88 | 138 | UcxAllocator *allocator = map->allocator; |
139 | ||
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | if(key.hash == 0) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | key.hash = ucx_hash((char*)key.data, key.len); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | |
31 | 144 | size_t slot = key.hash%map->size; |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
145 | UcxMapElement *restrict elm = map->map[slot]; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
146 | UcxMapElement *restrict prev = NULL; |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
147 | |
31 | 148 | while (elm != NULL && elm->key.hash < key.hash) { |
149 | prev = elm; | |
150 | elm = elm->next; | |
151 | } | |
152 | ||
153 | if (elm == NULL || elm->key.hash != key.hash) { | |
88 | 154 | UcxMapElement *e = (UcxMapElement*)allocator->malloc( |
155 | allocator->pool, | |
156 | sizeof(UcxMapElement)); | |
31 | 157 | if(e == NULL) { |
158 | return -1; | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | } |
31 | 160 | e->key.data = NULL; |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
161 | if (prev) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
162 | prev->next = e; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
163 | } else { |
31 | 164 | map->map[slot] = e; |
165 | } | |
166 | e->next = elm; | |
167 | elm = e; | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | } |
31 | 169 | |
170 | if(elm->key.data == NULL) { | |
88 | 171 | void *kd = allocator->malloc(allocator->pool, key.len); |
31 | 172 | if (kd == NULL) { |
173 | return -1; | |
174 | } | |
175 | memcpy(kd, key.data, key.len); | |
176 | key.data = kd; | |
177 | elm->key = key; | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
178 | map->count++; |
31 | 179 | } |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | elm->data = data; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182 | return 0; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184 | |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
185 | void* ucx_map_get_and_remove(UcxMap *map, UcxKey key, _Bool remove) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186 | if(key.hash == 0) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | key.hash = ucx_hash((char*)key.data, key.len); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | } |
31 | 189 | |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
190 | size_t slot = key.hash%map->size; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
191 | UcxMapElement *restrict elm = map->map[slot]; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
192 | UcxMapElement *restrict pelm = NULL; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
193 | while (elm && elm->key.hash <= key.hash) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194 | if(elm->key.hash == key.hash) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
195 | int n = (key.len > elm->key.len) ? elm->key.len : key.len; |
31 | 196 | if (memcmp(elm->key.data, key.data, n) == 0) { |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
197 | void *data = elm->data; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
198 | if (remove) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
199 | if (pelm) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
200 | pelm->next = elm->next; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
201 | } else { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
202 | map->map[slot] = elm->next; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
203 | } |
88 | 204 | map->allocator->free(map->allocator->pool, elm); |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
205 | map->count--; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
206 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
207 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
208 | return data; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
209 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | } |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
211 | pelm = elm; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
212 | elm = pelm->next; |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
213 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
214 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
215 | return NULL; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
216 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
217 | |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
218 | void *ucx_map_get(UcxMap *map, UcxKey key) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
219 | return ucx_map_get_and_remove(map, key, 0); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
220 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
221 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
222 | void *ucx_map_remove(UcxMap *map, UcxKey key) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
223 | return ucx_map_get_and_remove(map, key, 1); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
224 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
225 | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
226 | UcxKey ucx_key(void *data, size_t len) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
227 | UcxKey key; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
228 | key.data = data; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
229 | key.len = len; |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
230 | key.hash = ucx_hash((const char*) data, len); |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
231 | return key; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
232 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
233 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
234 | |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
36
diff
changeset
|
235 | int ucx_hash(const char *data, size_t len) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
236 | /* murmur hash 2 */ |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
237 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
238 | int m = 0x5bd1e995; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
239 | int r = 24; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
240 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
241 | int h = 25 ^ len; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
242 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
243 | int i = 0; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
244 | while (len >= 4) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
245 | int k = data[i + 0] & 0xFF; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
246 | k |= (data[i + 1] & 0xFF) << 8; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
247 | k |= (data[i + 2] & 0xFF) << 16; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
248 | k |= (data[i + 3] & 0xFF) << 24; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
249 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
250 | k *= m; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
251 | k ^= k >> r; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
252 | k *= m; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
253 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
254 | h *= m; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
255 | h ^= k; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
256 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
257 | i += 4; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
258 | len -= 4; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
259 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
260 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
261 | switch (len) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
262 | case 3: h ^= (data[i + 2] & 0xFF) << 16; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
263 | /* no break */ |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
264 | case 2: h ^= (data[i + 1] & 0xFF) << 8; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
265 | /* no break */ |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
266 | case 1: h ^= (data[i + 0] & 0xFF); h *= m; |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
267 | /* no break */ |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
268 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
270 | h ^= h >> 13; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
271 | h *= m; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272 | h ^= h >> 15; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
273 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
274 | return h; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
275 | } |
31 | 276 | |
277 | UcxMapIterator ucx_map_iterator(UcxMap *map) { | |
278 | UcxMapIterator i; | |
279 | i.map = map; | |
280 | i.cur = NULL; | |
281 | i.index = 0; | |
282 | return i; | |
283 | } | |
284 | ||
88 | 285 | int ucx_map_iter_next(UcxMapIterator *i, UcxKey *key, void **elm) { |
31 | 286 | UcxMapElement *e = i->cur; |
287 | ||
288 | if(e == NULL) { | |
289 | e = i->map->map[0]; | |
290 | } else { | |
291 | e = e->next; | |
292 | } | |
293 | ||
294 | while(i->index < i->map->size) { | |
295 | if(e != NULL) { | |
296 | if(e->data != NULL) { | |
297 | i->cur = e; | |
298 | *elm = e->data; | |
88 | 299 | *key = e->key; |
31 | 300 | return 0; |
301 | } | |
302 | ||
303 | e = e->next; | |
304 | } else { | |
305 | i->index++; | |
306 | ||
307 | if(i->index < i->map->size) { | |
308 | e = i->map->map[i->index]; | |
309 | } | |
310 | } | |
311 | } | |
312 | ||
313 | return 1; | |
314 | } | |
36
450d2d5f4735
server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
31
diff
changeset
|
315 |