forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrp_buffer.cpp
More file actions
88 lines (79 loc) · 2.06 KB
/
trp_buffer.cpp
File metadata and controls
88 lines (79 loc) · 2.06 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
/*
Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
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; version 2 of the License.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "trp_buffer.hpp"
TFPool::TFPool()
{
m_first_free = 0;
m_alloc_ptr = 0;
}
bool
TFPool::init(size_t mem, size_t page_sz)
{
unsigned char * ptr = (m_alloc_ptr = (unsigned char*)malloc(mem));
for (size_t i = 0; i + page_sz < mem; i += page_sz)
{
TFPage * p = (TFPage*)(ptr + i);
p->m_size = (Uint16)(page_sz - offsetof(TFPage, m_data));
p->init();
p->m_next = m_first_free;
m_first_free = p;
}
return true;
}
TFPool::~TFPool()
{
if (m_alloc_ptr)
free (m_alloc_ptr);
}
void
TFBuffer::validate() const
{
if (m_bytes_in_buffer == 0)
{
assert(m_head == m_tail);
if (m_head)
{
assert(m_head->m_bytes < m_head->m_size); // Full pages should be release
assert(m_head->m_bytes == m_head->m_start);
}
return;
}
else
{
assert(m_head != 0);
assert(m_tail != 0);
}
Uint32 sum = 0;
TFPage * p = m_head;
while (p)
{
assert(p->m_bytes <= p->m_size);
assert(p->m_start <= p->m_bytes);
assert((p->m_start & 3) == 0);
assert(p->m_bytes - p->m_start > 0);
assert(p->m_bytes - p->m_start <= (int)m_bytes_in_buffer);
assert(p->m_next != p);
if (p == m_tail)
{
assert(p->m_next == 0);
}
else
{
assert(p->m_next != 0);
}
sum += p->m_bytes - p->m_start;
p = p->m_next;
}
assert(sum == m_bytes_in_buffer);
}