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