diff options
author | Yotam Barnoy | 2010-12-22 14:48:51 +0000 |
---|---|---|
committer | Yotam Barnoy | 2010-12-22 14:48:51 +0000 |
commit | 6817d4b300f272f7ae51b8e3f5ab245f56d72780 (patch) | |
tree | 0d528759a3e9e22214e36250b514c08d6bca1d38 /backends/plugins/elf/memory-manager.h | |
parent | c309bbde28a70a23a4d0199244f6db574d7c7189 (diff) | |
download | scummvm-rg350-6817d4b300f272f7ae51b8e3f5ab245f56d72780.tar.gz scummvm-rg350-6817d4b300f272f7ae51b8e3f5ab245f56d72780.tar.bz2 scummvm-rg350-6817d4b300f272f7ae51b8e3f5ab245f56d72780.zip |
PLUGINS: add ELF memory manager to solve fragmentation
Following lordhoto's suggestion, I implemented a simple allocator that grabs the size of the biggest available plugin in memory. This is an elegant solution to the fragmentation problem, with the caveat that memory is wasted. As such, it's not suited for the DS, so I added a #define to disable it there.
svn-id: r55009
Diffstat (limited to 'backends/plugins/elf/memory-manager.h')
-rw-r--r-- | backends/plugins/elf/memory-manager.h | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/backends/plugins/elf/memory-manager.h b/backends/plugins/elf/memory-manager.h new file mode 100644 index 0000000000..5121ed163d --- /dev/null +++ b/backends/plugins/elf/memory-manager.h @@ -0,0 +1,83 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#ifndef ELF_MEMORY_MANAGER_H +#define ELF_MEMORY_MANAGER_H + +#include "common/singleton.h" +#include "common/list.h" +#include "common/mutex.h" + +/** + * A 'foolproof' way to prevent memory fragmentation. This class is used to + * serve as a permanent allocation to prevent the process of loading and + * unloading plugins from causing heavy fragmentation. + **/ + +#define ELFMemMan ELFMemoryManager::instance() + +class ELFMemoryManager : public Common::Singleton<ELFMemoryManager> { +public: + void trackPlugin(bool value); + void trackAlloc(size_t align, size_t size); + + void allocateHeap(); + + void *pluginAllocate(size_t size); + void *pluginAllocate(uint32 align, uint32 size); + void pluginDeallocate(void *ptr); + +private: + friend class Common::Singleton<ELFMemoryManager>; + + ELFMemoryManager(); + ~ELFMemoryManager(); + + void *allocateOnHeap(size_t align, size_t size); + void deallocateFromHeap(void *ptr); + + struct Allocation { + byte *start; + size_t size; + byte *end() { return start + size; } + Allocation(byte *a, size_t b) : start(a), size(b) {} + }; + + // heap + void *_heap; + size_t _heapAlign; // alignment of the heap + size_t _heapSize; // size of the heap + + // tracking allocations + bool _trackAllocs; // whether we are currently tracking + size_t _measuredSize; + size_t _measuredAlign; + + // real allocations + Common::List<Allocation> _allocList; + uint32 _bytesAllocated; +}; + +#endif /* ELF_MEMORY_MANAGER_H */ |