1 /* src/mm/memory.h - macros for memory management
3 Copyright (C) 1996-2005, 2006 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
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
29 Changes: Christian Thalinger
31 $Id: memory.h 7615 2007-03-29 23:10:59Z michi $
39 /* forward typedefs ***********************************************************/
41 typedef struct dumpblock_t dumpblock_t;
42 typedef struct dumpinfo_t dumpinfo_t;
51 /* ATTENTION: We need to define dumpblock_t and dumpinfo_t before
52 internal includes, as we need dumpinfo_t as nested structure in
55 /* dumpblock ******************************************************************/
57 #define DUMPBLOCKSIZE 2 << 13 /* 2 * 8192 bytes */
67 /* dump_allocation *************************************************************
69 This struct is used to record dump memory allocations for ENABLE_MEMCHECK.
71 *******************************************************************************/
73 #if defined(ENABLE_MEMCHECK)
74 typedef struct dump_allocation_t dump_allocation_t;
76 struct dump_allocation_t {
77 dump_allocation_t *next;
85 /* dumpinfo *******************************************************************/
88 dumpblock_t *currentdumpblock; /* the current top-most block */
89 s4 allocateddumpsize; /* allocated bytes in this area */
90 s4 useddumpsize; /* used bytes in this dump area */
91 #if defined(ENABLE_MEMCHECK)
92 dump_allocation_t *allocations; /* list of allocations in this area */
97 /* constants for ENABLE_MEMCHECK **********************************************/
99 #if defined(ENABLE_MEMCHECK)
100 #define MEMORY_CANARY_SIZE 16
101 #define MEMORY_CANARY_FIRST_BYTE 0xca
102 #define MEMORY_CLEAR_BYTE 0xa5
103 #endif /* defined(ENABLE_MEMCHECK) */
106 /* internal includes **********************************************************/
108 #include "mm/gc-common.h"
112 ---------------------------- Interface description -----------------------
114 There are two possible choices for allocating memory:
116 1. explicit allocating / deallocating
118 mem_alloc ..... allocate a memory block
119 mem_free ...... free a memory block
120 mem_realloc ... change size of a memory block (position may change)
121 mem_usage ..... amount of allocated memory
124 2. explicit allocating, automatic deallocating
126 dump_alloc .... allocate a memory block in the dump area
127 dump_realloc .. change size of a memory block (position may change)
128 dump_size ..... marks the current top of dump
129 dump_release .. free all memory requested after the mark
132 There are some useful macros:
134 NEW (type) ....... allocate memory for an element of type `type`
135 FREE (ptr,type) .. free memory
137 MNEW (type,num) .. allocate memory for an array
138 MFREE (ptr,type,num) .. free memory
140 MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
142 These macros do the same except they operate on the dump area:
144 DNEW, DMNEW, DMREALLOC (there is no DFREE)
147 -------------------------------------------------------------------------------
151 MEMORY_ALIGN (pos, size) ... make pos divisible by size. always returns an
155 OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
157 MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
162 #define MEMORY_ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
163 #define PADDING(pos,size) (MEMORY_ALIGN((pos),(size)) - (pos))
164 #define OFFSET(s,el) ((s4) ((ptrint) &(((s*) 0)->el)))
167 #define NEW(type) ((type *) mem_alloc(sizeof(type)))
168 #define FREE(ptr,type) mem_free((ptr), sizeof(type))
170 #define MNEW(type,num) ((type *) mem_alloc(sizeof(type) * (num)))
171 #define MFREE(ptr,type,num) mem_free((ptr), sizeof(type) * (num))
173 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
174 sizeof(type) * (num2))
177 #define DNEW(type) ((type *) dump_alloc(sizeof(type)))
178 #define DMNEW(type,num) ((type *) dump_alloc(sizeof(type) * (num)))
179 #define DMREALLOC(ptr,type,num1,num2) dump_realloc((ptr), sizeof(type) * (num1), \
180 sizeof(type) * (num2))
182 #define MCOPY(dest,src,type,num) memcpy((dest), (src), sizeof(type) * (num))
183 #define MSET(ptr,byte,type,num) memset((ptr), (byte), sizeof(type) * (num))
184 #define MZERO(ptr,type,num) MSET(ptr,0,type,num)
185 #define MMOVE(dest,src,type,num) memmove((dest), (src), sizeof(type) * (num))
187 #define CNEW(type,num) ((type *) memory_cnew(sizeof(type) * (num)))
188 #define CFREE(ptr,num) memory_cfree((ptr),(num))
191 /* GC macros ******************************************************************/
193 #if !defined(ENABLE_GC_CACAO)
195 /* Uncollectable memory which can contain references */
197 #define GCNEW_UNCOLLECTABLE(type,num) ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
199 #define GCNEW(type) heap_alloc(sizeof(type), true, NULL, true)
200 #define GCMNEW(type,num) heap_alloc(sizeof(type) * (num), true, NULL, true)
202 #define GCFREE(ptr) heap_free((ptr))
207 /* function prototypes ********************************************************/
209 /* initializes the memory subsystem */
210 bool memory_init(void);
212 void *memory_mmap_anon(void *addr, size_t len, int prot, int flags);
214 void *memory_cnew(s4 size);
215 void memory_cfree(void *p, s4 size);
217 void *mem_alloc(s4 size);
218 void mem_free(void *m, s4 size);
219 void *mem_realloc(void *src, s4 len1, s4 len2);
221 #if defined(ENABLE_THREADS)
222 bool memory_start_thread(void);
225 void *dump_alloc(s4 size);
226 void *dump_realloc(void *src, s4 len1, s4 len2);
228 void dump_release(s4 size);
230 #endif /* _MEMORY_H */
234 * These are local overrides for various environment variables in Emacs.
235 * Please do not remove this and leave it at the end of the file, where
236 * Emacs will automagically detect them.
237 * ---------------------------------------------------------------------
240 * indent-tabs-mode: t
244 * vim:noexpandtab:sw=4:ts=4: