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