]>
Commit | Line | Data |
---|---|---|
12f5832b DG |
1 | // This file is a wrapper around malloc.c, which is the upstream source file. |
2 | // It sets configuration flags and controls which symbols are exported. | |
320054e8 DG |
3 | |
4 | #include <stddef.h> | |
7ba6adfc | 5 | #include <malloc.h> |
320054e8 | 6 | |
12f5832b | 7 | // Define configuration macros for dlmalloc. |
dbfccac2 | 8 | |
12f5832b | 9 | // WebAssembly doesn't have mmap-style memory allocation. |
dbfccac2 DG |
10 | #define HAVE_MMAP 0 |
11 | ||
12f5832b | 12 | // WebAssembly doesn't support shrinking linear memory. |
dbfccac2 DG |
13 | #define MORECORE_CANNOT_TRIM 1 |
14 | ||
12f5832b | 15 | // Disable sanity checks to reduce code size. |
dbfccac2 DG |
16 | #define ABORT __builtin_unreachable() |
17 | ||
12f5832b | 18 | // If threads are enabled, enable support for threads. |
320054e8 | 19 | #ifdef _REENTRANT |
dbfccac2 DG |
20 | #define USE_LOCKS 1 |
21 | #endif | |
22 | ||
12f5832b | 23 | // Make malloc deterministic. |
dbfccac2 DG |
24 | #define LACKS_TIME_H 1 |
25 | ||
12f5832b | 26 | // Disable malloc statistics generation to reduce code size. |
dbfccac2 DG |
27 | #define NO_MALLINFO 1 |
28 | #define NO_MALLOC_STATS 1 | |
29 | ||
12f5832b | 30 | // Align malloc regions to 16, to avoid unaligned SIMD accesses. |
c9a57330 DG |
31 | #define MALLOC_ALIGNMENT 16 |
32 | ||
12f5832b DG |
33 | // Declare errno values used by dlmalloc. We define them like this to avoid |
34 | // putting specific errno values in the ABI. | |
dbfccac2 DG |
35 | extern const int __ENOMEM; |
36 | #define ENOMEM __ENOMEM | |
37 | extern const int __EINVAL; | |
38 | #define EINVAL __EINVAL | |
39 | ||
12f5832b DG |
40 | // Define USE_DL_PREFIX so that we leave dlmalloc's names prefixed with 'dl'. |
41 | // We define them as "static", and we wrap them with public names below. This | |
42 | // serves two purposes: | |
43 | // | |
44 | // One is to make it easy to control which symbols are exported; dlmalloc | |
45 | // defines several non-standard functions and we wish to explicitly control | |
46 | // which functions are part of our public-facing interface. | |
47 | // | |
48 | // The other is to protect against compilers optimizing based on the assumption | |
49 | // that they know what functions with names like "malloc" do. Code in the | |
50 | // implementation will call functions like "dlmalloc" and assume it can use | |
51 | // the resulting pointers to access the metadata outside of the nominally | |
52 | // allocated objects. However, if the function were named "malloc", compilers | |
53 | // might see code like that and assume it has undefined behavior and can be | |
54 | // optimized away. By using "dlmalloc" in the implementation, we don't need | |
55 | // -fno-builtin to avoid this problem. | |
dbfccac2 DG |
56 | #define USE_DL_PREFIX 1 |
57 | #define DLMALLOC_EXPORT static inline | |
58 | ||
12f5832b | 59 | // This isn't declared with DLMALLOC_EXPORT so make it static explicitly. |
320054e8 DG |
60 | static size_t dlmalloc_usable_size(void*); |
61 | ||
12f5832b | 62 | // Include the upstream dlmalloc's malloc.c. |
dbfccac2 DG |
63 | #include "malloc.c" |
64 | ||
12f5832b | 65 | // Export the public names. |
dbfccac2 DG |
66 | |
67 | void *malloc(size_t size) { | |
68 | return dlmalloc(size); | |
69 | } | |
70 | ||
71 | void free(void *ptr) { | |
7ba6adfc | 72 | dlfree(ptr); |
dbfccac2 DG |
73 | } |
74 | ||
75 | void *calloc(size_t nmemb, size_t size) { | |
76 | return dlcalloc(nmemb, size); | |
77 | } | |
78 | ||
79 | void *realloc(void *ptr, size_t size) { | |
80 | return dlrealloc(ptr, size); | |
81 | } | |
82 | ||
83 | int posix_memalign(void **memptr, size_t alignment, size_t size) { | |
84 | return dlposix_memalign(memptr, alignment, size); | |
85 | } | |
320054e8 DG |
86 | |
87 | void* aligned_alloc(size_t alignment, size_t bytes) { | |
88 | return dlmemalign(alignment, bytes); | |
89 | } | |
90 | ||
91 | size_t malloc_usable_size(void *ptr) { | |
92 | return dlmalloc_usable_size(ptr); | |
93 | } | |
322bd4ff DG |
94 | |
95 | // Define these to satisfy musl references. | |
96 | void *__libc_malloc(size_t) __attribute__((alias("malloc"))); | |
97 | void __libc_free(void *) __attribute__((alias("free"))); | |
98 | void *__libc_calloc(size_t nmemb, size_t size) __attribute__((alias("calloc"))); |