xref: /netbsd/external/gpl2/libmalloc/dist/malloc.h (revision 5734aa39)
1 /*	$NetBSD: malloc.h,v 1.2 2016/01/13 21:56:38 christos Exp $	*/
2 
3 /* Declarations for `malloc' and friends.
4    Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
5 		  Written May 1989 by Mike Haertel.
6 
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11 
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Library General Public License for more details.
16 
17 You should have received a copy of the GNU Library General Public
18 License along with this library; see the file COPYING.LIB.  If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA.
21 
22    The author may be reached (Email) at the address mike@ai.mit.edu,
23    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
24 
25 #ifndef _MALLOC_H
26 
27 #define _MALLOC_H	1
28 
29 #ifdef _MALLOC_INTERNAL
30 
31 #ifdef	HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34 
35 #if	defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
36 #include <string.h>
37 #else
38 #ifndef memset
39 #define	memset(s, zero, n)	bzero ((s), (n))
40 #endif
41 #ifndef memcpy
42 #define	memcpy(d, s, n)		bcopy ((s), (d), (n))
43 #endif
44 #endif
45 
46 #if	defined (__GNU_LIBRARY__) || (defined (__STDC__) && __STDC__)
47 #include <limits.h>
48 #else
49 #ifndef CHAR_BIT
50 #define	CHAR_BIT	8
51 #endif
52 #endif
53 
54 #ifdef	HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57 
58 #endif	/* _MALLOC_INTERNAL.  */
59 
60 
61 #ifdef	__cplusplus
62 extern "C"
63 {
64 #endif
65 
66 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
67 #ifndef __NetBSD__
68 #undef	__P
69 #define	__P(args)	args
70 #else
71 #include <sys/cdefs.h>
72 #endif
73 #undef	__ptr_t
74 #define	__ptr_t		void *
75 #else /* Not C++ or ANSI C.  */
76 #undef	__P
77 #define	__P(args)	()
78 #undef	const
79 #define	const
80 #undef	__ptr_t
81 #define	__ptr_t		char *
82 #endif /* C++ or ANSI C.  */
83 
84 #if defined (__STDC__) && __STDC__
85 #include <stddef.h>
86 #define	__malloc_size_t		size_t
87 #define	__malloc_ptrdiff_t	ptrdiff_t
88 #else
89 #define	__malloc_size_t		unsigned int
90 #define	__malloc_ptrdiff_t	int
91 #endif
92 
93 #ifndef	NULL
94 #define	NULL	0
95 #endif
96 
97 
98 /* Allocate SIZE bytes of memory.  */
99 extern __ptr_t malloc __P ((__malloc_size_t __size));
100 /* Re-allocate the previously allocated block
101    in __ptr_t, making the new block SIZE bytes long.  */
102 extern __ptr_t realloc __P ((__ptr_t __ptr, __malloc_size_t __size));
103 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0.  */
104 extern __ptr_t calloc __P ((__malloc_size_t __nmemb, __malloc_size_t __size));
105 /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
106 extern void free __P ((__ptr_t __ptr));
107 
108 /* Allocate SIZE bytes allocated to ALIGNMENT bytes.  */
109 extern __ptr_t memalign __P ((__malloc_size_t __alignment,
110 			      __malloc_size_t __size));
111 
112 /* Allocate SIZE bytes on a page boundary.  */
113 extern __ptr_t valloc __P ((__malloc_size_t __size));
114 
115 
116 #ifdef _MALLOC_INTERNAL
117 
118 /* The allocator divides the heap into blocks of fixed size; large
119    requests receive one or more whole blocks, and small requests
120    receive a fragment of a block.  Fragment sizes are powers of two,
121    and all fragments of a block are the same size.  When all the
122    fragments in a block have been freed, the block itself is freed.  */
123 #define INT_BIT		(CHAR_BIT * sizeof(int))
124 #define BLOCKLOG	(INT_BIT > 16 ? 12 : 9)
125 #define BLOCKSIZE	(1 << BLOCKLOG)
126 #define BLOCKIFY(SIZE)	(((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
127 
128 /* Determine the amount of memory spanned by the initial heap table
129    (not an absolute limit).  */
130 #define HEAP		(INT_BIT > 16 ? 4194304 : 65536)
131 
132 /* Number of contiguous free blocks allowed to build up at the end of
133    memory before they will be returned to the system.  */
134 #define FINAL_FREE_BLOCKS	8
135 
136 /* Data structure giving per-block information.  */
137 typedef union
138   {
139     /* Heap information for a busy block.  */
140     struct
141       {
142 	/* Zero for a large (multiblock) object, or positive giving the
143 	   logarithm to the base two of the fragment size.  */
144 	int type;
145 	union
146 	  {
147 	    struct
148 	      {
149 		__malloc_size_t nfree; /* Free frags in a fragmented block.  */
150 		__malloc_size_t first; /* First free fragment of the block.  */
151 	      } frag;
152 	    /* For a large object, in its first block, this has the number
153 	       of blocks in the object.  In the other blocks, this has a
154 	       negative number which says how far back the first block is.  */
155 	    __malloc_ptrdiff_t size;
156 	  } info;
157       } busy;
158     /* Heap information for a free block
159        (that may be the first of a free cluster).  */
160     struct
161       {
162 	__malloc_size_t size;	/* Size (in blocks) of a free cluster.  */
163 	__malloc_size_t next;	/* Index of next free cluster.  */
164 	__malloc_size_t prev;	/* Index of previous free cluster.  */
165       } free;
166   } malloc_info;
167 
168 /* Pointer to first block of the heap.  */
169 extern char *_heapbase;
170 
171 /* Table indexed by block number giving per-block information.  */
172 extern malloc_info *_heapinfo;
173 
174 /* Address to block number and vice versa.  */
175 #define BLOCK(A)	(((char *) (A) - _heapbase) / BLOCKSIZE + 1)
176 #define ADDRESS(B)	((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
177 
178 /* Current search index for the heap table.  */
179 extern __malloc_size_t _heapindex;
180 
181 /* Limit of valid info table indices.  */
182 extern __malloc_size_t _heaplimit;
183 
184 /* Doubly linked lists of free fragments.  */
185 struct list
186   {
187     struct list *next;
188     struct list *prev;
189   };
190 
191 /* Free list headers for each fragment size.  */
192 extern struct list _fraghead[];
193 
194 /* List of blocks allocated with `memalign' (or `valloc').  */
195 struct alignlist
196   {
197     struct alignlist *next;
198     __ptr_t aligned;		/* The address that memaligned returned.  */
199     __ptr_t exact;		/* The address that malloc returned.  */
200   };
201 extern struct alignlist *_aligned_blocks;
202 
203 /* Instrumentation.  */
204 extern __malloc_size_t _chunks_used;
205 extern __malloc_size_t _bytes_used;
206 extern __malloc_size_t _chunks_free;
207 extern __malloc_size_t _bytes_free;
208 
209 /* Internal version of `free' used in `morecore' (malloc.c). */
210 extern void _free_internal __P ((__ptr_t __ptr));
211 
212 #endif /* _MALLOC_INTERNAL.  */
213 
214 /* Given an address in the middle of a malloc'd object,
215    return the address of the beginning of the object.  */
216 extern __ptr_t malloc_find_object_address __P ((__ptr_t __ptr));
217 
218 /* Underlying allocation function; successive calls should
219    return contiguous pieces of memory.  */
220 extern __ptr_t (*__morecore) __P ((__malloc_ptrdiff_t __size));
221 
222 /* Default value of `__morecore'.  */
223 extern __ptr_t __default_morecore __P ((__malloc_ptrdiff_t __size));
224 
225 /* If not NULL, this function is called after each time
226    `__morecore' is called to increase the data size.  */
227 extern void (*__after_morecore_hook) __P ((void));
228 
229 /* Nonzero if `malloc' has been called and done its initialization.  */
230 extern int __malloc_initialized;
231 
232 /* Hooks for debugging versions.  */
233 extern void (*__malloc_initialize_hook) __P ((void));
234 extern void (*__free_hook) __P ((__ptr_t __ptr));
235 extern __ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
236 extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
237 extern __ptr_t (*__memalign_hook) __P ((__malloc_size_t __size,
238 					__malloc_size_t __alignment));
239 
240 /* Return values for `mprobe': these are the kinds of inconsistencies that
241    `mcheck' enables detection of.  */
242 enum mcheck_status
243   {
244     MCHECK_DISABLED = -1,	/* Consistency checking is not turned on.  */
245     MCHECK_OK,			/* Block is fine.  */
246     MCHECK_FREE,		/* Block freed twice.  */
247     MCHECK_HEAD,		/* Memory before the block was clobbered.  */
248     MCHECK_TAIL			/* Memory after the block was clobbered.  */
249   };
250 
251 /* Activate a standard collection of debugging hooks.  This must be called
252    before `malloc' is ever called.  ABORTFUNC is called with an error code
253    (see enum above) when an inconsistency is detected.  If ABORTFUNC is
254    null, the standard function prints on stderr and then calls `abort'.  */
255 extern int mcheck __P ((void (*__abortfunc) __P ((enum mcheck_status))));
256 
257 /* Check for aberrations in a particular malloc'd block.  You must have
258    called `mcheck' already.  These are the same checks that `mcheck' does
259    when you free or reallocate a block.  */
260 extern enum mcheck_status mprobe __P ((__ptr_t __ptr));
261 
262 /* Activate a standard collection of tracing hooks.  */
263 extern void mtrace __P ((void));
264 extern void muntrace __P ((void));
265 
266 /* Statistics available to the user.  */
267 struct mstats
268   {
269     __malloc_size_t bytes_total; /* Total size of the heap. */
270     __malloc_size_t chunks_used; /* Chunks allocated by the user. */
271     __malloc_size_t bytes_used;	/* Byte total of user-allocated chunks. */
272     __malloc_size_t chunks_free; /* Chunks in the free list. */
273     __malloc_size_t bytes_free;	/* Byte total of chunks in the free list. */
274   };
275 
276 /* Pick up the current statistics. */
277 extern struct mstats mstats __P ((void));
278 
279 /* Call WARNFUN with a warning message when memory usage is high.  */
280 extern void memory_warnings __P ((__ptr_t __start,
281 				  void (*__warnfun) __P ((const char *))));
282 
283 
284 /* Relocating allocator.  */
285 
286 /* Allocate SIZE bytes, and store the address in *HANDLEPTR.  */
287 extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
288 
289 /* Free the storage allocated in HANDLEPTR.  */
290 extern void r_alloc_free __P ((__ptr_t *__handleptr));
291 
292 /* Adjust the block at HANDLEPTR to be SIZE bytes long.  */
293 extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
294 
295 
296 #ifdef	__cplusplus
297 }
298 #endif
299 
300 #endif /* malloc.h  */
301