1*6a54128fSAndroid Build Coastguard Worker /* Declarations for `malloc' and friends. 2*6a54128fSAndroid Build Coastguard Worker Copyright 1990, 1991, 1992 Free Software Foundation, Inc. 3*6a54128fSAndroid Build Coastguard Worker Written May 1989 by Mike Haertel. 4*6a54128fSAndroid Build Coastguard Worker 5*6a54128fSAndroid Build Coastguard Worker This library is free software; you can redistribute it and/or 6*6a54128fSAndroid Build Coastguard Worker modify it under the terms of the GNU Library General Public License as 7*6a54128fSAndroid Build Coastguard Worker published by the Free Software Foundation; either version 2 of the 8*6a54128fSAndroid Build Coastguard Worker License, or (at your option) any later version. 9*6a54128fSAndroid Build Coastguard Worker 10*6a54128fSAndroid Build Coastguard Worker This library is distributed in the hope that it will be useful, 11*6a54128fSAndroid Build Coastguard Worker but WITHOUT ANY WARRANTY; without even the implied warranty of 12*6a54128fSAndroid Build Coastguard Worker MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13*6a54128fSAndroid Build Coastguard Worker Library General Public License for more details. 14*6a54128fSAndroid Build Coastguard Worker 15*6a54128fSAndroid Build Coastguard Worker You should have received a copy of the GNU Library General Public 16*6a54128fSAndroid Build Coastguard Worker License along with this library; see the file COPYING.LIB. If 17*6a54128fSAndroid Build Coastguard Worker not, write to the Free Software Foundation, Inc., 675 Mass Ave, 18*6a54128fSAndroid Build Coastguard Worker Cambridge, MA 02139, USA. 19*6a54128fSAndroid Build Coastguard Worker 20*6a54128fSAndroid Build Coastguard Worker The author may be reached (Email) at the address [email protected], 21*6a54128fSAndroid Build Coastguard Worker or (US mail) as Mike Haertel c/o Free Software Foundation. */ 22*6a54128fSAndroid Build Coastguard Worker 23*6a54128fSAndroid Build Coastguard Worker #ifndef _MTRACE_H 24*6a54128fSAndroid Build Coastguard Worker 25*6a54128fSAndroid Build Coastguard Worker #define _MTRACE_H 1 26*6a54128fSAndroid Build Coastguard Worker 27*6a54128fSAndroid Build Coastguard Worker #ifdef __cplusplus 28*6a54128fSAndroid Build Coastguard Worker extern "C" 29*6a54128fSAndroid Build Coastguard Worker { 30*6a54128fSAndroid Build Coastguard Worker #endif 31*6a54128fSAndroid Build Coastguard Worker 32*6a54128fSAndroid Build Coastguard Worker #if defined (__cplusplus) || (defined (__STDC__) && __STDC__) 33*6a54128fSAndroid Build Coastguard Worker #undef __P 34*6a54128fSAndroid Build Coastguard Worker #define __P(args) args 35*6a54128fSAndroid Build Coastguard Worker #undef __ptr_t 36*6a54128fSAndroid Build Coastguard Worker #define __ptr_t void * 37*6a54128fSAndroid Build Coastguard Worker #else /* Not C++ or ANSI C. */ 38*6a54128fSAndroid Build Coastguard Worker #undef __P 39*6a54128fSAndroid Build Coastguard Worker #define __P(args) () 40*6a54128fSAndroid Build Coastguard Worker #undef const 41*6a54128fSAndroid Build Coastguard Worker #define const 42*6a54128fSAndroid Build Coastguard Worker #undef __ptr_t 43*6a54128fSAndroid Build Coastguard Worker #define __ptr_t char * 44*6a54128fSAndroid Build Coastguard Worker #endif /* C++ or ANSI C. */ 45*6a54128fSAndroid Build Coastguard Worker 46*6a54128fSAndroid Build Coastguard Worker #ifndef NULL 47*6a54128fSAndroid Build Coastguard Worker #define NULL 0 48*6a54128fSAndroid Build Coastguard Worker #endif 49*6a54128fSAndroid Build Coastguard Worker 50*6a54128fSAndroid Build Coastguard Worker #ifdef __STDC__ 51*6a54128fSAndroid Build Coastguard Worker #include <stddef.h> 52*6a54128fSAndroid Build Coastguard Worker #else 53*6a54128fSAndroid Build Coastguard Worker #undef size_t 54*6a54128fSAndroid Build Coastguard Worker #define size_t unsigned int 55*6a54128fSAndroid Build Coastguard Worker #undef ptrdiff_t 56*6a54128fSAndroid Build Coastguard Worker #define ptrdiff_t int 57*6a54128fSAndroid Build Coastguard Worker #endif 58*6a54128fSAndroid Build Coastguard Worker 59*6a54128fSAndroid Build Coastguard Worker 60*6a54128fSAndroid Build Coastguard Worker /* Allocate SIZE bytes of memory. */ 61*6a54128fSAndroid Build Coastguard Worker extern __ptr_t malloc __P ((size_t __size)); 62*6a54128fSAndroid Build Coastguard Worker /* Re-allocate the previously allocated block 63*6a54128fSAndroid Build Coastguard Worker in __ptr_t, making the new block SIZE bytes long. */ 64*6a54128fSAndroid Build Coastguard Worker extern __ptr_t realloc __P ((__ptr_t __ptr, size_t __size)); 65*6a54128fSAndroid Build Coastguard Worker /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */ 66*6a54128fSAndroid Build Coastguard Worker extern __ptr_t calloc __P ((size_t __nmemb, size_t __size)); 67*6a54128fSAndroid Build Coastguard Worker /* Free a block allocated by `malloc', `realloc' or `calloc'. */ 68*6a54128fSAndroid Build Coastguard Worker extern void free __P ((__ptr_t __ptr)); 69*6a54128fSAndroid Build Coastguard Worker 70*6a54128fSAndroid Build Coastguard Worker /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */ 71*6a54128fSAndroid Build Coastguard Worker extern __ptr_t memalign __P ((size_t __alignment, size_t __size)); 72*6a54128fSAndroid Build Coastguard Worker 73*6a54128fSAndroid Build Coastguard Worker /* Allocate SIZE bytes on a page boundary. */ 74*6a54128fSAndroid Build Coastguard Worker extern __ptr_t valloc __P ((size_t __size)); 75*6a54128fSAndroid Build Coastguard Worker 76*6a54128fSAndroid Build Coastguard Worker 77*6a54128fSAndroid Build Coastguard Worker #ifdef _MALLOC_INTERNAL 78*6a54128fSAndroid Build Coastguard Worker 79*6a54128fSAndroid Build Coastguard Worker #include <stdio.h> /* Harmless, gets __GNU_LIBRARY__ defined. */ 80*6a54128fSAndroid Build Coastguard Worker 81*6a54128fSAndroid Build Coastguard Worker #if defined(__GNU_LIBRARY__) || defined(STDC_HEADERS) || defined(USG) 82*6a54128fSAndroid Build Coastguard Worker #include <string.h> 83*6a54128fSAndroid Build Coastguard Worker #else 84*6a54128fSAndroid Build Coastguard Worker #ifndef memset 85*6a54128fSAndroid Build Coastguard Worker #define memset(s, zero, n) bzero ((s), (n)) 86*6a54128fSAndroid Build Coastguard Worker #endif 87*6a54128fSAndroid Build Coastguard Worker #ifndef memcpy 88*6a54128fSAndroid Build Coastguard Worker #define memcpy(d, s, n) bcopy ((s), (d), (n)) 89*6a54128fSAndroid Build Coastguard Worker #endif 90*6a54128fSAndroid Build Coastguard Worker #endif 91*6a54128fSAndroid Build Coastguard Worker 92*6a54128fSAndroid Build Coastguard Worker 93*6a54128fSAndroid Build Coastguard Worker #if defined(__GNU_LIBRARY__) || defined(__STDC__) 94*6a54128fSAndroid Build Coastguard Worker #include <limits.h> 95*6a54128fSAndroid Build Coastguard Worker #else 96*6a54128fSAndroid Build Coastguard Worker #define CHAR_BIT 8 97*6a54128fSAndroid Build Coastguard Worker #endif 98*6a54128fSAndroid Build Coastguard Worker 99*6a54128fSAndroid Build Coastguard Worker /* The allocator divides the heap into blocks of fixed size; large 100*6a54128fSAndroid Build Coastguard Worker requests receive one or more whole blocks, and small requests 101*6a54128fSAndroid Build Coastguard Worker receive a fragment of a block. Fragment sizes are powers of two, 102*6a54128fSAndroid Build Coastguard Worker and all fragments of a block are the same size. When all the 103*6a54128fSAndroid Build Coastguard Worker fragments in a block have been freed, the block itself is freed. */ 104*6a54128fSAndroid Build Coastguard Worker #define INT_BIT (CHAR_BIT * sizeof(int)) 105*6a54128fSAndroid Build Coastguard Worker #define BLOCKLOG (INT_BIT > 16 ? 12 : 9) 106*6a54128fSAndroid Build Coastguard Worker #define BLOCKSIZE (1 << BLOCKLOG) 107*6a54128fSAndroid Build Coastguard Worker #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE) 108*6a54128fSAndroid Build Coastguard Worker 109*6a54128fSAndroid Build Coastguard Worker /* Determine the amount of memory spanned by the initial heap table 110*6a54128fSAndroid Build Coastguard Worker (not an absolute limit). */ 111*6a54128fSAndroid Build Coastguard Worker #define HEAP (INT_BIT > 16 ? 4194304 : 65536) 112*6a54128fSAndroid Build Coastguard Worker 113*6a54128fSAndroid Build Coastguard Worker /* Number of contiguous free blocks allowed to build up at the end of 114*6a54128fSAndroid Build Coastguard Worker memory before they will be returned to the system. */ 115*6a54128fSAndroid Build Coastguard Worker #define FINAL_FREE_BLOCKS 8 116*6a54128fSAndroid Build Coastguard Worker 117*6a54128fSAndroid Build Coastguard Worker /* Data structure giving per-block information. */ 118*6a54128fSAndroid Build Coastguard Worker typedef union 119*6a54128fSAndroid Build Coastguard Worker { 120*6a54128fSAndroid Build Coastguard Worker /* Heap information for a busy block. */ 121*6a54128fSAndroid Build Coastguard Worker struct 122*6a54128fSAndroid Build Coastguard Worker { 123*6a54128fSAndroid Build Coastguard Worker /* Zero for a large block, or positive giving the 124*6a54128fSAndroid Build Coastguard Worker logarithm to the base two of the fragment size. */ 125*6a54128fSAndroid Build Coastguard Worker int type; 126*6a54128fSAndroid Build Coastguard Worker union 127*6a54128fSAndroid Build Coastguard Worker { 128*6a54128fSAndroid Build Coastguard Worker struct 129*6a54128fSAndroid Build Coastguard Worker { 130*6a54128fSAndroid Build Coastguard Worker size_t nfree; /* Free fragments in a fragmented block. */ 131*6a54128fSAndroid Build Coastguard Worker size_t first; /* First free fragment of the block. */ 132*6a54128fSAndroid Build Coastguard Worker } frag; 133*6a54128fSAndroid Build Coastguard Worker /* Size (in blocks) of a large cluster. */ 134*6a54128fSAndroid Build Coastguard Worker size_t size; 135*6a54128fSAndroid Build Coastguard Worker } info; 136*6a54128fSAndroid Build Coastguard Worker } busy; 137*6a54128fSAndroid Build Coastguard Worker /* Heap information for a free block 138*6a54128fSAndroid Build Coastguard Worker (that may be the first of a free cluster). */ 139*6a54128fSAndroid Build Coastguard Worker struct 140*6a54128fSAndroid Build Coastguard Worker { 141*6a54128fSAndroid Build Coastguard Worker size_t size; /* Size (in blocks) of a free cluster. */ 142*6a54128fSAndroid Build Coastguard Worker size_t next; /* Index of next free cluster. */ 143*6a54128fSAndroid Build Coastguard Worker size_t prev; /* Index of previous free cluster. */ 144*6a54128fSAndroid Build Coastguard Worker } free; 145*6a54128fSAndroid Build Coastguard Worker } malloc_info; 146*6a54128fSAndroid Build Coastguard Worker 147*6a54128fSAndroid Build Coastguard Worker /* Pointer to first block of the heap. */ 148*6a54128fSAndroid Build Coastguard Worker extern char *_heapbase; 149*6a54128fSAndroid Build Coastguard Worker 150*6a54128fSAndroid Build Coastguard Worker /* Table indexed by block number giving per-block information. */ 151*6a54128fSAndroid Build Coastguard Worker extern malloc_info *_heapinfo; 152*6a54128fSAndroid Build Coastguard Worker 153*6a54128fSAndroid Build Coastguard Worker /* Address to block number and vice versa. */ 154*6a54128fSAndroid Build Coastguard Worker #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1) 155*6a54128fSAndroid Build Coastguard Worker #define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase)) 156*6a54128fSAndroid Build Coastguard Worker 157*6a54128fSAndroid Build Coastguard Worker /* Current search index for the heap table. */ 158*6a54128fSAndroid Build Coastguard Worker extern size_t _heapindex; 159*6a54128fSAndroid Build Coastguard Worker 160*6a54128fSAndroid Build Coastguard Worker /* Limit of valid info table indices. */ 161*6a54128fSAndroid Build Coastguard Worker extern size_t _heaplimit; 162*6a54128fSAndroid Build Coastguard Worker 163*6a54128fSAndroid Build Coastguard Worker /* Doubly linked lists of free fragments. */ 164*6a54128fSAndroid Build Coastguard Worker struct list 165*6a54128fSAndroid Build Coastguard Worker { 166*6a54128fSAndroid Build Coastguard Worker struct list *next; 167*6a54128fSAndroid Build Coastguard Worker struct list *prev; 168*6a54128fSAndroid Build Coastguard Worker }; 169*6a54128fSAndroid Build Coastguard Worker 170*6a54128fSAndroid Build Coastguard Worker /* Free list headers for each fragment size. */ 171*6a54128fSAndroid Build Coastguard Worker extern struct list _fraghead[]; 172*6a54128fSAndroid Build Coastguard Worker 173*6a54128fSAndroid Build Coastguard Worker /* List of blocks allocated with `memalign' (or `valloc'). */ 174*6a54128fSAndroid Build Coastguard Worker struct alignlist 175*6a54128fSAndroid Build Coastguard Worker { 176*6a54128fSAndroid Build Coastguard Worker struct alignlist *next; 177*6a54128fSAndroid Build Coastguard Worker __ptr_t aligned; /* The address that memaligned returned. */ 178*6a54128fSAndroid Build Coastguard Worker __ptr_t exact; /* The address that malloc returned. */ 179*6a54128fSAndroid Build Coastguard Worker }; 180*6a54128fSAndroid Build Coastguard Worker extern struct alignlist *_aligned_blocks; 181*6a54128fSAndroid Build Coastguard Worker 182*6a54128fSAndroid Build Coastguard Worker /* Instrumentation. */ 183*6a54128fSAndroid Build Coastguard Worker extern size_t _chunks_used; 184*6a54128fSAndroid Build Coastguard Worker extern size_t _bytes_used; 185*6a54128fSAndroid Build Coastguard Worker extern size_t _chunks_free; 186*6a54128fSAndroid Build Coastguard Worker extern size_t _bytes_free; 187*6a54128fSAndroid Build Coastguard Worker 188*6a54128fSAndroid Build Coastguard Worker /* Internal version of `free' used in `morecore' (malloc.c). */ 189*6a54128fSAndroid Build Coastguard Worker extern void _free_internal __P ((__ptr_t __ptr)); 190*6a54128fSAndroid Build Coastguard Worker 191*6a54128fSAndroid Build Coastguard Worker #endif /* _MALLOC_INTERNAL. */ 192*6a54128fSAndroid Build Coastguard Worker 193*6a54128fSAndroid Build Coastguard Worker /* Underlying allocation function; successive calls should 194*6a54128fSAndroid Build Coastguard Worker return contiguous pieces of memory. */ 195*6a54128fSAndroid Build Coastguard Worker extern __ptr_t (*__morecore) __P ((ptrdiff_t __size)); 196*6a54128fSAndroid Build Coastguard Worker 197*6a54128fSAndroid Build Coastguard Worker /* Default value of `__morecore'. */ 198*6a54128fSAndroid Build Coastguard Worker extern __ptr_t __default_morecore __P ((ptrdiff_t __size)); 199*6a54128fSAndroid Build Coastguard Worker 200*6a54128fSAndroid Build Coastguard Worker /* Nonzero if `malloc' has been called and done its initialization. */ 201*6a54128fSAndroid Build Coastguard Worker extern int __malloc_initialized; 202*6a54128fSAndroid Build Coastguard Worker 203*6a54128fSAndroid Build Coastguard Worker /* Hooks for debugging versions. */ 204*6a54128fSAndroid Build Coastguard Worker extern void (*__free_hook) __P ((__ptr_t __ptr)); 205*6a54128fSAndroid Build Coastguard Worker extern __ptr_t (*__malloc_hook) __P ((size_t __size)); 206*6a54128fSAndroid Build Coastguard Worker extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size)); 207*6a54128fSAndroid Build Coastguard Worker 208*6a54128fSAndroid Build Coastguard Worker /* Activate a standard collection of debugging hooks. */ 209*6a54128fSAndroid Build Coastguard Worker extern void mcheck __P ((void (*__func) __P ((void)))); 210*6a54128fSAndroid Build Coastguard Worker 211*6a54128fSAndroid Build Coastguard Worker /* Activate a standard collection of tracing hooks. */ 212*6a54128fSAndroid Build Coastguard Worker extern void mtrace __P ((void)); 213*6a54128fSAndroid Build Coastguard Worker 214*6a54128fSAndroid Build Coastguard Worker /* Statistics available to the user. */ 215*6a54128fSAndroid Build Coastguard Worker struct mstats 216*6a54128fSAndroid Build Coastguard Worker { 217*6a54128fSAndroid Build Coastguard Worker size_t bytes_total; /* Total size of the heap. */ 218*6a54128fSAndroid Build Coastguard Worker size_t chunks_used; /* Chunks allocated by the user. */ 219*6a54128fSAndroid Build Coastguard Worker size_t bytes_used; /* Byte total of user-allocated chunks. */ 220*6a54128fSAndroid Build Coastguard Worker size_t chunks_free; /* Chunks in the free list. */ 221*6a54128fSAndroid Build Coastguard Worker size_t bytes_free; /* Byte total of chunks in the free list. */ 222*6a54128fSAndroid Build Coastguard Worker }; 223*6a54128fSAndroid Build Coastguard Worker 224*6a54128fSAndroid Build Coastguard Worker /* Pick up the current statistics. */ 225*6a54128fSAndroid Build Coastguard Worker extern struct mstats mstats __P ((void)); 226*6a54128fSAndroid Build Coastguard Worker 227*6a54128fSAndroid Build Coastguard Worker #ifdef __cplusplus 228*6a54128fSAndroid Build Coastguard Worker } 229*6a54128fSAndroid Build Coastguard Worker #endif 230*6a54128fSAndroid Build Coastguard Worker 231*6a54128fSAndroid Build Coastguard Worker #endif /* mtrace.h */ 232