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
28 /* Harmless, gets __GNU_LIBRARY__ defined.
29 We must do this before #defining size_t and ptrdiff_t
30 because <stdio.h> tries to typedef them on some systems. */
39 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
41 #define __P(args) args
43 #define __ptr_t void *
44 #else /* Not C++ or ANSI C. */
50 #define __ptr_t char *
51 #endif /* C++ or ANSI C. */
61 #define size_t unsigned int
67 /* Allocate SIZE bytes of memory. */
68 extern __ptr_t malloc
__P ((size_t __size
));
69 /* Re-allocate the previously allocated block
70 in __ptr_t, making the new block SIZE bytes long. */
71 extern __ptr_t realloc
__P ((__ptr_t __ptr
, size_t __size
));
72 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
73 extern __ptr_t calloc
__P ((size_t __nmemb
, size_t __size
));
74 /* Free a block allocated by `malloc', `realloc' or `calloc'. */
75 extern void free
__P ((__ptr_t __ptr
));
77 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
78 extern __ptr_t memalign
__P ((size_t __alignment
, size_t __size
));
80 /* Allocate SIZE bytes on a page boundary. */
81 extern __ptr_t valloc
__P ((size_t __size
));
84 #ifdef _MALLOC_INTERNAL
90 #if defined(__GNU_LIBRARY__) || defined(STDC_HEADERS) || defined(USG)
94 #define memset(s, zero, n) bzero ((s), (n))
97 #define memcpy(d, s, n) bcopy ((s), (d), (n))
100 #define memmove(d, s, n) bcopy ((s), (d), (n))
105 #if defined(__GNU_LIBRARY__) || defined(__STDC__)
111 /* The allocator divides the heap into blocks of fixed size; large
112 requests receive one or more whole blocks, and small requests
113 receive a fragment of a block. Fragment sizes are powers of two,
114 and all fragments of a block are the same size. When all the
115 fragments in a block have been freed, the block itself is freed. */
116 #define INT_BIT (CHAR_BIT * sizeof(int))
117 #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
118 #define BLOCKSIZE (1 << BLOCKLOG)
119 #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
121 /* Determine the amount of memory spanned by the initial heap table
122 (not an absolute limit). */
123 #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
125 /* Number of contiguous free blocks allowed to build up at the end of
126 memory before they will be returned to the system. */
127 #define FINAL_FREE_BLOCKS 8
129 /* Data structure giving per-block information. */
132 /* Heap information for a busy block. */
135 /* Zero for a large block, or positive giving the
136 logarithm to the base two of the fragment size. */
142 size_t nfree
; /* Free fragments in a fragmented block. */
143 size_t first
; /* First free fragment of the block. */
145 /* Size (in blocks) of a large cluster. */
149 /* Heap information for a free block
150 (that may be the first of a free cluster). */
153 size_t size
; /* Size (in blocks) of a free cluster. */
154 size_t next
; /* Index of next free cluster. */
155 size_t prev
; /* Index of previous free cluster. */
159 /* Pointer to first block of the heap. */
160 extern char *_heapbase
;
162 /* Table indexed by block number giving per-block information. */
163 extern malloc_info
*_heapinfo
;
165 /* Address to block number and vice versa. */
166 #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
167 #define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
169 /* Current search index for the heap table. */
170 extern size_t _heapindex
;
172 /* Limit of valid info table indices. */
173 extern size_t _heaplimit
;
175 /* Doubly linked lists of free fragments. */
182 /* Count of blocks for each fragment size. */
183 extern int _fragblocks
[];
185 /* Free list headers for each fragment size. */
186 extern struct list _fraghead
[];
188 /* List of blocks allocated with `memalign' (or `valloc'). */
191 struct alignlist
*next
;
192 __ptr_t aligned
; /* The address that memaligned returned. */
193 __ptr_t exact
; /* The address that malloc returned. */
195 extern struct alignlist
*_aligned_blocks
;
197 /* Instrumentation. */
198 extern size_t _chunks_used
;
199 extern size_t _bytes_used
;
200 extern size_t _chunks_free
;
201 extern size_t _bytes_free
;
203 /* Internal version of `free' used in `morecore' (malloc.c). */
204 extern void _free_internal
__P ((__ptr_t __ptr
));
206 #endif /* _MALLOC_INTERNAL. */
208 /* Underlying allocation function; successive calls should
209 return contiguous pieces of memory. */
210 extern __ptr_t (*__morecore
) __P ((ptrdiff_t __size
));
212 /* Default value of `__morecore'. */
213 extern __ptr_t __default_morecore
__P ((ptrdiff_t __size
));
215 /* If not NULL, this function is called after each time
216 `__morecore' is called to increase the data size. */
217 extern void (*__after_morecore_hook
) __P ((void));
219 /* Nonzero if `malloc' has been called and done its initialization. */
220 extern int __malloc_initialized
;
222 /* Hooks for debugging versions. */
223 extern void (*__free_hook
) __P ((__ptr_t __ptr
));
224 extern __ptr_t (*__malloc_hook
) __P ((size_t __size
));
225 extern __ptr_t (*__realloc_hook
) __P ((__ptr_t __ptr
, size_t __size
));
227 /* Activate a standard collection of debugging hooks. */
228 extern int mcheck
__P ((void (*__bfunc
) __P ((char *)),
229 void (*__afunc
) __P ((void))));
231 /* Activate a standard collection of tracing hooks. */
232 extern void mtrace
__P ((void));
234 /* Statistics available to the user. */
237 size_t bytes_total
; /* Total size of the heap. */
238 size_t chunks_used
; /* Chunks allocated by the user. */
239 size_t bytes_used
; /* Byte total of user-allocated chunks. */
240 size_t chunks_free
; /* Chunks in the free list. */
241 size_t bytes_free
; /* Byte total of chunks in the free list. */
244 /* Pick up the current statistics. */
245 extern struct mstats mstats
__P ((void));
247 /* Call WARNFUN with a warning message when memory usage is high. */
248 extern void memory_warnings
__P ((__ptr_t __start
,
249 void (*__warnfun
) __P ((__const
char *))));
252 /* Relocating allocator. */
254 /* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
255 extern __ptr_t r_alloc
__P ((__ptr_t
*__handleptr
, size_t __size
));
257 /* Free the storage allocated in HANDLEPTR. */
258 extern void r_alloc_free
__P ((__ptr_t
*__handleptr
));
260 /* Adjust the block at HANDLEPTR to be SIZE bytes long. */
261 extern __ptr_t r_re_alloc
__P ((__ptr_t
*__handleptr
, size_t __size
));
268 #endif /* malloc.h */