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. */
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. */
36 #define malloc hide_malloc
37 #define calloc hide_calloc
38 #define realloc hide_realloc
39 #define free hide_free
46 # define PARAMS(paramlist) paramlist
49 # define NULL (void *) 0
53 # define size_t unsigned int
56 # define CONST /* nothing */
57 # define PARAMS(paramlist) ()
63 #undef malloc /* Undo the kludge to hide non-ANSI compliant declarations */
73 # define MIN(A, B) ((A) < (B) ? (A) : (B))
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 */
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. */
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)
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. */
99 #define ADDR2UINT(addr) ((unsigned int) ((char *) (addr) - (char *) NULL))
100 #define RESIDUAL(addr,bsize) ((unsigned int) (ADDR2UINT (addr) % (bsize)))
102 /* Determine the amount of memory spanned by the initial heap table
103 (not an absolute limit). */
105 #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
107 /* Number of contiguous free blocks allowed to build up at the end of
108 memory before they will be returned to the system. */
110 #define FINAL_FREE_BLOCKS 8
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
117 #define MALLOC_SEARCH_START mdp -> heapindex
119 /* Address to block number and vice versa. */
121 #define BLOCK(A) (((char *) (A) - mdp -> heapbase) / BLOCKSIZE + 1)
123 #define ADDRESS(B) ((PTR) (((B) - 1) * BLOCKSIZE + mdp -> heapbase))
125 /* Data structure giving per-block information. */
129 /* Heap information for a busy block. */
132 /* Zero for a large block, or positive giving the
133 logarithm to the base two of the fragment size. */
139 size_t nfree
; /* Free fragments in a fragmented block. */
140 size_t first
; /* First free fragment of the block. */
142 /* Size (in blocks) of a large cluster. */
146 /* Heap information for a free block (that may be the first of
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. */
156 /* List of blocks allocated with `mmemalign' (or `mvalloc'). */
160 struct alignlist
*next
;
161 PTR aligned
; /* The address that mmemaligned returned. */
162 PTR exact
; /* The address that malloc returned. */
165 /* Doubly linked lists of free fragments. */
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. */
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. */
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. */
195 /* The "magic number" for an mmalloc file. */
197 char magic
[MMALLOC_MAGIC_SIZE
];
199 /* The size in bytes of this structure, used as a sanity check when reusing
200 a previously created mapped file. */
202 unsigned int headersize
;
204 /* The version number of the mmalloc package that created this file. */
206 unsigned char version
;
208 /* Some flag bits to keep track of various internal things. */
212 /* If a system call made by the mmalloc package fails, the errno is
213 preserved for future examination. */
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. */
224 PTR (*morecore
) PARAMS ((struct mdesc
*, ptrdiff_t));
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(). */
230 void (*abortfunc
) PARAMS ((void));
232 /* Debugging hook for free. */
234 void (*mfree_hook
) PARAMS ((PTR
, PTR
));
236 /* Debugging hook for `malloc'. */
238 PTR (*mmalloc_hook
) PARAMS ((PTR
, size_t));
240 /* Debugging hook for realloc. */
242 PTR (*mrealloc_hook
) PARAMS ((PTR
, PTR
, size_t));
244 /* Number of info entries. */
248 /* Pointer to first block of the heap (base of the first block). */
252 /* Current search index for the heap table. */
253 /* Search index in the info table. */
257 /* Limit of valid info table indices. */
261 /* Block information table.
262 Allocated with malign/__mmalloc_free (not mmalloc/mfree). */
263 /* Table indexed by block number giving per-block information. */
265 malloc_info
*heapinfo
;
267 /* Instrumentation. */
269 struct mstats heapstats
;
271 /* Free list headers for each fragment size. */
272 /* Free lists for each fragment size. */
274 struct list fraghead
[BLOCKLOG
];
276 /* List of blocks allocated by memalign. */
278 struct alignlist
*aligned_blocks
;
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
286 /* The current location in the memory region for this malloc heap which
287 represents the end of memory in use. */
291 /* The end of the current memory region for this malloc heap. This is
292 the first location past the end of mapped memory. */
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. */
303 /* An array of keys to data within the mapped region, for use by the
306 void *keys
[MMALLOC_KEYS
];
310 /* Bits to look at in the malloc descriptor flags word */
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 */
316 /* Allocate SIZE bytes of memory. */
318 extern PTR mmalloc
PARAMS ((PTR
, size_t));
320 /* Re-allocate the previously allocated block in PTR, making the new block
323 extern PTR mrealloc
PARAMS ((PTR
, PTR
, size_t));
325 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
327 extern PTR mcalloc
PARAMS ((PTR
, size_t, size_t));
329 /* Free a block allocated by `mmalloc', `mrealloc' or `mcalloc'. */
331 extern void mfree
PARAMS ((PTR
, PTR
));
333 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
335 extern PTR mmemalign
PARAMS ((PTR
, size_t, size_t));
337 /* Allocate SIZE bytes on a page boundary. */
339 extern PTR mvalloc
PARAMS ((PTR
, size_t));
341 /* Activate a standard collection of debugging hooks. */
343 extern int mmcheck
PARAMS ((PTR
, void (*) (void)));
345 /* Pick up the current statistics. (see FIXME elsewhere) */
347 extern struct mstats mmstats
PARAMS ((PTR
));
349 /* Internal version of `mfree' used in `morecore'. */
351 extern void __mmalloc_free
PARAMS ((struct mdesc
*, PTR
));
353 /* Hooks for debugging versions. */
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));
359 /* A default malloc descriptor for the single sbrk() managed region. */
361 extern struct mdesc
*__mmalloc_default_mdp
;
363 /* Initialize the first use of the default malloc descriptor, which uses
366 extern struct mdesc
*__mmalloc_sbrk_init
PARAMS ((void));
368 /* Grow or shrink a contiguous region using sbrk(). */
370 extern PTR __mmalloc_sbrk_morecore
PARAMS ((struct mdesc
*, int));
372 /* Grow or shrink a contiguous mapped region using mmap().
373 Works much like sbrk() */
375 #if defined(HAVE_MMAP)
377 extern PTR __mmalloc_mmap_morecore
PARAMS ((struct mdesc
*, int));
381 /* Remap a mmalloc region that was previously mapped. */
383 extern PTR __mmalloc_remap_core
PARAMS ((struct mdesc
*));
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
391 #define MD_TO_MDP(md) \
393 ? (__mmalloc_default_mdp == NULL \
394 ? __mmalloc_sbrk_init () \
395 : __mmalloc_default_mdp) \
396 : (struct mdesc *) (md))
398 #endif /* __MMALLOC_H */