src/server/ucx/map.c

Sun, 23 Jun 2013 13:51:49 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 23 Jun 2013 13:51:49 +0200
changeset 75
6195c92262a2
parent 66
74babc0082b7
child 88
73b3485e96f1
permissions
-rw-r--r--

merge

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) {
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
35 if(size == 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
36 size = 16;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
37 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
38
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 UcxMap *map = (UcxMap*)malloc(sizeof(UcxMap));
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 if(map == NULL) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
44 map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 if(map->map == NULL) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 free(map);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 map->size = size;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
50 map->count = 0;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 return map;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
55 void ucx_map_free_elmlist(UcxMap *map) {
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
56 for (size_t n = 0 ; n < map->size ; n++) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
57 UcxMapElement *elem = map->map[n];
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
58 if (elem != NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
59 do {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
60 UcxMapElement *next = elem->next;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
61 free(elem->key.data);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
62 free(elem);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
63 elem = next;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
64 } while (elem != NULL);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
65 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
66 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
67 free(map->map);
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
68 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
69
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
70 void ucx_map_free(UcxMap *map) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
71 ucx_map_free_elmlist(map);
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
72 free(map);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
73 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
74
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
75 int ucx_map_copy(UcxMap *restrict from, UcxMap *restrict to,
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
76 copy_func fnc, void *data) {
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
77 UcxMapIterator i = ucx_map_iterator(from);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
78 void *value;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
79 UCX_MAP_FOREACH(value, i) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
80 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
81 if(ret != 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
82 return 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
83 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
84 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
85 return 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
86 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
87
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
88 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
89 size_t bs = (map->count * 5) >> 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
90 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
91 if(newmap == NULL) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
92 return NULL;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
93 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
94 ucx_map_copy(map, newmap, fnc, data);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
95 return newmap;
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
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
98 int ucx_map_rehash(UcxMap *map) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
99 size_t load = (map->size * 3) >> 2;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
100 if (map->count > load) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
101 UcxMap oldmap;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
102 oldmap.map = map->map;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
103 oldmap.size = map->size;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
104 oldmap.count = map->count;
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 map->size = (map->count * 5) >> 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
107 map->map = (UcxMapElement**)calloc(map->size, sizeof(UcxMapElement*));
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
108 if(map->map == NULL) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
109 *map = oldmap;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
110 return 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
111 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
112 map->count = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
113 ucx_map_copy(&oldmap, map, NULL, NULL);
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
114
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
115 /* free the UcxMapElement list of oldmap */
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
116 ucx_map_free_elmlist(&oldmap);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
117 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
118 return 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
119 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
120
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 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
122 if(key.hash == 0) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 key.hash = ucx_hash((char*)key.data, key.len);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
126 size_t slot = key.hash%map->size;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
127 UcxMapElement *restrict elm = map->map[slot];
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
128 UcxMapElement *restrict prev = NULL;
21
627b09ee74e4 New configuration loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 15
diff changeset
129
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
130 while (elm != NULL && elm->key.hash < key.hash) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
131 prev = elm;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
132 elm = elm->next;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
133 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
134
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
135 if (elm == NULL || elm->key.hash != key.hash) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
136 UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
137 if(e == NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
138 return -1;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 }
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
140 e->key.data = NULL;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
141 if (prev) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
142 prev->next = e;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
143 } else {
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
144 map->map[slot] = e;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
145 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
146 e->next = elm;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
147 elm = e;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 }
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
149
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
150 if(elm->key.data == NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
151 void *kd = malloc(key.len);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
152 if (kd == NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
153 return -1;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
154 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
155 memcpy(kd, key.data, key.len);
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
156 key.data = kd;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
157 elm->key = key;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
158 map->count++;
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
159 }
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 elm->data = data;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 return 0;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
165 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
166 if(key.hash == 0) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 key.hash = ucx_hash((char*)key.data, key.len);
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 }
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
169
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
170 size_t slot = key.hash%map->size;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
171 UcxMapElement *restrict elm = map->map[slot];
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
172 UcxMapElement *restrict pelm = NULL;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
173 while (elm && elm->key.hash <= key.hash) {
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 if(elm->key.hash == key.hash) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 int n = (key.len > elm->key.len) ? elm->key.len : key.len;
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
176 if (memcmp(elm->key.data, key.data, n) == 0) {
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
177 void *data = elm->data;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
178 if (remove) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
179 if (pelm) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
180 pelm->next = elm->next;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
181 } else {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
182 map->map[slot] = elm->next;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
183 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
184 free(elm);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
185 map->count--;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
186 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
187
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
188 return data;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 }
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
191 pelm = elm;
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
192 elm = pelm->next;
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 return NULL;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 }
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
198 void *ucx_map_get(UcxMap *map, UcxKey key) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
199 return ucx_map_get_and_remove(map, key, 0);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
200 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
201
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
202 void *ucx_map_remove(UcxMap *map, UcxKey key) {
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
203 return ucx_map_get_and_remove(map, key, 1);
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
204 }
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
205
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 UcxKey ucx_key(void *data, size_t len) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 UcxKey key;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 key.data = data;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 key.len = len;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
210 key.hash = ucx_hash((const char*) data, len);
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 return key;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 }
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
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
215 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
216 /* murmur hash 2 */
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 int m = 0x5bd1e995;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 int r = 24;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 int h = 25 ^ len;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 int i = 0;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 while (len >= 4) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 int k = data[i + 0] & 0xFF;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 k |= (data[i + 1] & 0xFF) << 8;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 k |= (data[i + 2] & 0xFF) << 16;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 k |= (data[i + 3] & 0xFF) << 24;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 k *= m;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 k ^= k >> r;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 k *= m;
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 h *= m;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 h ^= k;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 i += 4;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 len -= 4;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 }
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 switch (len) {
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 case 3: h ^= (data[i + 2] & 0xFF) << 16;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
243 /* no break */
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 case 2: h ^= (data[i + 1] & 0xFF) << 8;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
245 /* no break */
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 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
247 /* no break */
15
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 }
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 h ^= h >> 13;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 h *= m;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 h ^= h >> 15;
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 return h;
cff9c4101dd7 Replaced old utils with ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 }
31
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
256
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
257 UcxMapIterator ucx_map_iterator(UcxMap *map) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
258 UcxMapIterator i;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
259 i.map = map;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
260 i.cur = NULL;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
261 i.index = 0;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
262 return i;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
263 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
264
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
265 int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
266 UcxMapElement *e = i->cur;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
267
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
268 if(e == NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
269 e = i->map->map[0];
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
270 } else {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
271 e = e->next;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
272 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
273
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
274 while(i->index < i->map->size) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
275 if(e != NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
276 if(e->data != NULL) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
277 i->cur = e;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
278 *elm = e->data;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
279 return 0;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
280 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
281
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
282 e = e->next;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
283 } else {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
284 i->index++;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
285
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
286 if(i->index < i->map->size) {
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
287 e = i->map->map[i->index];
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
288 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
289 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
290 }
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
291
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
292 return 1;
280250e45ba6 updated ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
293 }
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
294
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
295 int ucx_map_load_enc(UcxMap *map, FILE *f, UcxAllocator allocator,
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
296 ucx_map_coder decoder, void* decdata) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
297
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
298 int c; int r, n;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
299
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
300 char *key, *value;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
301
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
302 while ((c = fgetc(f)) > 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
303 /* Discard leading spaces and comments */
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
304 if (c < 33) continue;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
305 if (c == '#' || c == '!') {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
306 while ((c = (char) fgetc(f)) > 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
307 if (c == '\n') break;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
308 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
309 continue;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
310 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
311
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
312 /* read into key buffer */
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
313 n = 16;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
314 key = (char*) malloc(n);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
315 r = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
316 do {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
317 if (c == '=') break;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
318 if (r > n - 2) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
319 n *= 2;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
320 key = (char*) realloc(key, n);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
321 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
322 key[r] = c;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
323 r++;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
324 } while ((c = fgetc(f)) > 0);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
325 if (c <= 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
326 free(key);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
327 return 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
328 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
329 key[r] = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
330 while (key[--r] == ' ') key[r] = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
331
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
332 /* skip whitespaces */
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
333 while ((c = fgetc(f)) > 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
334 if (c > 32) break;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
335 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
336 if (c <= 0) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
337 free(key);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
338 return 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
339 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
340
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
341 /* read into value buffer */
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
342 n = 64;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
343 value = (char*) malloc(n);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
344 r = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
345 do {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
346 if (c == '\n') break;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
347 if (r > n - 2) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
348 n *= 2;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
349 value = (char*) realloc(value, n);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
350 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
351 value[r] = c;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
352 r++;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
353 } while ((c = fgetc(f)) > 0);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
354 value[r] = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
355 while (value[--r] < 33) value[r] = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
356
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
357 if (decoder) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
358 size_t decodedSize;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
359 void *decoded = decoder(value, decdata, &decodedSize);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
360 free(value);
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
361 value = (char*) decoded;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
362 r = decodedSize;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
363 } else {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
364 r += 2;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
365 value = (char*) realloc(value, r);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
366 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
367
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
368 if (allocator.pool) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
369 void *pooledValue = allocator.malloc(allocator.pool, r);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
370 memcpy(pooledValue, value, r);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
371 free(value);
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
372 value = (char*) pooledValue;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
373 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
374
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
375 ucx_map_cstr_put(map, key, value);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
376 free(key);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
377 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
378
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
379 return 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
380 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
381
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
382 int ucx_map_store_enc(UcxMap *map, FILE *f,
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
383 ucx_map_coder encoder, void *encdata) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
384 UcxMapIterator iter = ucx_map_iterator(map);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
385 char *k, *v;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
386 sstr_t key, value;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
387 size_t written;
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
388
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
389 UCX_MAP_FOREACH(v, iter) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
390 k = (char*) iter.cur->key.data;
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
391 key = sstrn(k, iter.cur->key.len);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
392 if (encoder) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
393 size_t encodedSize;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
394 void *encoded = encoder(v, encdata, &encodedSize);
66
74babc0082b7 added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 36
diff changeset
395 value = sstrn((char*) encoded,encodedSize - 1);
36
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
396 } else {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
397 value = sstr(v);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
398 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
399
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
400 written = 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
401 written += fwrite(key.ptr, 1, key.length, f);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
402 written += fwrite(" = ", 1, 3, f);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
403 written += fwrite(value.ptr, 1, value.length, f);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
404 written += fwrite("\n", 1, 1, f);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
405
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
406 if (encoder) {
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
407 free(value.ptr);
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
408 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
409
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
410 if (written != key.length + value.length + 4) return 1;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
411 }
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
412
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
413 return 0;
450d2d5f4735 server can reload configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 31
diff changeset
414 }

mercurial