1 #ifndef JEMALLOC_INTERNAL_HOOK_H
2 #define JEMALLOC_INTERNAL_HOOK_H
4 #include "jemalloc/internal/tsd.h"
7 * This API is *extremely* experimental, and may get ripped out, changed in API-
8 * and ABI-incompatible ways, be insufficiently or incorrectly documented, etc.
10 * It allows hooking the stateful parts of the API to see changes as they
13 * Allocation hooks are called after the allocation is done, free hooks are
14 * called before the free is done, and expand hooks are called after the
15 * allocation is expanded.
17 * For realloc and rallocx, if the expansion happens in place, the expansion
18 * hook is called. If it is moved, then the alloc hook is called on the new
19 * location, and then the free hook is called on the old location (i.e. both
20 * hooks are invoked in between the alloc and the dalloc).
22 * If we return NULL from OOM, then usize might not be trustworthy. Calling
23 * realloc(NULL, size) only calls the alloc hook, and calling realloc(ptr, 0)
24 * only calls the free hook. (Calling realloc(NULL, 0) is treated as malloc(0),
25 * and only calls the alloc hook).
28 * Reentrancy is guarded against from within the hook implementation. If you
29 * call allocator functions from within a hook, the hooks will not be invoked
32 * The installation of a hook synchronizes with all its uses. If you can
33 * prove the installation of a hook happens-before a jemalloc entry point,
34 * then the hook will get invoked (unless there's a racing removal).
36 * Hook insertion appears to be atomic at a per-thread level (i.e. if a thread
37 * allocates and has the alloc hook invoked, then a subsequent free on the
38 * same thread will also have the free hook invoked).
40 * The *removal* of a hook does *not* block until all threads are done with
41 * the hook. Hook authors have to be resilient to this, and need some
42 * out-of-band mechanism for cleaning up any dynamically allocated memory
43 * associated with their hook.
45 * Order of hook execution is unspecified, and may be different than insertion
53 hook_alloc_posix_memalign,
54 hook_alloc_aligned_alloc,
60 /* The reallocating functions have both alloc and dalloc variants */
65 * We put the enum typedef after the enum, since this file may get included by
66 * jemalloc_cpp.cpp, and C++ disallows enum forward declarations.
68 typedef enum hook_alloc_e hook_alloc_t;
76 * The dalloc halves of reallocation (not called if in-place expansion
82 typedef enum hook_dalloc_e hook_dalloc_t;
90 typedef enum hook_expand_e hook_expand_t;
92 typedef void (*hook_alloc)(
93 void *extra, hook_alloc_t type, void *result, uintptr_t result_raw,
94 uintptr_t args_raw[3]);
96 typedef void (*hook_dalloc)(
97 void *extra, hook_dalloc_t type, void *address, uintptr_t args_raw[3]);
99 typedef void (*hook_expand)(
100 void *extra, hook_expand_t type, void *address, size_t old_usize,
101 size_t new_usize, uintptr_t result_raw, uintptr_t args_raw[4]);
103 typedef struct hooks_s hooks_t;
105 hook_alloc alloc_hook;
106 hook_dalloc dalloc_hook;
107 hook_expand expand_hook;
112 * Begin implementation details; everything above this point might one day live
113 * in a public API. Everything below this point never will.
117 * The realloc pathways haven't gotten any refactoring love in a while, and it's
118 * fairly difficult to pass information from the entry point to the hooks. We
119 * put the informaiton the hooks will need into a struct to encapsulate
122 * Much of these pathways are force-inlined, so that the compiler can avoid
123 * materializing this struct until we hit an extern arena function. For fairly
124 * goofy reasons, *many* of the realloc paths hit an extern arena function.
125 * These paths are cold enough that it doesn't matter; eventually, we should
126 * rewrite the realloc code to make the expand-in-place and the
127 * free-then-realloc paths more orthogonal, at which point we don't need to
128 * spread the hook logic all over the place.
130 typedef struct hook_ralloc_args_s hook_ralloc_args_t;
131 struct hook_ralloc_args_s {
132 /* I.e. as opposed to rallocx. */
135 * The expand hook takes 4 arguments, even if only 3 are actually used;
136 * we add an extra one in case the user decides to memcpy without
137 * looking too closely at the hooked function.
143 * Returns an opaque handle to be used when removing the hook. NULL means that
144 * we couldn't install the hook.
148 void *hook_install(tsdn_t *tsdn, hooks_t *hooks);
149 /* Uninstalls the hook with the handle previously returned from hook_install. */
150 void hook_remove(tsdn_t *tsdn, void *opaque);
154 void hook_invoke_alloc(hook_alloc_t type, void *result, uintptr_t result_raw,
155 uintptr_t args_raw[3]);
157 void hook_invoke_dalloc(hook_dalloc_t type, void *address,
158 uintptr_t args_raw[3]);
160 void hook_invoke_expand(hook_expand_t type, void *address, size_t old_usize,
161 size_t new_usize, uintptr_t result_raw, uintptr_t args_raw[4]);
163 #endif /* JEMALLOC_INTERNAL_HOOK_H */