| 1 | /* Declarations for `malloc' and friends. |
| 2 | Copyright 1990 Free Software Foundation |
| 3 | Written May 1989 by Mike Haertel. |
| 4 | |
| 5 | The author may be reached (Email) at the address mike@ai.mit.edu, |
| 6 | or (US mail) as Mike Haertel c/o Free Software Foundation. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 21 | |
| 22 | #ifndef _MALLOC_H |
| 23 | |
| 24 | #define _MALLOC_H 1 |
| 25 | |
| 26 | #ifndef __ONEFILE |
| 27 | #define __need_NULL |
| 28 | #define __need_size_t |
| 29 | #define __need_ptrdiff_t |
| 30 | #include <stddef.h> |
| 31 | #endif |
| 32 | |
| 33 | #ifdef _MALLOC_INTERNAL |
| 34 | |
| 35 | #ifndef __ONEFILE |
| 36 | #include <limits.h> |
| 37 | #endif |
| 38 | |
| 39 | /* The allocator divides the heap into blocks of fixed size; large |
| 40 | requests receive one or more whole blocks, and small requests |
| 41 | receive a fragment of a block. Fragment sizes are powers of two, |
| 42 | and all fragments of a block are the same size. When all the |
| 43 | fragments in a block have been freed, the block itself is freed. */ |
| 44 | #define INT_BIT (CHAR_BIT * sizeof(int)) |
| 45 | #define BLOCKLOG (INT_BIT > 16 ? 12 : 9) |
| 46 | #define BLOCKSIZE (1 << BLOCKLOG) |
| 47 | #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE) |
| 48 | |
| 49 | /* Determine the amount of memory spanned by the initial heap table |
| 50 | (not an absolute limit). */ |
| 51 | #define HEAP (INT_BIT > 16 ? 4194304 : 65536) |
| 52 | |
| 53 | /* Number of contiguous free blocks allowed to build up at the end of |
| 54 | memory before they will be returned to the system. */ |
| 55 | #define FINAL_FREE_BLOCKS 8 |
| 56 | |
| 57 | /* Where to start searching the free list when looking for new memory. |
| 58 | The two possible values are 0 and _heapindex. Starting at 0 seems |
| 59 | to reduce total memory usage, while starting at _heapindex seems to |
| 60 | run faster. */ |
| 61 | #define MALLOC_SEARCH_START _heapindex |
| 62 | |
| 63 | /* Data structure giving per-block information. */ |
| 64 | typedef union |
| 65 | { |
| 66 | /* Heap information for a busy block. */ |
| 67 | struct |
| 68 | { |
| 69 | /* Zero for a large block, or positive giving the |
| 70 | logarithm to the base two of the fragment size. */ |
| 71 | int type; |
| 72 | union |
| 73 | { |
| 74 | struct |
| 75 | { |
| 76 | size_t nfree; /* Free fragments in a fragmented block. */ |
| 77 | size_t first; /* First free fragment of the block. */ |
| 78 | } frag; |
| 79 | /* Size (in blocks) of a large cluster. */ |
| 80 | size_t size; |
| 81 | } info; |
| 82 | } busy; |
| 83 | /* Heap information for a free block (that may be the first of |
| 84 | a free cluster). */ |
| 85 | struct |
| 86 | { |
| 87 | size_t size; /* Size (in blocks) of a free cluster. */ |
| 88 | size_t next; /* Index of next free cluster. */ |
| 89 | size_t prev; /* Index of previous free cluster. */ |
| 90 | } free; |
| 91 | } malloc_info; |
| 92 | |
| 93 | /* Pointer to first block of the heap. */ |
| 94 | extern char *_heapbase; |
| 95 | |
| 96 | /* Table indexed by block number giving per-block information. */ |
| 97 | extern malloc_info *_heapinfo; |
| 98 | |
| 99 | /* Address to block number and vice versa. */ |
| 100 | #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1) |
| 101 | #define ADDRESS(B) ((PTR) (((B) - 1) * BLOCKSIZE + _heapbase)) |
| 102 | |
| 103 | /* Current search index for the heap table. */ |
| 104 | extern size_t _heapindex; |
| 105 | |
| 106 | /* Limit of valid info table indices. */ |
| 107 | extern size_t _heaplimit; |
| 108 | |
| 109 | /* Doubly linked lists of free fragments. */ |
| 110 | struct list |
| 111 | { |
| 112 | struct list *next; |
| 113 | struct list *prev; |
| 114 | }; |
| 115 | |
| 116 | /* Free list headers for each fragment size. */ |
| 117 | extern struct list _fraghead[]; |
| 118 | |
| 119 | /* Instrumentation. */ |
| 120 | extern size_t _chunks_used; |
| 121 | extern size_t _bytes_used; |
| 122 | extern size_t _chunks_free; |
| 123 | extern size_t _bytes_free; |
| 124 | |
| 125 | /* Internal version of free() used in morecore(). */ |
| 126 | extern void EXFUN(__free, (PTR __ptr)); |
| 127 | |
| 128 | #endif /* _MALLOC_INTERNAL. */ |
| 129 | |
| 130 | /* Underlying allocation function; successive calls should |
| 131 | return contiguous pieces of memory. */ |
| 132 | extern PTR EXFUN((*__morecore), (ptrdiff_t __size)); |
| 133 | |
| 134 | /* Default value of previous. */ |
| 135 | extern PTR EXFUN(__default_morecore, (ptrdiff_t __size)); |
| 136 | |
| 137 | /* Flag whether malloc has been called. */ |
| 138 | extern int __malloc_initialized; |
| 139 | |
| 140 | /* Hooks for debugging versions. */ |
| 141 | extern void EXFUN((*__free_hook), (PTR __ptr)); |
| 142 | extern PTR EXFUN((*__malloc_hook), (size_t __size)); |
| 143 | extern PTR EXFUN((*__realloc_hook), (PTR __ptr, size_t __size)); |
| 144 | |
| 145 | /* Activate a standard collection of debugging hooks. */ |
| 146 | extern void EXFUN(mcheck, (void EXFUN((*func), (NOARGS)))); |
| 147 | |
| 148 | /* Statistics available to the user. */ |
| 149 | struct mstats |
| 150 | { |
| 151 | size_t bytes_total; /* Total size of the heap. */ |
| 152 | size_t chunks_used; /* Chunks allocated by the user. */ |
| 153 | size_t bytes_used; /* Byte total of user-allocated chunks. */ |
| 154 | size_t chunks_free; /* Chunks in the free list. */ |
| 155 | size_t bytes_free; /* Byte total of chunks in the free list. */ |
| 156 | }; |
| 157 | |
| 158 | /* Pick up the current statistics. */ |
| 159 | extern struct mstats EXFUN(mstats, (NOARGS)); |
| 160 | |
| 161 | #endif /* malloc.h */ |