-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_pool.cpp
More file actions
94 lines (80 loc) · 1.61 KB
/
buffer_pool.cpp
File metadata and controls
94 lines (80 loc) · 1.61 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
#include "buffer_pool.h"
namespace ds
{
buffer_pool_t g_buffer_pool;
pool_t::pool_t()
{
m_data = 0;
m_row = 0;
m_col = 0;
}
buffer_pool_t::buffer_pool_t()
{
m_data = 0;
m_col = 0;
m_row = 0;
m_mask = 0;
}
buffer_pool_t::~buffer_pool_t()
{
if(m_row > 0)
{
if(m_col > 0)
{
for(size_t x = 0; x < m_row; ++ x)
{
delete[] m_data[x];
}
delete[] m_data;
}
}
m_data = 0;
m_row = 0;
m_col = 0;
m_mask = 0;
}
bool buffer_pool_t::init(size_t row, size_t col)
{
if(row == 0 || row > 32 || col == 0)
{
return false;
}
m_data = new uint8_t*[row];
for(size_t x = 0; x < row; ++ x)
{
m_data[x] = new uint8_t[col];
}
m_row = row;
m_col = col;
return true;
}
bool buffer_pool_t::alloc(pool_t& pool)
{
size_t x = 0;
for(; x < 32; ++ x)
{
if(!((1 << x) & m_mask))
{
break;
}
}
if(x == 32)
{
return false;
}
pool.m_data = m_data[x];
pool.m_row = x;
pool.m_col = m_col;
m_mask |= (1 << x);
return true;
}
bool buffer_pool_t::free(pool_t& pool)
{
if(pool.m_row > 32)
{
return false;
}
m_mask &= ~(1 << (pool.m_row));
return true;
}
}