LCOV - code coverage report
Current view: top level - include - chashmap.h (source / functions) Coverage Total Hit
Test: c_collections 2.0.0 Lines: 100.0 % 10 10
Test Date: 2026-11-15 18:57:05 Functions: - 0 0
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              : MIT License
       3              : 
       4              : Copyright (c) 2026 - A bunch of nerds
       5              : 
       6              : Permission is hereby granted, free of charge, to any person obtaining a copy
       7              : of this software and associated documentation files (the "Software"), to deal
       8              : in the Software without restriction, including without limitation the rights
       9              : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      10              : copies of the Software, and to permit persons to whom the Software is
      11              : furnished to do so, subject to the following conditions:
      12              : 
      13              : The above copyright notice and this permission notice shall be included in all
      14              : copies or substantial portions of the Software.
      15              : 
      16              : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      17              : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      18              : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      19              : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      20              : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      21              : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      22              : SOFTWARE.
      23              : */
      24              : 
      25              : #pragma once
      26              : 
      27              : #include <string.h>
      28              : 
      29              : #include "citerators.h"
      30              : #include "common.h"
      31              : 
      32              : // Everything from here to the matching pop is the public ABI and the shared
      33              : // library exports it. The library builds with -fvisibility=hidden, so anything
      34              : // outside these blocks stays internal.
      35              : #pragma GCC visibility push(default)
      36              : 
      37              : // A hash map that picks its backend from the key and value types.
      38              : //
      39              : // Open addressing is for keys and values that are both integral and at most
      40              : // 8 bytes: 16-byte slots of key and value with the occupied bits in a byte
      41              : // array at the tail of the same block, linear probing, backward-shift
      42              : // deletion (no deleted-slot markers), doubling past a load of 0.70 and
      43              : // halving under 0.25. It allocates nothing per entry and iterates in slot
      44              : // order.
      45              : //
      46              : // Separate chaining is for everything else (strings, long double, structs):
      47              : // chained nodes with 23 bytes of inline storage for a short key or value, at
      48              : // least 16 buckets (a power of two), growing by 4 past (buckets + 1) * 1.5
      49              : // entries and shrinking by 4 under (buckets + 1) / 8. Its entries sit on one
      50              : // list, so iteration goes newest first.
      51              : //
      52              : // int->int, long->double and float->uint32_t use open addressing;
      53              : // string->int, int->string and int->long double use separate chaining. Both
      54              : // accept a custom hash, which goes through a finalizer, and both start in a
      55              : // cheap fast hash mode and switch to a keyed one when the keys collide too
      56              : // much (see chashmap_create_full()).
      57              : 
      58              : // The default first size of the bucket array.
      59              : #define CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE 16
      60              : 
      61              : // The hash map. The struct is opaque and chmap is the handle.
      62              : typedef struct chashmap chashmap;
      63              : 
      64              : typedef chashmap *chmap;
      65              : 
      66              : // Creation
      67              : 
      68              : // Creates a map with every option. initial_bucket_array_size can't be 0; 1 to
      69              : // 16 gives 16, and a larger value is rounded up to a power of two. key_type and
      70              : // val_type pick the backend: Open addressing when both are integral and at
      71              : // most 8 bytes (char to long long, float, double; never long double) and
      72              : // custom_key_equality_proc is NULL, separate chaining otherwise. mmgmt_procs
      73              : // is your allocator or NULL. A custom_key_equality_proc needs a
      74              : // custom_hashing_proc that agrees with it (equal keys hash the same), or the
      75              : // call fails. On failure it gives NULL and an error string in *err. Destroy the
      76              : // map with chmap_destroy().
      77              : //
      78              : // Without custom procs a key of an unknown type (a struct) is compared and
      79              : // hashed byte by byte, padding included. Zero such a key with memset() before
      80              : // you set its members when you use the raw functions. Through the typed macros
      81              : // the map sees the macro's copy of the key; GCC 11 and later zero its padding,
      82              : // and with any other compiler a padded key needs both procs.
      83              : //
      84              : // A float or double -0.0 key becomes 0.0 before it's hashed, stored or
      85              : // compared, custom hash included. A long double key always uses separate
      86              : // chaining and is hashed and compared by value: -0.0L equals 0.0L, padding
      87              : // bits never matter, and every NaN is one key. A custom hash for long double
      88              : // gets the raw bytes, so it must be value-based itself (hash frexpl(), say), or
      89              : // equal values can land in different buckets.
      90              : //
      91              : // Every map starts in the fast mode: A Fibonacci multiply for integral, float,
      92              : // double and pointer keys, XXH64 with a secret seed for strings and other
      93              : // buffers, and the murmur3 finalizer on a custom hash. Inserts of new keys are
      94              : // measured, and a map whose keys collide more than a random function would let
      95              : // them (an insert far from its home slot, a chain of 20 nodes, or 256 inserts
      96              : // that cost too much in total) switches to the keyed mode: A seeded
      97              : // two-multiply mixer for fixed-width keys and custom results, SipHash-1-3 for
      98              : // buffers. A lookup never writes to the map. The switch keeps every entry, and
      99              : // under separate chaining every reference, the insertion order and every
     100              : // iterator; when an open-addressing rebuild can't allocate, the map stays fast
     101              : // and tries again later. Each growth of a keyed map tries the fast mode again.
     102              : // The secrets come from getrandom(2) once per process, and which keys are the
     103              : // same key never depends on the mode. chashmap_create_full(3) gives the exact
     104              : // thresholds and what an attacker can still force.
     105              : chmap chashmap_create_full(size_t initial_bucket_array_size,
     106              :                            ccol_data_type key_type, ccol_data_type val_type,
     107              :                            ccol_memmgmt_procs *mmgmt_procs,
     108              :                            ccol_hashing_proc custom_hashing_proc,
     109              :                            ccol_key_equality_proc custom_key_equality_proc,
     110              :                            char **err);
     111              : 
     112              : // Creates a hash map with the default allocator and the built-in hash.
     113              : // initial_bucket_array_size can't be 0; 1 to 16 gives 16, and a larger value
     114              : // is rounded up to a power of two. key_type and val_type describe the key and
     115              : // the value and pick the storage: Open addressing when both are integral and
     116              : // at most 8 bytes, separate chaining otherwise. Returns the map, or NULL with
     117              : // an error string in *err (err may be NULL; never free the string). Destroy
     118              : // the map with chashmap_destroy(). See also chashmap_create_full().
     119              : static inline __attribute__((always_inline)) chmap
     120          259 : chashmap_create(size_t initial_bucket_array_size, ccol_data_type key_type,
     121              :                 ccol_data_type val_type, char **err) {
     122          267 :   return chashmap_create_full(initial_bucket_array_size, key_type, val_type,
     123              :                               NULL, NULL, NULL, err);
     124              : }
     125              : 
     126              : // Creates a hash map that takes all its memory from mmgmt_procs, your
     127              : // allocator table (NULL for the default allocator), and hashes with the
     128              : // built-in hash. The table must stay valid until the map is destroyed.
     129              : // initial_bucket_array_size can't be 0; 1 to 16 gives 16, and a larger value
     130              : // is rounded up to a power of two. key_type and val_type pick open addressing
     131              : // when both are integral and at most 8 bytes, separate chaining otherwise.
     132              : // Returns the map, or NULL with an error string in *err (err may be NULL;
     133              : // never free the string). Destroy it with chashmap_destroy().
     134        15825 : static inline __attribute__((always_inline)) chmap chashmap_create_mp(
     135              :     size_t initial_bucket_array_size, ccol_data_type key_type,
     136              :     ccol_data_type val_type, ccol_memmgmt_procs *mmgmt_procs, char **err) {
     137        15825 :   return chashmap_create_full(initial_bucket_array_size, key_type, val_type,
     138              :                               mmgmt_procs, NULL, NULL, err);
     139              : }
     140              : 
     141              : // Creates a hash map that hashes keys with custom_hashing_proc and allocates
     142              : // with the default allocator. The hash must give the same value for keys the
     143              : // map treats as equal (equal contents for a string); the map mixes the result
     144              : // further, so any function of the key works. initial_bucket_array_size can't
     145              : // be 0; 1 to 16 gives 16, and a larger value is rounded up to a power of two.
     146              : // key_type and val_type describe the key and the value. Returns the map, or
     147              : // NULL with an error string in *err (err may be NULL; never free the string).
     148              : // Destroy it with chashmap_destroy().
     149              : static inline __attribute__((always_inline)) chmap
     150            5 : chashmap_create_ch(size_t initial_bucket_array_size, ccol_data_type key_type,
     151              :                    ccol_data_type val_type,
     152              :                    ccol_hashing_proc custom_hashing_proc, char **err) {
     153            5 :   return chashmap_create_full(initial_bucket_array_size, key_type, val_type,
     154              :                               NULL, custom_hashing_proc, NULL, err);
     155              : }
     156              : 
     157              : // Operations
     158              : 
     159              : // Returns the number of key-value pairs that the map holds. Asserts on a NULL
     160              : // chm.
     161              : size_t chashmap_size(chmap chm);
     162              : 
     163              : // Removes and frees every element, so the map can be filled again without a
     164              : // new map. A new_bucket_array_size other than 0 also resizes the bucket array
     165              : // to that size rounded up to a power of two (16 at least); 0 keeps the size.
     166              : // Returns ccol_success, or ccol_not_enough_memory when the resize fails; every
     167              : // element is gone either way. Asserts on a NULL chm.
     168              : ccol_retval chashmap_reset(chmap chm, size_t new_bucket_array_size);
     169              : 
     170              : // Inserts the key-value pair, or replaces the value when the key is already
     171              : // there (the stored key stays). The map copies the bytes of both pairs. This is
     172              : // the raw insert for code that must not stop the program on failure. Returns
     173              : // ccol_success for a new key and ccol_key_already_present for an update, which
     174              : // is also a success. Returns ccol_invalid_args for a NULL pointer, a size of 0,
     175              : // or a key or value size that doesn't match a fixed-width declared type (char
     176              : // to long long, float, double, long double, pointers; ccol_string and
     177              : // ccol_other_types aren't checked), ccol_container_full at ccol_max_elem_count
     178              : // and ccol_not_enough_memory. key_pair and val_pair may point into this map (a
     179              : // chashmap_get_elem_ref() result, say), even when the insert grows it. An
     180              : // insert can invalidate every reference into the map. Open addressing doubles
     181              : // past a load of 0.70; chaining grows by 4 once elem_count >=
     182              : // (bucket_count + 1) * 1.5.
     183              : ccol_retval chashmap_insert_elem(chmap chm, const cmap_pair *key_pair,
     184              :                                  const cmap_pair *val_pair);
     185              : 
     186              : // Looks up the key and copies its value into target_buf, so you keep a copy
     187              : // that no later change of the map affects: min(stored size, target_buf_size)
     188              : // bytes, with the rest of target_buf zeroed. Returns ccol_success,
     189              : // ccol_key_not_found, or ccol_invalid_args for a NULL pointer, a size of 0 or
     190              : // a key size that doesn't match a fixed-width key type.
     191              : ccol_retval chashmap_get_elem_copy(chmap chm, const cmap_pair *key_pair,
     192              :                                    void *target_buf, size_t target_buf_size);
     193              : 
     194              : // Finds the value of the key without copying it: Fills your *val_pair with the
     195              : // location and size of the stored value. You may change those bytes in place,
     196              : // but not ptr or size themselves (that changes only your copy). The pointer is
     197              : // valid until an insert, delete or resize; lookups never invalidate it.
     198              : // Returns ccol_success, ccol_key_not_found, or ccol_invalid_args for a NULL
     199              : // pointer, a size of 0 or a key size that doesn't match a fixed-width key
     200              : // type. Every failure with a non-NULL val_pair sets it to {NULL, 0}.
     201              : ccol_retval chashmap_get_elem_ref(chmap chm, const cmap_pair *key_pair,
     202              :                                   cmap_pair *val_pair);
     203              : 
     204              : // Internal: Gives the accessor a separate-chaining map keeps for the value of a
     205              : // key, whose ptr field is the stored char *; the string arm of chmap_get_ptr()
     206              : // needs its address. An open-addressing map gives ccol_invalid_args, and every
     207              : // failure sets *val_accessor to NULL. Use chmap_get_ptr() instead.
     208              : ccol_retval chashmap__get_val_accessor(chmap chm, const cmap_pair *key_pair,
     209              :                                        const cmap_pair **val_accessor);
     210              : 
     211              : // Deletes the key and frees the map's copies of the key and the value. This is
     212              : // the raw delete for code that must not stop the program. Returns
     213              : // ccol_success, ccol_key_not_found, or ccol_invalid_args for a NULL pointer, a
     214              : // size of 0 or a key size that doesn't match a fixed-width key type. Treat
     215              : // every reference into the map as invalid after a delete: On an
     216              : // open-addressing map it moves other entries. Open addressing halves below a
     217              : // load of 0.25 and chaining shrinks by 4 once elem_count <
     218              : // (bucket_count + 1) / 8, never below the minimum.
     219              : ccol_retval chashmap_remove_elem(chmap chm, const cmap_pair *key_pair);
     220              : 
     221              : // Iteration
     222              : 
     223              : // Internal: Frees an iterator that chashmap_begin_iter() (declared in
     224              : // citerators.h) made; every map iterator calls it through its free function.
     225              : void chashmap__iterator_destroy(cmap_iterator *iter);
     226              : 
     227              : // Destruction
     228              : 
     229              : // Internal: Frees every key, value and bucket of the map; the body of
     230              : // chashmap_destroy(). Use the macro instead.
     231              : void chashmap__destroy(chmap chm);
     232              : 
     233              : // Destroys the map and calls val_dtor (when not NULL) with dtor_ctx once for
     234              : // each live value, right before its entry is freed. Use it for a map whose
     235              : // values own memory, such as pointers to your own objects: It allocates
     236              : // nothing, so it reaches every value even when memory runs out, which a walk
     237              : // with chashmap_begin_iter() can't promise. val_pair is const and points into
     238              : // storage that is about to go away; read it and free what it owns. val_dtor
     239              : // must not change the map. A NULL chm does nothing, and chm isn't set to NULL.
     240              : void chashmap_destroy_with_dtor(chmap chm,
     241              :                                 void (*val_dtor)(const cmap_pair *val_pair,
     242              :                                                  void *dtor_ctx),
     243              :                                 void *dtor_ctx);
     244              : 
     245              : // Internal: Destroys *chm and sets it to NULL; the cleanup handler that the
     246              : // _scoped macros attach with ccol__destructor.
     247           12 : static inline void chashmap__destroy_cleanup(chmap *chm) {
     248           12 :   if (chm && *chm) {
     249            6 :     chashmap__destroy(*chm);
     250           11 :     *chm = NULL;
     251              :   }
     252              : }
     253              : 
     254              : // Destroys the map: Frees every key, value and bucket, and sets chm to NULL so
     255              : // the handle can't be used after the free. chm must be a modifiable lvalue and
     256              : // is evaluated once; a NULL handle does nothing. Pointers that the map gave
     257              : // you (a char * from chmap_get(), say) are invalid afterwards.
     258              : #define chashmap_destroy(chm)  \
     259              :   ccol__chashmap_destroy_impl( \
     260              :       chm, ccol__uniq(ccol__chashmap_destroy_slot, __COUNTER__))
     261              : 
     262              : // Internal: The body of chashmap_destroy(). It takes the address of chm once
     263              : // into slot, a name from ccol__uniq(), so destroy macros can nest without
     264              : // -Wshadow warnings.
     265              : #define ccol__chashmap_destroy_impl(chm, slot) \
     266              :   do {                                         \
     267              :     __typeof__(chm) *slot = &(chm);            \
     268              :     chashmap__destroy(*slot);                  \
     269              :     *slot = NULL;                              \
     270              :   } while (0)
     271              : 
     272              : // Type-inferred declaration and creation macros
     273              : 
     274              : // Makes the typed chmap_* macros usable on a map handle that was created in
     275              : // another scope, such as a function parameter or a struct field. It declares
     276              : // only the key and value type information that those macros read; it creates
     277              : // and destroys nothing. KeyT and ValT must be the types the map was created
     278              : // with. An array type is refused at compile time; wrap it in a struct.
     279              : //
     280              : //   void process(chmap map) {
     281              : //     chmap_redeclare(map, int, char *);
     282              : //     chmap_insert(map, 42, "hello");
     283              : //   }
     284              : #define chmap_redeclare(hm_name, KeyT, ValT)                               \
     285              :   ccol__refuse_array_types("chmap", KeyT, ValT);                           \
     286              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) = \
     287              :       NULL;                                                                \
     288              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) = NULL
     289              : 
     290              : // Declares a hash map handle hm_name with key type KeyT and value type ValT,
     291              : // without creating a map. The handle is left uninitialized: Create the map
     292              : // with chmap_init(), chmap_init_mp(), chmap_init_ch() or chmap_init_full()
     293              : // before any other chmap_* macro, and destroy it with chmap_destroy(). It also
     294              : // works inside a struct. An array key or value type is refused at compile
     295              : // time; wrap it in a struct.
     296              : #define chmap_declare(hm_name, KeyT, ValT)                                \
     297              :   ccol__refuse_array_types("chmap", KeyT, ValT);                          \
     298              :   __typeof__(KeyT) *__attribute__((unused)) hm_name##__ccol_key_type_var; \
     299              :   __typeof__(ValT) *__attribute__((unused)) hm_name##__ccol_val_type_var; \
     300              :   chmap hm_name
     301              : 
     302              : // Declares a hash map handle hm_name with key type KeyT and value type ValT,
     303              : // set to NULL, without creating a map. Create it later with chmap_init() or
     304              : // another chmap_init_* macro. When the variable goes out of scope the map is
     305              : // destroyed by itself; chmap_destroy() before that is allowed and leaves NULL
     306              : // behind. An array key or value type is refused at compile time; wrap it in a
     307              : // struct.
     308              : #define chmap_declare_scoped(hm_name, KeyT, ValT)                          \
     309              :   ccol__refuse_array_types("chmap", KeyT, ValT);                           \
     310              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) = \
     311              :       NULL;                                                                \
     312              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) = \
     313              :       NULL;                                                                \
     314              :   chmap hm_name ccol__destructor(chashmap__destroy_cleanup) = NULL
     315              : 
     316              : // Creates the map for a handle that chmap_declare() or chmap_declare_scoped()
     317              : // declared, with every option; the key and value types come from that
     318              : // declaration. mmgmt_procs is your allocator table (NULL for the default; it
     319              : // must outlive the map), custom_hashing_proc your hash and
     320              : // custom_key_equality_proc your key comparison (NULL for the built-in ones).
     321              : // An equality proc needs a hash proc that gives equal keys the same hash, and
     322              : // selects separate chaining. Starts with
     323              : // CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE buckets. Ends the program with a
     324              : // fatal error when the map can't be created.
     325              : #define chmap_init_full(hm_name, mmgmt_procs, custom_hashing_proc,          \
     326              :                         custom_key_equality_proc)                           \
     327              :   do {                                                                      \
     328              :     char *ccol__chmap_err = NULL;                                           \
     329              :     hm_name = chashmap_create_full(                                         \
     330              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     331              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     332              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     333              :         (mmgmt_procs), (custom_hashing_proc), (custom_key_equality_proc),   \
     334              :         &ccol__chmap_err);                                                  \
     335              :     if (!hm_name) {                                                         \
     336              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     337              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     338              :     }                                                                       \
     339              :   } while (0)
     340              : 
     341              : // Declares a hash map handle hm_name for key type KeyT and value type ValT and
     342              : // creates the map, with every option. mmgmt_procs is your allocator table
     343              : // (NULL for the default; it must outlive the map), custom_hashing_proc your
     344              : // hash and custom_key_equality_proc your key comparison (NULL for the built-in
     345              : // ones). An equality proc needs a hash proc that gives equal keys the same
     346              : // hash. Ends the program with a fatal error when the map can't be created.
     347              : // Destroy the map with chmap_destroy(). An array key or value type is refused
     348              : // at compile time.
     349              : #define chmap_construct_full(hm_name, KeyT, ValT, mmgmt_procs,              \
     350              :                              custom_hashing_proc, custom_key_equality_proc) \
     351              :   ccol__refuse_array_types("chmap", KeyT, ValT);                            \
     352              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     353              :       NULL;                                                                 \
     354              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     355              :       NULL;                                                                 \
     356              :   chmap hm_name = NULL;                                                     \
     357              :   do {                                                                      \
     358              :     char *ccol__chmap_err = NULL;                                           \
     359              :     hm_name = chashmap_create_full(                                         \
     360              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     361              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     362              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     363              :         (mmgmt_procs), (custom_hashing_proc), (custom_key_equality_proc),   \
     364              :         &ccol__chmap_err);                                                  \
     365              :     if (!hm_name) {                                                         \
     366              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     367              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     368              :     }                                                                       \
     369              :   } while (0)
     370              : 
     371              : // Declares a hash map handle hm_name for key type KeyT and value type ValT and
     372              : // creates the map, with every option; the map is destroyed by itself when
     373              : // hm_name goes out of scope. mmgmt_procs is your allocator table (NULL for the
     374              : // default; it must outlive the map), custom_hashing_proc your hash and
     375              : // custom_key_equality_proc your key comparison (NULL for the built-in ones).
     376              : // An equality proc needs a hash proc that gives equal keys the same hash. Ends
     377              : // the program with a fatal error when the map can't be created. An array key
     378              : // or value type is refused at compile time.
     379              : #define chmap_construct_full_scoped(hm_name, KeyT, ValT, mmgmt_procs,       \
     380              :                                     custom_hashing_proc,                    \
     381              :                                     custom_key_equality_proc)               \
     382              :   ccol__refuse_array_types("chmap", KeyT, ValT);                            \
     383              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     384              :       NULL;                                                                 \
     385              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     386              :       NULL;                                                                 \
     387              :   chmap hm_name ccol__destructor(chashmap__destroy_cleanup) = NULL;         \
     388              :   do {                                                                      \
     389              :     char *ccol__chmap_err = NULL;                                           \
     390              :     hm_name = chashmap_create_full(                                         \
     391              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     392              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     393              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     394              :         (mmgmt_procs), (custom_hashing_proc), (custom_key_equality_proc),   \
     395              :         &ccol__chmap_err);                                                  \
     396              :     if (!hm_name) {                                                         \
     397              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     398              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     399              :     }                                                                       \
     400              :   } while (0)
     401              : 
     402              : // Creates the map for a handle that chmap_declare() or chmap_declare_scoped()
     403              : // declared, with the default allocator and the built-in hash; the key and
     404              : // value types come from that declaration. Ends the program with a fatal error
     405              : // when the map can't be created.
     406              : #define chmap_init(hm_name)                                                 \
     407              :   do {                                                                      \
     408              :     char *ccol__chmap_err = NULL;                                           \
     409              :     hm_name = chashmap_create(                                              \
     410              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     411              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     412              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     413              :         &ccol__chmap_err);                                                  \
     414              :     if (!hm_name) {                                                         \
     415              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     416              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     417              :     }                                                                       \
     418              :   } while (0)
     419              : 
     420              : // Declares a hash map handle hm_name for key type KeyT and value type ValT and
     421              : // creates the map with the default allocator and the built-in hash; the usual
     422              : // way to start. Ends the program with a fatal error when the map can't be
     423              : // created. Destroy the map with chmap_destroy(). An array key or value type is
     424              : // refused at compile time; wrap it in a struct.
     425              : //
     426              : //   chmap_construct(ages, char *, int);    // separate chaining
     427              : //   chmap_insert(ages, "Alice", 30);
     428              : //   int age = chmap_get(ages, "Alice");    // 30
     429              : //   chmap_destroy(ages);
     430              : #define chmap_construct(hm_name, KeyT, ValT)                                \
     431              :   ccol__refuse_array_types("chmap", KeyT, ValT);                            \
     432              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     433              :       NULL;                                                                 \
     434              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     435              :       NULL;                                                                 \
     436              :   chmap hm_name = NULL;                                                     \
     437              :   do {                                                                      \
     438              :     char *ccol__chmap_err = NULL;                                           \
     439              :     hm_name = chashmap_create(                                              \
     440              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     441              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     442              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     443              :         &ccol__chmap_err);                                                  \
     444              :     if (!hm_name) {                                                         \
     445              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     446              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     447              :     }                                                                       \
     448              :   } while (0)
     449              : 
     450              : // Declares a hash map handle hm_name for key type KeyT and value type ValT and
     451              : // creates the map with the default allocator and the built-in hash. The map is
     452              : // destroyed by itself when hm_name goes out of scope. Ends the program with a
     453              : // fatal error when the map can't be created. An array key or value type is
     454              : // refused at compile time; wrap it in a struct.
     455              : #define chmap_construct_scoped(hm_name, KeyT, ValT)                         \
     456              :   ccol__refuse_array_types("chmap", KeyT, ValT);                            \
     457              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     458              :       NULL;                                                                 \
     459              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     460              :       NULL;                                                                 \
     461              :   chmap hm_name ccol__destructor(chashmap__destroy_cleanup) = NULL;         \
     462              :   do {                                                                      \
     463              :     char *ccol__chmap_err = NULL;                                           \
     464              :     hm_name = chashmap_create(                                              \
     465              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     466              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     467              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     468              :         &ccol__chmap_err);                                                  \
     469              :     if (!hm_name) {                                                         \
     470              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     471              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     472              :     }                                                                       \
     473              :   } while (0)
     474              : 
     475              : // Creates the map for a handle that chmap_declare() or chmap_declare_scoped()
     476              : // declared, taking all its memory from mmgmt_procs, your allocator table (NULL
     477              : // for the default). The table must stay valid until the map is destroyed. Ends
     478              : // the program with a fatal error when the map can't be created.
     479              : #define chmap_init_mp(hm_name, mmgmt_procs)                                 \
     480              :   do {                                                                      \
     481              :     char *ccol__chmap_err = NULL;                                           \
     482              :     hm_name = chashmap_create_mp(                                           \
     483              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     484              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     485              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     486              :         (mmgmt_procs), &ccol__chmap_err);                                   \
     487              :     if (!hm_name) {                                                         \
     488              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     489              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     490              :     }                                                                       \
     491              :   } while (0)
     492              : 
     493              : // Declares a hash map handle hm_name for key type KeyT and value type ValT and
     494              : // creates the map, taking all its memory from mmgmt_procs, your allocator
     495              : // table (NULL for the default). The table must stay valid until the map is
     496              : // destroyed. Ends the program with a fatal error when the map can't be
     497              : // created. Destroy the map with chmap_destroy(). An array key or value type is
     498              : // refused at compile time.
     499              : #define chmap_construct_mp(hm_name, KeyT, ValT, mmgmt_procs)                \
     500              :   ccol__refuse_array_types("chmap", KeyT, ValT);                            \
     501              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     502              :       NULL;                                                                 \
     503              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     504              :       NULL;                                                                 \
     505              :   chmap hm_name = NULL;                                                     \
     506              :   do {                                                                      \
     507              :     char *ccol__chmap_err = NULL;                                           \
     508              :     hm_name = chashmap_create_mp(                                           \
     509              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     510              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     511              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     512              :         (mmgmt_procs), &ccol__chmap_err);                                   \
     513              :     if (!hm_name) {                                                         \
     514              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     515              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     516              :     }                                                                       \
     517              :   } while (0)
     518              : 
     519              : // Declares a hash map handle hm_name for key type KeyT and value type ValT and
     520              : // creates the map, taking all its memory from mmgmt_procs, your allocator
     521              : // table (NULL for the default). The map is destroyed by itself when hm_name
     522              : // goes out of scope, and the table must stay valid until then. Ends the
     523              : // program with a fatal error when the map can't be created. An array key or
     524              : // value type is refused at compile time.
     525              : #define chmap_construct_mp_scoped(hm_name, KeyT, ValT, mmgmt_procs)         \
     526              :   ccol__refuse_array_types("chmap", KeyT, ValT);                            \
     527              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     528              :       NULL;                                                                 \
     529              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     530              :       NULL;                                                                 \
     531              :   chmap hm_name ccol__destructor(chashmap__destroy_cleanup) = NULL;         \
     532              :   do {                                                                      \
     533              :     char *ccol__chmap_err = NULL;                                           \
     534              :     hm_name = chashmap_create_mp(                                           \
     535              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     536              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     537              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     538              :         (mmgmt_procs), &ccol__chmap_err);                                   \
     539              :     if (!hm_name) {                                                         \
     540              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     541              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     542              :     }                                                                       \
     543              :   } while (0)
     544              : 
     545              : // Creates the map for a handle that chmap_declare() or chmap_declare_scoped()
     546              : // declared, hashing keys with custom_hashing_proc and allocating with the
     547              : // default allocator. The hash must give the same value for keys the map
     548              : // treats as equal (equal contents for a string). Ends the program with a fatal
     549              : // error when the map can't be created.
     550              : #define chmap_init_ch(hm_name, custom_hashing_proc)                         \
     551              :   do {                                                                      \
     552              :     char *ccol__chmap_err = NULL;                                           \
     553              :     hm_name = chashmap_create_ch(                                           \
     554              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     555              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     556              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     557              :         (custom_hashing_proc), &ccol__chmap_err);                           \
     558              :     if (!hm_name) {                                                         \
     559              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     560              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     561              :     }                                                                       \
     562              :   } while (0)
     563              : 
     564              : // Declares a hash map handle hm_name for key type KeyT and value type ValT and
     565              : // creates the map, hashing keys with custom_hashing_proc. The hash must give
     566              : // the same value for keys the map treats as equal (equal contents for a
     567              : // string). Ends the program with a fatal error when the map can't be created.
     568              : // Destroy the map with chmap_destroy(). An array key or value type is refused
     569              : // at compile time.
     570              : #define chmap_construct_ch(hm_name, KeyT, ValT, custom_hashing_proc)        \
     571              :   ccol__refuse_array_types("chmap", KeyT, ValT);                            \
     572              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     573              :       NULL;                                                                 \
     574              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     575              :       NULL;                                                                 \
     576              :   chmap hm_name = NULL;                                                     \
     577              :   do {                                                                      \
     578              :     char *ccol__chmap_err = NULL;                                           \
     579              :     hm_name = chashmap_create_ch(                                           \
     580              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     581              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     582              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     583              :         (custom_hashing_proc), &ccol__chmap_err);                           \
     584              :     if (!hm_name) {                                                         \
     585              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     586              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     587              :     }                                                                       \
     588              :   } while (0)
     589              : 
     590              : // Declares a hash map handle hm_name for key type KeyT and value type ValT and
     591              : // creates the map, hashing keys with custom_hashing_proc. The map is destroyed
     592              : // by itself when hm_name goes out of scope. The hash must give the same value
     593              : // for keys the map treats as equal (equal contents for a string). Ends the
     594              : // program with a fatal error when the map can't be created. An array key or
     595              : // value type is refused at compile time.
     596              : #define chmap_construct_ch_scoped(hm_name, KeyT, ValT, custom_hashing_proc) \
     597              :   ccol__refuse_array_types("chmap", KeyT, ValT);                            \
     598              :   __typeof__(KeyT) *hm_name##__ccol_key_type_var __attribute__((unused)) =  \
     599              :       NULL;                                                                 \
     600              :   __typeof__(ValT) *hm_name##__ccol_val_type_var __attribute__((unused)) =  \
     601              :       NULL;                                                                 \
     602              :   chmap hm_name ccol__destructor(chashmap__destroy_cleanup) = NULL;         \
     603              :   do {                                                                      \
     604              :     char *ccol__chmap_err = NULL;                                           \
     605              :     hm_name = chashmap_create_ch(                                           \
     606              :         CHMAP_DEFAULT_INITIAL_BUCKET_ARRAY_SIZE,                            \
     607              :         ccol_determine_ccol_data_type(*hm_name##__ccol_key_type_var),       \
     608              :         ccol_determine_ccol_data_type(*hm_name##__ccol_val_type_var),       \
     609              :         (custom_hashing_proc), &ccol__chmap_err);                           \
     610              :     if (!hm_name) {                                                         \
     611              :       ccol__fatal_err("Failed to create hash map '%s': %s", #hm_name,       \
     612              :                       ccol__chmap_err ? ccol__chmap_err : "unknown error"); \
     613              :     }                                                                       \
     614              :   } while (0)
     615              : 
     616              : // Type-inferred operation macros
     617              : 
     618              : // Destroys the map: Frees every key, value and bucket, and sets chm to NULL so
     619              : // the handle can't be used after the free. chm must be a modifiable lvalue and
     620              : // is evaluated once; a NULL handle does nothing. Pointers that the map gave
     621              : // you (a char * from chmap_get(), say) are invalid afterwards. A map from a
     622              : // _scoped macro may also be destroyed early this way.
     623              : #define chmap_destroy(chm) chashmap_destroy(chm)
     624              : 
     625              : // Inserts key with its value, or replaces the value when the key is already
     626              : // there. The map stores its own copies, a string included. Any expression
     627              : // works for either argument, and each is evaluated once. Both are converted to
     628              : // the declared types like a plain assignment (never a reinterpretation of
     629              : // bytes; no implicit conversion means a compile error), so a float key in an
     630              : // int map is found again by an int lookup. A character pointer type keeps the
     631              : // expression's own pointer type. Ends the program with a fatal error when the
     632              : // insert fails, for example for lack of memory or for a NULL string key or
     633              : // value (ccol_invalid_args).
     634              : #define chmap_insert(hm_name, key, ccol__chmap_val)                            \
     635              :   ccol__chmap_insert_impl(hm_name, (key), (ccol__chmap_val),                   \
     636              :                           ccol__uniq(ccol__chmap_insert_key_tmp, __COUNTER__), \
     637              :                           ccol__uniq(ccol__chmap_insert_val_tmp, __COUNTER__), \
     638              :                           ccol__uniq(ccol__chmap_insert_kp, __COUNTER__),      \
     639              :                           ccol__uniq(ccol__chmap_insert_vp, __COUNTER__),      \
     640              :                           ccol__uniq(ccol__chmap_insert_r, __COUNTER__))
     641              : 
     642              : // Internal: The body of chmap_insert(). It copies key and val into
     643              : // temporaries of the declared types and calls chashmap_insert_elem(). The
     644              : // temporaries get their names from ccol__uniq(), so the macro nests, and they
     645              : // keep a copy of each expression for the whole call, so an rvalue works.
     646              : #define ccol__chmap_insert_impl(hm_name, key, ccol__chmap_val,                 \
     647              :                                 ccol__chmap_key_tmp, ccol__chmap_val_tmp,      \
     648              :                                 ccol__chmap_kp, ccol__chmap_vp, ccol__chmap_r) \
     649              :   do {                                                                         \
     650              :     ccol__declared_or_own_type(hm_name##__ccol_key_type_var, (key))            \
     651              :         ccol__chmap_key_tmp = (key);                                           \
     652              :     ccol__clear_padding(&ccol__chmap_key_tmp);                                 \
     653              :     ccol__declared_or_own_type(hm_name##__ccol_val_type_var,                   \
     654              :                                (ccol__chmap_val)) ccol__chmap_val_tmp =        \
     655              :         (ccol__chmap_val);                                                     \
     656              :     cmap_pair *ccol__chmap_kp = &(cmap_pair){};                                \
     657              :     cmap_pair *ccol__chmap_vp = &(cmap_pair){};                                \
     658              :     ccol__populate_cmap_pair(ccol__chmap_kp, ccol__chmap_key_tmp);             \
     659              :     ccol__populate_cmap_pair(ccol__chmap_vp, ccol__chmap_val_tmp);             \
     660              :     ccol_retval ccol__chmap_r =                                                \
     661              :         chashmap_insert_elem(hm_name, ccol__chmap_kp, ccol__chmap_vp);         \
     662              :     if (ccol__chmap_r != ccol_success &&                                       \
     663              :         ccol__chmap_r != ccol_key_already_present) {                           \
     664              :       ccol__dump_key_to_stderr(ccol__chmap_kp->ptr, ccol__chmap_kp->size);     \
     665              :       ccol__fatal_err("chmap_insert('%s'): r: %d (%s)", #hm_name,              \
     666              :                       ccol__chmap_r, ccol_retval_to_str(ccol__chmap_r));       \
     667              :     }                                                                          \
     668              :   } while (0)
     669              : 
     670              : // Removes key and its value from the map and frees the map's copies of both.
     671              : // Evaluates to ccol_success, ccol_key_not_found, or ccol_invalid_args for a
     672              : // NULL string key. It never ends the program, because a missing key is an
     673              : // ordinary outcome. The key is converted to the declared key type, as
     674              : // chmap_insert() converts it.
     675              : #define chmap_remove(hm_name, key)                                             \
     676              :   ccol__chmap_remove_impl(hm_name, (key),                                      \
     677              :                           ccol__uniq(ccol__chmap_remove_key_tmp, __COUNTER__), \
     678              :                           ccol__uniq(ccol__chmap_remove_kp, __COUNTER__),      \
     679              :                           ccol__uniq(ccol__chmap_remove_r, __COUNTER__))
     680              : 
     681              : // Internal: The body of chmap_remove(). The key goes through a temporary of
     682              : // the declared key type, as in chmap_insert(), so it finds what an insert
     683              : // stored.
     684              : #define ccol__chmap_remove_impl(hm_name, key, ccol__chmap_key_tmp,             \
     685              :                                 ccol__chmap_kp, ccol__chmap_r)                 \
     686              :   ({                                                                           \
     687              :     ccol__declared_or_own_type(hm_name##__ccol_key_type_var, (key))            \
     688              :         ccol__chmap_key_tmp = (key);                                           \
     689              :     ccol__clear_padding(&ccol__chmap_key_tmp);                                 \
     690              :     cmap_pair *ccol__chmap_kp = &(cmap_pair){};                                \
     691              :     ccol__populate_cmap_pair(ccol__chmap_kp, ccol__chmap_key_tmp);             \
     692              :     ccol_retval ccol__chmap_r = chashmap_remove_elem(hm_name, ccol__chmap_kp); \
     693              :     ccol__chmap_r;                                                             \
     694              :   })
     695              : 
     696              : // Evaluates to a copy of the value stored for key, for a key you know is
     697              : // there. For a string value it gives the map's own char *, which stays valid
     698              : // until that key is replaced or removed or the map is destroyed; don't free
     699              : // it. Ends the program with a fatal error when the key is missing, for a NULL
     700              : // string key, and when the stored size doesn't match the declared value type.
     701              : // Use chmap_get_ptr() for a lookup that may miss.
     702              : #define chmap_get(hm_name, key)                                          \
     703              :   ccol__chmap_get_impl(hm_name, (key),                                   \
     704              :                        ccol__uniq(ccol__chmap_get_key_tmp, __COUNTER__), \
     705              :                        ccol__uniq(ccol__chmap_get_val, __COUNTER__),     \
     706              :                        ccol__uniq(ccol__chmap_get_kp, __COUNTER__),      \
     707              :                        ccol__uniq(ccol__chmap_get_vp, __COUNTER__),      \
     708              :                        ccol__uniq(ccol__chmap_get_r, __COUNTER__))
     709              : 
     710              : // Internal: The body of chmap_get(). The key goes through a temporary of the
     711              : // declared key type, as in chmap_insert(), so it finds what an insert stored.
     712              : #define ccol__chmap_get_impl(hm_name, key, ccol__chmap_key_tmp,               \
     713              :                              ccol__chmap_val, ccol__chmap_kp, ccol__chmap_vp, \
     714              :                              ccol__chmap_r)                                   \
     715              :   ({                                                                          \
     716              :     ccol__declared_or_own_type(hm_name##__ccol_key_type_var, (key))           \
     717              :         ccol__chmap_key_tmp = (key);                                          \
     718              :     ccol__clear_padding(&ccol__chmap_key_tmp);                                \
     719              :     __typeof__(*hm_name##__ccol_val_type_var) *ccol__chmap_val = NULL;        \
     720              :     cmap_pair *ccol__chmap_kp = &(cmap_pair){};                               \
     721              :     cmap_pair ccol__chmap_vp = {NULL, 0};                                     \
     722              :     ccol__populate_cmap_pair(ccol__chmap_kp, ccol__chmap_key_tmp);            \
     723              :     ccol_retval ccol__chmap_r =                                               \
     724              :         chashmap_get_elem_ref(hm_name, ccol__chmap_kp, &ccol__chmap_vp);      \
     725              :     if (ccol__chmap_r != ccol_success) {                                      \
     726              :       ccol__dump_key_to_stderr(ccol__chmap_kp->ptr, ccol__chmap_kp->size);    \
     727              :       ccol__fatal_err("chmap_get('%s'): r: %d (%s)", #hm_name, ccol__chmap_r, \
     728              :                       ccol_retval_to_str(ccol__chmap_r));                     \
     729              :     }                                                                         \
     730              :     if (ccol__is_char_ptr(*hm_name##__ccol_val_type_var)) {                   \
     731              :       ccol__chmap_val =                                                       \
     732              :           (__typeof__(*hm_name##__ccol_val_type_var) *)&(ccol__chmap_vp.ptr); \
     733              :     } else if (ccol__chmap_vp.size != sizeof(*ccol__chmap_val)) {             \
     734              :       ccol__fatal_err(                                                        \
     735              :           "chmap_get('%s'): value size mismatch - stored: %lu bytes, "        \
     736              :           "requested: %lu bytes; wrong type or missing chmap_redeclare()?",   \
     737              :           #hm_name, (unsigned long)ccol__chmap_vp.size,                       \
     738              :           (unsigned long)sizeof(*ccol__chmap_val));                           \
     739              :     } else {                                                                  \
     740              :       ccol__chmap_val =                                                       \
     741              :           (__typeof__(*hm_name##__ccol_val_type_var) *)(ccol__chmap_vp.ptr);  \
     742              :     }                                                                         \
     743              :     *ccol__chmap_val;                                                         \
     744              :   })
     745              : 
     746              : // Internal: Converts p to the result type of chmap_get_ptr(). For a string
     747              : // value type the target becomes const, because the only char * in the entry
     748              : // is the map's own accessor; storing another pointer there would leave its
     749              : // size describing the old string. Every other type comes back unchanged.
     750              : #define ccol__chmap_value_ptr_result(hm_name, p)                               \
     751              :   _Generic(*hm_name##__ccol_val_type_var,                                      \
     752              :       char *: (__typeof__(*hm_name##__ccol_val_type_var) const *)(p),          \
     753              :       const char *: (__typeof__(*hm_name##__ccol_val_type_var) const *)(p),    \
     754              :       signed char *: (__typeof__(*hm_name##__ccol_val_type_var) const *)(p),   \
     755              :       const signed char *: (__typeof__(*hm_name##__ccol_val_type_var)          \
     756              :                                 const *)(p),                                   \
     757              :       unsigned char *: (__typeof__(*hm_name##__ccol_val_type_var) const *)(p), \
     758              :       const unsigned char *: (__typeof__(*hm_name##__ccol_val_type_var)        \
     759              :                                   const *)(p),                                 \
     760              :       default: (p))
     761              : 
     762              : // Evaluates to a pointer to the value stored for key, or NULL when the map
     763              : // doesn't hold the key (or for a NULL string key); use it for a lookup that
     764              : // may miss or that changes the value in place. The pointer is valid until the
     765              : // next insert, delete or resize. Ends the program with a fatal error when the
     766              : // stored size doesn't match the declared value type. For a char * value the
     767              : // result is char *const *: Edit the bytes of the string within its length,
     768              : // and use chmap_insert() to replace it.
     769              : //
     770              : //   char *const *sptr = chmap_get_ptr(names, key);
     771              : //   if (sptr) (*sptr)[0] = 'j';
     772              : #define chmap_get_ptr(hm_name, key)                                         \
     773              :   ccol__chmap_get_ptr_impl(                                                 \
     774              :       hm_name, (key), ccol__uniq(ccol__chmap_get_ptr_key_tmp, __COUNTER__), \
     775              :       ccol__uniq(ccol__chmap_get_ptr_val, __COUNTER__),                     \
     776              :       ccol__uniq(ccol__chmap_get_ptr_kp, __COUNTER__),                      \
     777              :       ccol__uniq(ccol__chmap_get_ptr_vp, __COUNTER__),                      \
     778              :       ccol__uniq(ccol__chmap_get_ptr_acc, __COUNTER__),                     \
     779              :       ccol__uniq(ccol__chmap_get_ptr_r, __COUNTER__))
     780              : 
     781              : // Internal: The body of chmap_get_ptr(). Strings go through
     782              : // chashmap__get_val_accessor(), every other type through
     783              : // chashmap_get_elem_ref(). The value pair needs no initializer, because the
     784              : // lookup fills it on every path and it's read only after a success. The
     785              : // string arm goes through uintptr_t to drop the library's const without a
     786              : // -Wcast-qual warning in the caller's build.
     787              : #define ccol__chmap_get_ptr_impl(                                              \
     788              :     hm_name, key, ccol__chmap_key_tmp, ccol__chmap_val, ccol__chmap_kp,        \
     789              :     ccol__chmap_vp, ccol__chmap_acc, ccol__chmap_r)                            \
     790              :   ({                                                                           \
     791              :     ccol__declared_or_own_type(hm_name##__ccol_key_type_var, (key))            \
     792              :         ccol__chmap_key_tmp = (key);                                           \
     793              :     ccol__clear_padding(&ccol__chmap_key_tmp);                                 \
     794              :     __typeof__(*hm_name##__ccol_val_type_var) *ccol__chmap_val = NULL;         \
     795              :     cmap_pair *ccol__chmap_kp = &(cmap_pair){};                                \
     796              :     cmap_pair ccol__chmap_vp;                                                  \
     797              :     const cmap_pair *ccol__chmap_acc = NULL;                                   \
     798              :     ccol_retval ccol__chmap_r;                                                 \
     799              :     ccol__populate_cmap_pair(ccol__chmap_kp, ccol__chmap_key_tmp);             \
     800              :     if (ccol__is_char_ptr(*hm_name##__ccol_val_type_var)) {                    \
     801              :       ccol__chmap_r = chashmap__get_val_accessor(hm_name, ccol__chmap_kp,      \
     802              :                                                  &ccol__chmap_acc);            \
     803              :       if (ccol__chmap_r == ccol_success) {                                     \
     804              :         ccol__chmap_val = (__typeof__(*hm_name##__ccol_val_type_var)           \
     805              :                                *)(uintptr_t)&ccol__chmap_acc->ptr;             \
     806              :       }                                                                        \
     807              :     } else {                                                                   \
     808              :       ccol__chmap_r =                                                          \
     809              :           chashmap_get_elem_ref(hm_name, ccol__chmap_kp, &ccol__chmap_vp);     \
     810              :       if (ccol__chmap_r == ccol_success) {                                     \
     811              :         if (ccol__chmap_vp.size != sizeof(*ccol__chmap_val)) {                 \
     812              :           ccol__fatal_err(                                                     \
     813              :               "chmap_get_ptr('%s'): value size mismatch - stored: %lu "        \
     814              :               "bytes, requested: %lu bytes; wrong type or missing "            \
     815              :               "chmap_redeclare()?",                                            \
     816              :               #hm_name, (unsigned long)ccol__chmap_vp.size,                    \
     817              :               (unsigned long)sizeof(*ccol__chmap_val));                        \
     818              :         }                                                                      \
     819              :         ccol__chmap_val =                                                      \
     820              :             (__typeof__(*hm_name##__ccol_val_type_var) *)(ccol__chmap_vp.ptr); \
     821              :       }                                                                        \
     822              :     }                                                                          \
     823              :     (__typeof__(ccol__chmap_value_ptr_result(                                  \
     824              :         hm_name,                                                               \
     825              :         (__typeof__(*hm_name##__ccol_val_type_var) *)0)))(ccol__chmap_val);    \
     826              :   })
     827              : 
     828              : // Evaluates to the number of key-value pairs in the map, as a size_t. Asserts
     829              : // on a NULL handle.
     830              : #define chmap_size(chm) chashmap_size((chm))
     831              : 
     832              : // Removes and frees every entry, so the map can be filled again without a new
     833              : // map. A new_bucket_array_size other than 0 also resizes the bucket array to
     834              : // that size rounded up to a power of two (16 at least); 0 keeps the size.
     835              : // Evaluates to ccol_success, or ccol_not_enough_memory when the resize fails;
     836              : // every entry is gone either way. Asserts on a NULL handle.
     837              : #define chmap_reset(chm, new_bucket_array_size) \
     838              :   chashmap_reset((chm), (new_bucket_array_size))
     839              : 
     840              : #pragma GCC visibility pop
        

Generated by: LCOV version 2.0-1