1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * All rights reserved. 4 * 5 * 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. 14 * 15 * 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. 27 */ 28 29 #pragma once 30 31 #include <stdbool.h> 32 33 // Structures for android_mallopt. 34 35 typedef struct { 36 // Pointer to the buffer allocated by a call to M_GET_MALLOC_LEAK_INFO. 37 uint8_t* buffer; 38 // The size of the "info" buffer. 39 size_t overall_size; 40 // The size of a single entry. 41 size_t info_size; 42 // The sum of all allocations that have been tracked. Does not include 43 // any heap overhead. 44 size_t total_memory; 45 // The maximum number of backtrace entries. 46 size_t backtrace_size; 47 } android_mallopt_leak_info_t; 48 49 // Opcodes for android_mallopt. 50 51 enum { 52 // Marks the calling process as a profileable zygote child, possibly 53 // initializing profiling infrastructure. 54 M_INIT_ZYGOTE_CHILD_PROFILING = 1, 55 #define M_INIT_ZYGOTE_CHILD_PROFILING M_INIT_ZYGOTE_CHILD_PROFILING 56 M_RESET_HOOKS = 2, 57 #define M_RESET_HOOKS M_RESET_HOOKS 58 // Set an upper bound on the total size in bytes of all allocations made 59 // using the memory allocation APIs. 60 // arg = size_t* 61 // arg_size = sizeof(size_t) 62 M_SET_ALLOCATION_LIMIT_BYTES = 3, 63 #define M_SET_ALLOCATION_LIMIT_BYTES M_SET_ALLOCATION_LIMIT_BYTES 64 // Called after the zygote forks to indicate this is a child. 65 M_SET_ZYGOTE_CHILD = 4, 66 #define M_SET_ZYGOTE_CHILD M_SET_ZYGOTE_CHILD 67 68 // Options to dump backtraces of allocations. These options only 69 // work when malloc debug has been enabled. 70 71 // Writes the backtrace information of all current allocations to a file. 72 // NOTE: arg_size has to be sizeof(FILE*) because FILE is an opaque type. 73 // arg = FILE* 74 // arg_size = sizeof(FILE*) 75 M_WRITE_MALLOC_LEAK_INFO_TO_FILE = 5, 76 #define M_WRITE_MALLOC_LEAK_INFO_TO_FILE M_WRITE_MALLOC_LEAK_INFO_TO_FILE 77 // Get information about the backtraces of all 78 // arg = android_mallopt_leak_info_t* 79 // arg_size = sizeof(android_mallopt_leak_info_t) 80 M_GET_MALLOC_LEAK_INFO = 6, 81 #define M_GET_MALLOC_LEAK_INFO M_GET_MALLOC_LEAK_INFO 82 // Free the memory allocated and returned by M_GET_MALLOC_LEAK_INFO. 83 // arg = android_mallopt_leak_info_t* 84 // arg_size = sizeof(android_mallopt_leak_info_t) 85 M_FREE_MALLOC_LEAK_INFO = 7, 86 #define M_FREE_MALLOC_LEAK_INFO M_FREE_MALLOC_LEAK_INFO 87 }; 88 89 // Manipulates bionic-specific handling of memory allocation APIs such as 90 // malloc. Only for use by the Android platform itself. 91 // 92 // On success, returns true. On failure, returns false and sets errno. 93 extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size); 94