1 /* Declarations for `malloc' and friends.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3 Written May 1989 by Mike Haertel.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA.
20 The author may be reached (Email) at the address mike@ai.mit.edu,
21 or (US mail) as Mike Haertel c/o Free Software Foundation. */
32 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
34 #define __P(args) args
36 #define __ptr_t void *
37 #else /* Not C++ or ANSI C. */
43 #define __ptr_t char *
44 #endif /* C++ or ANSI C. */
54 #define size_t unsigned int
60 /* Allocate SIZE bytes of memory. */
61 extern __ptr_t malloc
__P ((size_t __size
));
62 /* Re-allocate the previously allocated block
63 in __ptr_t, making the new block SIZE bytes long. */
64 extern __ptr_t realloc
__P ((__ptr_t __ptr
, size_t __size
));
65 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
66 extern __ptr_t calloc
__P ((size_t __nmemb
, size_t __size
));
67 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
68 extern void free
__P ((__ptr_t __ptr
));
70 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
71 extern __ptr_t memalign
__P ((size_t __alignment
, size_t __size
));
73 /* Allocate SIZE bytes on a page boundary. */
74 extern __ptr_t valloc
__P ((size_t __size
));
77 #ifdef _MALLOC_INTERNAL
79 #include <stdio.h> /* Harmless, gets __GNU_LIBRARY__ defined. */
81 #if defined(__GNU_LIBRARY__) || defined(STDC_HEADERS) || defined(USG)
85 #define memset(s, zero, n) bzero ((s), (n))
88 #define memcpy(d, s, n) bcopy ((s), (d), (n))
93 #if defined(__GNU_LIBRARY__) || defined(__STDC__)
99 /* The allocator divides the heap into blocks of fixed size; large
100 requests receive one or more whole blocks, and small requests
101 receive a fragment of a block. Fragment sizes are powers of two,
102 and all fragments of a block are the same size. When all the
103 fragments in a block have been freed, the block itself is freed. */
104 #define INT_BIT (CHAR_BIT * sizeof(int))
105 #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
106 #define BLOCKSIZE (1 << BLOCKLOG)
107 #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
109 /* Determine the amount of memory spanned by the initial heap table
110 (not an absolute limit). */
111 #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
113 /* Number of contiguous free blocks allowed to build up at the end of
114 memory before they will be returned to the system. */
115 #define FINAL_FREE_BLOCKS 8
117 /* Data structure giving per-block information. */
120 /* Heap information for a busy block. */
123 /* Zero for a large block, or positive giving the
124 logarithm to the base two of the fragment size. */
130 size_t nfree
; /* Free fragments in a fragmented block. */
131 size_t first
; /* First free fragment of the block. */
133 /* Size (in blocks) of a large cluster. */
137 /* Heap information for a free block
138 (that may be the first of a free cluster). */
141 size_t size
; /* Size (in blocks) of a free cluster. */
142 size_t next
; /* Index of next free cluster. */
143 size_t prev
; /* Index of previous free cluster. */
147 /* Pointer to first block of the heap. */
148 extern char *_heapbase
;
150 /* Table indexed by block number giving per-block information. */
151 extern malloc_info
*_heapinfo
;
153 /* Address to block number and vice versa. */
154 #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
155 #define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
157 /* Current search index for the heap table. */
158 extern size_t _heapindex
;
160 /* Limit of valid info table indices. */
161 extern size_t _heaplimit
;
163 /* Doubly linked lists of free fragments. */
170 /* Free list headers for each fragment size. */
171 extern struct list _fraghead
[];
173 /* List of blocks allocated with `memalign' (or `valloc'). */
176 struct alignlist
*next
;
177 __ptr_t aligned
; /* The address that memaligned returned. */
178 __ptr_t exact
; /* The address that malloc returned. */
180 extern struct alignlist
*_aligned_blocks
;
182 /* Instrumentation. */
183 extern size_t _chunks_used
;
184 extern size_t _bytes_used
;
185 extern size_t _chunks_free
;
186 extern size_t _bytes_free
;
188 /* Internal version of `free' used in `morecore' (malloc.c). */
189 extern void _free_internal
__P ((__ptr_t __ptr
));
191 #endif /* _MALLOC_INTERNAL. */
193 /* Underlying allocation function; successive calls should
194 return contiguous pieces of memory. */
195 extern __ptr_t (*__morecore
) __P ((ptrdiff_t __size
));
197 /* Default value of `__morecore'. */
198 extern __ptr_t __default_morecore
__P ((ptrdiff_t __size
));
200 /* Nonzero if `malloc' has been called and done its initialization. */
201 extern int __malloc_initialized
;
203 /* Hooks for debugging versions. */
204 extern void (*__free_hook
) __P ((__ptr_t __ptr
));
205 extern __ptr_t (*__malloc_hook
) __P ((size_t __size
));
206 extern __ptr_t (*__realloc_hook
) __P ((__ptr_t __ptr
, size_t __size
));
208 /* Activate a standard collection of debugging hooks. */
209 extern void mcheck
__P ((void (*__func
) __P ((void))));
211 /* Activate a standard collection of tracing hooks. */
212 extern void mtrace
__P ((void));
214 /* Statistics available to the user. */
217 size_t bytes_total
; /* Total size of the heap. */
218 size_t chunks_used
; /* Chunks allocated by the user. */
219 size_t bytes_used
; /* Byte total of user-allocated chunks. */
220 size_t chunks_free
; /* Chunks in the free list. */
221 size_t bytes_free
; /* Byte total of chunks in the free list. */
224 /* Pick up the current statistics. */
225 extern struct mstats mstats
__P ((void));
231 #endif /* mtrace.h */