1 /* Declarations for `malloc' and friends.
2 Copyright 1990, 1991, 1992, 1993 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. */
27 #ifdef _MALLOC_INTERNAL
33 #if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
37 #define memset(s, zero, n) bzero ((s), (n))
40 #define memcpy(d, s, n) bcopy ((s), (d), (n))
43 #define memmove(d, s, n) bcopy ((s), (d), (n))
47 #if defined(__GNU_LIBRARY__) || defined(__STDC__)
53 #endif /* _MALLOC_INTERNAL. */
61 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
64 #define __P(args) args
66 #include <sys/cdefs.h>
69 #define __ptr_t void *
70 #else /* Not C++ or ANSI C. */
76 #define __ptr_t char *
77 #endif /* C++ or ANSI C. */
83 #define size_t unsigned int
93 /* Allocate SIZE bytes of memory. */
94 extern __ptr_t malloc
__P ((size_t __size
));
95 /* Re-allocate the previously allocated block
96 in __ptr_t, making the new block SIZE bytes long. */
97 extern __ptr_t realloc
__P ((__ptr_t __ptr
, size_t __size
));
98 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
99 extern __ptr_t calloc
__P ((size_t __nmemb
, size_t __size
));
100 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
101 extern void free
__P ((__ptr_t __ptr
));
103 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
104 extern __ptr_t memalign
__P ((size_t __alignment
, size_t __size
));
106 /* Allocate SIZE bytes on a page boundary. */
107 extern __ptr_t valloc
__P ((size_t __size
));
110 #ifdef _MALLOC_INTERNAL
112 /* The allocator divides the heap into blocks of fixed size; large
113 requests receive one or more whole blocks, and small requests
114 receive a fragment of a block. Fragment sizes are powers of two,
115 and all fragments of a block are the same size. When all the
116 fragments in a block have been freed, the block itself is freed. */
117 #define INT_BIT (CHAR_BIT * sizeof(int))
118 #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
119 #define BLOCKSIZE (1 << BLOCKLOG)
120 #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
122 /* Determine the amount of memory spanned by the initial heap table
123 (not an absolute limit). */
124 #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
126 /* Number of contiguous free blocks allowed to build up at the end of
127 memory before they will be returned to the system. */
128 #define FINAL_FREE_BLOCKS 8
130 /* Data structure giving per-block information. */
133 /* Heap information for a busy block. */
136 /* Zero for a large block, or positive giving the
137 logarithm to the base two of the fragment size. */
143 size_t nfree
; /* Free fragments in a fragmented block. */
144 size_t first
; /* First free fragment of the block. */
146 /* Size (in blocks) of a large cluster. */
150 /* Heap information for a free block
151 (that may be the first of a free cluster). */
154 size_t size
; /* Size (in blocks) of a free cluster. */
155 size_t next
; /* Index of next free cluster. */
156 size_t prev
; /* Index of previous free cluster. */
160 /* Pointer to first block of the heap. */
161 extern char *_heapbase
;
163 /* Table indexed by block number giving per-block information. */
164 extern malloc_info
*_heapinfo
;
166 /* Address to block number and vice versa. */
167 #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
168 #define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
170 /* Current search index for the heap table. */
171 extern size_t _heapindex
;
173 /* Limit of valid info table indices. */
174 extern size_t _heaplimit
;
176 /* Doubly linked lists of free fragments. */
183 /* Free list headers for each fragment size. */
184 extern struct list _fraghead
[];
186 /* List of blocks allocated with `memalign' (or `valloc'). */
189 struct alignlist
*next
;
190 __ptr_t aligned
; /* The address that memaligned returned. */
191 __ptr_t exact
; /* The address that malloc returned. */
193 extern struct alignlist
*_aligned_blocks
;
195 /* Instrumentation. */
196 extern size_t _chunks_used
;
197 extern size_t _bytes_used
;
198 extern size_t _chunks_free
;
199 extern size_t _bytes_free
;
201 /* Internal version of `free' used in `morecore' (malloc.c). */
202 extern void _free_internal
__P ((__ptr_t __ptr
));
204 #endif /* _MALLOC_INTERNAL. */
206 /* Underlying allocation function; successive calls should
207 return contiguous pieces of memory. */
208 extern __ptr_t (*__morecore
) __P ((ptrdiff_t __size
));
210 /* Default value of `__morecore'. */
211 extern __ptr_t __default_morecore
__P ((ptrdiff_t __size
));
213 /* If not NULL, this function is called after each time
214 `__morecore' is called to increase the data size. */
215 extern void (*__after_morecore_hook
) __P ((void));
217 /* Nonzero if `malloc' has been called and done its initialization. */
218 extern int __malloc_initialized
;
220 /* Hooks for debugging versions. */
221 extern void (*__free_hook
) __P ((__ptr_t __ptr
));
222 extern __ptr_t (*__malloc_hook
) __P ((size_t __size
));
223 extern __ptr_t (*__realloc_hook
) __P ((__ptr_t __ptr
, size_t __size
));
225 /* Activate a standard collection of debugging hooks. */
226 extern int mcheck
__P ((void (*__func
) __P ((void))));
228 /* Activate a standard collection of tracing hooks. */
229 extern void mtrace
__P ((void));
231 /* Statistics available to the user. */
234 size_t bytes_total
; /* Total size of the heap. */
235 size_t chunks_used
; /* Chunks allocated by the user. */
236 size_t bytes_used
; /* Byte total of user-allocated chunks. */
237 size_t chunks_free
; /* Chunks in the free list. */
238 size_t bytes_free
; /* Byte total of chunks in the free list. */
241 /* Pick up the current statistics. */
242 extern struct mstats mstats
__P ((void));
244 /* Call WARNFUN with a warning message when memory usage is high. */
245 extern void memory_warnings
__P ((__ptr_t __start
,
246 void (*__warnfun
) __P ((__const
char *))));
249 /* Relocating allocator. */
251 /* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
252 extern __ptr_t r_alloc
__P ((__ptr_t
*__handleptr
, size_t __size
));
254 /* Free the storage allocated in HANDLEPTR. */
255 extern void r_alloc_free
__P ((__ptr_t
*__handleptr
));
257 /* Adjust the block at HANDLEPTR to be SIZE bytes long. */
258 extern __ptr_t r_re_alloc
__P ((__ptr_t
*__handleptr
, size_t __size
));
265 #endif /* malloc.h */