MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trp_buffer.cpp
1 /*
2  Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "trp_buffer.hpp"
19 
20 TFPool::TFPool()
21 {
22  m_first_free = 0;
23  m_alloc_ptr = 0;
24 }
25 
26 bool
27 TFPool::init(size_t mem, size_t page_sz)
28 {
29  unsigned char * ptr = (m_alloc_ptr = (unsigned char*)malloc(mem));
30  for (size_t i = 0; i + page_sz < mem; i += page_sz)
31  {
32  TFPage * p = (TFPage*)(ptr + i);
33  p->m_size = (Uint16)(page_sz - offsetof(TFPage, m_data));
34  p->init();
35  p->m_next = m_first_free;
36  m_first_free = p;
37  }
38  return true;
39 }
40 
41 TFPool::~TFPool()
42 {
43  if (m_alloc_ptr)
44  free (m_alloc_ptr);
45 }
46 
47 void
48 TFBuffer::validate() const
49 {
50  if (m_bytes_in_buffer == 0)
51  {
52  assert(m_head == m_tail);
53  if (m_head)
54  {
55  assert(m_head->m_bytes < m_head->m_size); // Full pages should be release
56  assert(m_head->m_bytes == m_head->m_start);
57  }
58  return;
59  }
60  else
61  {
62  assert(m_head != 0);
63  assert(m_tail != 0);
64  }
65  Uint32 sum = 0;
66  TFPage * p = m_head;
67  while (p)
68  {
69  assert(p->m_bytes <= p->m_size);
70  assert(p->m_start <= p->m_bytes);
71  assert((p->m_start & 3) == 0);
72  assert(p->m_bytes - p->m_start > 0);
73  assert(p->m_bytes - p->m_start <= (int)m_bytes_in_buffer);
74  assert(p->m_next != p);
75  if (p == m_tail)
76  {
77  assert(p->m_next == 0);
78  }
79  else
80  {
81  assert(p->m_next != 0);
82  }
83  sum += p->m_bytes - p->m_start;
84  p = p->m_next;
85  }
86  assert(sum == m_bytes_in_buffer);
87 }
88