summaryrefslogtreecommitdiff
path: root/src/common/mem_arena.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2014-04-08 19:25:03 -0400
committerGravatar bunnei2014-04-08 19:25:03 -0400
commit63e46abdb8764bc97e91bae862c8d461e61b1965 (patch)
treee73f4aa25d7b4015a265e7bbfb6004dab7561027 /src/common/mem_arena.cpp
parentfixed some license headers that I missed (diff)
downloadyuzu-63e46abdb8764bc97e91bae862c8d461e61b1965.tar.gz
yuzu-63e46abdb8764bc97e91bae862c8d461e61b1965.tar.xz
yuzu-63e46abdb8764bc97e91bae862c8d461e61b1965.zip
got rid of 'src' folders in each sub-project
Diffstat (limited to 'src/common/mem_arena.cpp')
-rw-r--r--src/common/mem_arena.cpp477
1 files changed, 477 insertions, 0 deletions
diff --git a/src/common/mem_arena.cpp b/src/common/mem_arena.cpp
new file mode 100644
index 000000000..1a6fcf44d
--- /dev/null
+++ b/src/common/mem_arena.cpp
@@ -0,0 +1,477 @@
1// Copyright (C) 2003 Dolphin Project.
2
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, version 2.0 or later versions.
6
7// This program is distributed in the hope that it will be useful,
8// but WITHOUT ANY WARRANTY; without even the implied warranty of
9// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10// GNU General Public License 2.0 for more details.
11
12// A copy of the GPL 2.0 should have been included with the program.
13// If not, see http://www.gnu.org/licenses/
14
15// Official SVN repository and contact information can be found at
16// http://code.google.com/p/dolphin-emu/
17
18#include <string>
19
20#include "memory_util.h"
21#include "mem_arena.h"
22
23#ifdef _WIN32
24//#include "CommonWindows.h"
25#else
26#include <sys/stat.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <cerrno>
30#include <cstring>
31#ifdef ANDROID
32#include <sys/ioctl.h>
33#include <linux/ashmem.h>
34#endif
35#endif
36
37#ifdef IOS
38void* globalbase = NULL;
39#endif
40
41#ifdef ANDROID
42
43// Hopefully this ABI will never change...
44
45
46#define ASHMEM_DEVICE "/dev/ashmem"
47
48/*
49* ashmem_create_region - creates a new ashmem region and returns the file
50* descriptor, or <0 on error
51*
52* `name' is an optional label to give the region (visible in /proc/pid/maps)
53* `size' is the size of the region, in page-aligned bytes
54*/
55int ashmem_create_region(const char *name, size_t size)
56{
57 int fd, ret;
58
59 fd = open(ASHMEM_DEVICE, O_RDWR);
60 if (fd < 0)
61 return fd;
62
63 if (name) {
64 char buf[ASHMEM_NAME_LEN];
65
66 strncpy(buf, name, sizeof(buf));
67 ret = ioctl(fd, ASHMEM_SET_NAME, buf);
68 if (ret < 0)
69 goto error;
70 }
71
72 ret = ioctl(fd, ASHMEM_SET_SIZE, size);
73 if (ret < 0)
74 goto error;
75
76 return fd;
77
78error:
79 ERROR_LOG(MEMMAP, "NASTY ASHMEM ERROR: ret = %08x", ret);
80 close(fd);
81 return ret;
82}
83
84int ashmem_set_prot_region(int fd, int prot)
85{
86 return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);
87}
88
89int ashmem_pin_region(int fd, size_t offset, size_t len)
90{
91 struct ashmem_pin pin = { offset, len };
92 return ioctl(fd, ASHMEM_PIN, &pin);
93}
94
95int ashmem_unpin_region(int fd, size_t offset, size_t len)
96{
97 struct ashmem_pin pin = { offset, len };
98 return ioctl(fd, ASHMEM_UNPIN, &pin);
99}
100#endif // Android
101
102
103
104#ifndef _WIN32
105// do not make this "static"
106#if defined(MAEMO) || defined(MEEGO_EDITION_HARMATTAN)
107std::string ram_temp_file = "/home/user/gc_mem.tmp";
108#else
109std::string ram_temp_file = "/tmp/gc_mem.tmp";
110#endif
111#elif !defined(_XBOX)
112SYSTEM_INFO sysInfo;
113#endif
114
115
116// Windows mappings need to be on 64K boundaries, due to Alpha legacy.
117#ifdef _WIN32
118size_t roundup(size_t x) {
119#ifndef _XBOX
120 int gran = sysInfo.dwAllocationGranularity ? sysInfo.dwAllocationGranularity : 0x10000;
121#else
122 int gran = 0x10000; // 64k in 360
123#endif
124 return (x + gran - 1) & ~(gran - 1);
125}
126#else
127size_t roundup(size_t x) {
128 return x;
129}
130#endif
131
132
133void MemArena::GrabLowMemSpace(size_t size)
134{
135#ifdef _WIN32
136#ifndef _XBOX
137 hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD)(size), NULL);
138 GetSystemInfo(&sysInfo);
139#endif
140#elif defined(ANDROID)
141 // Use ashmem so we don't have to allocate a file on disk!
142 fd = ashmem_create_region("PPSSPP_RAM", size);
143 // Note that it appears that ashmem is pinned by default, so no need to pin.
144 if (fd < 0)
145 {
146 ERROR_LOG(MEMMAP, "Failed to grab ashmem space of size: %08x errno: %d", (int)size, (int)(errno));
147 return;
148 }
149#else
150 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
151 fd = open(ram_temp_file.c_str(), O_RDWR | O_CREAT, mode);
152 if (fd < 0)
153 {
154 ERROR_LOG(MEMMAP, "Failed to grab memory space as a file: %s of size: %08x errno: %d", ram_temp_file.c_str(), (int)size, (int)(errno));
155 return;
156 }
157 // delete immediately, we keep the fd so it still lives
158 unlink(ram_temp_file.c_str());
159 if (ftruncate(fd, size) != 0)
160 {
161 ERROR_LOG(MEMMAP, "Failed to ftruncate %d to size %08x", (int)fd, (int)size);
162 }
163 return;
164#endif
165}
166
167
168void MemArena::ReleaseSpace()
169{
170#ifdef _WIN32
171 CloseHandle(hMemoryMapping);
172 hMemoryMapping = 0;
173#elif defined(__SYMBIAN32__)
174 memmap->Close();
175 delete memmap;
176#else
177 close(fd);
178#endif
179}
180
181
182void *MemArena::CreateView(s64 offset, size_t size, void *base)
183{
184#ifdef _WIN32
185#ifdef _XBOX
186 size = roundup(size);
187 // use 64kb pages
188 void * ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
189 return ptr;
190#else
191 size = roundup(size);
192 void *ptr = MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base);
193 return ptr;
194#endif
195#else
196 void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED |
197 // Do not sync memory to underlying file. Linux has this by default.
198#ifdef BLACKBERRY
199 MAP_NOSYNCFILE |
200#elif defined(__FreeBSD__)
201 MAP_NOSYNC |
202#endif
203 ((base == 0) ? 0 : MAP_FIXED), fd, offset);
204
205 if (retval == MAP_FAILED)
206 {
207 NOTICE_LOG(MEMMAP, "mmap on %s (fd: %d) failed", ram_temp_file.c_str(), (int)fd);
208 return 0;
209 }
210 return retval;
211#endif
212}
213
214
215void MemArena::ReleaseView(void* view, size_t size)
216{
217#ifdef _WIN32
218#ifndef _XBOX
219 UnmapViewOfFile(view);
220#endif
221#elif defined(__SYMBIAN32__)
222 memmap->Decommit(((int)view - (int)memmap->Base()) & 0x3FFFFFFF, size);
223#else
224 munmap(view, size);
225#endif
226}
227
228#ifndef __SYMBIAN32__
229u8* MemArena::Find4GBBase()
230{
231#ifdef _M_X64
232#ifdef _WIN32
233 // 64 bit
234 u8* base = (u8*)VirtualAlloc(0, 0xE1000000, MEM_RESERVE, PAGE_READWRITE);
235 VirtualFree(base, 0, MEM_RELEASE);
236 return base;
237#else
238 // Very precarious - mmap cannot return an error when trying to map already used pages.
239 // This makes the Windows approach above unusable on Linux, so we will simply pray...
240 return reinterpret_cast<u8*>(0x2300000000ULL);
241#endif
242
243#else // 32 bit
244
245#ifdef _WIN32
246 u8* base = (u8*)VirtualAlloc(0, 0x10000000, MEM_RESERVE, PAGE_READWRITE);
247 if (base) {
248 VirtualFree(base, 0, MEM_RELEASE);
249 }
250 return base;
251#else
252#ifdef IOS
253 void* base = NULL;
254 if (globalbase == NULL){
255 base = mmap(0, 0x08000000, PROT_READ | PROT_WRITE,
256 MAP_ANON | MAP_SHARED, -1, 0);
257 if (base == MAP_FAILED) {
258 PanicAlert("Failed to map 128 MB of memory space: %s", strerror(errno));
259 return 0;
260 }
261 munmap(base, 0x08000000);
262 globalbase = base;
263 }
264 else{ base = globalbase; }
265#else
266 void* base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE,
267 MAP_ANON | MAP_SHARED, -1, 0);
268 if (base == MAP_FAILED) {
269 PanicAlert("Failed to map 256 MB of memory space: %s", strerror(errno));
270 return 0;
271 }
272 munmap(base, 0x10000000);
273#endif
274 return static_cast<u8*>(base);
275#endif
276#endif
277}
278#endif
279
280
281// yeah, this could also be done in like two bitwise ops...
282#define SKIP(a_flags, b_flags)
283// if (!(a_flags & MV_WII_ONLY) && (b_flags & MV_WII_ONLY))
284// continue;
285// if (!(a_flags & MV_FAKE_VMEM) && (b_flags & MV_FAKE_VMEM))
286// continue;
287
288static bool Memory_TryBase(u8 *base, const MemoryView *views, int num_views, u32 flags, MemArena *arena) {
289 // OK, we know where to find free space. Now grab it!
290 // We just mimic the popular BAT setup.
291 size_t position = 0;
292 size_t last_position = 0;
293
294#if defined(_XBOX)
295 void *ptr;
296#endif
297
298 // Zero all the pointers to be sure.
299 for (int i = 0; i < num_views; i++)
300 {
301 if (views[i].out_ptr_low)
302 *views[i].out_ptr_low = 0;
303 if (views[i].out_ptr)
304 *views[i].out_ptr = 0;
305 }
306
307 int i;
308 for (i = 0; i < num_views; i++)
309 {
310 const MemoryView &view = views[i];
311 if (view.size == 0)
312 continue;
313 SKIP(flags, view.flags);
314 if (view.flags & MV_MIRROR_PREVIOUS) {
315 position = last_position;
316 }
317 else {
318#ifdef __SYMBIAN32__
319 *(view.out_ptr_low) = (u8*)((int)arena->memmap->Base() + view.virtual_address);
320 arena->memmap->Commit(view.virtual_address & 0x3FFFFFFF, view.size);
321 }
322 *(view.out_ptr) = (u8*)((int)arena->memmap->Base() + view.virtual_address & 0x3FFFFFFF);
323#elif defined(_XBOX)
324 *(view.out_ptr_low) = (u8*)(base + view.virtual_address);
325 //arena->memmap->Commit(view.virtual_address & 0x3FFFFFFF, view.size);
326 ptr = VirtualAlloc(base + (view.virtual_address & 0x3FFFFFFF), view.size, MEM_COMMIT, PAGE_READWRITE);
327 }
328 *(view.out_ptr) = (u8*)base + (view.virtual_address & 0x3FFFFFFF);
329#else
330 *(view.out_ptr_low) = (u8*)arena->CreateView(position, view.size);
331 if (!*view.out_ptr_low)
332 goto bail;
333 }
334#ifdef _M_X64
335 *view.out_ptr = (u8*)arena->CreateView(
336 position, view.size, base + view.virtual_address);
337#else
338 if (view.flags & MV_MIRROR_PREVIOUS) { // TODO: should check if the two & 0x3FFFFFFF are identical.
339 // No need to create multiple identical views.
340 *view.out_ptr = *views[i - 1].out_ptr;
341 }
342 else {
343 *view.out_ptr = (u8*)arena->CreateView(
344 position, view.size, base + (view.virtual_address & 0x3FFFFFFF));
345 if (!*view.out_ptr)
346 goto bail;
347 }
348#endif
349
350#endif
351 last_position = position;
352 position += roundup(view.size);
353 }
354
355 return true;
356
357bail:
358 // Argh! ERROR! Free what we grabbed so far so we can try again.
359 for (int j = 0; j <= i; j++)
360 {
361 if (views[i].size == 0)
362 continue;
363 SKIP(flags, views[i].flags);
364 if (views[j].out_ptr_low && *views[j].out_ptr_low)
365 {
366 arena->ReleaseView(*views[j].out_ptr_low, views[j].size);
367 *views[j].out_ptr_low = NULL;
368 }
369 if (*views[j].out_ptr)
370 {
371#ifdef _M_X64
372 arena->ReleaseView(*views[j].out_ptr, views[j].size);
373#else
374 if (!(views[j].flags & MV_MIRROR_PREVIOUS))
375 {
376 arena->ReleaseView(*views[j].out_ptr, views[j].size);
377 }
378#endif
379 *views[j].out_ptr = NULL;
380 }
381 }
382 return false;
383}
384
385u8 *MemoryMap_Setup(const MemoryView *views, int num_views, u32 flags, MemArena *arena)
386{
387 size_t total_mem = 0;
388 int base_attempts = 0;
389
390 for (int i = 0; i < num_views; i++)
391 {
392 if (views[i].size == 0)
393 continue;
394 SKIP(flags, views[i].flags);
395 if ((views[i].flags & MV_MIRROR_PREVIOUS) == 0)
396 total_mem += roundup(views[i].size);
397 }
398 // Grab some pagefile backed memory out of the void ...
399#ifndef __SYMBIAN32__
400 arena->GrabLowMemSpace(total_mem);
401#endif
402
403 // Now, create views in high memory where there's plenty of space.
404#ifdef _M_X64
405 u8 *base = MemArena::Find4GBBase();
406 // This really shouldn't fail - in 64-bit, there will always be enough
407 // address space.
408 if (!Memory_TryBase(base, views, num_views, flags, arena))
409 {
410 PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
411 return 0;
412 }
413#elif defined(_XBOX)
414 // Reserve 256MB
415 u8 *base = (u8*)VirtualAlloc(0, 0x10000000, MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE);
416 if (!Memory_TryBase(base, views, num_views, flags, arena))
417 {
418 PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
419 exit(0);
420 return 0;
421 }
422#elif defined(_WIN32)
423 // Try a whole range of possible bases. Return once we got a valid one.
424 u32 max_base_addr = 0x7FFF0000 - 0x10000000;
425 u8 *base = NULL;
426
427 for (u32 base_addr = 0x01000000; base_addr < max_base_addr; base_addr += 0x400000)
428 {
429 base_attempts++;
430 base = (u8 *)base_addr;
431 if (Memory_TryBase(base, views, num_views, flags, arena))
432 {
433 INFO_LOG(MEMMAP, "Found valid memory base at %p after %i tries.", base, base_attempts);
434 base_attempts = 0;
435 break;
436 }
437 }
438#elif defined(__SYMBIAN32__)
439 arena->memmap = new RChunk();
440 arena->memmap->CreateDisconnectedLocal(0, 0, 0x10000000);
441 if (!Memory_TryBase(arena->memmap->Base(), views, num_views, flags, arena))
442 {
443 PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
444 return 0;
445 }
446 u8* base = arena->memmap->Base();
447#else
448 // Linux32 is fine with the x64 method, although limited to 32-bit with no automirrors.
449 u8 *base = MemArena::Find4GBBase();
450 if (!Memory_TryBase(base, views, num_views, flags, arena))
451 {
452 ERROR_LOG(MEMMAP, "MemoryMap_Setup: Failed finding a memory base.");
453 PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
454 return 0;
455 }
456#endif
457 if (base_attempts)
458 PanicAlert("No possible memory base pointer found!");
459 return base;
460}
461
462void MemoryMap_Shutdown(const MemoryView *views, int num_views, u32 flags, MemArena *arena)
463{
464 for (int i = 0; i < num_views; i++)
465 {
466 if (views[i].size == 0)
467 continue;
468 SKIP(flags, views[i].flags);
469 if (views[i].out_ptr_low && *views[i].out_ptr_low)
470 arena->ReleaseView(*views[i].out_ptr_low, views[i].size);
471 if (*views[i].out_ptr && (views[i].out_ptr_low && *views[i].out_ptr != *views[i].out_ptr_low))
472 arena->ReleaseView(*views[i].out_ptr, views[i].size);
473 *views[i].out_ptr = NULL;
474 if (views[i].out_ptr_low)
475 *views[i].out_ptr_low = NULL;
476 }
477}