| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <proton/object.h> |
| 23 |
|
| 24 |
#include "core/memory.h" |
| 25 |
|
| 26 |
#include <stddef.h> |
| 27 |
#include <assert.h> |
| 28 |
|
| 29 |
#define PNI_ENTRY_FREE (0) |
| 30 |
#define PNI_ENTRY_LINK (1) |
| 31 |
#define PNI_ENTRY_TAIL (2) |
| 32 |
|
| 33 |
typedef struct { |
| 34 |
void *key; |
| 35 |
void *value; |
| 36 |
size_t next; |
| 37 |
uint8_t state; |
| 38 |
} pni_entry_t; |
| 39 |
|
| 40 |
struct pn_map_t { |
| 41 |
const pn_class_t *key; |
| 42 |
const pn_class_t *value; |
| 43 |
pni_entry_t *entries; |
| 44 |
size_t capacity; |
| 45 |
size_t addressable; |
| 46 |
size_t size; |
| 47 |
uintptr_t (*hashcode)(void *key); |
| 48 |
bool (*equals)(void *a, void *b); |
| 49 |
float load_factor; |
| 50 |
}; |
| 51 |
|
| 52 |
static void pn_map_finalize(void *object) |
| 53 |
{ |
| 54 |
pn_map_t *map = (pn_map_t *) object; |
| 55 |
|
| 56 |
for (size_t i = 0; i < map->capacity; i++) { |
| 57 |
if (map->entries[i].state != PNI_ENTRY_FREE) { |
| 58 |
pn_class_decref(map->key, map->entries[i].key); |
| 59 |
pn_class_decref(map->value, map->entries[i].value); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
pni_mem_subdeallocate(pn_class(map), map, map->entries); |
| 64 |
} |
| 65 |
|
| 66 |
static uintptr_t pn_map_hashcode(void *object) |
| 67 |
{ |
| 68 |
pn_map_t *map = (pn_map_t *) object; |
| 69 |
|
| 70 |
uintptr_t hashcode = 0; |
| 71 |
|
| 72 |
for (size_t i = 0; i < map->capacity; i++) { |
| 73 |
if (map->entries[i].state != PNI_ENTRY_FREE) { |
| 74 |
void *key = map->entries[i].key; |
| 75 |
void *value = map->entries[i].value; |
| 76 |
hashcode += pn_hashcode(key) ^ pn_hashcode(value); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return hashcode; |
| 81 |
} |
| 82 |
|
| 83 |
static void pni_map_allocate(pn_map_t *map) |
| 84 |
{ |
| 85 |
map->entries = (pni_entry_t *) pni_mem_suballocate(pn_class(map), map, map->capacity * sizeof (pni_entry_t)); |
| 86 |
if (map->entries != NULL) { |
| 87 |
for (size_t i = 0; i < map->capacity; i++) { |
| 88 |
map->entries[i].key = NULL; |
| 89 |
map->entries[i].value = NULL; |
| 90 |
map->entries[i].next = 0; |
| 91 |
map->entries[i].state = PNI_ENTRY_FREE; |
| 92 |
} |
| 93 |
} |
| 94 |
map->size = 0; |
| 95 |
} |
| 96 |
|
| 97 |
static int pn_map_inspect(void *obj, pn_string_t *dst) |
| 98 |
{ |
| 99 |
assert(obj); |
| 100 |
pn_map_t *map = (pn_map_t *) obj; |
| 101 |
int err = pn_string_addf(dst, "{"); |
| 102 |
if (err) return err; |
| 103 |
pn_handle_t entry = pn_map_head(map); |
| 104 |
bool first = true; |
| 105 |
while (entry) { |
| 106 |
if (first) { |
| 107 |
first = false; |
| 108 |
} else { |
| 109 |
err = pn_string_addf(dst, ", "); |
| 110 |
if (err) return err; |
| 111 |
} |
| 112 |
err = pn_class_inspect(map->key, pn_map_key(map, entry), dst); |
| 113 |
if (err) return err; |
| 114 |
err = pn_string_addf(dst, ": "); |
| 115 |
if (err) return err; |
| 116 |
err = pn_class_inspect(map->value, pn_map_value(map, entry), dst); |
| 117 |
if (err) return err; |
| 118 |
entry = pn_map_next(map, entry); |
| 119 |
} |
| 120 |
return pn_string_addf(dst, "}"); |
| 121 |
} |
| 122 |
|
| 123 |
#define pn_map_initialize NULL |
| 124 |
#define pn_map_compare NULL |
| 125 |
|
| 126 |
pn_map_t *pn_map(const pn_class_t *key, const pn_class_t *value, |
| 127 |
size_t capacity, float load_factor) |
| 128 |
{ |
| 129 |
static const pn_class_t clazz = PN_CLASS(pn_map); |
| 130 |
|
| 131 |
pn_map_t *map = (pn_map_t *) pn_class_new(&clazz, sizeof(pn_map_t)); |
| 132 |
map->key = key; |
| 133 |
map->value = value; |
| 134 |
map->capacity = capacity ? capacity : 16; |
| 135 |
map->addressable = (size_t) (map->capacity * 0.86); |
| 136 |
if (!map->addressable) map->addressable = map->capacity; |
| 137 |
map->load_factor = load_factor; |
| 138 |
map->hashcode = pn_hashcode; |
| 139 |
map->equals = pn_equals; |
| 140 |
pni_map_allocate(map); |
| 141 |
return map; |
| 142 |
} |
| 143 |
|
| 144 |
size_t pn_map_size(pn_map_t *map) |
| 145 |
{ |
| 146 |
assert(map); |
| 147 |
return map->size; |
| 148 |
} |
| 149 |
|
| 150 |
static float pni_map_load(pn_map_t *map) |
| 151 |
{ |
| 152 |
return ((float) map->size) / ((float) map->addressable); |
| 153 |
} |
| 154 |
|
| 155 |
static bool pni_map_ensure(pn_map_t *map, size_t capacity) |
| 156 |
{ |
| 157 |
float load = pni_map_load(map); |
| 158 |
if (capacity <= map->capacity && load <= map->load_factor) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
size_t oldcap = map->capacity; |
| 163 |
|
| 164 |
while (map->capacity < capacity || pni_map_load(map) > map->load_factor) { |
| 165 |
map->capacity *= 2; |
| 166 |
map->addressable = (size_t) (0.86 * map->capacity); |
| 167 |
} |
| 168 |
|
| 169 |
pni_entry_t *entries = map->entries; |
| 170 |
pni_map_allocate(map); |
| 171 |
|
| 172 |
for (size_t i = 0; i < oldcap; i++) { |
| 173 |
if (entries[i].state != PNI_ENTRY_FREE) { |
| 174 |
void *key = entries[i].key; |
| 175 |
void *value = entries[i].value; |
| 176 |
pn_map_put(map, key, value); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
for (size_t i = 0; i < oldcap; i++) { |
| 181 |
if (entries[i].state != PNI_ENTRY_FREE) { |
| 182 |
void *key = entries[i].key; |
| 183 |
void *value = entries[i].value; |
| 184 |
pn_class_decref(map->key, key); |
| 185 |
pn_class_decref(map->value, value); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
pni_mem_subdeallocate(pn_class(map), map, entries); |
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
static pni_entry_t *pni_map_entry(pn_map_t *map, void *key, pni_entry_t **pprev, bool create) |
| 194 |
{ |
| 195 |
uintptr_t hashcode = map->hashcode(key); |
| 196 |
|
| 197 |
pni_entry_t *entry = &map->entries[hashcode % map->addressable]; |
| 198 |
pni_entry_t *prev = NULL; |
| 199 |
|
| 200 |
if (entry->state == PNI_ENTRY_FREE) { |
| 201 |
if (create) { |
| 202 |
entry->state = PNI_ENTRY_TAIL; |
| 203 |
entry->key = key; |
| 204 |
pn_class_incref(map->key, key); |
| 205 |
map->size++; |
| 206 |
return entry; |
| 207 |
} else { |
| 208 |
return NULL; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
while (true) { |
| 213 |
if (map->equals(entry->key, key)) { |
| 214 |
if (pprev) *pprev = prev; |
| 215 |
return entry; |
| 216 |
} |
| 217 |
|
| 218 |
if (entry->state == PNI_ENTRY_TAIL) { |
| 219 |
break; |
| 220 |
} else { |
| 221 |
prev = entry; |
| 222 |
entry = &map->entries[entry->next]; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if (create) { |
| 227 |
if (pni_map_ensure(map, map->size + 1)) { |
| 228 |
// if we had to grow the table we need to start over |
| 229 |
return pni_map_entry(map, key, pprev, create); |
| 230 |
} |
| 231 |
|
| 232 |
size_t empty = 0; |
| 233 |
for (size_t i = 0; i < map->capacity; i++) { |
| 234 |
size_t idx = map->capacity - i - 1; |
| 235 |
if (map->entries[idx].state == PNI_ENTRY_FREE) { |
| 236 |
empty = idx; |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
entry->next = empty; |
| 241 |
entry->state = PNI_ENTRY_LINK; |
| 242 |
map->entries[empty].state = PNI_ENTRY_TAIL; |
| 243 |
map->entries[empty].key = key; |
| 244 |
pn_class_incref(map->key, key); |
| 245 |
if (pprev) *pprev = entry; |
| 246 |
map->size++; |
| 247 |
return &map->entries[empty]; |
| 248 |
} else { |
| 249 |
return NULL; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
int pn_map_put(pn_map_t *map, void *key, void *value) |
| 254 |
{ |
| 255 |
assert(map); |
| 256 |
pni_entry_t *entry = pni_map_entry(map, key, NULL, true); |
| 257 |
void *dref_val = entry->value; |
| 258 |
entry->value = value; |
| 259 |
pn_class_incref(map->value, value); |
| 260 |
pn_class_decref(map->value, dref_val); |
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
void *pn_map_get(pn_map_t *map, void *key) |
| 265 |
{ |
| 266 |
assert(map); |
| 267 |
pni_entry_t *entry = pni_map_entry(map, key, NULL, false); |
| 268 |
return entry ? entry->value : NULL; |
| 269 |
} |
| 270 |
|
| 271 |
static void pni_map_rehash(pn_map_t *map, size_t index) |
| 272 |
{ |
| 273 |
//reinsert entries in chain starting at index |
| 274 |
assert(map); |
| 275 |
size_t i = index; |
| 276 |
bool complete = false; |
| 277 |
while (!complete) { |
| 278 |
pni_entry_t *entry = &map->entries[i]; |
| 279 |
assert(entry); |
| 280 |
assert(entry->state != PNI_ENTRY_FREE); |
| 281 |
size_t current = i; |
| 282 |
if (entry->state == PNI_ENTRY_TAIL) { |
| 283 |
complete = true; |
| 284 |
} else { |
| 285 |
assert(entry->state == PNI_ENTRY_LINK); |
| 286 |
i = entry->next; |
| 287 |
} |
| 288 |
uintptr_t hashcode = map->hashcode(entry->key); |
| 289 |
pni_entry_t *reloc = &map->entries[hashcode % map->addressable]; |
| 290 |
if (reloc->state == PNI_ENTRY_FREE) { |
| 291 |
//correct addressable slot is available, copy into that... |
| 292 |
reloc->state = PNI_ENTRY_TAIL; |
| 293 |
reloc->key = entry->key; |
| 294 |
reloc->value = entry->value; |
| 295 |
//...then free the current entry |
| 296 |
entry->key = NULL; |
| 297 |
entry->value = NULL; |
| 298 |
entry->state = PNI_ENTRY_FREE; |
| 299 |
entry->next = 0; |
| 300 |
} else { |
| 301 |
//iterate to end of chain... |
| 302 |
while (reloc->state == PNI_ENTRY_LINK) { |
| 303 |
reloc = &map->entries[reloc->next]; |
| 304 |
} |
| 305 |
assert(reloc->state == PNI_ENTRY_TAIL); |
| 306 |
//... and append current entry |
| 307 |
reloc->state = PNI_ENTRY_LINK; |
| 308 |
reloc->next = current; |
| 309 |
entry->state = PNI_ENTRY_TAIL; |
| 310 |
entry->next = 0; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
void pn_map_del(pn_map_t *map, void *key) |
| 316 |
{ |
| 317 |
assert(map); |
| 318 |
pni_entry_t *prev = NULL; |
| 319 |
pni_entry_t *entry = pni_map_entry(map, key, &prev, false); |
| 320 |
if (entry) { |
| 321 |
uint8_t orig_state = entry->state; |
| 322 |
size_t orig_next = entry->next; |
| 323 |
|
| 324 |
void *dref_key = entry->key; |
| 325 |
void *dref_value = entry->value; |
| 326 |
if (prev) { |
| 327 |
prev->next = 0; |
| 328 |
prev->state = PNI_ENTRY_TAIL; |
| 329 |
} |
| 330 |
entry->state = PNI_ENTRY_FREE; |
| 331 |
entry->next = 0; |
| 332 |
entry->key = NULL; |
| 333 |
entry->value = NULL; |
| 334 |
map->size--; |
| 335 |
|
| 336 |
if (orig_state == PNI_ENTRY_LINK) { |
| 337 |
pni_map_rehash(map, orig_next); |
| 338 |
} |
| 339 |
|
| 340 |
// do this last as it may trigger further deletions |
| 341 |
pn_class_decref(map->key, dref_key); |
| 342 |
pn_class_decref(map->value, dref_value); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
pn_handle_t pn_map_head(pn_map_t *map) |
| 347 |
{ |
| 348 |
assert(map); |
| 349 |
for (size_t i = 0; i < map->capacity; i++) |
| 350 |
{ |
| 351 |
if (map->entries[i].state != PNI_ENTRY_FREE) { |
| 352 |
return (pn_handle_t)(i + 1); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
pn_handle_t pn_map_next(pn_map_t *map, pn_handle_t entry) |
| 360 |
{ |
| 361 |
for (size_t i = (size_t)entry; i < map->capacity; i++) { |
| 362 |
if (map->entries[i].state != PNI_ENTRY_FREE) { |
| 363 |
return (pn_handle_t)(i + 1); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
return 0; |
| 368 |
} |
| 369 |
|
| 370 |
void *pn_map_key(pn_map_t *map, pn_handle_t entry) |
| 371 |
{ |
| 372 |
assert(map); |
| 373 |
assert(entry); |
| 374 |
return map->entries[(size_t)entry - 1].key; |
| 375 |
} |
| 376 |
|
| 377 |
void *pn_map_value(pn_map_t *map, pn_handle_t entry) |
| 378 |
{ |
| 379 |
assert(map); |
| 380 |
assert(entry); |
| 381 |
return map->entries[(size_t)entry - 1].value; |
| 382 |
} |
| 383 |
|
| 384 |
struct pn_hash_t { |
| 385 |
pn_map_t map; |
| 386 |
}; |
| 387 |
|
| 388 |
static uintptr_t pni_identity_hashcode(void *obj) |
| 389 |
{ |
| 390 |
return (uintptr_t ) obj; |
| 391 |
} |
| 392 |
|
| 393 |
static bool pni_identity_equals(void *a, void *b) |
| 394 |
{ |
| 395 |
return a == b; |
| 396 |
} |
| 397 |
|
| 398 |
#define CID_pni_uintptr CID_pn_void |
| 399 |
static const pn_class_t *pni_uintptr_reify(void *object); |
| 400 |
#define pni_uintptr_new NULL |
| 401 |
#define pni_uintptr_free NULL |
| 402 |
#define pni_uintptr_initialize NULL |
| 403 |
static void pni_uintptr_incref(void *object) {} |
| 404 |
static void pni_uintptr_decref(void *object) {} |
| 405 |
static int pni_uintptr_refcount(void *object) { return -1; } |
| 406 |
#define pni_uintptr_finalize NULL |
| 407 |
#define pni_uintptr_hashcode NULL |
| 408 |
#define pni_uintptr_compare NULL |
| 409 |
#define pni_uintptr_inspect NULL |
| 410 |
|
| 411 |
static const pn_class_t PN_UINTPTR[] = {PN_METACLASS(pni_uintptr)}; |
| 412 |
|
| 413 |
static const pn_class_t *pni_uintptr_reify(void *object) |
| 414 |
{ |
| 415 |
return PN_UINTPTR; |
| 416 |
} |
| 417 |
|
| 418 |
pn_hash_t *pn_hash(const pn_class_t *clazz, size_t capacity, float load_factor) |
| 419 |
{ |
| 420 |
pn_hash_t *hash = (pn_hash_t *) pn_map(PN_UINTPTR, clazz, capacity, load_factor); |
| 421 |
hash->map.hashcode = pni_identity_hashcode; |
| 422 |
hash->map.equals = pni_identity_equals; |
| 423 |
return hash; |
| 424 |
} |
| 425 |
|
| 426 |
size_t pn_hash_size(pn_hash_t *hash) |
| 427 |
{ |
| 428 |
return pn_map_size(&hash->map); |
| 429 |
} |
| 430 |
|
| 431 |
int pn_hash_put(pn_hash_t *hash, uintptr_t key, void *value) |
| 432 |
{ |
| 433 |
return pn_map_put(&hash->map, (void *) key, value); |
| 434 |
} |
| 435 |
|
| 436 |
void *pn_hash_get(pn_hash_t *hash, uintptr_t key) |
| 437 |
{ |
| 438 |
return pn_map_get(&hash->map, (void *) key); |
| 439 |
} |
| 440 |
|
| 441 |
void pn_hash_del(pn_hash_t *hash, uintptr_t key) |
| 442 |
{ |
| 443 |
pn_map_del(&hash->map, (void *) key); |
| 444 |
} |
| 445 |
|
| 446 |
pn_handle_t pn_hash_head(pn_hash_t *hash) |
| 447 |
{ |
| 448 |
return pn_map_head(&hash->map); |
| 449 |
} |
| 450 |
|
| 451 |
pn_handle_t pn_hash_next(pn_hash_t *hash, pn_handle_t entry) |
| 452 |
{ |
| 453 |
return pn_map_next(&hash->map, entry); |
| 454 |
} |
| 455 |
|
| 456 |
uintptr_t pn_hash_key(pn_hash_t *hash, pn_handle_t entry) |
| 457 |
{ |
| 458 |
return (uintptr_t) pn_map_key(&hash->map, entry); |
| 459 |
} |
| 460 |
|
| 461 |
void *pn_hash_value(pn_hash_t *hash, pn_handle_t entry) |
| 462 |
{ |
| 463 |
return pn_map_value(&hash->map, entry); |
| 464 |
} |