|
1 | 1 | /* malloc_hook.c - Provides low level hooks for malloc/free |
2 | 2 | * Copyright 2023 - chris.rohlf@gmail.com */ |
3 | 3 |
|
4 | | -#include "iso_alloc.h" |
5 | | -#include "iso_alloc_internal.h" |
| 4 | +/* This file must be included from iso_alloc_interfaces.c so that |
| 5 | + * alias targets (iso_alloc, iso_free, etc.) are defined in the same |
| 6 | + * translation unit. When compiled directly the guard |
| 7 | + * below produces an empty translation unit with no effect. */ |
| 8 | +#if !defined(ISO_IN_INTERFACES_C) |
| 9 | +#error "malloc_hook.c must be included from iso_alloc_interfaces.c (so aliases can work)" |
| 10 | +#endif |
| 11 | + |
| 12 | +#include "iso_alloc_hook.h" |
6 | 13 |
|
7 | 14 | /* The MALLOC_HOOK configuration allows us to hook the usual |
8 | 15 | * malloc interfaces and redirect them to the iso_alloc API. |
|
13 | 20 | */ |
14 | 21 | #if MALLOC_HOOK |
15 | 22 |
|
16 | | -EXTERNAL_API void *__libc_malloc(size_t s) { |
17 | | - return iso_alloc(s); |
18 | | -} |
| 23 | +/* malloc/free/calloc/realloc and their __libc_ variants are |
| 24 | + * direct linker aliases for the iso_ equivalents on GCC >= 9 |
| 25 | + * and Clang >= 10 (see iso_alloc_hook.h). This propagates all |
| 26 | + * function attributes (malloc, alloc_size, nothrow, etc.) from |
| 27 | + * the iso_ symbol to the exported symbol via copy(fun), and |
| 28 | + * eliminates the wrapper call entirely. |
| 29 | + * |
| 30 | + * Functions with differing signatures or custom logic (posix_memalign, |
| 31 | + * aligned_alloc, memalign, malloc_size, malloc_good_size) remain |
| 32 | + * as wrapper functions. */ |
19 | 33 |
|
20 | | -EXTERNAL_API void *malloc(size_t s) { |
21 | | - return iso_alloc(s); |
22 | | -} |
| 34 | +EXTERNAL_API void *__libc_malloc(size_t s) ISO_FORWARD1(iso_alloc, s) |
| 35 | +EXTERNAL_API void *malloc(size_t s) ISO_FORWARD1(iso_alloc, s) |
23 | 36 |
|
24 | | -EXTERNAL_API void __libc_free(void *p) { |
25 | | - iso_free(p); |
26 | | -} |
| 37 | +EXTERNAL_API void __libc_free(void *p) ISO_FORWARD0(iso_free, p) |
| 38 | +EXTERNAL_API void free(void *p) ISO_FORWARD0(iso_free, p) |
27 | 39 |
|
28 | | -EXTERNAL_API void free(void *p) { |
29 | | - iso_free(p); |
30 | | -} |
| 40 | +EXTERNAL_API void *__libc_calloc(size_t n, size_t s) ISO_FORWARD2(iso_calloc, n, s) |
| 41 | +EXTERNAL_API void *calloc(size_t n, size_t s) ISO_FORWARD2(iso_calloc, n, s) |
31 | 42 |
|
32 | | -EXTERNAL_API void *__libc_calloc(size_t n, size_t s) { |
33 | | - return iso_calloc(n, s); |
34 | | -} |
| 43 | +EXTERNAL_API void *__libc_realloc(void *p, size_t s) ISO_FORWARD2(iso_realloc, p, s) |
| 44 | +EXTERNAL_API void *realloc(void *p, size_t s) ISO_FORWARD2(iso_realloc, p, s) |
35 | 45 |
|
36 | | -EXTERNAL_API void *calloc(size_t n, size_t s) { |
37 | | - return iso_calloc(n, s); |
38 | | -} |
39 | | - |
40 | | -EXTERNAL_API void *__libc_realloc(void *p, size_t s) { |
41 | | - return iso_realloc(p, s); |
42 | | -} |
43 | | - |
44 | | -EXTERNAL_API void *realloc(void *p, size_t s) { |
45 | | - return iso_realloc(p, s); |
46 | | -} |
47 | | - |
48 | | -EXTERNAL_API void *__libc_reallocarray(void *p, size_t n, size_t s) { |
49 | | - return iso_reallocarray(p, n, s); |
50 | | -} |
51 | | - |
52 | | -EXTERNAL_API void *reallocarray(void *p, size_t n, size_t s) { |
53 | | - return iso_reallocarray(p, n, s); |
54 | | -} |
| 46 | +EXTERNAL_API void *__libc_reallocarray(void *p, size_t n, size_t s) ISO_FORWARD3(iso_reallocarray, p, n, s) |
| 47 | +EXTERNAL_API void *reallocarray(void *p, size_t n, size_t s) ISO_FORWARD3(iso_reallocarray, p, n, s) |
55 | 48 |
|
56 | 49 | EXTERNAL_API int __posix_memalign(void **r, size_t a, size_t s) { |
57 | 50 | if(is_pow2(a) == false) { |
@@ -104,9 +97,8 @@ EXTERNAL_API size_t malloc_good_size(size_t size) { |
104 | 97 | return ALIGN_SZ_UP(size); |
105 | 98 | } |
106 | 99 | #else |
107 | | -EXTERNAL_API size_t malloc_usable_size(void *ptr) { |
108 | | - return iso_chunksz(ptr); |
109 | | -} |
| 100 | +/* On Linux (non-Android) malloc_usable_size takes void* matching iso_chunksz */ |
| 101 | +EXTERNAL_API size_t malloc_usable_size(void *ptr) ISO_FORWARD1(iso_chunksz, ptr) |
110 | 102 | #endif |
111 | 103 |
|
112 | 104 | static void *libc_malloc(size_t s, const void *caller) { |
|
0 commit comments