|
26 | 26 | #include <stddef.h> |
27 | 27 | #include <stdarg.h> |
28 | 28 | #include <stdint.h> |
| 29 | +#include <stdlib.h> |
29 | 30 |
|
30 | 31 | enum { |
31 | 32 | _JOSE_CFG_ERR_BASE = 0x1053000000000000ULL, |
@@ -57,6 +58,23 @@ typedef struct jose_cfg jose_cfg_t; |
57 | 58 | typedef void (jose_cfg_err_t)(void *misc, const char *file, int line, |
58 | 59 | uint64_t err, const char *fmt, va_list ap); |
59 | 60 |
|
| 61 | +/** |
| 62 | + * Custom allocator function type signatures |
| 63 | + */ |
| 64 | +typedef void* (*jose_malloc_t)(size_t size); |
| 65 | +typedef void* (*jose_realloc_t)(void *ptr, size_t size); |
| 66 | +typedef void* (*jose_calloc_t)(size_t nmemb, size_t size); |
| 67 | +typedef void (*jose_free_t)(void *ptr); |
| 68 | + |
| 69 | +/** |
| 70 | + * Global memory allocator function pointers |
| 71 | + * These can be overridden using jose_set_alloc() |
| 72 | + */ |
| 73 | +extern jose_malloc_t jose_malloc; |
| 74 | +extern jose_realloc_t jose_realloc; |
| 75 | +extern jose_calloc_t jose_calloc; |
| 76 | +extern jose_free_t jose_free; |
| 77 | + |
60 | 78 | /** |
61 | 79 | * Creates a new configuration instance. |
62 | 80 | * |
@@ -134,4 +152,42 @@ jose_cfg_err(jose_cfg_t *cfg, const char *file, int line, uint64_t err, |
134 | 152 | jose_cfg_err(cfg, __FILE__, __LINE__, err, __VA_ARGS__) |
135 | 153 | #endif |
136 | 154 |
|
| 155 | +/** |
| 156 | + * Set custom memory allocator functions for JOSE operations globally. |
| 157 | + * |
| 158 | + * This allows you to override the default malloc/realloc/free used |
| 159 | + * throughout the JOSE library. All JOSE operations will use these |
| 160 | + * custom allocators once set. Useful for: |
| 161 | + * - Memory debugging and tracking |
| 162 | + * - Custom memory pools |
| 163 | + * - Secure memory allocation |
| 164 | + * - Cross-DLL memory management on Windows |
| 165 | + * |
| 166 | + * @param pmalloc Custom malloc function |
| 167 | + * @param prealloc Custom realloc function |
| 168 | + * @param pcalloc Custom calloc function |
| 169 | + * @param pfree Custom free function |
| 170 | + * @return 0 on success, errno on error |
| 171 | + */ |
| 172 | +int |
| 173 | +jose_set_alloc(jose_malloc_t pmalloc, jose_realloc_t prealloc, jose_free_t pfree, jose_calloc_t pcalloc); |
| 174 | + |
| 175 | +/** |
| 176 | + * Get current memory allocator functions. |
| 177 | + * |
| 178 | + * @param pmalloc Pointer to store current malloc function |
| 179 | + * @param prealloc Pointer to store current realloc function |
| 180 | + * @param pcalloc Pointer to store current calloc function |
| 181 | + * @param pfree Pointer to store current free function |
| 182 | + */ |
| 183 | +void |
| 184 | +jose_get_alloc(jose_malloc_t *pmalloc, jose_realloc_t *prealloc, jose_free_t *pfree, jose_calloc_t *pcalloc); |
| 185 | + |
| 186 | +/** |
| 187 | + * Reset global allocators to system defaults. |
| 188 | + * Useful for testing or when you want to stop using custom allocators. |
| 189 | + */ |
| 190 | +void |
| 191 | +jose_reset_alloc(void); |
| 192 | + |
137 | 193 | /** @} */ |
0 commit comments