2007-05-30 21:56:52 +00:00
|
|
|
/* 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.
|
2003-12-16 02:10:15 +00:00
|
|
|
*
|
|
|
|
* 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
|
2008-01-05 12:45:14 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-12-16 02:10:15 +00:00
|
|
|
* 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-12-16 02:10:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2004-10-21 12:43:49 +00:00
|
|
|
#include "sword1/memman.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2003-12-16 02:10:15 +00:00
|
|
|
#include "common/util.h"
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
namespace Sword1 {
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
MemMan::MemMan() {
|
2003-12-16 02:10:15 +00:00
|
|
|
_alloced = 0;
|
|
|
|
_memListFree = _memListFreeEnd = NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
MemMan::~MemMan() {
|
2004-11-09 04:06:10 +00:00
|
|
|
flush();
|
|
|
|
if (_alloced)
|
2009-05-31 10:02:16 +00:00
|
|
|
warning("deleting MemMan, still %d bytes alloced", _alloced);
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MemMan::alloc(MemHandle *bsMem, uint32 pSize, uint16 pCond) {
|
2003-12-16 02:10:15 +00:00
|
|
|
_alloced += pSize;
|
|
|
|
bsMem->data = (void*)malloc(pSize);
|
|
|
|
if (!bsMem->data)
|
|
|
|
error("MemMan::alloc(): Can't alloc %d bytes of memory.", pSize);
|
|
|
|
bsMem->cond = pCond;
|
|
|
|
bsMem->size = pSize;
|
|
|
|
if (pCond == MEM_CAN_FREE) {
|
|
|
|
warning("%d Bytes alloced as FREEABLE.", pSize); // why should one want to alloc mem if it can be freed?
|
|
|
|
addToFreeList(bsMem);
|
|
|
|
} else if (bsMem->next || bsMem->prev) // it's in our _freeAble list, remove it from there
|
2003-12-19 14:07:12 +00:00
|
|
|
removeFromFreeList(bsMem);
|
2003-12-16 02:10:15 +00:00
|
|
|
checkMemoryUsage();
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MemMan::freeNow(MemHandle *bsMem) {
|
2003-12-16 02:10:15 +00:00
|
|
|
if (bsMem->cond != MEM_FREED) {
|
|
|
|
_alloced -= bsMem->size;
|
|
|
|
removeFromFreeList(bsMem);
|
|
|
|
free(bsMem->data);
|
|
|
|
bsMem->cond = MEM_FREED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MemMan::setCondition(MemHandle *bsMem, uint16 pCond) {
|
2003-12-16 02:10:15 +00:00
|
|
|
if ((pCond == MEM_FREED) || (pCond > MEM_DONT_FREE))
|
|
|
|
error("MemMan::setCondition: program tried to set illegal memory condition");
|
|
|
|
if (bsMem->cond != pCond) {
|
|
|
|
bsMem->cond = pCond;
|
|
|
|
if (pCond == MEM_DONT_FREE)
|
|
|
|
removeFromFreeList(bsMem);
|
|
|
|
else if (pCond == MEM_CAN_FREE)
|
|
|
|
addToFreeList(bsMem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
void MemMan::flush() {
|
2003-12-20 09:12:54 +00:00
|
|
|
while (_memListFree) {
|
|
|
|
free(_memListFreeEnd->data);
|
|
|
|
_memListFreeEnd->data = NULL;
|
|
|
|
_memListFreeEnd->cond = MEM_FREED;
|
|
|
|
_alloced -= _memListFreeEnd->size;
|
|
|
|
removeFromFreeList(_memListFreeEnd);
|
|
|
|
}
|
|
|
|
if (_alloced)
|
|
|
|
warning("MemMan::flush: Something's wrong: still %d bytes alloced", _alloced);
|
|
|
|
}
|
|
|
|
|
2009-11-02 21:54:57 +00:00
|
|
|
void MemMan::checkMemoryUsage() {
|
2003-12-16 02:10:15 +00:00
|
|
|
while ((_alloced > MAX_ALLOC) && _memListFree) {
|
|
|
|
free(_memListFreeEnd->data);
|
|
|
|
_memListFreeEnd->data = NULL;
|
|
|
|
_memListFreeEnd->cond = MEM_FREED;
|
|
|
|
_alloced -= _memListFreeEnd->size;
|
|
|
|
removeFromFreeList(_memListFreeEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MemMan::addToFreeList(MemHandle *bsMem) {
|
2003-12-16 02:10:15 +00:00
|
|
|
if (bsMem->next || bsMem->prev) {
|
|
|
|
warning("addToFreeList: mem block is already in freeList");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bsMem->prev = NULL;
|
|
|
|
bsMem->next = _memListFree;
|
|
|
|
if (bsMem->next)
|
|
|
|
bsMem->next->prev = bsMem;
|
|
|
|
_memListFree = bsMem;
|
|
|
|
if (!_memListFreeEnd)
|
|
|
|
_memListFreeEnd = _memListFree;
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MemMan::removeFromFreeList(MemHandle *bsMem) {
|
2003-12-16 02:10:15 +00:00
|
|
|
if (_memListFree == bsMem)
|
|
|
|
_memListFree = bsMem->next;
|
|
|
|
if (_memListFreeEnd == bsMem)
|
|
|
|
_memListFreeEnd = bsMem->prev;
|
|
|
|
|
|
|
|
if (bsMem->next)
|
|
|
|
bsMem->next->prev = bsMem->prev;
|
|
|
|
if (bsMem->prev)
|
|
|
|
bsMem->prev->next = bsMem->next;
|
|
|
|
bsMem->next = bsMem->prev = NULL;
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MemMan::initHandle(MemHandle *bsMem) {
|
|
|
|
memset(bsMem, 0, sizeof(MemHandle));
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
} // End of namespace Sword1
|