$ show gnustep/libobjc2
This repository is the GNUstep Objective-C runtime library (libobjc2), a drop-in replacement for the GCC Objective-C runtime that implements the modern Objective-C runtime API for non-Apple platforms. It provides the core runtime services: message dispatch (via architecture-specific assembly fast paths and a C fallback), memory management (ARC retain/release, autorelease pools, weak references), block support, property access, exception handling (with C++ interop), and type-dependent dispatch. The architecture is a shared library with a modular design: loader.c handles module loading, selector_table.cc manages selector uniquing, class_table.c manages class loading and +load, sendmsg2.c implements message lookup, and arc.mm implements the ARC contract. It supports three ABIs (GCC legacy, GNUstep v1 non-fragile, GNUstep v2 modern) and runs on Linux, Windows, FreeBSD, Android, and WebAssembly. The codebase is well-structured with several reusable data structures (hopscotch hash tables, sparse arrays, platform-abstracted locks) and demonstrates strong engineering practices in performance-critical paths (cache-line alignment, atomic operations, copy-on-write dispatch tables). However, it carries significant complexity from legacy ABI compatibility and platform-specific ifdefs, and some components (C++ exception interop) rely on fragile runtime structure discovery.
The LLM identified 5 files worth highlighting for reuse:
A macro-based generic hopscotch hash table implementation used throughout the runtime for class tables, protocol tables, and alias tables. It provides O(1) expected lookup with good cache behavior and can be configured for single-threaded or lock-free use. The implementation is self-contained in a single header and can be extracted for use in other C projects needing a high-performance hash table.
A sparse array implementation with configurable depth and inline lookup, used for method dispatch tables (dtables). It supports copy-on-write semantics for safe sharing and provides O(1) access with minimal memory overhead. The header defines the interface and inline functions, while sarray2.c provides the implementation. This is a reusable data structure for any scenario requiring a sparse mapping with fast access.
A platform-abstracted locking header providing mutexes and spinlocks (ThinLock) with deadlock-free double-lock guards. It wraps pthreads mutexes, Windows critical sections, and other platform primitives into a consistent API. The ThinLock implementation uses atomic operations and is suitable for fine-grained locking in performance-critical code. This header can be reused in any C/C++ project needing portable locking.
A C++ template for page-aligned allocation of fixed-size objects, used for protocol metadata and other runtime structures. It allocates memory in pages and provides a simple free-list for reuse. The implementation is self-contained and can be extracted for use in projects needing efficient allocation of many same-sized objects with minimal fragmentation.
A small header providing helper functions for safe C++ allocation: allocate_zeroed (aligned, zeroed memory) and allocate_zeroed_array (array allocation with size check). These are used internally by the runtime but are generic enough to be reused in any C++ project that needs to allocate zeroed memory with proper alignment and overflow protection.