1 /******************************** -*- C -*- **************************** 2 * 3 * Memory allocation for Smalltalk 4 * 5 * 6 ***********************************************************************/ 7 8 /*********************************************************************** 9 * 10 * Copyright 2002, 2004, 2006, 2007, 2008 Free Software Foundation, Inc. 11 * Written by Paolo Bonzini. 12 * 13 * This file is part of GNU Smalltalk. 14 * 15 * GNU Smalltalk is free software; you can redistribute it and/or modify it 16 * under the terms of the GNU General Public License as published by the Free 17 * Software Foundation; either version 2, or (at your option) any later 18 * version. 19 * 20 * Linking GNU Smalltalk statically or dynamically with other modules is 21 * making a combined work based on GNU Smalltalk. Thus, the terms and 22 * conditions of the GNU General Public License cover the whole 23 * combination. 24 * 25 * In addition, as a special exception, the Free Software Foundation 26 * give you permission to combine GNU Smalltalk with free software 27 * programs or libraries that are released under the GNU LGPL and with 28 * independent programs running under the GNU Smalltalk virtual machine. 29 * 30 * You may copy and distribute such a system following the terms of the 31 * GNU GPL for GNU Smalltalk and the licenses of the other code 32 * concerned, provided that you include the source code of that other 33 * code when and as the GNU GPL requires distribution of source code. 34 * 35 * Note that people who make modified versions of GNU Smalltalk are not 36 * obligated to grant this special exception for their modified 37 * versions; it is their choice whether to do so. The GNU General 38 * Public License gives permission to release a modified version without 39 * this exception; this exception also makes it possible to release a 40 * modified version which carries forward this exception. 41 * 42 * GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT 43 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 44 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 45 * more details. 46 * 47 * You should have received a copy of the GNU General Public License along with 48 * GNU Smalltalk; see the file COPYING. If not, write to the Free Software 49 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 50 ***********************************************************************/ 51 52 #ifndef GST_ALLOC_H 53 #define GST_ALLOC_H 54 55 #define NUM_FREELISTS 47 56 57 /* Oh my God, how broken things sometimes are! */ 58 #if defined small 59 # undef small 60 #endif 61 62 typedef struct heap_freeobj 63 { 64 struct heap_freeobj *next; 65 } 66 heap_freeobj; 67 68 typedef struct heap_block 69 { 70 size_t size; /* Size of objects in this block */ 71 unsigned mmap_block : 1; /* Whether the block was mmap-ed */ 72 unsigned user : 1; 73 unsigned pad : 30; /* needed for linux/m68k? */ 74 union { 75 struct { 76 int nr; /* Nr of objects in block */ 77 int avail; /* Nr of objects available in block */ 78 struct heap_freeobj *free; /* Next free sub-block */ 79 struct heap_block *nfree; /* Next block on sub-freelist */ 80 long double data[1]; /* Give appropriate alignment */ 81 } small; 82 struct { 83 struct heap_block *next; /* Next block in freelist */ 84 } free; 85 union { 86 long double align; /* Give appropriate alignment */ 87 char data[1]; 88 } large; 89 } var; 90 } 91 heap_block; 92 93 #define vSmall var.small 94 #define vLarge var.large 95 #define vFree var.free 96 97 typedef struct heap_data heap_data; 98 99 typedef void (*allocating_hook_t) (heap_data *, heap_block *, size_t); 100 typedef heap_data *(*nomemory_hook_t) (heap_data *, size_t); 101 102 struct heap_data 103 { 104 heap_block *freelist[NUM_FREELISTS]; 105 int mmap_count; 106 size_t heap_total, heap_allocation_size, heap_limit; 107 int probes, failures, splits, matches; 108 109 allocating_hook_t after_allocating, before_prim_freeing, after_prim_allocating; 110 nomemory_hook_t nomemory; 111 }; 112 113 114 /* Allocate a chunk of N bytes from the independent heap H. Invoke 115 the out-of-memory hook if the heap's limit is reached, and return 116 NULL if memory cannot be allocated even after the hook returned. */ 117 extern PTR _gst_mem_alloc (heap_data *h, size_t n) 118 ATTRIBUTE_HIDDEN; 119 120 /* Free the memory chunk pointed to by P, which was allocated from the 121 independent heap H. */ 122 extern void _gst_mem_free (heap_data *h, PTR p) 123 ATTRIBUTE_HIDDEN; 124 125 /* Resize the memory chunk pointed to by P, which was allocated from the 126 independent heap H, so that its size becomes N. Return the new 127 pointer. Invoke the out-of-memory hook if the heap's limit is reached, 128 and return NULL if memory cannot be allocated even after the hook 129 returned. */ 130 extern PTR _gst_mem_realloc (heap_data *h, PTR p, size_t n) 131 ATTRIBUTE_HIDDEN; 132 133 /* Allocate a new, independent heap which allocates from the OS chunks 134 of HEAP_ALLOCATION_SIZE bytes, up to a limit of HEAP_LIMIT bytes. */ 135 extern heap_data *_gst_mem_new_heap (size_t heap_allocation_size, 136 size_t heap_limit) 137 ATTRIBUTE_HIDDEN; 138 139 /* Allocate a chunk of N bytes using malloc. Exit if this amount of 140 memory cannot be allocated. */ 141 extern PTR xmalloc (size_t n) 142 ATTRIBUTE_HIDDEN; 143 144 /* Allocate a chunk of S*N bytes using malloc, clear it and return a pointer 145 to its base. Exit if memory cannot be allocated. */ 146 extern PTR xcalloc (size_t n, size_t s) 147 ATTRIBUTE_HIDDEN; 148 149 /* Resize the memory chunk pointed to by P, which was allocated using 150 malloc, so that its size becomes N. Return the new pointer, or exit 151 if the memory cannot be allocated. */ 152 extern PTR xrealloc (PTR p, size_t n) 153 ATTRIBUTE_HIDDEN; 154 155 /* Allocate memory for a copy of the null-terminated string S using malloc, 156 duplicate the contents of S, and return the pointer to the copy. Exit 157 if the memory cannot be allocated. */ 158 extern char *xstrdup (const char *s) 159 ATTRIBUTE_HIDDEN; 160 161 /* Free the chunk pointed to by P, which was allocated using malloc. */ 162 extern void xfree (PTR p) 163 ATTRIBUTE_HIDDEN; 164 165 /* Print an error message, and exit if FATAL is non-zero. */ 166 extern void nomemory (int fatal) 167 ATTRIBUTE_HIDDEN; 168 169 #define obstack_chunk_alloc xmalloc 170 #define obstack_chunk_free xfree 171 172 #endif 173