1 /* Declarations for `malloc' and friends.
2    Copyright 1990, 1991, 1992 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 _MTRACE_H
24 
25 #define _MTRACE_H	1
26 
27 #ifdef	__cplusplus
28 extern "C"
29 {
30 #endif
31 
32 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
33 #undef	__P
34 #define	__P(args)	args
35 #undef	__ptr_t
36 #define	__ptr_t		void *
37 #else /* Not C++ or ANSI C.  */
38 #undef	__P
39 #define	__P(args)	()
40 #undef	const
41 #define	const
42 #undef	__ptr_t
43 #define	__ptr_t		char *
44 #endif /* C++ or ANSI C.  */
45 
46 #ifndef	NULL
47 #define	NULL	0
48 #endif
49 
50 #ifdef	__STDC__
51 #include <stddef.h>
52 #else
53 #undef	size_t
54 #define	size_t		unsigned int
55 #undef	ptrdiff_t
56 #define	ptrdiff_t	int
57 #endif
58 
59 
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));
69 
70 /* Allocate SIZE bytes allocated to ALIGNMENT bytes.  */
71 extern __ptr_t memalign __P ((size_t __alignment, size_t __size));
72 
73 /* Allocate SIZE bytes on a page boundary.  */
74 extern __ptr_t valloc __P ((size_t __size));
75 
76 
77 #ifdef _MALLOC_INTERNAL
78 
79 #include <stdio.h>		/* Harmless, gets __GNU_LIBRARY__ defined.  */
80 
81 #if	defined(__GNU_LIBRARY__) || defined(STDC_HEADERS) || defined(USG)
82 #include <string.h>
83 #else
84 #ifndef memset
85 #define	memset(s, zero, n)	bzero ((s), (n))
86 #endif
87 #ifndef memcpy
88 #define	memcpy(d, s, n)		bcopy ((s), (d), (n))
89 #endif
90 #endif
91 
92 
93 #if	defined(__GNU_LIBRARY__) || defined(__STDC__)
94 #include <limits.h>
95 #else
96 #define	CHAR_BIT	8
97 #endif
98 
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)
108 
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)
112 
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
116 
117 /* Data structure giving per-block information.  */
118 typedef union
119   {
120     /* Heap information for a busy block.  */
121     struct
122       {
123 	/* Zero for a large block, or positive giving the
124 	   logarithm to the base two of the fragment size.  */
125 	int type;
126 	union
127 	  {
128 	    struct
129 	      {
130 		size_t nfree;	/* Free fragments in a fragmented block.  */
131 		size_t first;	/* First free fragment of the block.  */
132 	      } frag;
133 	    /* Size (in blocks) of a large cluster.  */
134 	    size_t size;
135 	  } info;
136       } busy;
137     /* Heap information for a free block
138        (that may be the first of a free cluster).  */
139     struct
140       {
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.  */
144       } free;
145   } malloc_info;
146 
147 /* Pointer to first block of the heap.  */
148 extern char *_heapbase;
149 
150 /* Table indexed by block number giving per-block information.  */
151 extern malloc_info *_heapinfo;
152 
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))
156 
157 /* Current search index for the heap table.  */
158 extern size_t _heapindex;
159 
160 /* Limit of valid info table indices.  */
161 extern size_t _heaplimit;
162 
163 /* Doubly linked lists of free fragments.  */
164 struct list
165   {
166     struct list *next;
167     struct list *prev;
168   };
169 
170 /* Free list headers for each fragment size.  */
171 extern struct list _fraghead[];
172 
173 /* List of blocks allocated with `memalign' (or `valloc').  */
174 struct alignlist
175   {
176     struct alignlist *next;
177     __ptr_t aligned;		/* The address that memaligned returned.  */
178     __ptr_t exact;		/* The address that malloc returned.  */
179   };
180 extern struct alignlist *_aligned_blocks;
181 
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;
187 
188 /* Internal version of `free' used in `morecore' (malloc.c). */
189 extern void _free_internal __P ((__ptr_t __ptr));
190 
191 #endif /* _MALLOC_INTERNAL.  */
192 
193 /* Underlying allocation function; successive calls should
194    return contiguous pieces of memory.  */
195 extern __ptr_t (*__morecore) __P ((ptrdiff_t __size));
196 
197 /* Default value of `__morecore'.  */
198 extern __ptr_t __default_morecore __P ((ptrdiff_t __size));
199 
200 /* Nonzero if `malloc' has been called and done its initialization.  */
201 extern int __malloc_initialized;
202 
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));
207 
208 /* Activate a standard collection of debugging hooks.  */
209 extern void mcheck __P ((void (*__func) __P ((void))));
210 
211 /* Activate a standard collection of tracing hooks.  */
212 extern void mtrace __P ((void));
213 
214 /* Statistics available to the user.  */
215 struct mstats
216   {
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. */
222   };
223 
224 /* Pick up the current statistics. */
225 extern struct mstats mstats __P ((void));
226 
227 #ifdef	__cplusplus
228 }
229 #endif
230 
231 #endif /* mtrace.h  */
232