Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "linker_allocator.h" |
| 18 | |
| 19 | #include <stdlib.h> |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame^] | 20 | #include <sys/cdefs.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include "private/libc_logging.h" |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 24 | |
| 25 | static LinkerMemoryAllocator g_linker_allocator; |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame^] | 26 | static pid_t fallback_tid = 0; |
| 27 | |
| 28 | // Used by libdebuggerd_handler to switch allocators during a crash dump, in |
| 29 | // case the linker heap is corrupted. Do not use this function. |
| 30 | extern "C" void __linker_use_fallback_allocator() { |
| 31 | if (fallback_tid != 0) { |
| 32 | __libc_format_log(ANDROID_LOG_ERROR, "libc", |
| 33 | "attempted to set fallback allocator multiple times"); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | fallback_tid = gettid(); |
| 38 | } |
| 39 | |
| 40 | static LinkerMemoryAllocator& get_fallback_allocator() { |
| 41 | static LinkerMemoryAllocator fallback_allocator; |
| 42 | return fallback_allocator; |
| 43 | } |
| 44 | |
| 45 | static LinkerMemoryAllocator& get_allocator() { |
| 46 | if (__predict_false(fallback_tid) && __predict_false(gettid() == fallback_tid)) { |
| 47 | return get_fallback_allocator(); |
| 48 | } |
| 49 | return g_linker_allocator; |
| 50 | } |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 51 | |
| 52 | void* malloc(size_t byte_count) { |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame^] | 53 | return get_allocator().alloc(byte_count); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void* calloc(size_t item_count, size_t item_size) { |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame^] | 57 | return get_allocator().alloc(item_count*item_size); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void* realloc(void* p, size_t byte_count) { |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame^] | 61 | return get_allocator().realloc(p, byte_count); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void free(void* ptr) { |
Josh Gao | 9ccccc1 | 2017-02-09 10:54:44 -0800 | [diff] [blame^] | 65 | get_allocator().free(ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 66 | } |
| 67 | |