OLD | NEW |
1 // Copyright (c) 2014, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dartino project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 #include "src/vm/object_memory.h" | 5 #include "src/vm/object_memory.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 | 9 |
10 #include "src/shared/assert.h" | 10 #include "src/shared/assert.h" |
11 #include "src/shared/platform.h" | 11 #include "src/shared/platform.h" |
12 #include "src/shared/utils.h" | 12 #include "src/shared/utils.h" |
13 | 13 |
14 #include "src/vm/frame.h" | 14 #include "src/vm/frame.h" |
15 #include "src/vm/heap.h" | 15 #include "src/vm/heap.h" |
16 #include "src/vm/mark_sweep.h" | 16 #include "src/vm/mark_sweep.h" |
17 #include "src/vm/object.h" | 17 #include "src/vm/object.h" |
18 | 18 |
19 #ifdef FLETCH_TARGET_OS_LK | 19 #ifdef DARTINO_TARGET_OS_LK |
20 #include "lib/page_alloc.h" | 20 #include "lib/page_alloc.h" |
21 #endif | 21 #endif |
22 | 22 |
23 #ifdef FLETCH_TARGET_OS_CMSIS | 23 #ifdef DARTINO_TARGET_OS_CMSIS |
24 // TODO(sgjesse): Put this into an .h file | 24 // TODO(sgjesse): Put this into an .h file |
25 #define PAGE_SIZE_SHIFT 12 | 25 #define PAGE_SIZE_SHIFT 12 |
26 #define PAGE_SIZE (1 << PAGE_SIZE_SHIFT) | 26 #define PAGE_SIZE (1 << PAGE_SIZE_SHIFT) |
27 extern "C" void* page_alloc(size_t pages); | 27 extern "C" void* page_alloc(size_t pages); |
28 extern "C" void page_free(void* start, size_t pages); | 28 extern "C" void page_free(void* start, size_t pages); |
29 #endif | 29 #endif |
30 | 30 |
31 namespace fletch { | 31 namespace dartino { |
32 | 32 |
33 static Smi* chunk_end_sentinel() { return Smi::zero(); } | 33 static Smi* chunk_end_sentinel() { return Smi::zero(); } |
34 | 34 |
35 static bool HasSentinelAt(uword address) { | 35 static bool HasSentinelAt(uword address) { |
36 return *reinterpret_cast<Object**>(address) == chunk_end_sentinel(); | 36 return *reinterpret_cast<Object**>(address) == chunk_end_sentinel(); |
37 } | 37 } |
38 | 38 |
39 Chunk::~Chunk() { | 39 Chunk::~Chunk() { |
40 // If the memory for this chunk is external we leave it alone | 40 // If the memory for this chunk is external we leave it alone |
41 // and let the embedder deallocate it. | 41 // and let the embedder deallocate it. |
42 if (is_external()) return; | 42 if (is_external()) return; |
43 #if defined(FLETCH_TARGET_OS_CMSIS) || defined(FLETCH_TARGET_OS_LK) | 43 #if defined(DARTINO_TARGET_OS_CMSIS) || defined(DARTINO_TARGET_OS_LK) |
44 page_free(reinterpret_cast<void*>(base()), size() >> PAGE_SIZE_SHIFT); | 44 page_free(reinterpret_cast<void*>(base()), size() >> PAGE_SIZE_SHIFT); |
45 #elif defined(FLETCH_TARGET_OS_WIN) | 45 #elif defined(DARTINO_TARGET_OS_WIN) |
46 _aligned_free(reinterpret_cast<void*>(base())); | 46 _aligned_free(reinterpret_cast<void*>(base())); |
47 #else | 47 #else |
48 free(reinterpret_cast<void*>(base())); | 48 free(reinterpret_cast<void*>(base())); |
49 #endif | 49 #endif |
50 } | 50 } |
51 | 51 |
52 Space::~Space() { FreeAllChunks(); } | 52 Space::~Space() { FreeAllChunks(); } |
53 | 53 |
54 void Space::FreeAllChunks() { | 54 void Space::FreeAllChunks() { |
55 Chunk* current = first(); | 55 Chunk* current = first(); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 } | 152 } |
153 | 153 |
154 void PageDirectory::Delete() { | 154 void PageDirectory::Delete() { |
155 for (unsigned i = 0; i < ARRAY_SIZE(tables_); i++) { | 155 for (unsigned i = 0; i < ARRAY_SIZE(tables_); i++) { |
156 delete tables_[i]; | 156 delete tables_[i]; |
157 } | 157 } |
158 Clear(); | 158 Clear(); |
159 } | 159 } |
160 | 160 |
161 Mutex* ObjectMemory::mutex_; | 161 Mutex* ObjectMemory::mutex_; |
162 #ifdef FLETCH32 | 162 #ifdef DARTINO32 |
163 PageDirectory ObjectMemory::page_directory_; | 163 PageDirectory ObjectMemory::page_directory_; |
164 #else | 164 #else |
165 PageDirectory* ObjectMemory::page_directories_[1 << 13]; | 165 PageDirectory* ObjectMemory::page_directories_[1 << 13]; |
166 #endif | 166 #endif |
167 Atomic<uword> ObjectMemory::allocated_; | 167 Atomic<uword> ObjectMemory::allocated_; |
168 | 168 |
169 void ObjectMemory::Setup() { | 169 void ObjectMemory::Setup() { |
170 mutex_ = Platform::CreateMutex(); | 170 mutex_ = Platform::CreateMutex(); |
171 allocated_ = 0; | 171 allocated_ = 0; |
172 #ifdef FLETCH32 | 172 #ifdef DARTINO32 |
173 page_directory_.Clear(); | 173 page_directory_.Clear(); |
174 #else | 174 #else |
175 memset(&page_directories_, 0, kPointerSize * ARRAY_SIZE(page_directories_)); | 175 memset(&page_directories_, 0, kPointerSize * ARRAY_SIZE(page_directories_)); |
176 #endif | 176 #endif |
177 } | 177 } |
178 | 178 |
179 void ObjectMemory::TearDown() { | 179 void ObjectMemory::TearDown() { |
180 #ifdef FLETCH32 | 180 #ifdef DARTINO32 |
181 page_directory_.Delete(); | 181 page_directory_.Delete(); |
182 #else | 182 #else |
183 for (unsigned i = 0; i < ARRAY_SIZE(page_directories_); i++) { | 183 for (unsigned i = 0; i < ARRAY_SIZE(page_directories_); i++) { |
184 PageDirectory* directory = page_directories_[i]; | 184 PageDirectory* directory = page_directories_[i]; |
185 if (directory == NULL) continue; | 185 if (directory == NULL) continue; |
186 directory->Delete(); | 186 directory->Delete(); |
187 page_directories_[i] = NULL; | 187 page_directories_[i] = NULL; |
188 delete directory; | 188 delete directory; |
189 } | 189 } |
190 #endif | 190 #endif |
(...skipping 23 matching lines...) Expand all Loading... |
214 | 214 |
215 Chunk* ObjectMemory::AllocateChunk(Space* owner, int size) { | 215 Chunk* ObjectMemory::AllocateChunk(Space* owner, int size) { |
216 ASSERT(owner != NULL); | 216 ASSERT(owner != NULL); |
217 | 217 |
218 size = Utils::RoundUp(size, kPageSize); | 218 size = Utils::RoundUp(size, kPageSize); |
219 void* memory; | 219 void* memory; |
220 #if defined(__ANDROID__) | 220 #if defined(__ANDROID__) |
221 // posix_memalign doesn't exist on Android. We fallback to | 221 // posix_memalign doesn't exist on Android. We fallback to |
222 // memalign. | 222 // memalign. |
223 memory = memalign(kPageSize, size); | 223 memory = memalign(kPageSize, size); |
224 #elif defined(FLETCH_TARGET_OS_WIN) | 224 #elif defined(DARTINO_TARGET_OS_WIN) |
225 memory = _aligned_malloc(size, kPageSize); | 225 memory = _aligned_malloc(size, kPageSize); |
226 #elif defined(FLETCH_TARGET_OS_LK) || defined(FLETCH_TARGET_OS_CMSIS) | 226 #elif defined(DARTINO_TARGET_OS_LK) || defined(DARTINO_TARGET_OS_CMSIS) |
227 size = Utils::RoundUp(size, PAGE_SIZE); | 227 size = Utils::RoundUp(size, PAGE_SIZE); |
228 memory = page_alloc(size >> PAGE_SIZE_SHIFT); | 228 memory = page_alloc(size >> PAGE_SIZE_SHIFT); |
229 #else | 229 #else |
230 if (posix_memalign(&memory, kPageSize, size) != 0) return NULL; | 230 if (posix_memalign(&memory, kPageSize, size) != 0) return NULL; |
231 #endif | 231 #endif |
232 if (memory == NULL) return NULL; | 232 if (memory == NULL) return NULL; |
233 | 233 |
234 uword base = reinterpret_cast<uword>(memory); | 234 uword base = reinterpret_cast<uword>(memory); |
235 Chunk* chunk = new Chunk(owner, base, size); | 235 Chunk* chunk = new Chunk(owner, base, size); |
236 | 236 |
(...skipping 29 matching lines...) Expand all Loading... |
266 allocated_ -= chunk->size(); | 266 allocated_ -= chunk->size(); |
267 delete chunk; | 267 delete chunk; |
268 } | 268 } |
269 | 269 |
270 bool ObjectMemory::IsAddressInSpace(uword address, const Space* space) { | 270 bool ObjectMemory::IsAddressInSpace(uword address, const Space* space) { |
271 PageTable* table = GetPageTable(address); | 271 PageTable* table = GetPageTable(address); |
272 return (table != NULL) ? table->Get((address >> 12) & 0x3ff) == space : false; | 272 return (table != NULL) ? table->Get((address >> 12) & 0x3ff) == space : false; |
273 } | 273 } |
274 | 274 |
275 PageTable* ObjectMemory::GetPageTable(uword address) { | 275 PageTable* ObjectMemory::GetPageTable(uword address) { |
276 #ifdef FLETCH32 | 276 #ifdef DARTINO32 |
277 return page_directory_.Get(address >> 22); | 277 return page_directory_.Get(address >> 22); |
278 #else | 278 #else |
279 PageDirectory* directory = page_directories_[address >> 35]; | 279 PageDirectory* directory = page_directories_[address >> 35]; |
280 if (directory == NULL) return NULL; | 280 if (directory == NULL) return NULL; |
281 return directory->Get((address >> 22) & 0x1fff); | 281 return directory->Get((address >> 22) & 0x1fff); |
282 #endif | 282 #endif |
283 } | 283 } |
284 | 284 |
285 void ObjectMemory::SetPageTable(uword address, PageTable* table) { | 285 void ObjectMemory::SetPageTable(uword address, PageTable* table) { |
286 #ifdef FLETCH32 | 286 #ifdef DARTINO32 |
287 page_directory_.Set(address >> 22, table); | 287 page_directory_.Set(address >> 22, table); |
288 #else | 288 #else |
289 int index = address >> 35; | 289 int index = address >> 35; |
290 PageDirectory* directory = page_directories_[index]; | 290 PageDirectory* directory = page_directories_[index]; |
291 if (directory == NULL) { | 291 if (directory == NULL) { |
292 page_directories_[index] = directory = new PageDirectory(); | 292 page_directories_[index] = directory = new PageDirectory(); |
293 directory->Clear(); | 293 directory->Clear(); |
294 } | 294 } |
295 return directory->Set((address >> 22) & 0x1fff, table); | 295 return directory->Set((address >> 22) & 0x1fff, table); |
296 #endif | 296 #endif |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 StaticClassStructures::one_word_filler_class(); | 349 StaticClassStructures::one_word_filler_class(); |
350 } | 350 } |
351 current += Instance::kSize; | 351 current += Instance::kSize; |
352 } else { | 352 } else { |
353 current += object->Size(); | 353 current += object->Size(); |
354 } | 354 } |
355 } | 355 } |
356 } | 356 } |
357 } | 357 } |
358 | 358 |
359 } // namespace fletch | 359 } // namespace dartino |
OLD | NEW |