Line data Source code
1 : /* src/mm/memory.hpp - macros for memory management
2 :
3 : Copyright (C) 1996-2013
4 : CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 :
6 : This file is part of CACAO.
7 :
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.
12 :
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.
17 :
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
21 : 02110-1301, USA.
22 :
23 : */
24 :
25 :
26 : #ifndef MEMORY_HPP_
27 : #define MEMORY_HPP_ 1
28 :
29 : #include "config.h"
30 :
31 : #include <stdint.h>
32 : #include <cstddef> // for size_t
33 : #include <cstring> // for memset
34 :
35 : // Align the size of memory allocations to this size.
36 : #define ALIGNSIZE 8
37 : #define MEMORY_ALIGN(pos,size) ((((pos) + (size) - 1) / (size)) * (size))
38 :
39 :
40 : // Constants for ENABLE_MEMCHECK.
41 :
42 : #if defined(ENABLE_MEMCHECK)
43 : #define MEMORY_CANARY_SIZE 16
44 : #define MEMORY_CANARY_FIRST_BYTE 0xca
45 : #define MEMORY_CLEAR_BYTE 0xa5
46 : #endif
47 :
48 :
49 : // Includes.
50 : //#include "mm/dumpmemory.hpp"
51 :
52 : /*
53 : ---------------------------- Interface description -----------------------
54 :
55 : There are two possible choices for allocating memory:
56 :
57 : 1. explicit allocating / deallocating
58 :
59 : mem_alloc ..... allocate a memory block
60 : mem_free ...... free a memory block
61 : mem_realloc ... change size of a memory block (position may change)
62 : mem_usage ..... amount of allocated memory
63 :
64 : There are some useful macros:
65 :
66 : NEW (type) ....... allocate memory for an element of type `type`
67 : FREE (ptr,type) .. free memory
68 :
69 : MNEW (type,num) .. allocate memory for an array
70 : MFREE (ptr,type,num) .. free memory
71 :
72 : MREALLOC (ptr,type,num1,num2) .. enlarge the array to size num2
73 :
74 : -------------------------------------------------------------------------------
75 :
76 : Some more macros:
77 :
78 : MEMORY_ALIGN (pos, size) ... make pos divisible by size. always returns an
79 : address >= pos.
80 :
81 :
82 : OFFSET (s,el) ....... returns the offset of 'el' in structure 's' in bytes.
83 :
84 : MCOPY (dest,src,type,num) ... copy 'num' elements of type 'type'.
85 :
86 :
87 : */
88 :
89 : #define PADDING(pos,size) (MEMORY_ALIGN((pos),(size)) - (pos))
90 : #define OFFSET(s,el) ((int32_t) ((ptrint) &(((s*) 0)->el)))
91 :
92 :
93 : #define NEW(type) ((type *) mem_alloc(sizeof(type)))
94 : #define FREE(ptr,type) mem_free((ptr), sizeof(type))
95 :
96 : #define MNEW(type,num) ((type *) mem_alloc(sizeof(type) * (num)))
97 : #define MFREE(ptr,type,num) mem_free((ptr), sizeof(type) * (num))
98 :
99 : #define MREALLOC(ptr,type,num1,num2) mem_realloc((ptr), sizeof(type) * (num1), \
100 : sizeof(type) * (num2))
101 :
102 :
103 : #define MCOPY(dest,src,type,num) std::memcpy((dest), (src), sizeof(type) * (num))
104 : #define MSET(ptr,byte,type,num) std::memset((ptr), (byte), sizeof(type) * (num))
105 : #define MZERO(ptr,type,num) MSET(ptr,0,type,num)
106 : #define MMOVE(dest,src,type,num) std::memmove((dest), (src), sizeof(type) * (num))
107 :
108 :
109 : /* GC macros (boehm only) *****************************************************/
110 :
111 : #if defined(ENABLE_GC_BOEHM)
112 :
113 : /* Uncollectable memory which can contain references */
114 :
115 : #define GCNEW_UNCOLLECTABLE(type,num) ((type *) heap_alloc_uncollectable(sizeof(type) * (num)))
116 :
117 : #define GCNEW(type) heap_alloc(sizeof(type), true, NULL, true)
118 : #define GCMNEW(type,num) heap_alloc(sizeof(type) * (num), true, NULL, true)
119 :
120 : #define GCFREE(ptr) heap_free((ptr))
121 :
122 : #endif
123 :
124 :
125 : /* function prototypes ********************************************************/
126 :
127 : bool memory_init(void);
128 :
129 : void memory_mprotect(void *addr, size_t len, int prot);
130 :
131 : void *memory_checked_alloc(size_t size);
132 :
133 : void *memory_cnew(int32_t size);
134 : void memory_cfree(void *p, int32_t size);
135 :
136 : void *mem_alloc(int32_t size);
137 : void mem_free(void *m, int32_t size);
138 : void *mem_realloc(void *src, int32_t len1, int32_t len2);
139 :
140 : bool memory_start_thread(void);
141 :
142 : // **** a stl style memory allocator
143 : template<class T>
144 : class MemoryAllocator {
145 : public:
146 : // Type definitions.
147 : typedef T value_type;
148 : typedef T* pointer;
149 : typedef const T* const_pointer;
150 : typedef T& reference;
151 : typedef const T& const_reference;
152 : typedef std::size_t size_type;
153 : typedef std::ptrdiff_t difference_type;
154 :
155 : // Constructors and destructor, nothing to do because the
156 : // allocator has no state.
157 249790 : MemoryAllocator() throw() {}
158 : MemoryAllocator(const MemoryAllocator&) throw() {}
159 : template <class U> MemoryAllocator(const MemoryAllocator<U>&) throw() {}
160 :
161 248322 : ~MemoryAllocator() throw() {}
162 :
163 : // ** Return address
164 : inline pointer address ( reference x ) const { return &x; }
165 : inline const_pointer address ( const_reference x ) const { return &x; }
166 :
167 : // ** Allocate block of storage
168 250470 : inline pointer allocate(size_type n, const_pointer hint=0)
169 : {
170 250470 : return static_cast<pointer>(mem_alloc(n * sizeof(T)));
171 : }
172 :
173 : // ** Reallocate block of storage (non-standard!)
174 63873 : inline pointer reallocate(pointer p, size_type old_sz, size_type new_sz)
175 : {
176 63873 : return static_cast<pointer>(mem_realloc(p, old_sz * sizeof(T), new_sz * sizeof(T)));
177 : }
178 :
179 : // ** Release block of storage
180 250306 : inline void deallocate(pointer p, size_type n)
181 : {
182 250306 : mem_free(p,n);
183 250306 : }
184 :
185 : // ** Maximum size possible to allocate
186 : // TODO
187 :
188 : // ** Construct an object
189 : void construct(pointer p, const T& value) {
190 : new ((void*) p) T(value);
191 : }
192 :
193 : // ** Destroy an object
194 : void destroy(pointer p) {
195 : p->~T();
196 : }
197 : };
198 :
199 : /// Allow operator new to allocate with mem_alloc
200 : enum MemAllocPlacement { MemAlloc };
201 :
202 4244 : inline void *operator new(size_t size, MemAllocPlacement) {
203 4244 : return mem_alloc(size);
204 : }
205 :
206 : #endif // MEMORY_HPP_
207 :
208 :
209 : /*
210 : * These are local overrides for various environment variables in Emacs.
211 : * Please do not remove this and leave it at the end of the file, where
212 : * Emacs will automagically detect them.
213 : * ---------------------------------------------------------------------
214 : * Local variables:
215 : * mode: c++
216 : * indent-tabs-mode: t
217 : * c-basic-offset: 4
218 : * tab-width: 4
219 : * End:
220 : * vim:noexpandtab:sw=4:ts=4:
221 : */
|