// hakmem_tiny_registry.c // Phase 2B-3: Registry Operations // Extracted from hakmem_tiny.c (lines 2653-2674, 2677-2701) #include #include #include "hakmem_tiny.h" #include "hakmem_tiny_registry_api.h" // External globals extern SlabRegistryEntry g_slab_registry[]; // registry_hash() is defined as static inline in hakmem_tiny_registry_api.h int registry_register(uintptr_t slab_base, TinySlab* owner) { pthread_mutex_lock(&g_tiny_registry_lock); int hash = registry_hash(slab_base); // Linear probing (max 8 attempts) for (int i = 0; i < SLAB_REGISTRY_MAX_PROBE; i++) { int idx = (hash + i) & SLAB_REGISTRY_MASK; SlabRegistryEntry* entry = &g_slab_registry[idx]; if (entry->slab_base == 0) { // Empty slot found entry->slab_base = slab_base; atomic_store_explicit(&entry->owner, owner, memory_order_release); pthread_mutex_unlock(&g_tiny_registry_lock); return 1; } } // Registry full (collision limit exceeded) pthread_mutex_unlock(&g_tiny_registry_lock); return 0; } void registry_unregister(uintptr_t slab_base) { pthread_mutex_lock(&g_tiny_registry_lock); int hash = registry_hash(slab_base); // Linear probing search for (int i = 0; i < SLAB_REGISTRY_MAX_PROBE; i++) { int idx = (hash + i) & SLAB_REGISTRY_MASK; SlabRegistryEntry* entry = &g_slab_registry[idx]; if (entry->slab_base == slab_base) { // Found - clear entry (atomic store prevents TOCTOU race) atomic_store_explicit(&entry->owner, NULL, memory_order_release); entry->slab_base = 0; pthread_mutex_unlock(&g_tiny_registry_lock); return; } if (entry->slab_base == 0) { // Empty slot - not found pthread_mutex_unlock(&g_tiny_registry_lock); return; } } pthread_mutex_unlock(&g_tiny_registry_lock); }