blob: 18ef93e9dd7235e02d788a92d02cb2239e2abf16 [file] [log] [blame]
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070027 */
28
29#include "linker_allocator.h"
30
31#include <stdlib.h>
Josh Gao9ccccc12017-02-09 10:54:44 -080032#include <sys/cdefs.h>
33#include <unistd.h>
34
35#include "private/libc_logging.h"
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070036
37static LinkerMemoryAllocator g_linker_allocator;
Josh Gao9ccccc12017-02-09 10:54:44 -080038static pid_t fallback_tid = 0;
39
40// Used by libdebuggerd_handler to switch allocators during a crash dump, in
41// case the linker heap is corrupted. Do not use this function.
42extern "C" void __linker_use_fallback_allocator() {
43 if (fallback_tid != 0) {
44 __libc_format_log(ANDROID_LOG_ERROR, "libc",
45 "attempted to set fallback allocator multiple times");
46 return;
47 }
48
49 fallback_tid = gettid();
50}
51
52static LinkerMemoryAllocator& get_fallback_allocator() {
53 static LinkerMemoryAllocator fallback_allocator;
54 return fallback_allocator;
55}
56
57static LinkerMemoryAllocator& get_allocator() {
58 if (__predict_false(fallback_tid) && __predict_false(gettid() == fallback_tid)) {
59 return get_fallback_allocator();
60 }
61 return g_linker_allocator;
62}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070063
64void* malloc(size_t byte_count) {
Josh Gao9ccccc12017-02-09 10:54:44 -080065 return get_allocator().alloc(byte_count);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070066}
67
68void* calloc(size_t item_count, size_t item_size) {
Josh Gao9ccccc12017-02-09 10:54:44 -080069 return get_allocator().alloc(item_count*item_size);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070070}
71
72void* realloc(void* p, size_t byte_count) {
Josh Gao9ccccc12017-02-09 10:54:44 -080073 return get_allocator().realloc(p, byte_count);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070074}
75
76void free(void* ptr) {
Josh Gao9ccccc12017-02-09 10:54:44 -080077 get_allocator().free(ptr);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070078}
79