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