| 342 | } |
| 343 | |
| 344 | void strmap_clear_(struct strmap *map) |
| 345 | { |
| 346 | uintptr_t inline_stack[STRMAP_NUM_ITER_PARENTS]; |
| 347 | uintptr_t *stack = inline_stack; |
| 348 | size_t num = 0, max = STRMAP_NUM_ITER_PARENTS; |
| 349 | uintptr_t cur; |
| 350 | bool have_cur; |
| 351 | |
| 352 | if (!map->u.n) |
| 353 | return; |
| 354 | |
| 355 | /* Post-order without recursion: slot pointers tagged in their |
| 356 | * low bits (0 = visit child[0], 1 = visit child[1], 2 = free). */ |
| 357 | cur = (uintptr_t)map; |
| 358 | have_cur = true; |
| 359 | |
| 360 | while (have_cur || num) { |
| 361 | struct strmap *slot; |
| 362 | unsigned int tag; |
| 363 | |
| 364 | if (!have_cur) |
| 365 | cur = stack[--num]; |
| 366 | have_cur = false; |
| 367 | slot = (struct strmap *)(cur & ~(uintptr_t)3); |
| 368 | tag = cur & 3; |
| 369 | |
| 370 | if (slot->v) { |
| 371 | /* Leaf: caller-owned, keep. */ |
| 372 | continue; |
| 373 | } |
| 374 | if (tag == 2) { |
| 375 | free(slot->u.n); |
| 376 | continue; |
| 377 | } |
| 378 | |
| 379 | /* tag 0 or 1: requeue for the next phase, then descend. */ |
| 380 | { |
| 381 | struct strmap *child = &slot->u.n->child[tag]; |
| 382 | |
| 383 | if (num == max) { |
| 384 | uintptr_t *ns; |
| 385 | size_t nmax = max ? max * 2 : 64; |
| 386 | |
| 387 | if (stack == inline_stack) { |
| 388 | ns = malloc(nmax * sizeof(*ns)); |
| 389 | if (ns) |
| 390 | memcpy(ns, stack, |
| 391 | num * sizeof(*ns)); |
| 392 | } else { |
| 393 | ns = realloc(stack, nmax * sizeof(*ns)); |
| 394 | } |
| 395 | if (ns) { |
| 396 | stack = ns; |
| 397 | max = nmax; |
| 398 | } else { |
| 399 | /* OOM: finish this subtree |
| 400 | * recursively. */ |
| 401 | clear(*child); |