assembly-hashmap 0.1.0

A single-threaded u64→u64 hashmap with x86-64 assembly operations and Rust-side growth.
Documentation
#define _POSIX_C_SOURCE 200112L

#include "hashmap.h"
#include "hashmap_impl.h"

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#define GROUP HM_GROUP
#define EMPTY HM_EMPTY

_Static_assert(offsetof(struct HashMap, ctrl) == 0, "ctrl");
_Static_assert(offsetof(struct HashMap, slots) == 8, "slots");
_Static_assert(offsetof(struct HashMap, bucket_mask) == 16, "mask");
_Static_assert(offsetof(struct HashMap, items) == 24, "items");
_Static_assert(offsetof(struct HashMap, growth_left) == 32, "growth");
_Static_assert(offsetof(struct HashMap, cap_items) == 40, "cap");

static size_t bucket_mask_to_capacity(size_t bucket_mask) {
    if (bucket_mask < 8) {
        return bucket_mask;
    }
    return ((bucket_mask + 1) / 8) * 7;
}

HashMap *hm_create(size_t min_items) {
    size_t buckets = GROUP;
    while (bucket_mask_to_capacity(buckets - 1) < min_items) {
        buckets *= 2;
        if (buckets > ((size_t)1 << 60)) {
            return NULL;
        }
    }

    HashMap *m = (HashMap *)calloc(1, sizeof(*m));
    if (!m) {
        return NULL;
    }

    size_t ctrl_bytes = buckets + GROUP;
    size_t slot_off = (ctrl_bytes + 63u) & ~(size_t)63u;
    size_t slot_bytes = buckets * 16u;
    size_t total = slot_off + slot_bytes;

    void *block = NULL;
    if (posix_memalign(&block, 64, total) != 0) {
        free(m);
        return NULL;
    }

    uint8_t *ctrl = (uint8_t *)block;
    uint64_t *slots = (uint64_t *)(ctrl + slot_off);

    memset(ctrl, (int)EMPTY, ctrl_bytes);

    m->ctrl = ctrl;
    m->slots = slots;
    m->bucket_mask = buckets - 1;
    m->items = 0;
    m->growth_left = bucket_mask_to_capacity(buckets - 1);
    m->cap_items = m->growth_left;
    m->block = block;
    return m;
}

void hm_destroy(HashMap *map) {
    if (!map) {
        return;
    }
    free(map->block);
    free(map);
}