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 <stddef.h>
15
16/*
17 * The allocator vtable. All five callbacks must be set together; we
18 * do not interleave callbacks from different backends.
19 *
20 * Contract on each callback:
21 * malloc(sz) -> non-NULL pointer of >= sz bytes, or NULL on OOM.
22 * calloc(n, sz) -> zeroed; return NULL on OOM or n*sz overflow.
23 * realloc(p, sz) -> like malloc; if p != NULL, contents are preserved.
24 * free(p) -> p may be NULL.
25 * aligned(a, sz) -> a is a power of two and >= sizeof(void *); NULL on OOM.
26 * aligned_free(p) -> frees memory from aligned(); p may be NULL.
27 *
28 * aligned() and aligned_free() are a matched pair: memory from
29 * aligned() MUST be released with aligned_free(), never free(). On
30 * some platforms (Windows _aligned_malloc) the two heaps are distinct
31 * and crossing them corrupts the heap.
32 */
34 void *(*malloc)(size_t sz);
35 void *(*calloc)(size_t n, size_t sz);
36 void *(*realloc)(void *p, size_t sz);
37 void (*free)(void *p);
38 void *(*aligned)(size_t align, size_t sz);
39 void (*aligned_free)(void *p);
40};
41
42/*
43 * Public-internal API. Every function returns an int status code
44 * except __os_free which has no failure mode.
45 *
46 * PUBLIC: int __os_malloc __P((size_t, void **));
47 * PUBLIC: int __os_calloc __P((size_t, size_t, void **));
48 * PUBLIC: int __os_realloc __P((void *, size_t, void **));
49 * PUBLIC: size_t __os_msize __P((void *));
50 * PUBLIC: void __os_free __P((void *));
51 * PUBLIC: int __os_strdup __P((const char *, char **));
52 * PUBLIC: int __os_aligned_alloc __P((size_t, size_t, void **));
53 * PUBLIC: void __os_aligned_free __P((void *));
54 * PUBLIC: int __os_alloc_set_hook __P((const struct __os_alloc_hook *));
55 * PUBLIC: int __os_alloc_get_hook __P((struct __os_alloc_hook *));
56 */
57int __os_malloc(size_t sz, void **out);
58int __os_calloc(size_t n, size_t sz, void **out);
59int __os_realloc(void *p, size_t sz, void **out);
60size_t __os_msize(void *p);
61void __os_free(void *p);
62int __os_strdup(const char *s, char **out);
63int __os_aligned_alloc(size_t align, size_t sz, void **out);
64void __os_aligned_free(void *p);
65int __os_alloc_set_hook(const struct __os_alloc_hook *hook);
66int __os_alloc_get_hook(struct __os_alloc_hook *out);
67
68#endif /* XTC_OS_ALLOC_H */