10 #include <sys/types.h>
14 #ifndef HUGE_PAGE_SIZE
15 #define HUGE_PAGE_SIZE (2UL * 1024UL * 1024UL)
18 typedef struct ck_huge_alloc_entry {
22 struct ck_huge_alloc_entry *next;
23 } ck_huge_alloc_entry_t;
30 if (align == 0)
return n;
31 return (n + align - 1) & ~(align - 1);
36 ck_huge_alloc_entry_t *entry = malloc(
sizeof(*entry));
42 entry->was_mmap = was_mmap;
55 if ((*node)->ptr == ptr) {
56 ck_huge_alloc_entry_t *entry = *node;
61 node = &(*node)->next;
72 void *p = mmap(NULL, len,
73 PROT_READ | PROT_WRITE,
74 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
76 if (p != MAP_FAILED) {
87 fprintf(stderr,
"ck_huge_alloc: aligned_alloc failed for %zu bytes: %s\n",
88 len, strerror(errno));
93 (void)madvise(q, len, MADV_HUGEPAGE);
103 if (!ptr || bytes == 0) {
114 if (entry->was_mmap) {
115 munmap(ptr, entry->len);
void * ck_huge_alloc(size_t bytes)
static size_t align_up_bytes(size_t n, size_t align)
static ck_huge_alloc_entry_t * detach_allocation(void *ptr)
static ck_huge_alloc_entry_t * g_alloc_list
static int record_allocation(void *ptr, size_t len, int was_mmap)
static pthread_mutex_t g_alloc_mutex
void ck_huge_free(void *ptr, size_t bytes)