aboutsummaryrefslogtreecommitdiff
path: root/sword2/memory.cpp
diff options
context:
space:
mode:
authorTorbjörn Andersson2004-09-07 06:29:57 +0000
committerTorbjörn Andersson2004-09-07 06:29:57 +0000
commite6f19ff83a08078295456fc9ed472f9f2da9f190 (patch)
tree589d57277de5873412d86e576a423652e872e5fb /sword2/memory.cpp
parentb2ac15afa466bb527729a3d91f79b6efe586ebb8 (diff)
downloadscummvm-rg350-e6f19ff83a08078295456fc9ed472f9f2da9f190.tar.gz
scummvm-rg350-e6f19ff83a08078295456fc9ed472f9f2da9f190.tar.bz2
scummvm-rg350-e6f19ff83a08078295456fc9ed472f9f2da9f190.zip
Much like an early civilization with no concept of the number zero, the
new memory manager didn't have the concept of the NULL pointer. Now it does. If ScummVM ever crashed for you when using the phone early in the game, this patch hopefully fixes that bug. (If it didn't crash for you, memory block zero was still allocated, so 0 still decoded to a valid pointer.) svn-id: r14937
Diffstat (limited to 'sword2/memory.cpp')
-rw-r--r--sword2/memory.cpp14
1 files changed, 11 insertions, 3 deletions
diff --git a/sword2/memory.cpp b/sword2/memory.cpp
index c8f0087996..6d348cfd29 100644
--- a/sword2/memory.cpp
+++ b/sword2/memory.cpp
@@ -37,6 +37,8 @@
// could only handle up to 999 blocks of memory. That means we can encode a
// pointer as a 10-bit id and a 22-bit offset into the block. Judging by early
// testing, both should be plenty.
+//
+// The number zero is used to represent the NULL pointer.
#include "common/stdafx.h"
#include "sword2/sword2.h"
@@ -92,6 +94,9 @@ MemoryManager::~MemoryManager() {
}
int32 MemoryManager::encodePtr(byte *ptr) {
+ if (ptr == NULL)
+ return 0;
+
int idx = findPointerInIndex(ptr);
assert(idx != -1);
@@ -99,15 +104,18 @@ int32 MemoryManager::encodePtr(byte *ptr) {
uint32 id = _memBlockIndex[idx]->id;
uint32 offset = ptr - _memBlocks[id].ptr;
- assert(id <= 0x03ff);
+ assert(id < 0x03ff);
assert(offset <= 0x003fffff);
assert(offset < _memBlocks[id].size);
- return (id << 22) | (ptr - _memBlocks[id].ptr);
+ return ((id + 1) << 22) | (ptr - _memBlocks[id].ptr);
}
byte *MemoryManager::decodePtr(int32 n) {
- uint32 id = (n & 0xffc00000) >> 22;
+ if (n == 0)
+ return NULL;
+
+ uint32 id = ((n & 0xffc00000) >> 22) - 1;
uint32 offset = n & 0x003fffff;
assert(_memBlocks[id].ptr);