MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trp_buffer.hpp
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 #ifndef trp_buffer_hpp
19 #define trp_buffer_hpp
20 
21 #include <ndb_global.h>
22 #include <ndb_socket.h> // struct iovec
23 
24 struct TFPage
25 {
26  inline Uint32 max_data_bytes() const {
27  return m_size;
28  }
29 
30  inline Uint32 get_free_bytes() const {
31  return m_size - m_bytes;
32  }
33 
34  inline bool is_full() const {
35  return m_bytes == m_size;
36  }
37 
38  inline void init () {
39  m_bytes = 0;
40  m_start = 0;
41  m_ref_count = 0;
42  }
43 
44  static TFPage* ptr(struct iovec p) {
45  UintPtr v = UintPtr(p.iov_base);
46  v -= offsetof(TFPage, m_data);
47  return (TFPage*)v;
48  }
49 
50 
54  Uint16 m_bytes;
55 
59  Uint16 m_start;
60 
64  Uint16 m_size;
65 
69  Uint16 m_ref_count;
70 
74  struct TFPage * m_next;
75 
79  char m_data[8];
80 };
81 
86 struct TFSentinel
87 {
88  Uint64 data[sizeof(TFPage) / 8];
89 
90  TFSentinel() {
91  for (Uint32 i = 0; i < NDB_ARRAY_SIZE(data); i++)
92  data[i] = 0;
93  }
94 
95  TFPage* getPtr() { return new (&data[0]) TFPage;}
96 };
97 
98 struct TFBuffer
99 {
100  TFBuffer() { m_bytes_in_buffer = 0; m_head = m_tail = 0;}
101  Uint32 m_bytes_in_buffer;
102  struct TFPage * m_head;
103  struct TFPage * m_tail;
104 
105  void validate() const;
106 };
107 
109 {
110 #ifdef VM_TRACE
111  const TFBuffer& buf;
112  TFBuffer m_save;
113  TFBufferGuard(const TFBuffer& _buf) : buf(_buf), m_save(_buf) {
114  buf.validate();
115  }
116  ~TFBufferGuard() {
117  buf.validate();
118  }
119 #else
120  TFBufferGuard(const TFBuffer&) {}
121 #endif
122 };
123 
124 class TFPool
125 {
126  unsigned char * m_alloc_ptr;
127  TFPage * m_first_free;
128 public:
129  TFPool();
130  ~TFPool();
131 
132  bool init(size_t total_memory, size_t page_sz = 32768);
133  bool inited() const { return m_alloc_ptr != 0;}
134 
135  TFPage* try_alloc(Uint32 N); // Return linked list of most N pages
136  Uint32 try_alloc(struct iovec tmp[], Uint32 cnt);
137 
138  void release(TFPage* first, TFPage* last);
139  void release_list(TFPage*);
140 };
141 
142 inline
143 TFPage *
144 TFPool::try_alloc(Uint32 n)
145 {
146  TFPage * h = m_first_free;
147  if (h)
148  {
149  TFPage * p = h;
150  TFPage * prev = 0;
151  while (p != 0 && n != 0)
152  {
153  prev = p;
154  p = p->m_next;
155  n--;
156  }
157  prev->m_next = 0;
158  m_first_free = p;
159  }
160  return h;
161 }
162 
163 inline
164 Uint32
165 TFPool::try_alloc(struct iovec tmp[], Uint32 cnt)
166 {
167  TFPage * p = try_alloc(cnt);
168  Uint32 i = 0;
169  while (p)
170  {
171  p->init();
172  tmp[i].iov_base = p->m_data;
173  tmp[i].iov_len = p->m_size;
174 
175  i++;
176  p = p->m_next;
177  }
178  return i;
179 }
180 
181 inline
182 void
183 TFPool::release(TFPage* first, TFPage* last)
184 {
185  last->m_next = m_first_free;
186  m_first_free = first;
187 }
188 
189 inline
190 void
191 TFPool::release_list(TFPage* head)
192 {
193  TFPage * tail = head;
194  while (tail->m_next != 0)
195  tail = tail->m_next;
196  release(head, tail);
197 }
198 
199 #endif