The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3 | * |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 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 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 7 | * |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 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. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 15 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 16 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | /** |
| 20 | * @file malloc.h |
| 21 | * @brief Heap memory allocation. |
| 22 | * |
| 23 | * [Debugging Native Memory Use](https://source.android.com/devices/tech/debug/native-memory) |
| 24 | * is the canonical source for documentation on Android's heap debugging |
| 25 | * features. |
| 26 | */ |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 27 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 28 | #include <sys/cdefs.h> |
| 29 | #include <stddef.h> |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 31 | |
| 32 | __BEGIN_DECLS |
| 33 | |
Elliott Hughes | 0d1a8a5 | 2018-07-24 19:36:51 +0000 | [diff] [blame] | 34 | // Remove this workaround once b/37423073 is fixed. |
| 35 | #if !__has_attribute(alloc_size) |
George Burgess IV | 7cc779f | 2017-02-09 00:00:31 -0800 | [diff] [blame] | 36 | #define __BIONIC_ALLOC_SIZE(...) |
Elliott Hughes | 36a88e8 | 2016-07-30 09:58:15 -0700 | [diff] [blame] | 37 | #else |
| 38 | #define __BIONIC_ALLOC_SIZE(...) __attribute__((__alloc_size__(__VA_ARGS__))) |
| 39 | #endif |
| 40 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 41 | /** |
| 42 | * [malloc(3)](http://man7.org/linux/man-pages/man3/malloc.3.html) allocates |
| 43 | * memory on the heap. |
| 44 | * |
| 45 | * Returns a pointer to the allocated memory on success and returns a null |
| 46 | * pointer and sets `errno` on failure. |
| 47 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 48 | void* malloc(size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(1) __wur; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * [calloc(3)](http://man7.org/linux/man-pages/man3/calloc.3.html) allocates |
| 52 | * and clears memory on the heap. |
| 53 | * |
| 54 | * Returns a pointer to the allocated memory on success and returns a null |
| 55 | * pointer and sets `errno` on failure. |
| 56 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 57 | void* calloc(size_t __item_count, size_t __item_size) __mallocfunc __BIONIC_ALLOC_SIZE(1,2) __wur; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * [realloc(3)](http://man7.org/linux/man-pages/man3/realloc.3.html) resizes |
| 61 | * allocated memory on the heap. |
| 62 | * |
| 63 | * Returns a pointer (which may be different from `__ptr`) to the resized |
| 64 | * memory on success and returns a null pointer and sets `errno` on failure. |
| 65 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 66 | void* realloc(void* __ptr, size_t __byte_count) __BIONIC_ALLOC_SIZE(2) __wur; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 69 | * [reallocarray(3)](http://man7.org/linux/man-pages/man3/realloc.3.html) resizes |
| 70 | * allocated memory on the heap. |
| 71 | * |
| 72 | * Equivalent to `realloc(__ptr, __item_count * __item_size)` but fails if the |
| 73 | * multiplication overflows. |
| 74 | * |
| 75 | * Returns a pointer (which may be different from `__ptr`) to the resized |
| 76 | * memory on success and returns a null pointer and sets `errno` on failure. |
| 77 | */ |
| 78 | void* reallocarray(void* __ptr, size_t __item_count, size_t __item_size) __BIONIC_ALLOC_SIZE(2, 3) __wur __INTRODUCED_IN(29); |
| 79 | |
| 80 | /** |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 81 | * [free(3)](http://man7.org/linux/man-pages/man3/free.3.html) deallocates |
| 82 | * memory on the heap. |
| 83 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 84 | void free(void* __ptr); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 85 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 86 | /** |
| 87 | * [memalign(3)](http://man7.org/linux/man-pages/man3/memalign.3.html) allocates |
| 88 | * memory on the heap with the required alignment. |
| 89 | * |
| 90 | * Returns a pointer to the allocated memory on success and returns a null |
| 91 | * pointer and sets `errno` on failure. |
| 92 | * |
| 93 | * See also posix_memalign(). |
| 94 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 95 | void* memalign(size_t __alignment, size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(2) __wur; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * [malloc_usable_size(3)](http://man7.org/linux/man-pages/man3/malloc_usable_size.3.html) |
| 99 | * returns the actual size of the given heap block. |
| 100 | * |
| 101 | * Available since API level 17. |
| 102 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 103 | size_t malloc_usable_size(const void* __ptr) __INTRODUCED_IN(17); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 104 | |
Ian Rogers | 2c344d3 | 2012-08-28 15:53:10 -0700 | [diff] [blame] | 105 | #ifndef STRUCT_MALLINFO_DECLARED |
| 106 | #define STRUCT_MALLINFO_DECLARED 1 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 107 | struct mallinfo { |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 108 | /** Total number of non-mmapped bytes currently allocated from OS. */ |
| 109 | size_t arena; |
| 110 | /** Number of free chunks. */ |
| 111 | size_t ordblks; |
| 112 | /** (Unused.) */ |
| 113 | size_t smblks; |
| 114 | /** (Unused.) */ |
| 115 | size_t hblks; |
| 116 | /** Total number of bytes in mmapped regions. */ |
| 117 | size_t hblkhd; |
| 118 | /** Maximum total allocated space; greater than total if trimming has occurred. */ |
| 119 | size_t usmblks; |
| 120 | /** (Unused.) */ |
| 121 | size_t fsmblks; |
| 122 | /** Total allocated space (normal or mmapped.) */ |
| 123 | size_t uordblks; |
| 124 | /** Total free space. */ |
| 125 | size_t fordblks; |
| 126 | /** Upper bound on number of bytes releasable by a trim operation. */ |
| 127 | size_t keepcost; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 128 | }; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 129 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 130 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 131 | /** |
| 132 | * [mallinfo(3)](http://man7.org/linux/man-pages/man3/mallinfo.3.html) returns |
| 133 | * information about the current state of the heap. |
| 134 | */ |
Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 135 | struct mallinfo mallinfo(void); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 136 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 137 | /** |
| 138 | * [malloc_info(3)](http://man7.org/linux/man-pages/man3/malloc_info.3.html) |
| 139 | * writes information about the current state of the heap to the given stream. |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 140 | * |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 141 | * The XML structure for malloc_info() is as follows: |
| 142 | * ``` |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 143 | * <malloc version="jemalloc-1"> |
| 144 | * <heap nr="INT"> |
| 145 | * <allocated-large>INT</allocated-large> |
| 146 | * <allocated-huge>INT</allocated-huge> |
| 147 | * <allocated-bins>INT</allocated-bins> |
| 148 | * <bins-total>INT</bins-total> |
| 149 | * <bin nr="INT"> |
| 150 | * <allocated>INT</allocated> |
| 151 | * <nmalloc>INT</nmalloc> |
| 152 | * <ndalloc>INT</ndalloc> |
| 153 | * </bin> |
| 154 | * <!-- more bins --> |
| 155 | * </heap> |
| 156 | * <!-- more heaps --> |
| 157 | * </malloc> |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 158 | * ``` |
| 159 | * |
| 160 | * Available since API level 23. |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 161 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 162 | int malloc_info(int __must_be_zero, FILE* __fp) __INTRODUCED_IN(23); |
Dan Albert | 4caa1f0 | 2014-08-20 09:16:57 -0700 | [diff] [blame] | 163 | |
Christopher Ferris | 8daea55 | 2018-10-23 11:17:24 -0700 | [diff] [blame^] | 164 | /** |
| 165 | * mallopt() option to set the decay time. Valid values are 0 and 1. |
| 166 | * |
| 167 | * Available since API level 27. |
| 168 | */ |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 169 | #define M_DECAY_TIME -100 |
Christopher Ferris | 8daea55 | 2018-10-23 11:17:24 -0700 | [diff] [blame^] | 170 | /** |
| 171 | * mallopt() option to immediately purge any memory not in use. This |
| 172 | * will release the memory back to the kernel. The value is ignored. |
| 173 | * |
| 174 | * Available since API level 28. |
| 175 | */ |
Tim Murray | ac578f2 | 2018-10-15 16:26:56 -0700 | [diff] [blame] | 176 | #define M_PURGE -101 |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * [mallopt(3)](http://man7.org/linux/man-pages/man3/mallopt.3.html) modifies |
| 180 | * heap behavior. Values of `__option` are the `M_` constants from this header. |
| 181 | * |
| 182 | * Returns 1 on success, 0 on error. |
| 183 | * |
| 184 | * Available since API level 26. |
| 185 | */ |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 186 | int mallopt(int __option, int __value) __INTRODUCED_IN(26); |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 187 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 188 | /** |
| 189 | * [__malloc_hook(3)](http://man7.org/linux/man-pages/man3/__malloc_hook.3.html) |
| 190 | * is called to implement malloc(). By default this points to the system's |
| 191 | * implementation. |
| 192 | * |
| 193 | * Available since API level 28. |
| 194 | * |
| 195 | * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md) |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 196 | */ |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 197 | extern void* (*volatile __malloc_hook)(size_t __byte_count, const void* __caller) __INTRODUCED_IN(28); |
| 198 | |
| 199 | /** |
| 200 | * [__realloc_hook(3)](http://man7.org/linux/man-pages/man3/__realloc_hook.3.html) |
| 201 | * is called to implement realloc(). By default this points to the system's |
| 202 | * implementation. |
| 203 | * |
| 204 | * Available since API level 28. |
| 205 | * |
| 206 | * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md) |
| 207 | */ |
| 208 | extern void* (*volatile __realloc_hook)(void* __ptr, size_t __byte_count, const void* __caller) __INTRODUCED_IN(28); |
| 209 | |
| 210 | /** |
| 211 | * [__free_hook(3)](http://man7.org/linux/man-pages/man3/__free_hook.3.html) |
| 212 | * is called to implement free(). By default this points to the system's |
| 213 | * implementation. |
| 214 | * |
| 215 | * Available since API level 28. |
| 216 | * |
| 217 | * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md) |
| 218 | */ |
| 219 | extern void (*volatile __free_hook)(void* __ptr, const void* __caller) __INTRODUCED_IN(28); |
| 220 | |
| 221 | /** |
| 222 | * [__memalign_hook(3)](http://man7.org/linux/man-pages/man3/__memalign_hook.3.html) |
| 223 | * is called to implement memalign(). By default this points to the system's |
| 224 | * implementation. |
| 225 | * |
| 226 | * Available since API level 28. |
| 227 | * |
| 228 | * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md) |
| 229 | */ |
| 230 | extern void* (*volatile __memalign_hook)(size_t __alignment, size_t __byte_count, const void* __caller) __INTRODUCED_IN(28); |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 231 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 232 | __END_DECLS |