]> git.proxmox.com Git - wasi-libc.git/blob - dlmalloc/src/dlmalloc.c
cb9cc81270403668d9ce569c8e1fecfa5ec138a5
[wasi-libc.git] / dlmalloc / src / dlmalloc.c
1 /*
2 * This file is a wrapper around malloc.c, which is the upstream source file.
3 * It sets configuration flags and controls which symbols are exported.
4 */
5
6 #include <stddef.h>
7 #include <malloc.h>
8
9 /* Define configuration macros for dlmalloc. */
10
11 /* WebAssembly doesn't have mmap-style memory allocation. */
12 #define HAVE_MMAP 0
13
14 /* WebAssembly doesn't support shrinking linear memory. */
15 #define MORECORE_CANNOT_TRIM 1
16
17 /* Disable sanity checks to reduce code size. */
18 #define ABORT __builtin_unreachable()
19
20 /* If threads are enabled, enable support for threads. */
21 #ifdef _REENTRANT
22 #define USE_LOCKS 1
23 #endif
24
25 /* Make malloc deterministic. */
26 #define LACKS_TIME_H 1
27
28 /* Disable malloc statistics generation to reduce code size. */
29 #define NO_MALLINFO 1
30 #define NO_MALLOC_STATS 1
31
32 /* Align malloc regions to 16, to avoid unaligned SIMD accesses. */
33 #define MALLOC_ALIGNMENT 16
34
35 /*
36 * Declare errno values used by dlmalloc. We define them like this to avoid
37 * putting specific errno values in the ABI.
38 */
39 extern const int __ENOMEM;
40 #define ENOMEM __ENOMEM
41 extern const int __EINVAL;
42 #define EINVAL __EINVAL
43
44 /* Prefix dlmalloc's names with 'dl'. We wrap them with public names below. */
45 #define USE_DL_PREFIX 1
46 #define DLMALLOC_EXPORT static inline
47
48 /* This isn't declared with DLMALLOC_EXPORT so make it static explicitly. */
49 static size_t dlmalloc_usable_size(void*);
50
51 /* Include the upstream dlmalloc's malloc.c. */
52 #include "malloc.c"
53
54 /* Export the public names. */
55
56 void *malloc(size_t size) {
57 return dlmalloc(size);
58 }
59
60 void free(void *ptr) {
61 dlfree(ptr);
62 }
63
64 void *calloc(size_t nmemb, size_t size) {
65 return dlcalloc(nmemb, size);
66 }
67
68 void *realloc(void *ptr, size_t size) {
69 return dlrealloc(ptr, size);
70 }
71
72 int posix_memalign(void **memptr, size_t alignment, size_t size) {
73 return dlposix_memalign(memptr, alignment, size);
74 }
75
76 void* aligned_alloc(size_t alignment, size_t bytes) {
77 return dlmemalign(alignment, bytes);
78 }
79
80 size_t malloc_usable_size(void *ptr) {
81 return dlmalloc_usable_size(ptr);
82 }