libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
os_alloc.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/os_alloc.h
6 * Hookable allocator abstraction. Default backend is malloc(3);
7 * a vtable lets PG (or libumem, or jemalloc) substitute its own
8 * primitives. See M1_CLAIMS.md, M1-M8.
9 */
10
11#ifndef XTC_OS_ALLOC_H
12#define XTC_OS_ALLOC_H
13
14#include "xtc_export.h"
15
16#include <stddef.h>
17
18/*
19 * The allocator vtable. All six callbacks must be set together; we
20 * do not interleave callbacks from different backends.
21 *
22 * Contract on each callback:
23 * malloc(sz) -> non-NULL pointer of >= sz bytes, or NULL on OOM.
24 * calloc(n, sz) -> zeroed; return NULL on OOM or n*sz overflow.
25 * realloc(p, sz) -> like malloc; if p != NULL, contents are preserved.
26 * free(p) -> p may be NULL.
27 * aligned(a, sz) -> a is a power of two and >= sizeof(void *); NULL on OOM.
28 * aligned_free(p) -> frees memory from aligned(); p may be NULL.
29 *
30 * aligned() and aligned_free() are a matched pair: memory from
31 * aligned() MUST be released with aligned_free(), never free(). On
32 * some platforms (Windows _aligned_malloc) the two heaps are distinct
33 * and crossing them corrupts the heap.
34 *
35 * __os_alloc_set_hook COPIES this structure, so it need not outlive
36 * the call -- a stack local is fine.
37 */
39 void *(*malloc)(size_t sz);
40 void *(*calloc)(size_t n, size_t sz);
41 void *(*realloc)(void *p, size_t sz);
42 void (*free)(void *p);
43 void *(*aligned)(size_t align, size_t sz);
44 void (*aligned_free)(void *p);
45};
46
47/*
48 * Public-internal API. Every function returns an int status code
49 * except __os_free which has no failure mode.
50 *
51 * PUBLIC: int __os_malloc __P((size_t, void **));
52 * PUBLIC: int __os_calloc __P((size_t, size_t, void **));
53 * PUBLIC: int __os_realloc __P((void *, size_t, void **));
54 * PUBLIC: size_t __os_msize __P((void *));
55 * PUBLIC: void __os_free __P((void *));
56 * PUBLIC: int __os_strdup __P((const char *, char **));
57 * PUBLIC: int __os_aligned_alloc __P((size_t, size_t, void **));
58 * PUBLIC: void __os_aligned_free __P((void *));
59 * PUBLIC: int __os_alloc_set_hook __P((const struct __os_alloc_hook *));
60 * PUBLIC: int __os_alloc_get_hook __P((struct __os_alloc_hook *));
61 */
62XTC_API int __os_malloc(size_t sz, void **out);
63XTC_API int __os_calloc(size_t n, size_t sz, void **out);
64XTC_API int __os_realloc(void *p, size_t sz, void **out);
65XTC_API size_t __os_msize(void *p);
66XTC_API void __os_free(void *p);
67XTC_API int __os_strdup(const char *s, char **out);
68XTC_API int __os_aligned_alloc(size_t align, size_t sz, void **out);
69XTC_API void __os_aligned_free(void *p);
70XTC_API int __os_alloc_set_hook(const struct __os_alloc_hook *hook);
71XTC_API int __os_alloc_get_hook(struct __os_alloc_hook *out);
72
73#endif /* XTC_OS_ALLOC_H */