← Back to C-Kernel-Engine Docs Doxygen Source Documentation
 
Loading...
Searching...
No Matches
ckernel_alloc_v6.c File Reference
#include "ckernel_alloc.h"
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 
#define HUGE_PAGE_SIZE   (2UL * 1024UL * 1024UL)
 

Functions

static size_t align_up_bytes (size_t n, size_t align)
 
void * ck_huge_alloc (size_t bytes)
 
void ck_huge_free (void *ptr, size_t bytes)
 
static ck_huge_alloc_entry_t * detach_allocation (void *ptr)
 
static int record_allocation (void *ptr, size_t len, int was_mmap)
 

Variables

static ck_huge_alloc_entry_t * g_alloc_list = NULL
 
static pthread_mutex_t g_alloc_mutex = PTHREAD_MUTEX_INITIALIZER
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 1 of file ckernel_alloc_v6.c.

◆ HUGE_PAGE_SIZE

#define HUGE_PAGE_SIZE   (2UL * 1024UL * 1024UL)

Definition at line 15 of file ckernel_alloc_v6.c.

Function Documentation

◆ align_up_bytes()

static size_t align_up_bytes ( size_t  n,
size_t  align 
)
static

Definition at line 28 of file ckernel_alloc_v6.c.

29{
30 if (align == 0) return n;
31 return (n + align - 1) & ~(align - 1);
32}

Referenced by ck_huge_alloc().

◆ ck_huge_alloc()

void * ck_huge_alloc ( size_t  bytes)

Allocate a large, contiguous memory region for model weights/activations.

Implementation strategy:

  • Try to allocate 2MB-aligned memory backed by huge pages where possible.
  • Fall back to aligned_alloc + madvise(MADV_HUGEPAGE) when explicit huge pages are not available.

Returns NULL on failure.

Definition at line 67 of file ckernel_alloc_v6.c.

68{
69 size_t len = align_up_bytes(bytes, HUGE_PAGE_SIZE);
70
71 /* First, try explicit huge pages via mmap + MAP_HUGETLB. */
72 void *p = mmap(NULL, len,
73 PROT_READ | PROT_WRITE,
74 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
75 -1, 0);
76 if (p != MAP_FAILED) {
77 if (!record_allocation(p, len, 1)) {
78 munmap(p, len);
79 return NULL;
80 }
81 return p;
82 }
83
84 /* Fallback: aligned_alloc with transparent hugepage hint. */
85 void *q = aligned_alloc(HUGE_PAGE_SIZE, len);
86 if (!q) {
87 fprintf(stderr, "ck_huge_alloc: aligned_alloc failed for %zu bytes: %s\n",
88 len, strerror(errno));
89 return NULL;
90 }
91
92 /* Best-effort hint; ignore errors. */
93 (void)madvise(q, len, MADV_HUGEPAGE);
94 if (!record_allocation(q, len, 0)) {
95 free(q);
96 return NULL;
97 }
98 return q;
99}
static size_t align_up_bytes(size_t n, size_t align)
static int record_allocation(void *ptr, size_t len, int was_mmap)
#define HUGE_PAGE_SIZE

References align_up_bytes(), align_up_bytes(), HUGE_PAGE_SIZE, record_allocation(), and record_allocation().

◆ ck_huge_free()

void ck_huge_free ( void *  ptr,
size_t  bytes 
)

Free memory allocated by ck_huge_alloc.

The bytes parameter should be the same size passed to ck_huge_alloc.

Definition at line 101 of file ckernel_alloc_v6.c.

102{
103 if (!ptr || bytes == 0) {
104 return;
105 }
106
107 ck_huge_alloc_entry_t *entry = detach_allocation(ptr);
108 if (!entry) {
109 /* Fall back to malloc/free if the allocation wasn't tracked. */
110 free(ptr);
111 return;
112 }
113
114 if (entry->was_mmap) {
115 munmap(ptr, entry->len);
116 } else {
117 free(ptr);
118 }
119
120 free(entry);
121}
static ck_huge_alloc_entry_t * detach_allocation(void *ptr)

References detach_allocation(), and detach_allocation().

◆ detach_allocation()

static ck_huge_alloc_entry_t * detach_allocation ( void *  ptr)
static

Definition at line 50 of file ckernel_alloc_v6.c.

51{
52 pthread_mutex_lock(&g_alloc_mutex);
53 ck_huge_alloc_entry_t **node = &g_alloc_list;
54 while (*node) {
55 if ((*node)->ptr == ptr) {
56 ck_huge_alloc_entry_t *entry = *node;
57 *node = entry->next;
58 pthread_mutex_unlock(&g_alloc_mutex);
59 return entry;
60 }
61 node = &(*node)->next;
62 }
63 pthread_mutex_unlock(&g_alloc_mutex);
64 return NULL;
65}
static ck_huge_alloc_entry_t * g_alloc_list
static pthread_mutex_t g_alloc_mutex

References g_alloc_list, and g_alloc_mutex.

Referenced by ck_huge_free().

◆ record_allocation()

static int record_allocation ( void *  ptr,
size_t  len,
int  was_mmap 
)
static

Definition at line 34 of file ckernel_alloc_v6.c.

35{
36 ck_huge_alloc_entry_t *entry = malloc(sizeof(*entry));
37 if (!entry) {
38 return 0;
39 }
40 entry->ptr = ptr;
41 entry->len = len;
42 entry->was_mmap = was_mmap;
43 pthread_mutex_lock(&g_alloc_mutex);
44 entry->next = g_alloc_list;
45 g_alloc_list = entry;
46 pthread_mutex_unlock(&g_alloc_mutex);
47 return 1;
48}

References g_alloc_list, and g_alloc_mutex.

Referenced by ck_huge_alloc().

Variable Documentation

◆ g_alloc_list

ck_huge_alloc_entry_t* g_alloc_list = NULL
static

Definition at line 26 of file ckernel_alloc_v6.c.

Referenced by detach_allocation(), and record_allocation().

◆ g_alloc_mutex

pthread_mutex_t g_alloc_mutex = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 25 of file ckernel_alloc_v6.c.

Referenced by detach_allocation(), and record_allocation().