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