1 /* Prototypes and definition for malloc implementation.
2    Copyright (C) 1996,97,99,2000,2002,2003,2004 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19 
20 #ifndef _MALLOC_H
21 #define _MALLOC_H 1
22 
23 #ifdef _LIBC
24 #include <features.h>
25 #endif
26 
27 /*
28   $Id: malloc.h,v 1.7 2004/08/08 12:34:57 wg Exp $
29   `ptmalloc2', a malloc implementation for multiple threads without
30   lock contention, by Wolfram Gloger <wg@malloc.de>.
31 
32   VERSION 2.7.0
33 
34   This work is mainly derived from malloc-2.7.0 by Doug Lea
35   <dl@cs.oswego.edu>, which is available from:
36 
37                  ftp://gee.cs.oswego.edu/pub/misc/malloc.c
38 
39   This trimmed-down header file only provides function prototypes and
40   the exported data structures.  For more detailed function
41   descriptions and compile-time options, see the source file
42   `malloc.c'.
43 */
44 
45 #if defined(__STDC__) || defined (__cplusplus)
46 # include <stddef.h>
47 # define __malloc_ptr_t  void *
48 #else
49 # undef  size_t
50 # define size_t          unsigned int
51 # undef  ptrdiff_t
52 # define ptrdiff_t       int
53 # define __malloc_ptr_t  char *
54 #endif
55 
56 #ifdef _LIBC
57 /* Used by GNU libc internals. */
58 # define __malloc_size_t size_t
59 # define __malloc_ptrdiff_t ptrdiff_t
60 #elif !defined __attribute_malloc__
61 # define __attribute_malloc__
62 #endif
63 
64 #ifdef __GNUC__
65 
66 /* GCC can always grok prototypes.  For C++ programs we add throw()
67    to help it optimize the function calls.  But this works only with
68    gcc 2.8.x and egcs.  */
69 # if defined __cplusplus && (__GNUC__ >= 3 || __GNUC_MINOR__ >= 8)
70 #  define __THROW	throw ()
71 # else
72 #  define __THROW
73 # endif
74 # define __MALLOC_P(args)	args __THROW
75 /* This macro will be used for functions which might take C++ callback
76    functions.  */
77 # define __MALLOC_PMT(args)	args
78 
79 #else	/* Not GCC.  */
80 
81 # define __THROW
82 
83 # if (defined __STDC__ && __STDC__) || defined __cplusplus
84 
85 #  define __MALLOC_P(args)	args
86 #  define __MALLOC_PMT(args)	args
87 
88 #  ifndef __const
89 #   define __const	 const
90 #  endif
91 
92 # else	/* Not ANSI C or C++.  */
93 
94 #  define __MALLOC_P(args)	()	/* No prototypes.  */
95 #  define __MALLOC_PMT(args)	()
96 
97 #  ifndef __const
98 #   define __const
99 #  endif
100 
101 # endif	/* ANSI C or C++.  */
102 
103 #endif	/* GCC.  */
104 
105 #ifndef NULL
106 # ifdef __cplusplus
107 #  define NULL	0
108 # else
109 #  define NULL	((__malloc_ptr_t) 0)
110 # endif
111 #endif
112 
113 #ifdef __cplusplus
114 extern "C" {
115 #endif
116 
117 /* Nonzero if the malloc is already initialized.  */
118 #ifdef _LIBC
119 /* In the GNU libc we rename the global variable
120    `__malloc_initialized' to `__libc_malloc_initialized'.  */
121 # define __malloc_initialized __libc_malloc_initialized
122 #endif
123 extern int __malloc_initialized;
124 
125 /* Allocate SIZE bytes of memory.  */
126 extern __malloc_ptr_t malloc __MALLOC_P ((size_t __size)) __attribute_malloc__;
127 
128 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0.  */
129 extern __malloc_ptr_t calloc __MALLOC_P ((size_t __nmemb, size_t __size))
130        __attribute_malloc__;
131 
132 /* Re-allocate the previously allocated block in __ptr, making the new
133    block SIZE bytes long.  */
134 extern __malloc_ptr_t realloc __MALLOC_P ((__malloc_ptr_t __ptr,
135 					   size_t __size))
136        __attribute_malloc__;
137 
138 /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
139 extern void free __MALLOC_P ((__malloc_ptr_t __ptr));
140 
141 /* Free a block allocated by `calloc'. */
142 extern void cfree __MALLOC_P ((__malloc_ptr_t __ptr));
143 
144 /* Allocate SIZE bytes allocated to ALIGNMENT bytes.  */
145 extern __malloc_ptr_t memalign __MALLOC_P ((size_t __alignment, size_t __size));
146 
147 /* Allocate SIZE bytes on a page boundary.  */
148 extern __malloc_ptr_t valloc __MALLOC_P ((size_t __size)) __attribute_malloc__;
149 
150 /* Equivalent to valloc(minimum-page-that-holds(n)), that is, round up
151    __size to nearest pagesize. */
152 extern __malloc_ptr_t  pvalloc __MALLOC_P ((size_t __size))
153        __attribute_malloc__;
154 
155 /* Underlying allocation function; successive calls should return
156    contiguous pieces of memory.  */
157 extern __malloc_ptr_t (*__morecore) __MALLOC_PMT ((ptrdiff_t __size));
158 
159 /* Default value of `__morecore'.  */
160 extern __malloc_ptr_t __default_morecore __MALLOC_P ((ptrdiff_t __size))
161        __attribute_malloc__;
162 
163 /* SVID2/XPG mallinfo structure */
164 
165 struct mallinfo {
166   int arena;    /* non-mmapped space allocated from system */
167   int ordblks;  /* number of free chunks */
168   int smblks;   /* number of fastbin blocks */
169   int hblks;    /* number of mmapped regions */
170   int hblkhd;   /* space in mmapped regions */
171   int usmblks;  /* maximum total allocated space */
172   int fsmblks;  /* space available in freed fastbin blocks */
173   int uordblks; /* total allocated space */
174   int fordblks; /* total free space */
175   int keepcost; /* top-most, releasable (via malloc_trim) space */
176 };
177 
178 /* Returns a copy of the updated current mallinfo. */
179 extern struct mallinfo mallinfo __MALLOC_P ((void));
180 
181 /* SVID2/XPG mallopt options */
182 #ifndef M_MXFAST
183 # define M_MXFAST  1	/* maximum request size for "fastbins" */
184 #endif
185 #ifndef M_NLBLKS
186 # define M_NLBLKS  2	/* UNUSED in this malloc */
187 #endif
188 #ifndef M_GRAIN
189 # define M_GRAIN   3	/* UNUSED in this malloc */
190 #endif
191 #ifndef M_KEEP
192 # define M_KEEP    4	/* UNUSED in this malloc */
193 #endif
194 
195 /* mallopt options that actually do something */
196 #define M_TRIM_THRESHOLD    -1
197 #define M_TOP_PAD           -2
198 #define M_MMAP_THRESHOLD    -3
199 #define M_MMAP_MAX          -4
200 #define M_CHECK_ACTION      -5
201 
202 /* General SVID/XPG interface to tunable parameters. */
203 extern int mallopt __MALLOC_P ((int __param, int __val));
204 
205 /* Release all but __pad bytes of freed top-most memory back to the
206    system. Return 1 if successful, else 0. */
207 extern int malloc_trim __MALLOC_P ((size_t __pad));
208 
209 /* Report the number of usable allocated bytes associated with allocated
210    chunk __ptr. */
211 extern size_t malloc_usable_size __MALLOC_P ((__malloc_ptr_t __ptr));
212 
213 /* Prints brief summary statistics on stderr. */
214 extern void malloc_stats __MALLOC_P ((void));
215 
216 /* Record the state of all malloc variables in an opaque data structure. */
217 extern __malloc_ptr_t malloc_get_state __MALLOC_P ((void));
218 
219 /* Restore the state of all malloc variables from data obtained with
220    malloc_get_state(). */
221 extern int malloc_set_state __MALLOC_P ((__malloc_ptr_t __ptr));
222 
223 /* Called once when malloc is initialized; redefining this variable in
224    the application provides the preferred way to set up the hook
225    pointers. */
226 extern void (*__malloc_initialize_hook) __MALLOC_PMT ((void));
227 /* Hooks for debugging and user-defined versions. */
228 extern void (*__free_hook) __MALLOC_PMT ((__malloc_ptr_t __ptr,
229 					__const __malloc_ptr_t));
230 extern __malloc_ptr_t (*__malloc_hook) __MALLOC_PMT ((size_t __size,
231 						    __const __malloc_ptr_t));
232 extern __malloc_ptr_t (*__realloc_hook) __MALLOC_PMT ((__malloc_ptr_t __ptr,
233 						     size_t __size,
234 						     __const __malloc_ptr_t));
235 extern __malloc_ptr_t (*__memalign_hook) __MALLOC_PMT ((size_t __alignment,
236 						      size_t __size,
237 						      __const __malloc_ptr_t));
238 extern void (*__after_morecore_hook) __MALLOC_PMT ((void));
239 
240 /* Activate a standard set of debugging hooks. */
241 extern void __malloc_check_init __MALLOC_P ((void));
242 
243 /* Internal routines, operating on "arenas".  */
244 struct malloc_state;
245 typedef struct malloc_state *mstate;
246 
247 extern mstate         _int_new_arena __MALLOC_P ((size_t __ini_size));
248 extern __malloc_ptr_t _int_malloc __MALLOC_P ((mstate __m, size_t __size));
249 extern void           _int_free __MALLOC_P ((mstate __m, __malloc_ptr_t __ptr));
250 extern __malloc_ptr_t _int_realloc __MALLOC_P ((mstate __m,
251 						__malloc_ptr_t __ptr,
252 						size_t __size));
253 extern __malloc_ptr_t _int_memalign __MALLOC_P ((mstate __m, size_t __alignment,
254 						 size_t __size));
255 /* Return arena number __n, or 0 if out of bounds.  Arena 0 is the
256    main arena.  */
257 extern mstate         _int_get_arena __MALLOC_P ((int __n));
258 
259 /* Implementation-specific mallinfo.  More detailed than mallinfo, and
260    also works for size_t wider than int.  */
261 struct malloc_arena_info {
262     int    nfastblocks;    /* number of freed "fastchunks" */
263     int    nbinblocks;     /* number of available chunks in bins */
264     size_t fastavail;      /* total space in freed "fastchunks" */
265     size_t binavail;       /* total space in binned chunks */
266     size_t top_size;       /* size of top chunk */
267     size_t system_mem;     /* bytes allocated from system in this arena */
268     size_t max_system_mem; /* max. bytes allocated from system */
269     /* Statistics for locking.  Only kept if THREAD_STATS is defined
270        at compile time.  */
271     long   stat_lock_direct, stat_lock_loop, stat_lock_wait;
272 };
273 
274 struct malloc_global_info {
275     int    n_mmaps;         /* number of mmap'ed chunks */
276     int    max_n_mmaps;     /* max. number of mmap'ed chunks reached */
277     size_t mmapped_mem;     /* total bytes allocated in mmap'ed chunks */
278     size_t max_mmapped_mem; /* max. bytes allocated in mmap'ed chunks */
279     size_t max_total_mem;   /* only kept for NO_THREADS */
280     int    stat_n_heaps;    /* only kept if THREAD_STATS is defined */
281 };
282 
283 extern void _int_get_arena_info __MALLOC_P ((mstate __m,
284 					     struct malloc_arena_info *__ma));
285 extern void _int_get_global_info __MALLOC_P ((struct malloc_global_info *__m));
286 
287 #ifdef __cplusplus
288 } /* end of extern "C" */
289 #endif
290 
291 #endif /* malloc.h */
292