master
1/* Prototypes and definition for malloc implementation.
2 Copyright (C) 1996-2025 Free Software Foundation, Inc.
3 Copyright The GNU Toolchain Authors.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20#ifndef _MALLOC_H
21#define _MALLOC_H 1
22
23#include <features.h>
24#include <stddef.h>
25#include <stdio.h>
26
27#ifdef _LIBC
28# define __MALLOC_HOOK_VOLATILE
29# define __MALLOC_DEPRECATED
30#else
31# define __MALLOC_HOOK_VOLATILE volatile
32# define __MALLOC_DEPRECATED __attribute_deprecated__
33#endif
34
35
36__BEGIN_DECLS
37
38/* Allocate SIZE bytes of memory. */
39extern void *malloc (size_t __size) __THROW __attribute_malloc__
40 __attribute_alloc_size__ ((1)) __wur;
41
42/* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
43extern void *calloc (size_t __nmemb, size_t __size)
44__THROW __attribute_malloc__ __attribute_alloc_size__ ((1, 2)) __wur;
45
46/* Re-allocate the previously allocated block in __ptr, making the new
47 block SIZE bytes long. */
48/* __attribute_malloc__ is not used, because if realloc returns
49 the same pointer that was passed to it, aliasing needs to be allowed
50 between objects pointed by the old and new pointers. */
51extern void *realloc (void *__ptr, size_t __size)
52__THROW __attribute_warn_unused_result__ __attribute_alloc_size__ ((2));
53
54/*
55 * zig patch: reallocarray introduced in glibc 2.26
56 * https://sourceware.org/git/?p=glibc.git;a=commit;h=2e0bbbfbf95fc9e22692e93658a6fbdd2d4554da
57 */
58#if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 26) || __GLIBC__ > 2
59/* Re-allocate the previously allocated block in PTR, making the new
60 block large enough for NMEMB elements of SIZE bytes each. */
61/* __attribute_malloc__ is not used, because if reallocarray returns
62 the same pointer that was passed to it, aliasing needs to be allowed
63 between objects pointed by the old and new pointers. */
64extern void *reallocarray (void *__ptr, size_t __nmemb, size_t __size)
65 __THROW __attribute_warn_unused_result__ __attribute_alloc_size__ ((2, 3))
66 __attr_dealloc_free;
67#endif /* glibc v2.26 and later */
68
69/* Free a block allocated by `malloc', `realloc' or `calloc'. */
70extern void free (void *__ptr) __THROW;
71
72/* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
73extern void *memalign (size_t __alignment, size_t __size)
74 __THROW __attribute_malloc__ __attribute_alloc_align__ ((1))
75 __attribute_alloc_size__ ((2)) __wur __attr_dealloc_free;
76
77/* Allocate SIZE bytes on a page boundary. */
78extern void *valloc (size_t __size) __THROW __attribute_malloc__
79 __attribute_alloc_size__ ((1)) __wur __attr_dealloc_free;
80
81/* Equivalent to valloc(minimum-page-that-holds(n)), that is, round up
82 __size to nearest pagesize. */
83extern void *pvalloc (size_t __size) __THROW __attribute_malloc__
84 __wur __attr_dealloc_free;
85
86/* SVID2/XPG mallinfo structure */
87
88struct mallinfo
89{
90 int arena; /* non-mmapped space allocated from system */
91 int ordblks; /* number of free chunks */
92 int smblks; /* number of fastbin blocks */
93 int hblks; /* number of mmapped regions */
94 int hblkhd; /* space in mmapped regions */
95 int usmblks; /* always 0, preserved for backwards compatibility */
96 int fsmblks; /* space available in freed fastbin blocks */
97 int uordblks; /* total allocated space */
98 int fordblks; /* total free space */
99 int keepcost; /* top-most, releasable (via malloc_trim) space */
100};
101
102/* SVID2/XPG mallinfo2 structure which can handle allocations
103 bigger than 4GB. */
104
105struct mallinfo2
106{
107 size_t arena; /* non-mmapped space allocated from system */
108 size_t ordblks; /* number of free chunks */
109 size_t smblks; /* number of fastbin blocks */
110 size_t hblks; /* number of mmapped regions */
111 size_t hblkhd; /* space in mmapped regions */
112 size_t usmblks; /* always 0, preserved for backwards compatibility */
113 size_t fsmblks; /* space available in freed fastbin blocks */
114 size_t uordblks; /* total allocated space */
115 size_t fordblks; /* total free space */
116 size_t keepcost; /* top-most, releasable (via malloc_trim) space */
117};
118
119/* Returns a copy of the updated current mallinfo. */
120extern struct mallinfo mallinfo (void) __THROW __MALLOC_DEPRECATED;
121
122/* Returns a copy of the updated current mallinfo. */
123extern struct mallinfo2 mallinfo2 (void) __THROW;
124
125/* SVID2/XPG mallopt options */
126#ifndef M_MXFAST
127# define M_MXFAST 1 /* maximum request size for "fastbins" */
128#endif
129#ifndef M_NLBLKS
130# define M_NLBLKS 2 /* UNUSED in this malloc */
131#endif
132#ifndef M_GRAIN
133# define M_GRAIN 3 /* UNUSED in this malloc */
134#endif
135#ifndef M_KEEP
136# define M_KEEP 4 /* UNUSED in this malloc */
137#endif
138
139/* mallopt options that actually do something */
140#define M_TRIM_THRESHOLD -1
141#define M_TOP_PAD -2
142#define M_MMAP_THRESHOLD -3
143#define M_MMAP_MAX -4
144#define M_CHECK_ACTION -5
145#define M_PERTURB -6
146#define M_ARENA_TEST -7
147#define M_ARENA_MAX -8
148
149/* General SVID/XPG interface to tunable parameters. */
150extern int mallopt (int __param, int __val) __THROW;
151
152/* Release all but __pad bytes of freed top-most memory back to the
153 system. Return 1 if successful, else 0. */
154extern int malloc_trim (size_t __pad) __THROW;
155
156/* Report the number of usable allocated bytes associated with allocated
157 chunk __ptr. */
158extern size_t malloc_usable_size (void *__ptr) __THROW;
159
160/* Prints brief summary statistics on stderr. */
161extern void malloc_stats (void) __THROW;
162
163/* Output information about state of allocator to stream FP. */
164extern int malloc_info (int __options, FILE *__fp) __THROW;
165
166__END_DECLS
167#endif /* malloc.h */