-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjp_alloc.h
More file actions
70 lines (61 loc) · 2.64 KB
/
Copy pathjp_alloc.h
File metadata and controls
70 lines (61 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* jp_alloc.h - per-thread buddy-split allocator (C11)
*
* Public interface for jp_alloc, a per-thread buddy-split allocator.
* See jp_alloc.c for the implementation.
*
* Usage:
* 1. Link jp_alloc.c into your binary. It overrides malloc/free/calloc/
* realloc globally, so all allocations (including libc-internal) route
* through jp_alloc's pools.
* 2. #include this header in any file that wants the jp_* API directly.
* 3. Define JP_ALLOC_IMPLEMENTATION before including this header in
* jp_alloc.c to get the real declaration of jp_alloc_reset (not the
* inline stub).
*
* Configuration (compile-time, all optional):
* -DJP_ALLOC_DEBUG Free-list/double-free/corruption checks
* -DJP_ALLOC_INTERMEDIATE_K=4 Intermediate class ratio (0, 4, or 6)
* -DJP_ALLOC_MADVISE_SIZE=64K Minimum advised pool block size
* -DJP_ALLOC_MADVISE_MODE=... NONE, DONTNEED, or Linux FREE
*
* Platform requirements:
* - GCC 4.7+ or Clang 3.0+ (uses __atomic builtins, _Thread_local, _Alignas)
* - MSVC 2015+ with the compiler fallbacks in jp_alloc.c
* - POSIX (pthreads) or Windows with MinGW (WinPthread)
* - 32-bit and 64-bit supported; no platform-specific intrinsics
*/
#ifndef JP_ALLOC_H
#define JP_ALLOC_H
#include <stddef.h>
#define JP_ALLOC_MADVISE_NONE 0
#define JP_ALLOC_MADVISE_DONTNEED 1
#define JP_ALLOC_MADVISE_FREE 2
#ifdef JP_ALLOC_IMPLEMENTATION
/* jp_alloc.c is linked — real definition of jp_alloc_reset lives there */
void jp_alloc_reset(void);
#else
/* Inline stub used when jp_alloc.c is not linked (no-op cleanup hook) */
static inline void jp_alloc_reset(void) { }
#endif
/* Direct API — also exported as malloc/free/calloc/realloc overrides */
void *jp_alloc(size_t size);
void jp_free(void *mem);
void *jp_calloc(size_t num, size_t nsize);
void *jp_realloc(void *mem, size_t new_size);
void *jp_alloc_aligned(size_t alignment, size_t size);
size_t jp_good_size(size_t size);
/* Sized allocations expose the complete raw size-class block. The same size
* (or another size mapping to the same class) must be supplied when freeing.
* Sized and unsized allocations share the underlying freelists, but their
* allocation/free APIs must not be mixed for a live pointer. */
void *jp_alloc_sized(size_t size);
void jp_free_sized(void *mem, size_t size);
void *jp_realloc_sized(void *mem, size_t old_size, size_t new_size);
struct jp_pool_config {
size_t size;
size_t alignment;
};
#define JP_POOL_CONFIG(type) { sizeof(type), _Alignof(type) }
void *jp_pool_alloc(const struct jp_pool_config *pool);
void jp_pool_free(const struct jp_pool_config *pool, void *mem);
#endif /* JP_ALLOC_H */