projects/aports·blob master · refs

103 lines · 3.6 KiB

1--- a/src/3rdparty/chromium/base/allocator/allocator_shim_default_dispatch_to_glibc.cc
2+++ b/src/3rdparty/chromium/base/allocator/allocator_shim_default_dispatch_to_glibc.cc
3@@ -8,6 +8,7 @@
4 #include <dlfcn.h>
5 #include <malloc.h>
6
7+#if defined(__GLIBC__)
8 // This translation unit defines a default dispatch for the allocator shim which
9 // routes allocations to libc functions.
10 // The code here is strongly inspired from tcmalloc's libc_override_glibc.h.
11@@ -87,3 +88,92 @@ const AllocatorDispatch AllocatorDispatc
12 nullptr, /* aligned_free_function */
13 nullptr, /* next */
14 };
15+
16+#else // defined(__GLIBC__)
17+
18+#include <dlfcn.h>
19+
20+extern "C" {
21+// Declare function pointers to the memory functions
22+typedef void* (*t_libc_malloc)(size_t size);
23+typedef void* (*t_libc_calloc)(size_t n, size_t size);
24+typedef void* (*t_libc_realloc)(void* address, size_t size);
25+typedef void* (*t_libc_memalign)(size_t alignment, size_t size);
26+typedef void (*t_libc_free)(void* ptr);
27+typedef size_t (*t_libc_malloc_usable_size)(void* ptr);
28+
29+// Static instances of pointers to libc.so dl symbols
30+static t_libc_malloc libc_malloc = NULL;
31+static t_libc_calloc libc_calloc = NULL;
32+static t_libc_realloc libc_realloc = NULL;
33+static t_libc_memalign libc_memalign = NULL;
34+static t_libc_free libc_free = NULL;
35+static t_libc_malloc_usable_size libc_malloc_usable_size = NULL;
36+
37+// resolve the symbols in libc.so
38+void musl_libc_memory_init(void)
39+{
40+ libc_malloc = (t_libc_malloc) dlsym(RTLD_NEXT, "malloc");
41+ libc_calloc = (t_libc_calloc) dlsym(RTLD_NEXT, "calloc");
42+ libc_realloc = (t_libc_realloc) dlsym(RTLD_NEXT, "realloc");
43+ libc_memalign = (t_libc_memalign) dlsym(RTLD_NEXT, "memalign");
44+ libc_free = (t_libc_free) dlsym(RTLD_NEXT, "free");
45+ libc_malloc_usable_size = (t_libc_malloc_usable_size) dlsym(RTLD_NEXT, "malloc_usable_size");
46+}
47+} // extern "C"
48+
49+namespace {
50+
51+using base::allocator::AllocatorDispatch;
52+
53+void* MuslMalloc(const AllocatorDispatch*, size_t size, void* context) {
54+ if (!libc_malloc)
55+ musl_libc_memory_init();
56+ return (*libc_malloc)(size);
57+}
58+
59+void* MuslCalloc(const AllocatorDispatch*, size_t n, size_t size, void* context) {
60+ if (!libc_calloc)
61+ musl_libc_memory_init();
62+ return (*libc_calloc)(n, size);
63+}
64+
65+void* MuslRealloc(const AllocatorDispatch*, void* address, size_t size, void* context) {
66+ if (!libc_realloc)
67+ musl_libc_memory_init();
68+ return (*libc_realloc)(address, size);
69+}
70+
71+void* MuslMemalign(const AllocatorDispatch*, size_t alignment, size_t size, void* context) {
72+ if (!libc_memalign)
73+ musl_libc_memory_init();
74+ return (*libc_memalign)(alignment, size);
75+}
76+
77+void MuslFree(const AllocatorDispatch*, void* address, void* context) {
78+ if (!libc_free)
79+ musl_libc_memory_init();
80+ (*libc_free)(address);
81+}
82+
83+size_t MuslGetSizeEstimate(const AllocatorDispatch*, void* address, void* context) {
84+ // TODO(siggi, primiano): malloc_usable_size may need redirection in the
85+ // presence of interposing shims that divert allocations.
86+ if (!libc_malloc_usable_size)
87+ musl_libc_memory_init();
88+ return (*libc_malloc_usable_size)(address);
89+}
90+
91+} // namespace
92+
93+const AllocatorDispatch AllocatorDispatch::default_dispatch = {
94+ &MuslMalloc, /* alloc_function */
95+ &MuslCalloc, /* alloc_zero_initialized_function */
96+ &MuslMemalign, /* alloc_aligned_function */
97+ &MuslRealloc, /* realloc_function */
98+ &MuslFree, /* free_function */
99+ &MuslGetSizeEstimate, /* get_size_estimate_function */
100+ nullptr, /* next */
101+};
102+
103+#endif