blob: abe17a44dc96a0a43d45c75d3c498f72a98efbc9 [file] [log] [blame]
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <private/bionic_config.h>
18#include <private/bionic_globals.h>
19
20#include <stdio.h>
21
22#include <async_safe/log.h>
23
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -070024#if !defined(LIBC_STATIC)
Evgeny Eltsin143dbdd2020-07-31 18:28:34 +020025extern "C" void* native_bridge_calloc(size_t, size_t);
26extern "C" void native_bridge_free(void*);
27extern "C" struct mallinfo native_bridge_mallinfo();
28extern "C" void* native_bridge_malloc(size_t);
29extern "C" size_t native_bridge_malloc_usable_size(const void*);
30extern "C" void* native_bridge_memalign(size_t, size_t);
31extern "C" int native_bridge_posix_memalign(void**, size_t, size_t);
32extern "C" void* native_bridge_realloc(void*, size_t);
33extern "C" int native_bridge_malloc_iterate(uintptr_t, size_t, void (*)(uintptr_t, size_t, void*), void*);
34extern "C" void native_bridge_malloc_disable();
35extern "C" void native_bridge_malloc_enable();
36extern "C" int native_bridge_mallopt(int, int);
37extern "C" void* native_bridge_aligned_alloc(size_t, size_t);
38
39#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
40extern "C" void* native_bridge_pvalloc(size_t);
41extern "C" void* native_bridge_valloc(size_t);
42#endif
43
44extern "C" int native_bridge_malloc_info_helper(int options, int fd);
45
46static int native_bridge_malloc_info(int options, FILE* fp) {
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -070047 // FILE objects cannot cross architecture boundary!
48 // HACK: extract underlying file descriptor and use it instead.
49 // TODO(b/146494184): at the moment malloc_info succeeds but writes nothing to memory streams!
50 fflush(fp);
51 int fd = fileno(fp);
52 if (fd == -1) {
53 return 0;
54 }
55
Evgeny Eltsin143dbdd2020-07-31 18:28:34 +020056 return native_bridge_malloc_info_helper(options, fd);
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -070057}
58
59static void malloc_init_impl(libc_globals* globals) {
60 static const MallocDispatch malloc_default_dispatch __attribute__((unused)) = {
Evgeny Eltsin143dbdd2020-07-31 18:28:34 +020061 native_bridge_calloc,
62 native_bridge_free,
63 native_bridge_mallinfo,
64 native_bridge_malloc,
65 native_bridge_malloc_usable_size,
66 native_bridge_memalign,
67 native_bridge_posix_memalign,
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -070068#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Evgeny Eltsin143dbdd2020-07-31 18:28:34 +020069 native_bridge_pvalloc,
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -070070#endif
Evgeny Eltsin143dbdd2020-07-31 18:28:34 +020071 native_bridge_realloc,
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -070072#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Evgeny Eltsin143dbdd2020-07-31 18:28:34 +020073 native_bridge_valloc,
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -070074#endif
Evgeny Eltsin143dbdd2020-07-31 18:28:34 +020075 native_bridge_malloc_iterate,
76 native_bridge_malloc_disable,
77 native_bridge_malloc_enable,
78 native_bridge_mallopt,
79 native_bridge_aligned_alloc,
80 native_bridge_malloc_info,
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -070081 };
Evgeny Eltsin1024f052021-02-23 16:43:27 +010082
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -070083 globals->malloc_dispatch_table = malloc_default_dispatch;
Evgeny Eltsin1024f052021-02-23 16:43:27 +010084 atomic_store(&globals->default_dispatch_table, &malloc_default_dispatch);
85 atomic_store(&globals->current_dispatch_table, &malloc_default_dispatch);
Lev Rumyantsev0ea3b6f2020-06-25 13:13:53 -070086}
87
88// Initializes memory allocation framework.
89// This routine is called from __libc_init routines in libc_init_dynamic.cpp.
90__LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) {
91 malloc_init_impl(globals);
92}
93#endif // !LIBC_STATIC