-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS_BytePool.cpp
More file actions
146 lines (143 loc) · 2.79 KB
/
DS_BytePool.cpp
File metadata and controls
146 lines (143 loc) · 2.79 KB
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
#include "DS_BytePool.h"
#include <assert.h>
#ifndef __APPLE__
// Use stdlib and not malloc for compatibility
#include <stdlib.h>
#endif
using namespace DataStructures;
BytePool::BytePool()
{
pool128.SetPageSize(8192*4);
pool512.SetPageSize(8192*4);
pool2048.SetPageSize(8192*4);
pool8192.SetPageSize(8192*4);
}
BytePool::~BytePool()
{
}
void BytePool::SetPageSize(int size)
{
pool128.SetPageSize(size);
pool512.SetPageSize(size);
pool2048.SetPageSize(size);
pool8192.SetPageSize(size);
}
unsigned char *BytePool::Allocate(int bytesWanted)
{
#ifdef _DISABLE_BYTE_POOL
return malloc(bytesWanted);
#endif
unsigned char *out;
if (bytesWanted <= 127)
{
#ifdef _THREADSAFE_BYTE_POOL
mutex128.Lock();
#endif
out = (unsigned char*) pool128.Allocate();
#ifdef _THREADSAFE_BYTE_POOL
mutex128.Unlock();
#endif
out[0]=0;
return ((unsigned char*) out)+1;
}
if (bytesWanted <= 511)
{
#ifdef _THREADSAFE_BYTE_POOL
mutex512.Lock();
#endif
out = (unsigned char*) pool512.Allocate();
#ifdef _THREADSAFE_BYTE_POOL
mutex512.Unlock();
#endif
out[0]=1;
return ((unsigned char*) out)+1;
}
if (bytesWanted <= 2047)
{
#ifdef _THREADSAFE_BYTE_POOL
mutex2048.Lock();
#endif
out = (unsigned char*) pool2048.Allocate();
#ifdef _THREADSAFE_BYTE_POOL
mutex2048.Unlock();
#endif
out[0]=2;
return ((unsigned char*) out)+1;
}
if (bytesWanted <= 8191)
{
#ifdef _THREADSAFE_BYTE_POOL
mutex8192.Lock();
#endif
out = (unsigned char*) pool8192.Allocate();
#ifdef _THREADSAFE_BYTE_POOL
mutex8192.Unlock();
#endif
out[0]=3;
return ((unsigned char*) out)+1;
}
out = (unsigned char*)malloc(bytesWanted+1);
out[0]=(unsigned char)255;
return out+1;
}
void BytePool::Release(unsigned char *data)
{
#ifdef _DISABLE_BYTE_POOL
HexFree(data);
#endif
unsigned char *realData = data-1;
switch (realData[0])
{
case 0:
#ifdef _THREADSAFE_BYTE_POOL
mutex128.Lock();
#endif
pool128.Release((unsigned char(*)[128]) realData );
#ifdef _THREADSAFE_BYTE_POOL
mutex128.Unlock();
#endif
break;
case 1:
#ifdef _THREADSAFE_BYTE_POOL
mutex512.Lock();
#endif
pool512.Release((unsigned char(*)[512]) realData );
#ifdef _THREADSAFE_BYTE_POOL
mutex512.Unlock();
#endif
break;
case 2:
#ifdef _THREADSAFE_BYTE_POOL
mutex2048.Lock();
#endif
pool2048.Release((unsigned char(*)[2048]) realData );
#ifdef _THREADSAFE_BYTE_POOL
mutex2048.Unlock();
#endif
break;
case 3:
#ifdef _THREADSAFE_BYTE_POOL
mutex8192.Lock();
#endif
pool8192.Release((unsigned char(*)[8192]) realData );
#ifdef _THREADSAFE_BYTE_POOL
mutex8192.Unlock();
#endif
break;
case 255:
free(realData);
break;
default:
assert(0);
break;
}
}
void BytePool::Clear(void)
{
#ifdef _THREADSAFE_BYTE_POOL
pool128.Clear();
pool512.Clear();
pool2048.Clear();
pool8192.Clear();
#endif
}