Lev Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 24 | #if !defined(LIBC_STATIC) |
Evgeny Eltsin | 143dbdd | 2020-07-31 18:28:34 +0200 | [diff] [blame] | 25 | extern "C" void* native_bridge_calloc(size_t, size_t); |
| 26 | extern "C" void native_bridge_free(void*); |
| 27 | extern "C" struct mallinfo native_bridge_mallinfo(); |
| 28 | extern "C" void* native_bridge_malloc(size_t); |
| 29 | extern "C" size_t native_bridge_malloc_usable_size(const void*); |
| 30 | extern "C" void* native_bridge_memalign(size_t, size_t); |
| 31 | extern "C" int native_bridge_posix_memalign(void**, size_t, size_t); |
| 32 | extern "C" void* native_bridge_realloc(void*, size_t); |
| 33 | extern "C" int native_bridge_malloc_iterate(uintptr_t, size_t, void (*)(uintptr_t, size_t, void*), void*); |
| 34 | extern "C" void native_bridge_malloc_disable(); |
| 35 | extern "C" void native_bridge_malloc_enable(); |
| 36 | extern "C" int native_bridge_mallopt(int, int); |
| 37 | extern "C" void* native_bridge_aligned_alloc(size_t, size_t); |
| 38 | |
| 39 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 40 | extern "C" void* native_bridge_pvalloc(size_t); |
| 41 | extern "C" void* native_bridge_valloc(size_t); |
| 42 | #endif |
| 43 | |
| 44 | extern "C" int native_bridge_malloc_info_helper(int options, int fd); |
| 45 | |
| 46 | static int native_bridge_malloc_info(int options, FILE* fp) { |
Lev Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 47 | // 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 Eltsin | 143dbdd | 2020-07-31 18:28:34 +0200 | [diff] [blame] | 56 | return native_bridge_malloc_info_helper(options, fd); |
Lev Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void malloc_init_impl(libc_globals* globals) { |
| 60 | static const MallocDispatch malloc_default_dispatch __attribute__((unused)) = { |
Evgeny Eltsin | 143dbdd | 2020-07-31 18:28:34 +0200 | [diff] [blame] | 61 | 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 Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 68 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
Evgeny Eltsin | 143dbdd | 2020-07-31 18:28:34 +0200 | [diff] [blame] | 69 | native_bridge_pvalloc, |
Lev Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 70 | #endif |
Evgeny Eltsin | 143dbdd | 2020-07-31 18:28:34 +0200 | [diff] [blame] | 71 | native_bridge_realloc, |
Lev Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 72 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
Evgeny Eltsin | 143dbdd | 2020-07-31 18:28:34 +0200 | [diff] [blame] | 73 | native_bridge_valloc, |
Lev Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 74 | #endif |
Evgeny Eltsin | 143dbdd | 2020-07-31 18:28:34 +0200 | [diff] [blame] | 75 | 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 Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 81 | }; |
Evgeny Eltsin | 1024f05 | 2021-02-23 16:43:27 +0100 | [diff] [blame^] | 82 | |
Lev Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 83 | globals->malloc_dispatch_table = malloc_default_dispatch; |
Evgeny Eltsin | 1024f05 | 2021-02-23 16:43:27 +0100 | [diff] [blame^] | 84 | atomic_store(&globals->default_dispatch_table, &malloc_default_dispatch); |
| 85 | atomic_store(&globals->current_dispatch_table, &malloc_default_dispatch); |
Lev Rumyantsev | 0ea3b6f | 2020-06-25 13:13:53 -0700 | [diff] [blame] | 86 | } |
| 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 |