1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "private-lib-core.h"
typedef struct lws_map_hashtable {
struct lws_map *map_owner; /* so items can find map */
lws_dll2_owner_t ho;
} lws_map_hashtable_t;
struct lws_map {
lws_map_info_t info;
/* array of info.modulo x lws_map_hashtable_t overallocated */
};
typedef struct lws_map_item {
lws_dll2_t list; /* owned by hashtable */
size_t keylen;
size_t valuelen;
/* key then value is overallocated */
} lws_map_item_t;
/*
* lwsac-aware allocator
*/
void *
lws_map_alloc_lwsac(struct lws_map *map, size_t x)
{
return lwsac_use((struct lwsac **)map->info.opaque, x,
(size_t)map->info.aux);
}
void
lws_map_free_lwsac(void *v)
{
}
/*
* Default allocation / free if none given in info
*/
void *
lws_map_alloc_lws_malloc(struct lws_map *mo, size_t x)
{
return lws_malloc(x, __func__);
}
void
lws_map_free_lws_free(void *v)
{
lws_free(v);
}
/*
* This just needs to approximate a flat distribution, it's not related to
* security at all.
*/
lws_map_hash_t
lws_map_hash_from_key_default(const lws_map_key_t key, size_t kl)
{
lws_map_hash_t h = 0x12345678;
const uint8_t *u = (const uint8_t *)key;
while (kl--)
h = ((
(((h & 0x1fffffff /* coverity */ ) << 7) |
(h >> 25)) +
0xa1b2c3d4) ^ (*u++)) ^ h;
return h;
}
int
lws_map_compare_key_default(const lws_map_key_t key1, size_t kl1,
const lws_map_value_t key2, size_t kl2)
{
if (kl1 != kl2)
return 1;
return memcmp(key1, key2, kl1);
}
lws_map_t *
lws_map_create(const lws_map_info_t *info)
{
lws_map_t *map;
lws_map_alloc_t a = info->_alloc;
size_t modulo = info->modulo;
lws_map_hashtable_t *ht;
size_t size;
if (!a)
a = lws_map_alloc_lws_malloc;
if (!modulo)
modulo = 8;
size = sizeof(*map) + (modulo * sizeof(lws_map_hashtable_t));
map = lws_malloc(size, __func__);
if (!map)
return NULL;
memset(map, 0, size);
map->info = *info;
map->info._alloc = a;
map->info.modulo = modulo;
if (!info->_free)
map->info._free = lws_map_free_lws_free;
if (!info->_hash)
map->info._hash = lws_map_hash_from_key_default;
if (!info->_compare)
map->info._compare = lws_map_compare_key_default;
ht = (lws_map_hashtable_t *)&map[1];
while (modulo--)
ht[modulo].map_owner = map;
return map;
}
static int
ho_free_item(struct lws_dll2 *d, void *user)
{
lws_map_item_t *i = lws_container_of(d, lws_map_item_t, list);
lws_map_item_destroy(i);
return 0;
}
void
lws_map_destroy(lws_map_t **pmap)
{
lws_map_hashtable_t *ht;
lws_map_t *map = *pmap;
if (!map)
return;
/* empty out all the hashtables */
ht = (lws_map_hashtable_t *)&(map[1]);
while (map->info.modulo--) {
lws_dll2_foreach_safe(&ht->ho, ht, ho_free_item);
ht++;
}
/* free the map itself */
lws_free_set_NULL(*pmap);
}
lws_map_item_t *
lws_map_item_create(lws_map_t *map,
const lws_map_key_t key, size_t keylen,
const lws_map_value_t value, size_t valuelen)
{
lws_map_hashtable_t *ht;
lws_map_item_t *item;
lws_map_hash_t h;
size_t hti;
uint8_t *u;
item = lws_map_item_lookup(map, key, keylen);
if (item)
lws_map_item_destroy(item);
item = map->info._alloc(map, sizeof(*item) + keylen + valuelen);
if (!item)
return NULL;
lws_dll2_clear(&item->list);
item->keylen = keylen;
item->valuelen = valuelen;
u = (uint8_t *)&item[1];
memcpy(u, key, keylen);
u += keylen;
if (value)
memcpy(u, value, valuelen);
h = map->info._hash(key, keylen);
hti = h % map->info.modulo;
ht = (lws_map_hashtable_t *)&map[1];
lws_dll2_add_head(&item->list, &ht[hti].ho);
return item;
}
void
lws_map_item_destroy(lws_map_item_t *item)
{
lws_map_hashtable_t *ht = lws_container_of(item->list.owner,
lws_map_hashtable_t, ho);
lws_dll2_remove(&item->list);
ht->map_owner->info._free(item);
}
lws_map_item_t *
lws_map_item_lookup(lws_map_t *map, const lws_map_key_t key, size_t keylen)
{
lws_map_hash_t h = map->info._hash(key, keylen);
lws_map_hashtable_t *ht = (lws_map_hashtable_t *)&map[1];
lws_start_foreach_dll(struct lws_dll2 *, p,
ht[h % map->info.modulo].ho.head) {
lws_map_item_t *i = lws_container_of(p, lws_map_item_t, list);
if (!map->info._compare(key, keylen, &i[1], i->keylen))
return i;
} lws_end_foreach_dll(p);
return NULL;
}
const void *
lws_map_item_key(lws_map_item_t *_item)
{
return ((void *)&_item[1]);
}
const void *
lws_map_item_value(lws_map_item_t *_item)
{
return (void *)(((uint8_t *)&_item[1]) + _item->keylen);
}
size_t
lws_map_item_key_len(lws_map_item_t *_item)
{
return _item->keylen;
}
size_t
lws_map_item_value_len(lws_map_item_t *_item)
{
return _item->valuelen;
}
|