1 /* Declarations for `mmalloc' and friends.
2 Copyright 1990, 1991, 1992 Free Software Foundation
4 Written May 1989 by Mike Haertel.
5 Heavily modified Mar 1992 by Fred Fish. (fnf@cygnus.com)
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.
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.
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.
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. */
33 # define PARAMS(paramlist) paramlist
36 # define NULL (void *) 0
40 # define size_t unsigned int
43 # define CONST /* nothing */
44 # define PARAMS(paramlist) ()
50 #undef malloc /* Undo the kludge to hide non-ANSI compliant declarations */
56 # define MIN(A, B) ((A) < (B) ? (A) : (B))
59 #define MMALLOC_MAGIC "mmalloc" /* Mapped file magic number */
60 #define MMALLOC_MAGIC_SIZE 8 /* Size of magic number buf */
61 #define MMALLOC_VERSION 1 /* Current mmalloc version */
62 #define MMALLOC_KEYS 16 /* Keys for application use */
64 /* The allocator divides the heap into blocks of fixed size; large
65 requests receive one or more whole blocks, and small requests
66 receive a fragment of a block. Fragment sizes are powers of two,
67 and all fragments of a block are the same size. When all the
68 fragments in a block have been freed, the block itself is freed. */
70 #define INT_BIT (CHAR_BIT * sizeof(int))
71 #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
72 #define BLOCKSIZE ((unsigned int) 1 << BLOCKLOG)
73 #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
75 /* The difference between two pointers is a signed int. On machines where
76 the data addresses have the high bit set, we need to ensure that the
77 difference becomes an unsigned int when we are using the address as an
78 integral value. In addition, when using with the '%' operator, the
79 sign of the result is machine dependent for negative values, so force
80 it to be treated as an unsigned int. */
82 #define ADDR2UINT(addr) ((unsigned int) ((char *) (addr) - (char *) NULL))
83 #define RESIDUAL(addr,bsize) ((unsigned int) (ADDR2UINT (addr) % (bsize)))
85 /* Determine the amount of memory spanned by the initial heap table
86 (not an absolute limit). */
88 #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
90 /* Number of contiguous free blocks allowed to build up at the end of
91 memory before they will be returned to the system. */
93 #define FINAL_FREE_BLOCKS 8
95 /* Where to start searching the free list when looking for new memory.
96 The two possible values are 0 and heapindex. Starting at 0 seems
97 to reduce total memory usage, while starting at heapindex seems to
100 #define MALLOC_SEARCH_START mdp -> heapindex
102 /* Address to block number and vice versa. */
104 #define BLOCK(A) (((char *) (A) - mdp -> heapbase) / BLOCKSIZE + 1)
106 #define ADDRESS(B) ((PTR) (((B) - 1) * BLOCKSIZE + mdp -> heapbase))
108 /* Data structure giving per-block information. */
112 /* Heap information for a busy block. */
115 /* Zero for a large block, or positive giving the
116 logarithm to the base two of the fragment size. */
122 size_t nfree
; /* Free fragments in a fragmented block. */
123 size_t first
; /* First free fragment of the block. */
125 /* Size (in blocks) of a large cluster. */
129 /* Heap information for a free block (that may be the first of
133 size_t size
; /* Size (in blocks) of a free cluster. */
134 size_t next
; /* Index of next free cluster. */
135 size_t prev
; /* Index of previous free cluster. */
139 /* List of blocks allocated with `mmemalign' (or `mvalloc'). */
143 struct alignlist
*next
;
144 PTR aligned
; /* The address that mmemaligned returned. */
145 PTR exact
; /* The address that malloc returned. */
148 /* Doubly linked lists of free fragments. */
156 /* Statistics available to the user.
157 FIXME: By design, the internals of the malloc package are no longer
158 exported to the user via an include file, so access to this data needs
159 to be via some other mechanism, such as mmstat_<something> where the
160 return value is the <something> the user is interested in. */
164 size_t bytes_total
; /* Total size of the heap. */
165 size_t chunks_used
; /* Chunks allocated by the user. */
166 size_t bytes_used
; /* Byte total of user-allocated chunks. */
167 size_t chunks_free
; /* Chunks in the free list. */
168 size_t bytes_free
; /* Byte total of chunks in the free list. */
171 /* Internal structure that defines the format of the malloc-descriptor.
172 This gets written to the base address of the region that mmalloc is
173 managing, and thus also becomes the file header for the mapped file,
174 if such a file exists. */
178 /* The "magic number" for an mmalloc file. */
180 char magic
[MMALLOC_MAGIC_SIZE
];
182 /* The size in bytes of this structure, used as a sanity check when reusing
183 a previously created mapped file. */
185 unsigned int headersize
;
187 /* The version number of the mmalloc package that created this file. */
189 unsigned char version
;
191 /* Some flag bits to keep track of various internal things. */
195 /* If a system call made by the mmalloc package fails, the errno is
196 preserved for future examination. */
200 /* Pointer to the function that is used to get more core, or return core
201 to the system, for requests using this malloc descriptor. For memory
202 mapped regions, this is the mmap() based routine. There may also be
203 a single malloc descriptor that points to an sbrk() based routine
204 for systems without mmap() or for applications that call the mmalloc()
205 package with a NULL malloc descriptor. */
207 PTR (*morecore
) PARAMS ((struct mdesc
*, ptrdiff_t));
209 /* Pointer to the function that causes an abort when the memory checking
210 features are activated. By default this is set to abort(), but can
211 be set to another function by the application using mmalloc(). */
213 void (*abortfunc
) PARAMS ((void));
215 /* Debugging hook for free. */
217 void (*mfree_hook
) PARAMS ((PTR
, PTR
));
219 /* Debugging hook for `malloc'. */
221 PTR (*mmalloc_hook
) PARAMS ((PTR
, size_t));
223 /* Debugging hook for realloc. */
225 PTR (*mrealloc_hook
) PARAMS ((PTR
, PTR
, size_t));
227 /* Number of info entries. */
231 /* Pointer to first block of the heap (base of the first block). */
235 /* Current search index for the heap table. */
236 /* Search index in the info table. */
240 /* Limit of valid info table indices. */
244 /* Block information table.
245 Allocated with malign/__mmalloc_free (not mmalloc/mfree). */
246 /* Table indexed by block number giving per-block information. */
248 malloc_info
*heapinfo
;
250 /* Instrumentation. */
252 struct mstats heapstats
;
254 /* Free list headers for each fragment size. */
255 /* Free lists for each fragment size. */
257 struct list fraghead
[BLOCKLOG
];
259 /* List of blocks allocated by memalign. */
261 struct alignlist
*aligned_blocks
;
263 /* The base address of the memory region for this malloc heap. This
264 is the location where the bookkeeping data for mmap and for malloc
269 /* The current location in the memory region for this malloc heap which
270 represents the end of memory in use. */
274 /* The end of the current memory region for this malloc heap. This is
275 the first location past the end of mapped memory. */
279 /* Open file descriptor for the file to which this malloc heap is mapped.
280 This will always be a valid file descriptor, since /dev/zero is used
281 by default if no open file is supplied by the client. Also note that
282 it may change each time the region is mapped and unmapped. */
286 /* An array of keys to data within the mapped region, for use by the
289 void *keys
[MMALLOC_KEYS
];
293 /* Bits to look at in the malloc descriptor flags word */
295 #define MMALLOC_DEVZERO (1 << 0) /* Have mapped to /dev/zero */
296 #define MMALLOC_INITIALIZED (1 << 1) /* Initialized mmalloc */
297 #define MMALLOC_MMCHECK_USED (1 << 2) /* mmcheck() called already */
299 /* Allocate SIZE bytes of memory. */
301 extern PTR mmalloc
PARAMS ((PTR
, size_t));
303 /* Re-allocate the previously allocated block in PTR, making the new block
306 extern PTR mrealloc
PARAMS ((PTR
, PTR
, size_t));
308 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
310 extern PTR mcalloc
PARAMS ((PTR
, size_t, size_t));
312 /* Free a block allocated by `mmalloc', `mrealloc' or `mcalloc'. */
314 extern void mfree
PARAMS ((PTR
, PTR
));
316 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
318 extern PTR mmemalign
PARAMS ((PTR
, size_t, size_t));
320 /* Allocate SIZE bytes on a page boundary. */
322 extern PTR mvalloc
PARAMS ((PTR
, size_t));
324 /* Activate a standard collection of debugging hooks. */
326 extern int mmcheck
PARAMS ((PTR
, void (*) (void)));
328 /* Pick up the current statistics. (see FIXME elsewhere) */
330 extern struct mstats mmstats
PARAMS ((PTR
));
332 /* Internal version of `mfree' used in `morecore'. */
334 extern void __mmalloc_free
PARAMS ((struct mdesc
*, PTR
));
336 /* Hooks for debugging versions. */
338 extern void (*__mfree_hook
) PARAMS ((PTR
, PTR
));
339 extern PTR (*__mmalloc_hook
) PARAMS ((PTR
, size_t));
340 extern PTR (*__mrealloc_hook
) PARAMS ((PTR
, PTR
, size_t));
342 /* A default malloc descriptor for the single sbrk() managed region. */
344 extern struct mdesc
*__mmalloc_default_mdp
;
346 /* Initialize the first use of the default malloc descriptor, which uses
349 extern struct mdesc
*__mmalloc_sbrk_init
PARAMS ((void));
351 /* Grow or shrink a contiguous region using sbrk(). */
353 extern PTR __mmalloc_sbrk_morecore
PARAMS ((struct mdesc
*, int));
355 /* Grow or shrink a contiguous mapped region using mmap().
356 Works much like sbrk() */
358 #if defined(HAVE_MMAP)
360 extern PTR __mmalloc_mmap_morecore
PARAMS ((struct mdesc
*, int));
364 /* Remap a mmalloc region that was previously mapped. */
366 extern PTR __mmalloc_remap_core
PARAMS ((struct mdesc
*));
368 /* Macro to convert from a user supplied malloc descriptor to pointer to the
369 internal malloc descriptor. If the user supplied descriptor is NULL, then
370 use the default internal version, initializing it if necessary. Otherwise
371 just cast the user supplied version (which is void *) to the proper type
374 #define MD_TO_MDP(md) \
376 ? (__mmalloc_default_mdp == NULL \
377 ? __mmalloc_sbrk_init () \
378 : __mmalloc_default_mdp) \
379 : (struct mdesc *) (md))
381 #endif /* __MMALLOC_H */