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 5868 2006-10-30 11:21:36Z edwin $
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 /* dumpinfo *******************************************************************/
70 dumpblock_t *currentdumpblock;
76 /* internal includes **********************************************************/
82 ---------------------------- Interface description -----------------------
84 There are two possible choices for allocating memory:
86 1. explicit allocating / deallocating
88 mem_alloc ..... allocate a memory block
89 mem_free ...... free a memory block
90 mem_realloc ... change size of a memory block (position may change)
91 mem_usage ..... amount of allocated memory
94 2. explicit allocating, automatic deallocating
96 dump_alloc .... allocate a memory block in the dump area
97 dump_realloc .. change size of a memory block (position may change)
98 dump_size ..... marks the current top of dump
99 dump_release .. free all memory requested after the mark
102 There are some useful macros:
104 NEW (type) ....... allocate memory for an element of type `type`
105 FREE (ptr,type) .. free memory
107 MNEW (type,num) .. allocate memory for an array
108 MFREE (ptr,type,num) .. free memory
110 MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
112 These macros do the same except they operate on the dump area:
114 DNEW, DMNEW, DMREALLOC (there is no DFREE)
117 -------------------------------------------------------------------------------
121 MEMORY_ALIGN (pos, size) ... make pos divisible by size. always returns an
125 OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
127 MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
132 #define MEMORY_ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
133 #define PADDING(pos,size) (MEMORY_ALIGN((pos),(size)) - (pos))
134 #define OFFSET(s,el) ((s4) ((ptrint) &(((s*) 0)->el)))
136 #if !defined(DISABLE_GC)
138 #define NEW(type) ((type *) mem_alloc(sizeof(type)))
139 #define FREE(ptr,type) mem_free((ptr), sizeof(type))
141 #define MNEW(type,num) ((type *) mem_alloc(sizeof(type) * (num)))
142 #define MFREE(ptr,type,num) mem_free((ptr), sizeof(type) * (num))
144 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
145 sizeof(type) * (num2))
149 #define NEW(type) GCNEW(type)
150 #define FREE(ptr,type) GCFREE(ptr)
152 #define MNEW(type,num) GCMNEW(type,num)
153 #define MFREE(ptr,type,num) GCFREE(ptr)
155 #define MREALLOC(ptr,type,num1,num2) nogc_realloc((ptr), sizeof(type) * (num1), \
156 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_cnew(s4 size);
192 void memory_cfree(void *p, s4 size);
194 void *mem_alloc(s4 size);
195 void mem_free(void *m, s4 size);
196 void *mem_realloc(void *src, s4 len1, s4 len2);
198 void *dump_alloc(s4 size);
199 void *dump_realloc(void *src, s4 len1, s4 len2);
201 void dump_release(s4 size);
203 #endif /* _MEMORY_H */
207 * These are local overrides for various environment variables in Emacs.
208 * Please do not remove this and leave it at the end of the file, where
209 * Emacs will automagically detect them.
210 * ---------------------------------------------------------------------
213 * indent-tabs-mode: t