MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WOPool.cpp
1 /*
2  Copyright (C) 2006-2008 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "WOPool.hpp"
20 #include <ndbd_exit_codes.h>
21 #include <NdbOut.hpp>
22 
23 WOPool::WOPool()
24 {
25  memset(this, 0, sizeof(* this));
26  m_current_pos = WOPage::WOPAGE_WORDS;
27 }
28 
29 void
30 WOPool::init(const Record_info& ri, const Pool_context& pc)
31 {
32  m_ctx = pc;
33  m_record_info = ri;
34  m_record_info.m_size = ((ri.m_size + 3) >> 2); // Align to word boundary
35  m_record_info.m_offset_magic = ((ri.m_offset_magic + 3) >> 2);
36  m_memroot = (WOPage*)m_ctx.get_memroot();
37  ndbout_c("WOPool::init(%x, %d)",ri.m_type_id, m_record_info.m_size);
38 }
39 
40 bool
41 WOPool::seize_new_page(Ptr<void>& ptr)
42 {
43  WOPage* page;
44  Uint32 page_no = RNIL;
45  if ((page = (WOPage*)m_ctx.alloc_page(m_record_info.m_type_id, &page_no)))
46  {
47  if (m_current_page)
48  {
49  m_current_page->m_ref_count = m_current_ref_count;
50  }
51 
52  m_current_pos = 0;
53  m_current_ref_count = 0;
54  m_current_page_no = page_no;
55  m_current_page = page;
56  page->m_type_id = m_record_info.m_type_id;
57  bool ret = seize(ptr);
58  assert(ret);
59  return true;
60  }
61  return false;
62 }
63 
64 void
65 WOPool::release_not_current(Ptr<void> ptr)
66 {
67  WOPage* page = (WOPage*)(UintPtr(ptr.p) & ~(GLOBAL_PAGE_SIZE - 1));
68  Uint32 cnt = page->m_ref_count;
69  Uint32 type = page->m_type_id;
70  Uint32 ri_type = m_record_info.m_type_id;
71  if (likely(cnt && type == ri_type))
72  {
73  if (cnt == 1)
74  {
75  m_ctx.release_page(ri_type, ptr.i >> POOL_RECORD_BITS);
76  return;
77  }
78  page->m_ref_count = cnt - 1;
79  return;
80  }
81 
82  handle_inconsistent_release(ptr);
83 }
84 
85 void
86 WOPool::handle_invalid_release(Ptr<void> ptr)
87 {
88  char buf[255];
89 
90  Uint32 pos = ptr.i & POOL_RECORD_MASK;
91  Uint32 pageI = ptr.i >> POOL_RECORD_BITS;
92  Uint32 * record_ptr_p = (Uint32*)ptr.p;
93  Uint32 * record_ptr_i = (m_memroot+pageI)->m_data + pos;
94 
95  Uint32 magic = * (record_ptr_p + m_record_info.m_offset_magic);
96  BaseString::snprintf(buf, sizeof(buf),
97  "Invalid memory release: ptr (%x %p %p) magic: (%.8x %.8x) memroot: %p page: %x",
98  ptr.i, ptr.p, record_ptr_i, magic, m_record_info.m_type_id,
99  m_memroot,
100  (m_memroot+pageI)->m_type_id);
101 
102  m_ctx.handleAbort(NDBD_EXIT_PRGERR, buf);
103 }
104 
105 void
106 WOPool::handle_invalid_get_ptr(Uint32 ptrI)
107 {
108  char buf[255];
109 
110  Uint32 pos = ptrI & POOL_RECORD_MASK;
111  Uint32 pageI = ptrI >> POOL_RECORD_BITS;
112  Uint32 * record_ptr_i = (m_memroot+pageI)->m_data + pos;
113 
114  Uint32 magic = * (record_ptr_i + m_record_info.m_offset_magic);
115  BaseString::snprintf(buf, sizeof(buf),
116  "Invalid memory access: ptr (%x %p) magic: (%.8x %.8x) memroot: %p page: %x",
117  ptrI, record_ptr_i, magic, m_record_info.m_type_id,
118  m_memroot,
119  (m_memroot+pageI)->m_type_id);
120 
121  m_ctx.handleAbort(NDBD_EXIT_PRGERR, buf);
122 }
123 
124 void
125 WOPool::handle_inconsistent_release(Ptr<void> ptr)
126 {
127  WOPage* page = (WOPage*)(UintPtr(ptr.p) & ~(GLOBAL_PAGE_SIZE - 1));
128  Uint32 cnt = page->m_ref_count;
129  Uint32 type = page->m_type_id;
130  Uint32 ri_type = m_record_info.m_type_id;
131 
132  char buf[255];
133 
134  BaseString::snprintf(buf, sizeof(buf),
135  "Memory corruption: ptr (%x %p) page (%d %x %x)",
136  ptr.i, ptr.p, cnt, type, ri_type);
137 
138  m_ctx.handleAbort(NDBD_EXIT_PRGERR, buf);
139 }