1 /* src/mm/memory.h - macros for memory management
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 /* forward typedefs ***********************************************************/
33 typedef struct dumpblock_t dumpblock_t;
34 typedef struct dumpinfo_t dumpinfo_t;
43 /* ATTENTION: We need to define dumpblock_t and dumpinfo_t before
44 internal includes, as we need dumpinfo_t as nested structure in
47 /* dumpblock ******************************************************************/
49 #define DUMPBLOCKSIZE 2 << 13 /* 2 * 8192 bytes */
59 /* dump_allocation *************************************************************
61 This struct is used to record dump memory allocations for ENABLE_MEMCHECK.
63 *******************************************************************************/
65 #if defined(ENABLE_MEMCHECK)
66 typedef struct dump_allocation_t dump_allocation_t;
68 struct dump_allocation_t {
69 dump_allocation_t *next;
77 /* dumpinfo *******************************************************************/
80 dumpblock_t *currentdumpblock; /* the current top-most block */
81 s4 allocateddumpsize; /* allocated bytes in this area */
82 s4 useddumpsize; /* used bytes in this dump area */
83 #if defined(ENABLE_MEMCHECK)
84 dump_allocation_t *allocations; /* list of allocations in this area */
89 /* internal includes **********************************************************/
91 #include "mm/gc-common.h"
95 ---------------------------- Interface description -----------------------
97 There are two possible choices for allocating memory:
99 1. explicit allocating / deallocating
101 mem_alloc ..... allocate a memory block
102 mem_free ...... free a memory block
103 mem_realloc ... change size of a memory block (position may change)
104 mem_usage ..... amount of allocated memory
107 2. explicit allocating, automatic deallocating
109 dump_alloc .... allocate a memory block in the dump area
110 dump_realloc .. change size of a memory block (position may change)
111 dump_size ..... marks the current top of dump
112 dump_release .. free all memory requested after the mark
115 There are some useful macros:
117 NEW (type) ....... allocate memory for an element of type `type`
118 FREE (ptr,type) .. free memory
120 MNEW (type,num) .. allocate memory for an array
121 MFREE (ptr,type,num) .. free memory
123 MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
125 These macros do the same except they operate on the dump area:
127 DNEW, DMNEW, DMREALLOC (there is no DFREE)
130 -------------------------------------------------------------------------------
134 MEMORY_ALIGN (pos, size) ... make pos divisible by size. always returns an
138 OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
140 MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
145 #define MEMORY_ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
146 #define PADDING(pos,size) (MEMORY_ALIGN((pos),(size)) - (pos))
147 #define OFFSET(s,el) ((s4) ((ptrint) &(((s*) 0)->el)))
150 #define NEW(type) ((type *) mem_alloc(sizeof(type)))
151 #define FREE(ptr,type) mem_free((ptr), sizeof(type))
153 #define MNEW(type,num) ((type *) mem_alloc(sizeof(type) * (num)))
154 #define MFREE(ptr,type,num) mem_free((ptr), sizeof(type) * (num))
156 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
157 sizeof(type) * (num2))
160 #define DNEW(type) ((type *) dump_alloc(sizeof(type)))
161 #define DMNEW(type,num) ((type *) dump_alloc(sizeof(type) * (num)))
162 #define DMREALLOC(ptr,type,num1,num2) dump_realloc((ptr), sizeof(type) * (num1), \
163 sizeof(type) * (num2))
165 #define MCOPY(dest,src,type,num) memcpy((dest), (src), sizeof(type) * (num))
166 #define MSET(ptr,byte,type,num) memset((ptr), (byte), sizeof(type) * (num))
167 #define MZERO(ptr,type,num) MSET(ptr,0,type,num)
168 #define MMOVE(dest,src,type,num) memmove((dest), (src), sizeof(type) * (num))
170 #define CNEW(type,num) ((type *) memory_cnew(sizeof(type) * (num)))
171 #define CFREE(ptr,num) memory_cfree((ptr),(num))
174 /* GC macros ******************************************************************/
176 /* Uncollectable memory which can contain references */
178 #define GCNEW_UNCOLLECTABLE(type,num) ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
180 #define GCNEW(type) heap_allocate(sizeof(type), true, NULL)
181 #define GCMNEW(type,num) heap_allocate(sizeof(type) * (num), true, NULL)
183 #define GCFREE(ptr) heap_free((ptr))
186 /* function prototypes ********************************************************/
188 /* initializes the memory subsystem */
189 bool memory_init(void);
191 void *memory_mmap_anon(void *addr, size_t len, int prot, int flags);
193 void *memory_cnew(s4 size);
194 void memory_cfree(void *p, s4 size);
196 void *mem_alloc(s4 size);
197 void mem_free(void *m, s4 size);
198 void *mem_realloc(void *src, s4 len1, s4 len2);
200 #if defined(ENABLE_THREADS)
201 bool memory_start_thread(void);
204 void *dump_alloc(s4 size);
205 void *dump_realloc(void *src, s4 len1, s4 len2);
207 void dump_release(s4 size);
209 #endif /* _MEMORY_H */
213 * These are local overrides for various environment variables in Emacs.
214 * Please do not remove this and leave it at the end of the file, where
215 * Emacs will automagically detect them.
216 * ---------------------------------------------------------------------
219 * indent-tabs-mode: t