diff options
author | Paul Gilbert | 2012-06-08 23:00:48 +1000 |
---|---|---|
committer | Paul Gilbert | 2012-06-08 23:00:48 +1000 |
commit | d4777379d23b70aa07feec3ef2e90fcf376f4117 (patch) | |
tree | 1309d5caa0cd9d075ad6ef2ea25d8f9ada125828 /engines/tony/mpal/memory.cpp | |
parent | 1866cbd0fb0d7df1c1a4059e98b5d0f6d851c2d4 (diff) | |
download | scummvm-rg350-d4777379d23b70aa07feec3ef2e90fcf376f4117.tar.gz scummvm-rg350-d4777379d23b70aa07feec3ef2e90fcf376f4117.tar.bz2 scummvm-rg350-d4777379d23b70aa07feec3ef2e90fcf376f4117.zip |
TONY: Refactored the memory manager to increase performance
Diffstat (limited to 'engines/tony/mpal/memory.cpp')
-rw-r--r-- | engines/tony/mpal/memory.cpp | 128 |
1 files changed, 53 insertions, 75 deletions
diff --git a/engines/tony/mpal/memory.cpp b/engines/tony/mpal/memory.cpp index b5bd77f838..b50f9d7c37 100644 --- a/engines/tony/mpal/memory.cpp +++ b/engines/tony/mpal/memory.cpp @@ -30,122 +30,100 @@ namespace Tony { namespace MPAL { /****************************************************************************\ -* MemoryItem methods -\****************************************************************************/ - -/** - * Constructor - * @param Data sizee - */ -MemoryItem::MemoryItem(uint32 size) { - _size = size; - _buffer = (size == 0) ? NULL : new byte[size]; -} - -/** - * Destructor - */ -MemoryItem::~MemoryItem() { - delete[] _buffer; -} - -/** - * Returns a pointer to the resource - */ -MemoryItem::operator void *() { - return dataPointer(); -} - -/****************************************************************************\ * MemoryManager methods \****************************************************************************/ -MemoryManager::MemoryManager() { -} - -/** - * Destructor - */ -MemoryManager::~MemoryManager() { - Common::List<MemoryItem *>::iterator i; - for (i = _memoryBlocks.begin(); i != _memoryBlocks.end(); ++i) { - MemoryItem *item = *i; - delete item; - } -} +const int BLOCK_ID = 0x12345678; /** * Allocates a new memory block - * @returns Returns a MemoryItem instance for the new block + * @return Returns a MemoryItem instance for the new block */ -MemoryItem &MemoryManager::allocate(uint32 size, uint flags) { - MemoryItem *newItem = new MemoryItem(size); +HANDLE MemoryManager::allocate(uint32 size, uint flags) { + MemoryItem *newItem = (MemoryItem *)malloc(sizeof(MemoryItem) + size); + newItem->_id = BLOCK_ID; + newItem->_size = size; + newItem->_lockCount = 0; + + // If requested, clear the allocated data block if ((flags & GMEM_ZEROINIT) != 0) { - byte *dataP = (byte *)newItem->dataPointer(); + byte *dataP = newItem->_data; Common::fill(dataP, dataP + size, 0); } - _memoryBlocks.push_back(newItem); - - return *newItem; + return (HANDLE)newItem; } /** * Allocates a new memory block and returns its data pointer - * @returns Data pointer to allocated block + * @return Data pointer to allocated block */ -HGLOBAL MemoryManager::alloc(uint32 size, uint flags) { - MemoryItem &newItem = allocate(size, flags); - return (HGLOBAL)newItem.dataPointer(); +void *MemoryManager::alloc(uint32 size, uint flags) { + MemoryItem *item = (MemoryItem *)allocate(size, flags); + ++item->_lockCount; + return &item->_data[0]; } +#define OFFSETOF(type, field) ((unsigned long) &(((type *) 0)->field)) + /** * Returns a reference to the MemoryItem for a gien byte pointer * @param block Byte pointer */ -MemoryItem &MemoryManager::getItem(HGLOBAL handle) { - Common::List<MemoryItem *>::iterator i; - for (i = _memoryBlocks.begin(); i != _memoryBlocks.end(); ++i) { - MemoryItem *item = *i; - if (item->dataPointer() == handle) - return *item; - } - - error("Could not locate a memory block"); +MemoryItem *MemoryManager::getItem(HGLOBAL handle) { + MemoryItem *rec = (MemoryItem *)((byte *)handle - OFFSETOF(MemoryItem, _data)); + assert(rec->_id == BLOCK_ID); + return rec; } /** - * Square bracketes operator - * @param block Byte pointer + * Returns a size of a memory block given its pointer */ -MemoryItem &MemoryManager::operator[](HGLOBAL handle) { - return getItem(handle); +uint32 MemoryManager::getSize(HANDLE handle) { + MemoryItem *item = (MemoryItem *)handle; + assert(item->_id == BLOCK_ID); + return item->_size; } /** - * Returns a size of a memory block given its pointer + * Erases a given item */ -uint32 MemoryManager::getSize(HGLOBAL handle) { - MemoryItem &item = getItem(handle); - return item.size(); +void MemoryManager::freeBlock(HANDLE handle) { + MemoryItem *item = (MemoryItem *)handle; + assert(item->_id == BLOCK_ID); + free(item); } /** * Erases a given item */ -void MemoryManager::erase(MemoryItem *item) { - delete item; - _memoryBlocks.remove(item); +void MemoryManager::destroyItem(HANDLE handle) { + MemoryItem *item = getItem(handle); + assert(item->_id == BLOCK_ID); + free(item); } /** - * Erases a given item + * Locks an item for access */ -void MemoryManager::erase(HGLOBAL handle) { - MemoryItem &item = getItem(handle); - erase(&item); +byte *MemoryManager::lockItem(HANDLE handle) { + MemoryItem *item = (MemoryItem *)handle; + assert(item->_id == BLOCK_ID); + ++item->_lockCount; + return &item->_data[0]; } +/** + * Unlocks a locked item + */ +void MemoryManager::unlockItem(HANDLE handle) { + MemoryItem *item = (MemoryItem *)handle; + assert(item->_id == BLOCK_ID); + assert(item->_lockCount > 0); + --item->_lockCount; +} + + /****************************************************************************\ * Stand-alone methods \****************************************************************************/ |