From b9fa49a1762c3652c1f607fb8a7431e52fe74b2e Mon Sep 17 00:00:00 2001 From: Won Star Date: Fri, 30 Jun 2006 16:38:43 +0000 Subject: Align memory address properly. This fixes crash on the GP32. svn-id: r23354 --- backends/gp32/gp32std_memory.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'backends/gp32') diff --git a/backends/gp32/gp32std_memory.cpp b/backends/gp32/gp32std_memory.cpp index f42982587f..422c46b00d 100644 --- a/backends/gp32/gp32std_memory.cpp +++ b/backends/gp32/gp32std_memory.cpp @@ -190,12 +190,15 @@ void *gp_memset(void *dst, int val, size_t count) { #define MALLOC_MASK 0xAB800000 +// WE HAVE TO ALIGN MEMORY ADDRESS ON THE ARM PROCESSOR! +#define ALIGNED_SIZE(size) ((size) + (4 - ((size) & 3))) + void *gp_malloc(size_t size) { uint32 np; uint32 *up; // size + 8 bytes : stores block size - int allocSize = size + sizeof(uint32) + sizeof(uint32); + int allocSize = ALIGNED_SIZE(size) + sizeof(uint32) + sizeof(uint32); if (allocSize <= USER_BLOCK_SIZE) { np = (uint32) MemBlock::addBlock(allocSize); if (!np) { @@ -208,8 +211,8 @@ void *gp_malloc(size_t size) { if (np) { up = (uint32 *)np; - *up = size | MALLOC_MASK; // mem size: up to 8mb - up = (uint32 *)(np + size + sizeof(uint32)); + *up = ALIGNED_SIZE(size) | MALLOC_MASK; // mem size: up to 8mb + up = (uint32 *)(np + ALIGNED_SIZE(size) + sizeof(uint32)); *up = 0x1234; // catches oob acess return (void *)(np + sizeof(uint32)); } @@ -227,7 +230,7 @@ void gp_free(void *block) { np = ((uint32) block) - sizeof(uint32); up = (uint32 *) np; - if (*up == 0x43210900) { + if (*up == 0x4321) { warning("%s: double deallocation", __FUNCTION__); return; } @@ -244,9 +247,9 @@ void gp_free(void *block) { return; } - *up = 0x43210900; np = ((uint32) block) - sizeof(uint32); up = (uint32 *) np; + *up = 0x4321; if (blockSize + 8 <= USER_BLOCK_SIZE) { MemBlock::deleteBlock(up); -- cgit v1.2.3