summaryrefslogtreecommitdiff
path: root/tools/MakeDef/tsmall.cpp
blob: 7fac553572ed939c49f7c447cfef2b9a742b2fe7 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <string.h>

#include "h_object.h"
#include "h_util.h"

HStaticSmallAllocator::HStaticSmallAllocator(size_t aPageSize) :
	HSmallAllocator(aPageSize)
{
	char* tData = new char[sizeof(HDataPage) + aPageSize];

	first = (HDataPage*)tData;
	first->free = aPageSize;
	first->next = NULL;
}

void HStaticSmallAllocator::tide()
{
	HDataPage* p1;

	for (HDataPage* p = first->next; p != NULL; p = p1) {
		p1 = p->next;
		delete p;
	}

	memset(&first->next, 0, pageSize + sizeof(void*));

	first->free = pageSize;
	first->next = NULL;
}

//====[ HSmallAllocator ]======================================================

HSmallAllocator::HSmallAllocator(size_t aPageSize) :
	first(NULL),
	pageSize(aPageSize)
{}

HSmallAllocator::~HSmallAllocator()
{
	tide();
}

char* HSmallAllocator::placeStr(const char* str)
{
	if (str == NULL)
		str = "";

	size_t len = strlen(str) + 1;

	void* p = allocateSpace(len);
	if (p == NULL)
		return NULL;

	memcpy(p, str, len);
	return (char*)p;
}

void* HSmallAllocator::placeData(const void* data, size_t len)
{
	void* p = allocateSpace(len);
	if (p == NULL)
		return NULL;

	if (data != NULL)
		memcpy(p, data, len);

	return p;
}

void* HSmallAllocator::allocateSpace(size_t bytes)
{
	HDataPage* p;

	if (bytes > pageSize) {
		size_t dataLen = sizeof(HDataPage) + bytes;
		char* data = new char[dataLen];
		memset(data, 0, dataLen);

		if (first == NULL)
			first = (HDataPage*)data;
		else {
			for (p = first; p->next != NULL; p = p->next)
				;

			p->next = (HDataPage*)data;
		}

		return data + sizeof(HDataPage);
	}

	for (p = first; p != NULL; p = p->next)
		if (p->free >= bytes)
			break;

	if (p == NULL || p->free < bytes) {
		size_t dataLen = sizeof(HDataPage) + pageSize;
		char* data = new char[dataLen];
		memset(data, 0, dataLen);

		p = (HDataPage*)data;
		p->free = pageSize;

		p->next = first;
		first = p;
	}

	char* res = (char*)(p + 1) + pageSize - p->free;   p->free -= bytes;
	return res;
}

void HSmallAllocator::tide()
{
	HDataPage* p1;

	for (HDataPage* p = first; p != 0; p = p1) {
		p1 = p->next;
		delete p;
	}

	first = NULL;
}

//=============================================================================

void* operator new(size_t bytes, HSmallAllocator& owner)
{
	return owner.allocateSpace(bytes);
}

//=============================================================================

void* HSmallAllocatorObject::operator new(size_t bytes, HSmallAllocator& owner)
{
	return owner.allocateSpace(bytes);
}

void* HSmallAllocatorObject::operator new[](size_t bytes, HSmallAllocator& owner) {
	return owner.allocateSpace(bytes);
}

//=============================================================================

void* HSmartSmallAllocatorObject::operator new(size_t bytes, HSmallAllocator& owner)
{
	char* result = (char*)owner.allocateSpace(bytes + sizeof(void*));
	*(HSmallAllocator**)result = &owner;
	return result + sizeof(void*);
}

void* HSmartSmallAllocatorObject::operator new[](size_t bytes, HSmallAllocator& owner) {
	char* result = (char*)owner.allocateSpace(bytes + sizeof(void*));
	*(HSmallAllocator**)result = &owner;
	return result + sizeof(void*);
}