-
Notifications
You must be signed in to change notification settings - Fork 1
/
DAS.cpp
91 lines (73 loc) · 1.46 KB
/
DAS.cpp
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
// Dumb Allocation System
//#define DEBUG_DAS
#include "DumbAllocator.h"
#ifdef DEBUG_DAS
#include <stdio.h>
#endif
#include <stdlib.h>
static DumbAllocationSystem *gDAS = NULL;
void das_set_global(DumbAllocationSystem *a)
{
gDAS = a;
}
void* das_alloc(size_t size)
{
return gDAS->alloc(size);
}
void das_free(void* p, size_t size)
{
return gDAS->free(p, size);
}
bool DumbAllocationSystem::initialize(size_t pool_size, size_t pool_additional_size)
{
if (pool_size == 0)
return false;
size_t s = (pool_size + sizeof(int*)-1) / sizeof(int*);
buf = (int*)malloc( s * sizeof(int*) );
if (buf)
{
next = buf;
end = buf + s;
return true;
}
total_free = 0;
next = buf;
return false;
}
void* DumbAllocationSystem::alloc(size_t size)
{
size_t s = (size + sizeof(int*)-1) / sizeof(int*);
if (s==0)
return NULL;
if (end >= next + s)
{
int* result = next;
next += s;
return (void*)result;
}
return NULL;
}
void DumbAllocationSystem::free(void* p, size_t size)
{
total_free+=size;
}
void DumbAllocationSystem::reset(void)
{
#ifdef DEBUG_DAS
printf("total alloc: %u\n", ((unsigned int)next-(unsigned int)buf));
printf("total free: %u\n", total_free);
#endif
next = buf;
total_free = 0;
}
void DumbAllocationSystem::finalize(void)
{
if (buf)
{
::free(buf);
}
buf = NULL;
end = NULL;
next = NULL;
total_free = 0;
}