From 3bc8adf4264cddef7d5d415b85444a4c8e60f1cd Mon Sep 17 00:00:00 2001 From: Sacha Date: Tue, 12 Nov 2013 02:05:47 +1000 Subject: [PATCH] Fix crashes on Symbian related to memory allocation. --- Common/ArmEmitter.h | 4 +++- Common/MemoryUtil.cpp | 25 +++++++++++++++---------- Common/MemoryUtil.h | 3 +++ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Common/ArmEmitter.h b/Common/ArmEmitter.h index 868c5bcdb3..ee76005df8 100644 --- a/Common/ArmEmitter.h +++ b/Common/ArmEmitter.h @@ -635,7 +635,9 @@ public: // Call this when shutting down. Don't rely on the destructor, even though it'll do the job. void FreeCodeSpace() { -#ifndef __SYMBIAN32__ +#ifdef __SYMBIAN32__ + FreeExecutableMemory(region); +#else FreeMemoryPages(region, region_size); #endif region = NULL; diff --git a/Common/MemoryUtil.cpp b/Common/MemoryUtil.cpp index a263e21eda..287ab46ad3 100644 --- a/Common/MemoryUtil.cpp +++ b/Common/MemoryUtil.cpp @@ -45,10 +45,16 @@ #ifdef __SYMBIAN32__ #include -#define SYMBIAN_CODECHUNK_SIZE 1024*1024*20; +#define CODECHUNK_SIZE 1024*1024*20 static RChunk* g_code_chunk = NULL; static RHeap* g_code_heap = NULL; -static void* g_next_ptr = NULL; +static u8* g_next_ptr = NULL; + +void FreeExecutableMemory(void* ptr) +{ + // Just reset the ptr to the base + g_next_ptr = g_orig_ptr; +} #endif // This is purposely not a full wrapper for virtualalloc/mmap, but it @@ -63,14 +69,13 @@ void* AllocateExecutableMemory(size_t size, bool low) //memory chunk for all the executable code for the JIT if( g_code_chunk == NULL && g_code_heap == NULL) { - TInt minsize = SYMBIAN_CODECHUNK_SIZE; - TInt maxsize = SYMBIAN_CODECHUNK_SIZE + 3*GetPageSize(); //some offsets g_code_chunk = new RChunk(); - g_code_chunk->CreateLocalCode(minsize, maxsize); - g_code_heap = UserHeap::ChunkHeap(*g_code_chunk, minsize, 1, maxsize); - g_next_ptr = (void*) g_code_heap->Alloc( minsize ); + g_code_chunk->CreateLocalCode(CODECHUNK_SIZE, CODECHUNK_SIZE + 3*GetPageSize()); + g_code_heap = UserHeap::ChunkHeap(*g_code_chunk, CODECHUNK_SIZE, 1, CODECHUNK_SIZE + 3*GetPageSize()); + g_next_ptr = reinterpret_cast(g_code_heap->AllocZ(CODECHUNK_SIZE)); + g_orig_ptr = g_next_ptr; } - void* ptr = g_next_ptr; + void* ptr = (void*)g_next_ptr; g_next_ptr += size; #else static char *map_hint = 0; @@ -123,7 +128,7 @@ void* AllocateMemoryPages(size_t size) #ifdef _WIN32 void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); #elif defined(__SYMBIAN32__) - void* ptr = new u8[size]; + void* ptr = malloc(size); #else void* ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); #endif @@ -173,7 +178,7 @@ void FreeMemoryPages(void* ptr, size_t size) PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg()); ptr = NULL; // Is this our responsibility? #elif defined(__SYMBIAN32__) - delete [] ptr; + free(ptr); #else munmap(ptr, size); #endif diff --git a/Common/MemoryUtil.h b/Common/MemoryUtil.h index 2ae8fea943..d39307e59b 100644 --- a/Common/MemoryUtil.h +++ b/Common/MemoryUtil.h @@ -30,6 +30,9 @@ void* AllocateAlignedMemory(size_t size,size_t alignment); void FreeAlignedMemory(void* ptr); void WriteProtectMemory(void* ptr, size_t size, bool executable = false); void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute = false); +#ifdef __SYMBIAN32__ +void FreeExecutableMemory(void* ptr); +#endif inline int GetPageSize() { return 4096; }