]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/malloclib/malloc.h
Imported from ../bash-1.14.7.tar.gz.
[thirdparty/bash.git] / lib / malloclib / malloc.h
1 /* Declarations for `malloc' and friends.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Written May 1989 by Mike Haertel.
4
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.
9
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.
14
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.
19
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. */
22
23 #ifndef _MALLOC_H
24
25 #define _MALLOC_H 1
26
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. */
31 #include <stdio.h>
32 #endif
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif
38
39 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
40 #undef __P
41 #define __P(args) args
42 #undef __ptr_t
43 #define __ptr_t void *
44 #else /* Not C++ or ANSI C. */
45 #undef __P
46 #define __P(args) ()
47 #undef const
48 #define const
49 #undef __ptr_t
50 #define __ptr_t char *
51 #endif /* C++ or ANSI C. */
52
53 #ifndef NULL
54 #define NULL 0
55 #endif
56
57 #ifdef __STDC__
58 #include <stddef.h>
59 #else
60 #undef size_t
61 #define size_t unsigned int
62 #undef ptrdiff_t
63 #define ptrdiff_t int
64 #endif
65
66
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));
76
77 /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
78 extern __ptr_t memalign __P ((size_t __alignment, size_t __size));
79
80 /* Allocate SIZE bytes on a page boundary. */
81 extern __ptr_t valloc __P ((size_t __size));
82
83
84 #ifdef _MALLOC_INTERNAL
85
86 #ifdef HAVE_CONFIG_H
87 #include "config.h"
88 #endif
89
90 #if defined(__GNU_LIBRARY__) || defined(STDC_HEADERS) || defined(USG)
91 #include <string.h>
92 #else
93 #ifndef memset
94 #define memset(s, zero, n) bzero ((s), (n))
95 #endif
96 #ifndef memcpy
97 #define memcpy(d, s, n) bcopy ((s), (d), (n))
98 #endif
99 #ifndef memmove
100 #define memmove(d, s, n) bcopy ((s), (d), (n))
101 #endif
102 #endif
103
104
105 #if defined(__GNU_LIBRARY__) || defined(__STDC__)
106 #include <limits.h>
107 #else
108 #define CHAR_BIT 8
109 #endif
110
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)
120
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)
124
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
128
129 /* Data structure giving per-block information. */
130 typedef union
131 {
132 /* Heap information for a busy block. */
133 struct
134 {
135 /* Zero for a large block, or positive giving the
136 logarithm to the base two of the fragment size. */
137 int type;
138 union
139 {
140 struct
141 {
142 size_t nfree; /* Free fragments in a fragmented block. */
143 size_t first; /* First free fragment of the block. */
144 } frag;
145 /* Size (in blocks) of a large cluster. */
146 size_t size;
147 } info;
148 } busy;
149 /* Heap information for a free block
150 (that may be the first of a free cluster). */
151 struct
152 {
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. */
156 } free;
157 } malloc_info;
158
159 /* Pointer to first block of the heap. */
160 extern char *_heapbase;
161
162 /* Table indexed by block number giving per-block information. */
163 extern malloc_info *_heapinfo;
164
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))
168
169 /* Current search index for the heap table. */
170 extern size_t _heapindex;
171
172 /* Limit of valid info table indices. */
173 extern size_t _heaplimit;
174
175 /* Doubly linked lists of free fragments. */
176 struct list
177 {
178 struct list *next;
179 struct list *prev;
180 };
181
182 /* Count of blocks for each fragment size. */
183 extern int _fragblocks[];
184
185 /* Free list headers for each fragment size. */
186 extern struct list _fraghead[];
187
188 /* List of blocks allocated with `memalign' (or `valloc'). */
189 struct alignlist
190 {
191 struct alignlist *next;
192 __ptr_t aligned; /* The address that memaligned returned. */
193 __ptr_t exact; /* The address that malloc returned. */
194 };
195 extern struct alignlist *_aligned_blocks;
196
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;
202
203 /* Internal version of `free' used in `morecore' (malloc.c). */
204 extern void _free_internal __P ((__ptr_t __ptr));
205
206 #endif /* _MALLOC_INTERNAL. */
207
208 /* Underlying allocation function; successive calls should
209 return contiguous pieces of memory. */
210 extern __ptr_t (*__morecore) __P ((ptrdiff_t __size));
211
212 /* Default value of `__morecore'. */
213 extern __ptr_t __default_morecore __P ((ptrdiff_t __size));
214
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));
218
219 /* Nonzero if `malloc' has been called and done its initialization. */
220 extern int __malloc_initialized;
221
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));
226
227 /* Activate a standard collection of debugging hooks. */
228 extern int mcheck __P ((void (*__bfunc) __P ((char *)),
229 void (*__afunc) __P ((void))));
230
231 /* Activate a standard collection of tracing hooks. */
232 extern void mtrace __P ((void));
233
234 /* Statistics available to the user. */
235 struct mstats
236 {
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. */
242 };
243
244 /* Pick up the current statistics. */
245 extern struct mstats mstats __P ((void));
246
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 *))));
250
251
252 /* Relocating allocator. */
253
254 /* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
255 extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, size_t __size));
256
257 /* Free the storage allocated in HANDLEPTR. */
258 extern void r_alloc_free __P ((__ptr_t *__handleptr));
259
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));
262
263
264 #ifdef __cplusplus
265 }
266 #endif
267
268 #endif /* malloc.h */