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