← Back to C-Kernel-Engine Docs Doxygen Source Documentation
memory_pool.h
Go to the documentation of this file.
1 /*
2  * Memory Pool - Thread-safe allocator
3  *
4  * Ported from HPC_Embeddings for use in C-Kernel-Engine.
5  * Provides fast allocations with optional thread safety.
6  *
7  * By Anthony Shivakumar
8  */
9 
10 #ifndef CK_TOKENIZER_MEMPOOL_H
11 #define CK_TOKENIZER_MEMPOOL_H
12 
13 #include <stddef.h>
14 #include <stdint.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /* Default pool size: 16MB */
21 #define CK_TOKENIZER_POOL_DEFAULT_SIZE (16 * 1024 * 1024)
22 
23 /* Block size: 1MB */
24 #define CK_TOKENIZER_POOL_BLOCK_SIZE (1024 * 1024)
25 
26 /* Memory pool structure */
27 typedef struct CKTokenizerMemPool {
28  char *memory; /* Pool memory */
29  size_t size; /* Total pool size */
30  size_t used; /* Bytes used */
31  size_t alignment; /* Allocation alignment */
33 
34 /**
35  * Initialize a memory pool.
36  *
37  * @param pool Pool to initialize
38  * @param size Pool size in bytes (0 = use default)
39  * @return 0 on success, -1 on error
40  */
41 int ck_tokenizer_mempool_init(CKTokenizerMemPool *pool, size_t size);
42 
43 /**
44  * Free a memory pool.
45  *
46  * @param pool Pool to free
47  */
49 
50 /**
51  * Allocate from pool.
52  *
53  * @param pool Pool to allocate from
54  * @param size Size in bytes
55  * @return Pointer to allocated memory, or NULL on error
56  */
57 void *ck_tokenizer_mempool_alloc(CKTokenizerMemPool *pool, size_t size);
58 
59 /**
60  * Allocate aligned memory from pool.
61  *
62  * @param pool Pool to allocate from
63  * @param size Size in bytes
64  * @param align Alignment (must be power of 2)
65  * @return Pointer to allocated memory, or NULL on error
66  */
67 void *ck_tokenizer_mempool_alloc_aligned(CKTokenizerMemPool *pool, size_t size, size_t align);
68 
69 /**
70  * Allocate and copy string (strdup equivalent).
71  *
72  * @param pool Pool to allocate from
73  * @param str String to copy
74  * @return Pointer to copied string, or NULL on error
75  */
76 char *ck_tokenizer_mempool_strdup(CKTokenizerMemPool *pool, const char *str);
77 
78 /**
79  * Allocate and copy string with length.
80  *
81  * @param pool Pool to allocate from
82  * @param str String to copy
83  * @param len Length to copy (-1 for null-terminated)
84  * @return Pointer to copied string, or NULL on error
85  */
86 char *ck_tokenizer_mempool_strndup(CKTokenizerMemPool *pool, const char *str, int len);
87 
88 /**
89  * Reset pool (mark all memory as free).
90  *
91  * @param pool Pool to reset
92  */
94 
95 /**
96  * Get used bytes in pool.
97  *
98  * @param pool Pool to query
99  * @return Number of bytes used
100  */
102 
103 /**
104  * Get available bytes in pool.
105  *
106  * @param pool Pool to query
107  * @return Number of bytes available
108  */
110 
111 /**
112  * Get allocation count.
113  *
114  * @param pool Pool to query
115  * @return Number of allocations
116  */
118 
119 #ifdef __cplusplus
120 }
121 #endif
122 
123 #endif /* CK_TOKENIZER_MEMPOOL_H */
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
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
size_t ck_tokenizer_mempool_alloc_count(CKTokenizerMemPool *pool)