1 /* src/mm/memory.h - macros for memory management
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA
39 #include "mm/codememory.h"
40 #include "mm/dumpmemory.h"
43 /* constants for ENABLE_MEMCHECK **********************************************/
45 #if defined(ENABLE_MEMCHECK)
46 #define MEMORY_CANARY_SIZE 16
47 #define MEMORY_CANARY_FIRST_BYTE 0xca
48 #define MEMORY_CLEAR_BYTE 0xa5
49 #endif /* defined(ENABLE_MEMCHECK) */
52 /* internal includes **********************************************************/
54 #include "mm/gc-common.h"
58 ---------------------------- Interface description -----------------------
60 There are two possible choices for allocating memory:
62 1. explicit allocating / deallocating
64 mem_alloc ..... allocate a memory block
65 mem_free ...... free a memory block
66 mem_realloc ... change size of a memory block (position may change)
67 mem_usage ..... amount of allocated memory
69 There are some useful macros:
71 NEW (type) ....... allocate memory for an element of type `type`
72 FREE (ptr,type) .. free memory
74 MNEW (type,num) .. allocate memory for an array
75 MFREE (ptr,type,num) .. free memory
77 MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
79 -------------------------------------------------------------------------------
83 MEMORY_ALIGN (pos, size) ... make pos divisible by size. always returns an
87 OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
89 MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
94 #define MEMORY_ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
95 #define PADDING(pos,size) (MEMORY_ALIGN((pos),(size)) - (pos))
96 #define OFFSET(s,el) ((int32_t) ((ptrint) &(((s*) 0)->el)))
99 #define NEW(type) ((type *) mem_alloc(sizeof(type)))
100 #define FREE(ptr,type) mem_free((ptr), sizeof(type))
102 #define MNEW(type,num) ((type *) mem_alloc(sizeof(type) * (num)))
103 #define MFREE(ptr,type,num) mem_free((ptr), sizeof(type) * (num))
105 #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
106 sizeof(type) * (num2))
109 #define MCOPY(dest,src,type,num) memcpy((dest), (src), sizeof(type) * (num))
110 #define MSET(ptr,byte,type,num) memset((ptr), (byte), sizeof(type) * (num))
111 #define MZERO(ptr,type,num) MSET(ptr,0,type,num)
112 #define MMOVE(dest,src,type,num) memmove((dest), (src), sizeof(type) * (num))
115 /* GC macros (boehm only) *****************************************************/
117 #if defined(ENABLE_GC_BOEHM)
119 /* Uncollectable memory which can contain references */
121 #define GCNEW_UNCOLLECTABLE(type,num) ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
123 #define GCNEW(type) heap_alloc(sizeof(type), true, NULL, true)
124 #define GCMNEW(type,num) heap_alloc(sizeof(type) * (num), true, NULL, true)
126 #define GCFREE(ptr) heap_free((ptr))
131 /* function prototypes ********************************************************/
133 bool memory_init(void);
135 void memory_mprotect(void *addr, size_t len, int prot);
137 void *memory_checked_alloc(size_t size);
139 void *memory_cnew(int32_t size);
140 void memory_cfree(void *p, int32_t size);
142 void *mem_alloc(int32_t size);
143 void mem_free(void *m, int32_t size);
144 void *mem_realloc(void *src, int32_t len1, int32_t len2);
146 #if defined(ENABLE_THREADS)
147 bool memory_start_thread(void);
154 #endif /* _MEMORY_H */
158 * These are local overrides for various environment variables in Emacs.
159 * Please do not remove this and leave it at the end of the file, where
160 * Emacs will automagically detect them.
161 * ---------------------------------------------------------------------
164 * indent-tabs-mode: t
168 * vim:noexpandtab:sw=4:ts=4: