$ show troydhanson/uthash
uthash is a mature, header-only C library providing generic, intrusive data structures via macros. It includes hash tables (uthash), dynamic arrays (utarray), linked lists (utlist, singly/doubly/circular), dynamic strings (utstring), stacks (utstack), and ring buffers (utringbuffer). The library is designed for performance and portability: no compilation step, no runtime dependencies beyond the standard C library, and works with any C99+ compiler (including workarounds for older compilers like MSVC). The core design pattern embeds a small handle (e.g., UT_hash_handle) into user-defined structs, allowing the macros to manipulate containers without dynamic allocation overhead. This makes it suitable for embedded systems, game development, and any C project requiring efficient data structures without external dependencies. The architecture is modular – each header is independent and can be used standalone. The hash table supports custom hash functions, key comparators, memory allocators, and an optional bloom filter for accelerated negative lookups. Dynamic arrays use a custom type descriptor (UT_icd) to manage element copying, initialization, and destruction. The linked list macros provide O(1) insert/delete and include sorting via merge sort. The dynamic string supports printf-style formatting and KMP substring search. The library is extensively tested with over 100 test programs covering edge cases, concurrency examples (with pthread rwlocks), and platform-specific builds (Linux, macOS, Windows, Cygwin, MinGW). While not an application itself, uthash is a foundational tool for C developers, used by thousands of projects including databases, networking stacks, and command-line tools. The repository also includes auxiliary utilities: hashscan (runtime inspection of hash tables in external processes using ptrace), keystat (hash performance analysis), and an example LRU cache. The codebase is stable (v2.4.0) with active maintenance via GitHub Actions and issue tracking.
The LLM identified 4 files worth highlighting for reuse:
Core header providing macros for hash table operations (HASH_ADD, HASH_FIND, HASH_DEL, HASH_SORT, HASH_SELECT, HASH_CLEAR, etc.) on user-defined structs. Embeds UT_hash_handle to store hash value, next/prev pointers, and bucket list. Supports custom hash functions, key comparators, memory allocators, and an optional bloom filter. The macro design achieves type safety via __typeof__ or decltype with fallbacks for old compilers. Highly optimized for performance with power-of-two resizing and incremental rehashing.
Provides macros for singly (LL_), doubly (DL_), and circular doubly (CDL_) linked lists. Intrusive: users add next/prev pointers to their struct. Supports O(1) insert/delete, concatenation, sorting (merge sort), and foreach iteration. The '2' suffix variants allow custom field names for next/prev. No memory allocation overhead; the list is managed entirely through pointers embedded in the elements. Essential for embedded systems and low-level C.
Macros for a dynamic string buffer that supports printf-style appending (utstring_printf), binary safe concatenation (utstring_bincpy), and substring search using Knuth-Morris-Pratt (utstring_find, utstring_findR). Uses a simple struct with a char array, length, and capacity. Memory is managed automatically (realloc). Lightweight and fast, ideal for building strings without malloc overhead per operation.
Macros for a generic dynamic array that uses a custom type descriptor (UT_icd) to define element size, copy, init, and destructor. Provides push_back, pop, insert, delete, sort, find, foreach, and concatenation. Predefined UT_icd for int, string, and pointer types. The implementation is efficient, using realloc for resizing and memmove for shifts. Suitable for any C project needing a growable array with user-defined element semantics.