← Back to C-Kernel-Engine Docs Doxygen Source Documentation
memory_pool.c
Go to the documentation of this file.
1 /*
2  * Memory Pool Implementation
3  *
4  * Ported from HPC_Embeddings for use in C-Kernel-Engine tokenizer.
5  */
6 
8 #include <stdlib.h>
9 #include <string.h>
10 
12  if (size == 0) {
14  }
15 
16  pool->memory = (char *)malloc(size);
17  if (!pool->memory) {
18  return -1;
19  }
20 
21  pool->size = size;
22  pool->used = 0;
23  pool->alignment = 8;
24 
25  return 0;
26 }
27 
29  if (pool->memory) {
30  free(pool->memory);
31  pool->memory = NULL;
32  }
33  pool->size = 0;
34  pool->used = 0;
35 }
36 
37 static size_t align_up(size_t n, size_t align) {
38  return (n + align - 1) & ~(align - 1);
39 }
40 
42  if (!pool || !pool->memory || size == 0) {
43  return NULL;
44  }
45 
46  size = align_up(size, pool->alignment);
47 
48  if (pool->used + size > pool->size) {
49  return NULL;
50  }
51 
52  void *ptr = pool->memory + pool->used;
53  pool->used += size;
54 
55  return ptr;
56 }
57 
58 void *ck_tokenizer_mempool_alloc_aligned(CKTokenizerMemPool *pool, size_t size, size_t align) {
59  if (!pool || !pool->memory || size == 0) {
60  return NULL;
61  }
62 
63  /* Validate alignment is power of 2 */
64  if (align & (align - 1)) {
65  return NULL;
66  }
67 
68  /* Align up the allocation size */
69  size = align_up(size, align);
70 
71  /* Align the current position */
72  size_t misalignment = pool->used & (align - 1);
73  size_t padding = (misalignment == 0) ? 0 : (align - misalignment);
74 
75  if (pool->used + padding + size > pool->size) {
76  return NULL;
77  }
78 
79  pool->used += padding;
80  void *ptr = pool->memory + pool->used;
81  pool->used += size;
82 
83  return ptr;
84 }
85 
86 char *ck_tokenizer_mempool_strdup(CKTokenizerMemPool *pool, const char *str) {
87  if (!pool || !str) {
88  return NULL;
89  }
90  return ck_tokenizer_mempool_strndup(pool, str, -1);
91 }
92 
93 char *ck_tokenizer_mempool_strndup(CKTokenizerMemPool *pool, const char *str, int len) {
94  if (!pool || !str) {
95  return NULL;
96  }
97 
98  if (len < 0) {
99  len = (int)strlen(str);
100  }
101 
102  char *copy = (char *)ck_tokenizer_mempool_alloc(pool, len + 1);
103  if (!copy) {
104  return NULL;
105  }
106 
107  memcpy(copy, str, len);
108  copy[len] = '\0';
109 
110  return copy;
111 }
112 
114  if (pool) {
115  pool->used = 0;
116  }
117 }
118 
120  return pool ? pool->used : 0;
121 }
122 
124  if (!pool) {
125  return 0;
126  }
127  return pool->size - pool->used;
128 }
char * ck_tokenizer_mempool_strndup(CKTokenizerMemPool *pool, const char *str, int len)
Definition: memory_pool.c:93
char * ck_tokenizer_mempool_strdup(CKTokenizerMemPool *pool, const char *str)
Definition: memory_pool.c:86
static size_t align_up(size_t n, size_t align)
Definition: memory_pool.c:37
void ck_tokenizer_mempool_reset(CKTokenizerMemPool *pool)
Definition: memory_pool.c:113
void * ck_tokenizer_mempool_alloc_aligned(CKTokenizerMemPool *pool, size_t size, size_t align)
Definition: memory_pool.c:58
int ck_tokenizer_mempool_init(CKTokenizerMemPool *pool, size_t size)
Definition: memory_pool.c:11
size_t ck_tokenizer_mempool_available(CKTokenizerMemPool *pool)
Definition: memory_pool.c:123
size_t ck_tokenizer_mempool_used(CKTokenizerMemPool *pool)
Definition: memory_pool.c:119
void ck_tokenizer_mempool_free(CKTokenizerMemPool *pool)
Definition: memory_pool.c:28
void * ck_tokenizer_mempool_alloc(CKTokenizerMemPool *pool, size_t size)
Definition: memory_pool.c:41
#define CK_TOKENIZER_POOL_DEFAULT_SIZE
Definition: memory_pool.h:21