1 /*
2    Copyright (c) 2010, 2013, 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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "trp_buffer.hpp"
26 
TFPool()27 TFPool::TFPool()
28 {
29   m_first_free = 0;
30   m_alloc_ptr = 0;
31 }
32 
33 bool
init(size_t mem,size_t page_sz)34 TFPool::init(size_t mem, size_t page_sz)
35 {
36   unsigned char * ptr = (m_alloc_ptr = (unsigned char*)malloc(mem));
37   for (size_t i = 0; i + page_sz < mem; i += page_sz)
38   {
39     TFPage * p = (TFPage*)(ptr + i);
40     p->m_size = (Uint16)(page_sz - offsetof(TFPage, m_data));
41     assert(((UintPtr)(&p->m_data[0]) & 3) == 0);
42     p->init();
43     p->m_next = m_first_free;
44     m_first_free = p;
45   }
46   m_tot_send_buffer = mem;
47   m_tot_used_send_buffer = 0;
48   return true;
49 }
50 
~TFPool()51 TFPool::~TFPool()
52 {
53   if (m_alloc_ptr)
54     free (m_alloc_ptr);
55 }
56 
TFMTPool(const char * name)57 TFMTPool::TFMTPool(const char * name)
58 {
59   NdbMutex_InitWithName(&m_mutex, name);
60 }
61 
62 void
validate() const63 TFBuffer::validate() const
64 {
65   if (m_bytes_in_buffer == 0)
66   {
67     assert(m_head == m_tail);
68     if (m_head)
69     {
70       assert(m_head->m_start < m_head->m_size);  // Full pages should be release
71       assert(m_head->m_bytes == 0);
72     }
73     return;
74   }
75   else
76   {
77     assert(m_head != 0);
78     assert(m_tail != 0);
79   }
80   Uint32 sum = 0;
81   TFPage * p = m_head;
82   while (p)
83   {
84     assert(p->m_bytes <= p->m_size);
85     assert(p->m_start <= p->m_size);
86     assert(p->m_start + p->m_bytes <= p->m_size);
87     assert(p->m_bytes <= (int)m_bytes_in_buffer);
88     assert(p->m_next != p);
89     if (p == m_tail)
90     {
91       assert(p->m_next == 0);
92     }
93     else
94     {
95       assert(p->m_next != 0);
96     }
97     sum += p->m_bytes;
98     p = p->m_next;
99   }
100   assert(sum == m_bytes_in_buffer);
101 }
102 
103