Get rid of include of <stdlib.h>, which is not guaranteed to be present
[binutils-gdb.git] / mmalloc / mmalloc.h
1 /* Declarations for `mmalloc' and friends.
2 Copyright 1990, 1991, 1992 Free Software Foundation
3
4 Written May 1989 by Mike Haertel.
5 Heavily modified Mar 1992 by Fred Fish. (fnf@cygnus.com)
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA.
21
22 The author may be reached (Email) at the address mike@ai.mit.edu,
23 or (US mail) as Mike Haertel c/o Free Software Foundation. */
24
25
26 #ifndef __MMALLOC_H
27 #define __MMALLOC_H 1
28
29 #ifdef __STDC__
30 # include <stddef.h>
31 # define PTR void *
32 # define CONST const
33 # define PARAMS(paramlist) paramlist
34 # include <limits.h>
35 # ifndef NULL
36 # define NULL (void *) 0
37 # endif
38 #else
39 # undef size_t
40 # define size_t unsigned int
41 # define CHAR_BIT 8
42 # define PTR char *
43 # define CONST /* nothing */
44 # define PARAMS(paramlist) ()
45 # ifndef NULL
46 # define NULL 0
47 # endif
48 #endif
49
50 #undef malloc /* Undo the kludge to hide non-ANSI compliant declarations */
51 #undef calloc
52 #undef realloc
53 #undef free
54
55 #ifndef MIN
56 # define MIN(A, B) ((A) < (B) ? (A) : (B))
57 #endif
58
59 #define MMALLOC_MAGIC "mmalloc" /* Mapped file magic number */
60 #define MMALLOC_MAGIC_SIZE 8 /* Size of magic number buf */
61 #define MMALLOC_VERSION 1 /* Current mmalloc version */
62 #define MMALLOC_KEYS 16 /* Keys for application use */
63
64 /* The allocator divides the heap into blocks of fixed size; large
65 requests receive one or more whole blocks, and small requests
66 receive a fragment of a block. Fragment sizes are powers of two,
67 and all fragments of a block are the same size. When all the
68 fragments in a block have been freed, the block itself is freed. */
69
70 #define INT_BIT (CHAR_BIT * sizeof(int))
71 #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
72 #define BLOCKSIZE ((unsigned int) 1 << BLOCKLOG)
73 #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
74
75 /* The difference between two pointers is a signed int. On machines where
76 the data addresses have the high bit set, we need to ensure that the
77 difference becomes an unsigned int when we are using the address as an
78 integral value. In addition, when using with the '%' operator, the
79 sign of the result is machine dependent for negative values, so force
80 it to be treated as an unsigned int. */
81
82 #define ADDR2UINT(addr) ((unsigned int) ((char *) (addr) - (char *) NULL))
83 #define RESIDUAL(addr,bsize) ((unsigned int) (ADDR2UINT (addr) % (bsize)))
84
85 /* Determine the amount of memory spanned by the initial heap table
86 (not an absolute limit). */
87
88 #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
89
90 /* Number of contiguous free blocks allowed to build up at the end of
91 memory before they will be returned to the system. */
92
93 #define FINAL_FREE_BLOCKS 8
94
95 /* Where to start searching the free list when looking for new memory.
96 The two possible values are 0 and heapindex. Starting at 0 seems
97 to reduce total memory usage, while starting at heapindex seems to
98 run faster. */
99
100 #define MALLOC_SEARCH_START mdp -> heapindex
101
102 /* Address to block number and vice versa. */
103
104 #define BLOCK(A) (((char *) (A) - mdp -> heapbase) / BLOCKSIZE + 1)
105
106 #define ADDRESS(B) ((PTR) (((B) - 1) * BLOCKSIZE + mdp -> heapbase))
107
108 /* Data structure giving per-block information. */
109
110 typedef union
111 {
112 /* Heap information for a busy block. */
113 struct
114 {
115 /* Zero for a large block, or positive giving the
116 logarithm to the base two of the fragment size. */
117 int type;
118 union
119 {
120 struct
121 {
122 size_t nfree; /* Free fragments in a fragmented block. */
123 size_t first; /* First free fragment of the block. */
124 } frag;
125 /* Size (in blocks) of a large cluster. */
126 size_t size;
127 } info;
128 } busy;
129 /* Heap information for a free block (that may be the first of
130 a free cluster). */
131 struct
132 {
133 size_t size; /* Size (in blocks) of a free cluster. */
134 size_t next; /* Index of next free cluster. */
135 size_t prev; /* Index of previous free cluster. */
136 } free;
137 } malloc_info;
138
139 /* List of blocks allocated with `mmemalign' (or `mvalloc'). */
140
141 struct alignlist
142 {
143 struct alignlist *next;
144 PTR aligned; /* The address that mmemaligned returned. */
145 PTR exact; /* The address that malloc returned. */
146 };
147
148 /* Doubly linked lists of free fragments. */
149
150 struct list
151 {
152 struct list *next;
153 struct list *prev;
154 };
155
156 /* Statistics available to the user.
157 FIXME: By design, the internals of the malloc package are no longer
158 exported to the user via an include file, so access to this data needs
159 to be via some other mechanism, such as mmstat_<something> where the
160 return value is the <something> the user is interested in. */
161
162 struct mstats
163 {
164 size_t bytes_total; /* Total size of the heap. */
165 size_t chunks_used; /* Chunks allocated by the user. */
166 size_t bytes_used; /* Byte total of user-allocated chunks. */
167 size_t chunks_free; /* Chunks in the free list. */
168 size_t bytes_free; /* Byte total of chunks in the free list. */
169 };
170
171 /* Internal structure that defines the format of the malloc-descriptor.
172 This gets written to the base address of the region that mmalloc is
173 managing, and thus also becomes the file header for the mapped file,
174 if such a file exists. */
175
176 struct mdesc
177 {
178 /* The "magic number" for an mmalloc file. */
179
180 char magic[MMALLOC_MAGIC_SIZE];
181
182 /* The size in bytes of this structure, used as a sanity check when reusing
183 a previously created mapped file. */
184
185 unsigned int headersize;
186
187 /* The version number of the mmalloc package that created this file. */
188
189 unsigned char version;
190
191 /* Some flag bits to keep track of various internal things. */
192
193 unsigned int flags;
194
195 /* If a system call made by the mmalloc package fails, the errno is
196 preserved for future examination. */
197
198 int errno;
199
200 /* Pointer to the function that is used to get more core, or return core
201 to the system, for requests using this malloc descriptor. For memory
202 mapped regions, this is the mmap() based routine. There may also be
203 a single malloc descriptor that points to an sbrk() based routine
204 for systems without mmap() or for applications that call the mmalloc()
205 package with a NULL malloc descriptor. */
206
207 PTR (*morecore) PARAMS ((struct mdesc *, ptrdiff_t));
208
209 /* Pointer to the function that causes an abort when the memory checking
210 features are activated. By default this is set to abort(), but can
211 be set to another function by the application using mmalloc(). */
212
213 void (*abortfunc) PARAMS ((void));
214
215 /* Debugging hook for free. */
216
217 void (*mfree_hook) PARAMS ((PTR, PTR));
218
219 /* Debugging hook for `malloc'. */
220
221 PTR (*mmalloc_hook) PARAMS ((PTR, size_t));
222
223 /* Debugging hook for realloc. */
224
225 PTR (*mrealloc_hook) PARAMS ((PTR, PTR, size_t));
226
227 /* Number of info entries. */
228
229 size_t heapsize;
230
231 /* Pointer to first block of the heap (base of the first block). */
232
233 char *heapbase;
234
235 /* Current search index for the heap table. */
236 /* Search index in the info table. */
237
238 size_t heapindex;
239
240 /* Limit of valid info table indices. */
241
242 size_t heaplimit;
243
244 /* Block information table.
245 Allocated with malign/__mmalloc_free (not mmalloc/mfree). */
246 /* Table indexed by block number giving per-block information. */
247
248 malloc_info *heapinfo;
249
250 /* Instrumentation. */
251
252 struct mstats heapstats;
253
254 /* Free list headers for each fragment size. */
255 /* Free lists for each fragment size. */
256
257 struct list fraghead[BLOCKLOG];
258
259 /* List of blocks allocated by memalign. */
260
261 struct alignlist *aligned_blocks;
262
263 /* The base address of the memory region for this malloc heap. This
264 is the location where the bookkeeping data for mmap and for malloc
265 begins. */
266
267 char *base;
268
269 /* The current location in the memory region for this malloc heap which
270 represents the end of memory in use. */
271
272 char *breakval;
273
274 /* The end of the current memory region for this malloc heap. This is
275 the first location past the end of mapped memory. */
276
277 char *top;
278
279 /* Open file descriptor for the file to which this malloc heap is mapped.
280 This will always be a valid file descriptor, since /dev/zero is used
281 by default if no open file is supplied by the client. Also note that
282 it may change each time the region is mapped and unmapped. */
283
284 int fd;
285
286 /* An array of keys to data within the mapped region, for use by the
287 application. */
288
289 void *keys[MMALLOC_KEYS];
290
291 };
292
293 /* Bits to look at in the malloc descriptor flags word */
294
295 #define MMALLOC_DEVZERO (1 << 0) /* Have mapped to /dev/zero */
296 #define MMALLOC_INITIALIZED (1 << 1) /* Initialized mmalloc */
297 #define MMALLOC_MMCHECK_USED (1 << 2) /* mmcheck() called already */
298
299 /* Allocate SIZE bytes of memory. */
300
301 extern PTR mmalloc PARAMS ((PTR, size_t));
302
303 /* Re-allocate the previously allocated block in PTR, making the new block
304 SIZE bytes long. */
305
306 extern PTR mrealloc PARAMS ((PTR, PTR, size_t));
307
308 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
309
310 extern PTR mcalloc PARAMS ((PTR, size_t, size_t));
311
312 /* Free a block allocated by `mmalloc', `mrealloc' or `mcalloc'. */
313
314 extern void mfree PARAMS ((PTR, PTR));
315
316 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
317
318 extern PTR mmemalign PARAMS ((PTR, size_t, size_t));
319
320 /* Allocate SIZE bytes on a page boundary. */
321
322 extern PTR mvalloc PARAMS ((PTR, size_t));
323
324 /* Activate a standard collection of debugging hooks. */
325
326 extern int mmcheck PARAMS ((PTR, void (*) (void)));
327
328 /* Pick up the current statistics. (see FIXME elsewhere) */
329
330 extern struct mstats mmstats PARAMS ((PTR));
331
332 /* Internal version of `mfree' used in `morecore'. */
333
334 extern void __mmalloc_free PARAMS ((struct mdesc *, PTR));
335
336 /* Hooks for debugging versions. */
337
338 extern void (*__mfree_hook) PARAMS ((PTR, PTR));
339 extern PTR (*__mmalloc_hook) PARAMS ((PTR, size_t));
340 extern PTR (*__mrealloc_hook) PARAMS ((PTR, PTR, size_t));
341
342 /* A default malloc descriptor for the single sbrk() managed region. */
343
344 extern struct mdesc *__mmalloc_default_mdp;
345
346 /* Initialize the first use of the default malloc descriptor, which uses
347 an sbrk() region. */
348
349 extern struct mdesc *__mmalloc_sbrk_init PARAMS ((void));
350
351 /* Grow or shrink a contiguous region using sbrk(). */
352
353 extern PTR __mmalloc_sbrk_morecore PARAMS ((struct mdesc *, int));
354
355 /* Grow or shrink a contiguous mapped region using mmap().
356 Works much like sbrk() */
357
358 #if defined(HAVE_MMAP)
359
360 extern PTR __mmalloc_mmap_morecore PARAMS ((struct mdesc *, int));
361
362 #endif
363
364 /* Remap a mmalloc region that was previously mapped. */
365
366 extern PTR __mmalloc_remap_core PARAMS ((struct mdesc *));
367
368 /* Macro to convert from a user supplied malloc descriptor to pointer to the
369 internal malloc descriptor. If the user supplied descriptor is NULL, then
370 use the default internal version, initializing it if necessary. Otherwise
371 just cast the user supplied version (which is void *) to the proper type
372 (struct mdesc *). */
373
374 #define MD_TO_MDP(md) \
375 ((md) == NULL \
376 ? (__mmalloc_default_mdp == NULL \
377 ? __mmalloc_sbrk_init () \
378 : __mmalloc_default_mdp) \
379 : (struct mdesc *) (md))
380
381 #endif /* __MMALLOC_H */