Sat, 18 Aug 2012 11:39:34 +0200
using non blocking IO for request input
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | */ |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | #include <stdlib.h> |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | #include <string.h> |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | #include "map.h" |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | UcxMap *ucx_map_new(size_t size) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap)); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | if(map == NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | return NULL; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | |
31 | 16 | map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*)); |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | if(map->map == NULL) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | free(map); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | return NULL; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | map->size = size; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | return map; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | |
31 | 26 | void ucx_map_free(UcxMap *map) { |
27 | for (size_t n = 0 ; n < map->size ; n++) { | |
28 | UcxMapElement *elem = map->map[n]; | |
29 | if (elem != NULL) { | |
30 | do { | |
31 | UcxMapElement *next = elem->next; | |
32 | free(elem->key.data); | |
33 | free(elem); | |
34 | elem = next; | |
35 | } while (elem != NULL); | |
36 | } | |
37 | } | |
38 | free(map->map); | |
39 | free(map); | |
40 | } | |
41 | ||
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | int ucx_map_put(UcxMap *map, UcxKey key, void *data) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | if(key.hash == 0) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | key.hash = ucx_hash((char*)key.data, key.len); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | |
31 | 47 | size_t slot = key.hash%map->size; |
48 | UcxMapElement *elm = map->map[slot]; | |
49 | UcxMapElement *prev = NULL; | |
21
627b09ee74e4
New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
15
diff
changeset
|
50 | |
31 | 51 | while (elm != NULL && elm->key.hash < key.hash) { |
52 | prev = elm; | |
53 | elm = elm->next; | |
54 | } | |
55 | ||
56 | if (elm == NULL || elm->key.hash != key.hash) { | |
57 | UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement)); | |
58 | if(e == NULL) { | |
59 | return -1; | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | } |
31 | 61 | e->key.data = NULL; |
62 | if (prev == NULL) { | |
63 | map->map[slot] = e; | |
64 | } else { | |
65 | prev->next = e; | |
66 | } | |
67 | e->next = elm; | |
68 | elm = e; | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | } |
31 | 70 | |
71 | if(elm->key.data == NULL) { | |
72 | void *kd = malloc(key.len); | |
73 | if (kd == NULL) { | |
74 | return -1; | |
75 | } | |
76 | memcpy(kd, key.data, key.len); | |
77 | key.data = kd; | |
78 | elm->key = key; | |
79 | } | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | elm->data = data; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | return 0; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | void* ucx_map_get(UcxMap *map, UcxKey key) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | if(key.hash == 0) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | key.hash = ucx_hash((char*)key.data, key.len); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | } |
31 | 89 | |
90 | UcxMapElement *elm = map->map[key.hash%map->size]; | |
91 | while (elm != NULL && elm->key.hash <= key.hash) { | |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | if(elm->key.hash == key.hash) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | int n = (key.len > elm->key.len) ? elm->key.len : key.len; |
31 | 94 | if (memcmp(elm->key.data, key.data, n) == 0) { |
15
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | return elm->data; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | elm = elm->next; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | return NULL; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | UcxKey ucx_key(void *data, size_t len) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | UcxKey key; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | key.data = data; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107 | key.len = len; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108 | key.hash = ucx_hash(data, len); |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
109 | return key; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
111 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | int ucx_hash(char *data, size_t len) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | /* murmur hash 2 */ |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 | int m = 0x5bd1e995; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | int r = 24; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | int h = 25 ^ len; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | int i = 0; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | while (len >= 4) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123 | int k = data[i + 0] & 0xFF; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124 | k |= (data[i + 1] & 0xFF) << 8; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125 | k |= (data[i + 2] & 0xFF) << 16; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
126 | k |= (data[i + 3] & 0xFF) << 24; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
127 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128 | k *= m; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
129 | k ^= k >> r; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130 | k *= m; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132 | h *= m; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | h ^= k; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
134 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135 | i += 4; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136 | len -= 4; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | switch (len) { |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | case 3: h ^= (data[i + 2] & 0xFF) << 16; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | case 2: h ^= (data[i + 1] & 0xFF) << 8; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142 | case 1: h ^= (data[i + 0] & 0xFF); h *= m; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | } |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145 | h ^= h >> 13; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146 | h *= m; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | h ^= h >> 15; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148 | |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | return h; |
cff9c4101dd7
Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | } |
31 | 151 | |
152 | UcxMapIterator ucx_map_iterator(UcxMap *map) { | |
153 | UcxMapIterator i; | |
154 | i.map = map; | |
155 | i.cur = NULL; | |
156 | i.index = 0; | |
157 | return i; | |
158 | } | |
159 | ||
160 | int ucx_map_iter_next(UcxMapIterator *i, void **elm) { | |
161 | UcxMapElement *e = i->cur; | |
162 | ||
163 | if(e == NULL) { | |
164 | e = i->map->map[0]; | |
165 | } else { | |
166 | e = e->next; | |
167 | } | |
168 | ||
169 | while(i->index < i->map->size) { | |
170 | if(e != NULL) { | |
171 | if(e->data != NULL) { | |
172 | i->cur = e; | |
173 | *elm = e->data; | |
174 | return 0; | |
175 | } | |
176 | ||
177 | e = e->next; | |
178 | } else { | |
179 | i->index++; | |
180 | ||
181 | if(i->index < i->map->size) { | |
182 | e = i->map->map[i->index]; | |
183 | } | |
184 | } | |
185 | } | |
186 | ||
187 | return 1; | |
188 | } |