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 five 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 */
36 void *(*malloc)(size_t sz);
37 void *(*calloc)(size_t n, size_t sz);
38 void *(*realloc)(void *p, size_t sz);
39 void (*free)(void *p);
40 void *(*aligned)(size_t align, size_t sz);
41 void (*aligned_free)(void *p);
42};
43
44/*
45 * Public-internal API. Every function returns an int status code
46 * except __os_free which has no failure mode.
47 *
48 * PUBLIC: int __os_malloc __P((size_t, void **));
49 * PUBLIC: int __os_calloc __P((size_t, size_t, void **));
50 * PUBLIC: int __os_realloc __P((void *, size_t, void **));
51 * PUBLIC: size_t __os_msize __P((void *));
52 * PUBLIC: void __os_free __P((void *));
53 * PUBLIC: int __os_strdup __P((const char *, char **));
54 * PUBLIC: int __os_aligned_alloc __P((size_t, size_t, void **));
55 * PUBLIC: void __os_aligned_free __P((void *));
56 * PUBLIC: int __os_alloc_set_hook __P((const struct __os_alloc_hook *));
57 * PUBLIC: int __os_alloc_get_hook __P((struct __os_alloc_hook *));
58 */
59XTC_API int __os_malloc(size_t sz, void **out);
60XTC_API int __os_calloc(size_t n, size_t sz, void **out);
61XTC_API int __os_realloc(void *p, size_t sz, void **out);
62XTC_API size_t __os_msize(void *p);
63XTC_API void __os_free(void *p);
64XTC_API int __os_strdup(const char *s, char **out);
65XTC_API int __os_aligned_alloc(size_t align, size_t sz, void **out);
66XTC_API void __os_aligned_free(void *p);
67XTC_API int __os_alloc_set_hook(const struct __os_alloc_hook *hook);
68XTC_API int __os_alloc_get_hook(struct __os_alloc_hook *out);
69
70#endif /* XTC_OS_ALLOC_H */