packages feed

croaring-ffi-1.0: test/cbits/counting-allocator.c

#include <stdatomic.h>
#include <stdlib.h>

static atomic_size_t allocations;

void *counting_malloc(size_t size) {
    atomic_fetch_add(&allocations, 1);
    return malloc(size);
}

void *counting_realloc(void *block, size_t size) {
    atomic_fetch_add(&allocations, 1);
    return realloc(block, size);
}

void *counting_calloc(size_t count, size_t size) {
    atomic_fetch_add(&allocations, 1);
    return calloc(count, size);
}

void *counting_aligned_malloc(size_t alignment, size_t size) {
    void *block = NULL;
    atomic_fetch_add(&allocations, 1);
    return posix_memalign(&block, alignment, size) == 0 ? block : NULL;
}

size_t counting_allocations(void) { return atomic_load(&allocations); }