blob: 598a50bc5a42e5ecbcdb506db89814930f86d7f7 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Ian Rogers2c344d32012-08-28 15:53:10 -07002 * Copyright (C) 2012 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 *
Ian Rogers2c344d32012-08-28 15:53:10 -07004 * 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 Project1dc9e472009-03-03 19:28:35 -08007 *
Ian Rogers2c344d32012-08-28 15:53:10 -07008 * 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 Project1dc9e472009-03-03 19:28:35 -080015 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080016
Elliott Hughes462e90c2018-08-21 16:10:48 -070017#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 Rogers2c344d32012-08-28 15:53:10 -070027
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080028#include <sys/cdefs.h>
29#include <stddef.h>
Dan Albert4caa1f02014-08-20 09:16:57 -070030#include <stdio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031
32__BEGIN_DECLS
33
Elliott Hughes36a88e82016-07-30 09:58:15 -070034#define __BIONIC_ALLOC_SIZE(...) __attribute__((__alloc_size__(__VA_ARGS__)))
Elliott Hughes36a88e82016-07-30 09:58:15 -070035
Elliott Hughes462e90c2018-08-21 16:10:48 -070036/**
37 * [malloc(3)](http://man7.org/linux/man-pages/man3/malloc.3.html) allocates
38 * memory on the heap.
39 *
40 * Returns a pointer to the allocated memory on success and returns a null
41 * pointer and sets `errno` on failure.
42 */
Elliott Hughesfaa74342017-08-11 17:34:44 -070043void* malloc(size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(1) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070044
45/**
46 * [calloc(3)](http://man7.org/linux/man-pages/man3/calloc.3.html) allocates
47 * and clears memory on the heap.
48 *
49 * Returns a pointer to the allocated memory on success and returns a null
50 * pointer and sets `errno` on failure.
51 */
Elliott Hughesfaa74342017-08-11 17:34:44 -070052void* calloc(size_t __item_count, size_t __item_size) __mallocfunc __BIONIC_ALLOC_SIZE(1,2) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070053
54/**
55 * [realloc(3)](http://man7.org/linux/man-pages/man3/realloc.3.html) resizes
56 * allocated memory on the heap.
57 *
58 * Returns a pointer (which may be different from `__ptr`) to the resized
59 * memory on success and returns a null pointer and sets `errno` on failure.
60 */
Elliott Hughesfaa74342017-08-11 17:34:44 -070061void* realloc(void* __ptr, size_t __byte_count) __BIONIC_ALLOC_SIZE(2) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070062
63/**
Elliott Hughesb1770852018-09-18 12:52:42 -070064 * [reallocarray(3)](http://man7.org/linux/man-pages/man3/realloc.3.html) resizes
65 * allocated memory on the heap.
66 *
67 * Equivalent to `realloc(__ptr, __item_count * __item_size)` but fails if the
68 * multiplication overflows.
69 *
70 * Returns a pointer (which may be different from `__ptr`) to the resized
71 * memory on success and returns a null pointer and sets `errno` on failure.
72 */
73void* reallocarray(void* __ptr, size_t __item_count, size_t __item_size) __BIONIC_ALLOC_SIZE(2, 3) __wur __INTRODUCED_IN(29);
74
75/**
Elliott Hughes462e90c2018-08-21 16:10:48 -070076 * [free(3)](http://man7.org/linux/man-pages/man3/free.3.html) deallocates
77 * memory on the heap.
78 */
Elliott Hughesfaa74342017-08-11 17:34:44 -070079void free(void* __ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080
Elliott Hughes462e90c2018-08-21 16:10:48 -070081/**
82 * [memalign(3)](http://man7.org/linux/man-pages/man3/memalign.3.html) allocates
83 * memory on the heap with the required alignment.
84 *
85 * Returns a pointer to the allocated memory on success and returns a null
86 * pointer and sets `errno` on failure.
87 *
88 * See also posix_memalign().
89 */
Elliott Hughesfaa74342017-08-11 17:34:44 -070090void* memalign(size_t __alignment, size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(2) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070091
92/**
93 * [malloc_usable_size(3)](http://man7.org/linux/man-pages/man3/malloc_usable_size.3.html)
94 * returns the actual size of the given heap block.
95 *
96 * Available since API level 17.
97 */
Elliott Hughesfaa74342017-08-11 17:34:44 -070098size_t malloc_usable_size(const void* __ptr) __INTRODUCED_IN(17);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099
Ian Rogers2c344d32012-08-28 15:53:10 -0700100#ifndef STRUCT_MALLINFO_DECLARED
101#define STRUCT_MALLINFO_DECLARED 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102struct mallinfo {
Elliott Hughes462e90c2018-08-21 16:10:48 -0700103 /** Total number of non-mmapped bytes currently allocated from OS. */
104 size_t arena;
105 /** Number of free chunks. */
106 size_t ordblks;
107 /** (Unused.) */
108 size_t smblks;
109 /** (Unused.) */
110 size_t hblks;
111 /** Total number of bytes in mmapped regions. */
112 size_t hblkhd;
113 /** Maximum total allocated space; greater than total if trimming has occurred. */
114 size_t usmblks;
115 /** (Unused.) */
116 size_t fsmblks;
117 /** Total allocated space (normal or mmapped.) */
118 size_t uordblks;
119 /** Total free space. */
120 size_t fordblks;
121 /** Upper bound on number of bytes releasable by a trim operation. */
122 size_t keepcost;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123};
Elliott Hughes462e90c2018-08-21 16:10:48 -0700124#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125
Elliott Hughes462e90c2018-08-21 16:10:48 -0700126/**
127 * [mallinfo(3)](http://man7.org/linux/man-pages/man3/mallinfo.3.html) returns
Elliott Hughes4fa9b8c2019-04-30 12:39:46 -0700128 * information about the current state of the heap. Note that mallinfo() is
129 * inherently unreliable and consider using malloc_info() instead.
Elliott Hughes462e90c2018-08-21 16:10:48 -0700130 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700131struct mallinfo mallinfo(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132
Elliott Hughes462e90c2018-08-21 16:10:48 -0700133/**
134 * [malloc_info(3)](http://man7.org/linux/man-pages/man3/malloc_info.3.html)
135 * writes information about the current state of the heap to the given stream.
Dan Albert4caa1f02014-08-20 09:16:57 -0700136 *
Elliott Hughes462e90c2018-08-21 16:10:48 -0700137 * The XML structure for malloc_info() is as follows:
138 * ```
Dan Albert4caa1f02014-08-20 09:16:57 -0700139 * <malloc version="jemalloc-1">
140 * <heap nr="INT">
141 * <allocated-large>INT</allocated-large>
142 * <allocated-huge>INT</allocated-huge>
143 * <allocated-bins>INT</allocated-bins>
144 * <bins-total>INT</bins-total>
145 * <bin nr="INT">
146 * <allocated>INT</allocated>
147 * <nmalloc>INT</nmalloc>
148 * <ndalloc>INT</ndalloc>
149 * </bin>
150 * <!-- more bins -->
151 * </heap>
152 * <!-- more heaps -->
153 * </malloc>
Elliott Hughes462e90c2018-08-21 16:10:48 -0700154 * ```
155 *
156 * Available since API level 23.
Dan Albert4caa1f02014-08-20 09:16:57 -0700157 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700158int malloc_info(int __must_be_zero, FILE* __fp) __INTRODUCED_IN(23);
Dan Albert4caa1f02014-08-20 09:16:57 -0700159
Christopher Ferris8daea552018-10-23 11:17:24 -0700160/**
161 * mallopt() option to set the decay time. Valid values are 0 and 1.
162 *
163 * Available since API level 27.
164 */
Chih-Hung Hsiehfa658eb2020-03-04 11:14:42 -0800165#define M_DECAY_TIME (-100)
Christopher Ferris8daea552018-10-23 11:17:24 -0700166/**
167 * mallopt() option to immediately purge any memory not in use. This
168 * will release the memory back to the kernel. The value is ignored.
169 *
170 * Available since API level 28.
171 */
Chih-Hung Hsiehfa658eb2020-03-04 11:14:42 -0800172#define M_PURGE (-101)
Peter Collingbourne978eb162020-09-21 15:26:02 -0700173/*
174 * mallopt() option for per-thread memory initialization tuning.
175 * The value argument should be one of:
176 * 1: Disable automatic heap initialization and, where possible, memory tagging,
177 * on this thread.
178 * 0: Normal behavior.
179 *
180 * Available since API level 31.
181 */
182#define M_THREAD_DISABLE_MEM_INIT (-103)
Christopher Ferris88448792020-07-28 14:15:31 -0700183/**
184 * mallopt() option to set the maximum number of items in the secondary
185 * cache of the scudo allocator.
186 *
187 * Available since API level 31.
188 */
189#define M_CACHE_COUNT_MAX (-200)
190/**
191 * mallopt() option to set the maximum size in bytes of a cacheable item in
192 * the secondary cache of the scudo allocator.
193 *
194 * Available since API level 31.
195 */
196#define M_CACHE_SIZE_MAX (-201)
197/**
198 * mallopt() option to increase the maximum number of shared thread-specific
199 * data structures that can be created. This number cannot be decreased,
200 * only increased and only applies to the scudo allocator.
201 *
202 * Available since API level 31.
203 */
204#define M_TSDS_COUNT_MAX (-202)
Elliott Hughes462e90c2018-08-21 16:10:48 -0700205
206/**
Elliott Hughes446b4dd2021-01-14 13:34:20 -0800207 * mallopt() option to disable heap initialization across the whole process.
208 * If the hardware supports memory tagging, this also disables memory tagging.
209 * May be called at any time, including when multiple threads are running. The
210 * value is unused but must be set to 0.
211 *
212 * Note that these memory mitigations are only implemented in scudo and
213 * therefore this will have no effect when using another allocator (such as
214 * jemalloc on Android Go devices).
215 *
216 * Available since API level 31.
217 */
218#define M_BIONIC_DISABLE_MEMORY_MITIGATIONS (-203)
219
220/**
221 * mallopt() option to change the heap tagging state. May be called at any
222 * time, including when multiple threads are running.
223 * The value must be one of the M_HEAP_TAGGING_LEVEL_ constants.
224 *
225 * Available since API level 31.
226 */
227#define M_BIONIC_SET_HEAP_TAGGING_LEVEL (-204)
228
229/**
230 * Constants for use with the M_BIONIC_SET_HEAP_TAGGING_LEVEL mallopt() option.
231 */
232enum HeapTaggingLevel {
233 /**
234 * Disable heap tagging and memory tag checks (if supported).
235 * Heap tagging may not be re-enabled after being disabled.
236 */
237 M_HEAP_TAGGING_LEVEL_NONE = 0,
238#define M_HEAP_TAGGING_LEVEL_NONE M_HEAP_TAGGING_LEVEL_NONE
239 /**
240 * Address-only tagging. Heap pointers have a non-zero tag in the
241 * most significant ("top") byte which is checked in free(). Memory
242 * accesses ignore the tag using arm64's Top Byte Ignore (TBI) feature.
243 */
244 M_HEAP_TAGGING_LEVEL_TBI = 1,
245#define M_HEAP_TAGGING_LEVEL_TBI M_HEAP_TAGGING_LEVEL_TBI
246 /**
247 * Enable heap tagging and asynchronous memory tag checks (if supported).
248 * Disable stack trace collection.
249 */
250 M_HEAP_TAGGING_LEVEL_ASYNC = 2,
251#define M_HEAP_TAGGING_LEVEL_ASYNC M_HEAP_TAGGING_LEVEL_ASYNC
252 /**
253 * Enable heap tagging and synchronous memory tag checks (if supported).
254 * Enable stack trace collection.
255 */
256 M_HEAP_TAGGING_LEVEL_SYNC = 3,
257#define M_HEAP_TAGGING_LEVEL_SYNC M_HEAP_TAGGING_LEVEL_SYNC
258};
259
260/**
Elliott Hughes462e90c2018-08-21 16:10:48 -0700261 * [mallopt(3)](http://man7.org/linux/man-pages/man3/mallopt.3.html) modifies
262 * heap behavior. Values of `__option` are the `M_` constants from this header.
263 *
264 * Returns 1 on success, 0 on error.
265 *
266 * Available since API level 26.
267 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700268int mallopt(int __option, int __value) __INTRODUCED_IN(26);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700269
Elliott Hughes462e90c2018-08-21 16:10:48 -0700270/**
271 * [__malloc_hook(3)](http://man7.org/linux/man-pages/man3/__malloc_hook.3.html)
272 * is called to implement malloc(). By default this points to the system's
273 * implementation.
274 *
275 * Available since API level 28.
276 *
277 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md)
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800278 */
Elliott Hughes462e90c2018-08-21 16:10:48 -0700279extern void* (*volatile __malloc_hook)(size_t __byte_count, const void* __caller) __INTRODUCED_IN(28);
280
281/**
282 * [__realloc_hook(3)](http://man7.org/linux/man-pages/man3/__realloc_hook.3.html)
283 * is called to implement realloc(). By default this points to the system's
284 * implementation.
285 *
286 * Available since API level 28.
287 *
288 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md)
289 */
290extern void* (*volatile __realloc_hook)(void* __ptr, size_t __byte_count, const void* __caller) __INTRODUCED_IN(28);
291
292/**
293 * [__free_hook(3)](http://man7.org/linux/man-pages/man3/__free_hook.3.html)
294 * is called to implement free(). By default this points to the system's
295 * implementation.
296 *
297 * Available since API level 28.
298 *
299 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md)
300 */
301extern void (*volatile __free_hook)(void* __ptr, const void* __caller) __INTRODUCED_IN(28);
302
303/**
304 * [__memalign_hook(3)](http://man7.org/linux/man-pages/man3/__memalign_hook.3.html)
305 * is called to implement memalign(). By default this points to the system's
306 * implementation.
307 *
308 * Available since API level 28.
309 *
310 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md)
311 */
312extern void* (*volatile __memalign_hook)(size_t __alignment, size_t __byte_count, const void* __caller) __INTRODUCED_IN(28);
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800313
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800314__END_DECLS