aboutsummaryrefslogtreecommitdiff
path: root/engines/lure/memory.cpp
blob: a18e512d8ebc54006f4dc0995d108af730d17030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* 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$
 *
 */

#include "lure/memory.h"
#include "common/file.h"

namespace Lure {

MemoryBlock *Memory::allocate(uint32 size) {
	MemoryBlock *block = new MemoryBlock(size);
	return block;
}

MemoryBlock *Memory::duplicate(MemoryBlock *src) {
	MemoryBlock *block = new MemoryBlock(src);
	return block;
}

uint8 *Memory::alloc(uint32 size) {
	return (uint8 *) malloc(size);
}

void Memory::dealloc(void *block) {
	free(block);
}

/*--------------------------------------------------------------------------*/

MemoryBlock::MemoryBlock(uint32 size1) {
	_data = (uint8 *) malloc(size1);
	if (!_data) error ("Failed allocating memory block");
	_size = size1;
}

MemoryBlock::MemoryBlock(MemoryBlock *src) {
	_size = src->size();
	_data = (uint8 *) malloc(_size);
	if (!_data) error ("Failed allocating memory block");
	memcpy(_data, src->data(), _size);
}

MemoryBlock::~MemoryBlock() {
	free(_data);
}

void MemoryBlock::empty() {
	::memset(_data, 0, _size);
}

void MemoryBlock::setBytes(int c, size_t startIndex, size_t num) {
	byte *p = _data + startIndex;
	::memset(p, c, num);
}

void MemoryBlock::copyFrom(MemoryBlock *src) {
	copyFrom(src, 0, 0, src->size());
}

void MemoryBlock::copyFrom(MemoryBlock *src, uint32 srcPos, uint32 destPos, uint32 srcLen) {
	if ((srcPos + srcLen > src->size()) || (destPos + srcLen > size()))
		error("Memory block overrun in block copy");

	uint8 *pDest = _data + destPos;
	uint8 *pSrc = src->data() + srcPos;
	memcpy(pDest, pSrc, srcLen);
}

void MemoryBlock::copyFrom(const byte *src, uint32 srcPos, uint32 destPos, uint32 srcLen) {
	byte *pDest = _data + destPos;
	const byte *pSrc = src + srcPos;
	memcpy(pDest, pSrc, srcLen);
}

void MemoryBlock::reallocate(uint32 size1) {
	_size = size1;
	_data = (byte *) realloc(_data, size1);
	if (!_data) error ("Failed reallocating memory block");
}

void MemoryBlock::saveToFile(const Common::String &filename) {
	Common::File *f = new Common::File();
	f->open(filename.c_str(), Common::File::kFileWriteMode);
	f->write(_data, _size);
	f->close();
	delete f;
}

} // end of namespace Lure