blob: 7144224e98b81f2cbe64f285a3edbe4bf9352df0 [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 Hughes0d1a8a52018-07-24 19:36:51 +000034// Remove this workaround once b/37423073 is fixed.
35#if !__has_attribute(alloc_size)
George Burgess IV7cc779f2017-02-09 00:00:31 -080036#define __BIONIC_ALLOC_SIZE(...)
Elliott Hughes36a88e82016-07-30 09:58:15 -070037#else
38#define __BIONIC_ALLOC_SIZE(...) __attribute__((__alloc_size__(__VA_ARGS__)))
39#endif
40
Elliott Hughes462e90c2018-08-21 16:10:48 -070041/**
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 Hughesfaa74342017-08-11 17:34:44 -070048void* malloc(size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(1) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070049
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 Hughesfaa74342017-08-11 17:34:44 -070057void* calloc(size_t __item_count, size_t __item_size) __mallocfunc __BIONIC_ALLOC_SIZE(1,2) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070058
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 Hughesfaa74342017-08-11 17:34:44 -070066void* realloc(void* __ptr, size_t __byte_count) __BIONIC_ALLOC_SIZE(2) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070067
68/**
69 * [free(3)](http://man7.org/linux/man-pages/man3/free.3.html) deallocates
70 * memory on the heap.
71 */
Elliott Hughesfaa74342017-08-11 17:34:44 -070072void free(void* __ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073
Elliott Hughes462e90c2018-08-21 16:10:48 -070074/**
75 * [memalign(3)](http://man7.org/linux/man-pages/man3/memalign.3.html) allocates
76 * memory on the heap with the required alignment.
77 *
78 * Returns a pointer to the allocated memory on success and returns a null
79 * pointer and sets `errno` on failure.
80 *
81 * See also posix_memalign().
82 */
Elliott Hughesfaa74342017-08-11 17:34:44 -070083void* memalign(size_t __alignment, size_t __byte_count) __mallocfunc __BIONIC_ALLOC_SIZE(2) __wur;
Elliott Hughes462e90c2018-08-21 16:10:48 -070084
85/**
86 * [malloc_usable_size(3)](http://man7.org/linux/man-pages/man3/malloc_usable_size.3.html)
87 * returns the actual size of the given heap block.
88 *
89 * Available since API level 17.
90 */
Elliott Hughesfaa74342017-08-11 17:34:44 -070091size_t malloc_usable_size(const void* __ptr) __INTRODUCED_IN(17);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
Ian Rogers2c344d32012-08-28 15:53:10 -070093#ifndef STRUCT_MALLINFO_DECLARED
94#define STRUCT_MALLINFO_DECLARED 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095struct mallinfo {
Elliott Hughes462e90c2018-08-21 16:10:48 -070096 /** Total number of non-mmapped bytes currently allocated from OS. */
97 size_t arena;
98 /** Number of free chunks. */
99 size_t ordblks;
100 /** (Unused.) */
101 size_t smblks;
102 /** (Unused.) */
103 size_t hblks;
104 /** Total number of bytes in mmapped regions. */
105 size_t hblkhd;
106 /** Maximum total allocated space; greater than total if trimming has occurred. */
107 size_t usmblks;
108 /** (Unused.) */
109 size_t fsmblks;
110 /** Total allocated space (normal or mmapped.) */
111 size_t uordblks;
112 /** Total free space. */
113 size_t fordblks;
114 /** Upper bound on number of bytes releasable by a trim operation. */
115 size_t keepcost;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116};
Elliott Hughes462e90c2018-08-21 16:10:48 -0700117#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118
Elliott Hughes462e90c2018-08-21 16:10:48 -0700119/**
120 * [mallinfo(3)](http://man7.org/linux/man-pages/man3/mallinfo.3.html) returns
121 * information about the current state of the heap.
122 */
Elliott Hughes3b2096a2016-07-22 18:57:12 -0700123struct mallinfo mallinfo(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
Elliott Hughes462e90c2018-08-21 16:10:48 -0700125/**
126 * [malloc_info(3)](http://man7.org/linux/man-pages/man3/malloc_info.3.html)
127 * writes information about the current state of the heap to the given stream.
Dan Albert4caa1f02014-08-20 09:16:57 -0700128 *
Elliott Hughes462e90c2018-08-21 16:10:48 -0700129 * The XML structure for malloc_info() is as follows:
130 * ```
Dan Albert4caa1f02014-08-20 09:16:57 -0700131 * <malloc version="jemalloc-1">
132 * <heap nr="INT">
133 * <allocated-large>INT</allocated-large>
134 * <allocated-huge>INT</allocated-huge>
135 * <allocated-bins>INT</allocated-bins>
136 * <bins-total>INT</bins-total>
137 * <bin nr="INT">
138 * <allocated>INT</allocated>
139 * <nmalloc>INT</nmalloc>
140 * <ndalloc>INT</ndalloc>
141 * </bin>
142 * <!-- more bins -->
143 * </heap>
144 * <!-- more heaps -->
145 * </malloc>
Elliott Hughes462e90c2018-08-21 16:10:48 -0700146 * ```
147 *
148 * Available since API level 23.
Dan Albert4caa1f02014-08-20 09:16:57 -0700149 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700150int malloc_info(int __must_be_zero, FILE* __fp) __INTRODUCED_IN(23);
Dan Albert4caa1f02014-08-20 09:16:57 -0700151
Elliott Hughes462e90c2018-08-21 16:10:48 -0700152/** mallopt() option to set the decay time. Valid values are 0 and 1. */
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700153#define M_DECAY_TIME -100
Elliott Hughes462e90c2018-08-21 16:10:48 -0700154
155/**
156 * [mallopt(3)](http://man7.org/linux/man-pages/man3/mallopt.3.html) modifies
157 * heap behavior. Values of `__option` are the `M_` constants from this header.
158 *
159 * Returns 1 on success, 0 on error.
160 *
161 * Available since API level 26.
162 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700163int mallopt(int __option, int __value) __INTRODUCED_IN(26);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700164
Elliott Hughes462e90c2018-08-21 16:10:48 -0700165/**
166 * [__malloc_hook(3)](http://man7.org/linux/man-pages/man3/__malloc_hook.3.html)
167 * is called to implement malloc(). By default this points to the system's
168 * implementation.
169 *
170 * Available since API level 28.
171 *
172 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md)
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800173 */
Elliott Hughes462e90c2018-08-21 16:10:48 -0700174extern void* (*volatile __malloc_hook)(size_t __byte_count, const void* __caller) __INTRODUCED_IN(28);
175
176/**
177 * [__realloc_hook(3)](http://man7.org/linux/man-pages/man3/__realloc_hook.3.html)
178 * is called to implement realloc(). By default this points to the system's
179 * implementation.
180 *
181 * Available since API level 28.
182 *
183 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md)
184 */
185extern void* (*volatile __realloc_hook)(void* __ptr, size_t __byte_count, const void* __caller) __INTRODUCED_IN(28);
186
187/**
188 * [__free_hook(3)](http://man7.org/linux/man-pages/man3/__free_hook.3.html)
189 * is called to implement free(). By default this points to the system's
190 * implementation.
191 *
192 * Available since API level 28.
193 *
194 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md)
195 */
196extern void (*volatile __free_hook)(void* __ptr, const void* __caller) __INTRODUCED_IN(28);
197
198/**
199 * [__memalign_hook(3)](http://man7.org/linux/man-pages/man3/__memalign_hook.3.html)
200 * is called to implement memalign(). By default this points to the system's
201 * implementation.
202 *
203 * Available since API level 28.
204 *
205 * See also: [extra documentation](https://android.googlesource.com/platform/bionic/+/master/libc/malloc_hooks/README.md)
206 */
207extern void* (*volatile __memalign_hook)(size_t __alignment, size_t __byte_count, const void* __caller) __INTRODUCED_IN(28);
Christopher Ferrisdb478a62018-02-07 18:42:14 -0800208
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209__END_DECLS