mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / src / kernel / vm / WOPool.hpp
blob781d4831bacacd5511dc445ef27d0dc7c9100f0e
1 /* Copyright (c) 2003, 2006, 2007 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #ifndef WOPOOL_HPP
17 #define WOPOOL_HPP
19 #include "Pool.hpp"
21 struct WOPage
23 STATIC_CONST( WOPAGE_WORDS = GLOBAL_PAGE_SIZE_WORDS - 2 );
25 Uint32 m_type_id;
26 Uint32 m_ref_count;
27 Uint32 m_data[WOPAGE_WORDS];
30 /**
31 * Write Once Pool
33 struct WOPool
35 Record_info m_record_info;
36 WOPage* m_memroot;
37 WOPage* m_current_page;
38 Pool_context m_ctx;
39 Uint32 m_current_page_no;
40 Uint16 m_current_pos;
41 Uint16 m_current_ref_count;
42 public:
43 WOPool();
45 void init(const Record_info& ri, const Pool_context& pc);
46 bool seize(Ptr<void>&);
47 void release(Ptr<void>);
48 void * getPtr(Uint32 i);
50 private:
51 bool seize_new_page(Ptr<void>&);
52 void release_not_current(Ptr<void>);
54 void handle_invalid_release(Ptr<void>);
55 void handle_invalid_get_ptr(Uint32 i);
56 void handle_inconsistent_release(Ptr<void>);
59 inline
60 bool
61 WOPool::seize(Ptr<void>& ptr)
63 Uint32 pos = m_current_pos;
64 Uint32 size = m_record_info.m_size;
65 WOPage *pageP = m_current_page;
66 if (likely(pos + size < WOPage::WOPAGE_WORDS))
68 ptr.i = (m_current_page_no << POOL_RECORD_BITS) + pos;
69 ptr.p = (pageP->m_data + pos);
70 pageP->m_data[pos+m_record_info.m_offset_magic] = ~(Uint32)m_record_info.m_type_id;
71 m_current_pos = pos + size;
72 m_current_ref_count++;
73 return true;
76 return seize_new_page(ptr);
79 inline
80 void
81 WOPool::release(Ptr<void> ptr)
83 Uint32 cur_page = m_current_page_no;
84 Uint32 ptr_page = ptr.i >> POOL_RECORD_BITS;
85 Uint32 *magic_ptr = (((Uint32*)ptr.p)+m_record_info.m_offset_magic);
86 Uint32 magic_val = *magic_ptr;
88 if (likely(magic_val == ~(Uint32)m_record_info.m_type_id))
90 * magic_ptr = 0;
91 if (cur_page == ptr_page)
93 if (m_current_ref_count == 1)
95 m_current_pos = 0;
97 m_current_ref_count--;
98 return;
100 return release_not_current(ptr);
102 handle_invalid_release(ptr);
105 inline
106 void*
107 WOPool::getPtr(Uint32 i)
109 Uint32 page_no = i >> POOL_RECORD_BITS;
110 Uint32 page_idx = i & POOL_RECORD_MASK;
111 WOPage * page = m_memroot + page_no;
112 Uint32 * record = page->m_data + page_idx;
113 Uint32 magic_val = * (record + m_record_info.m_offset_magic);
114 if (likely(magic_val == ~(Uint32)m_record_info.m_type_id))
116 return record;
118 handle_invalid_get_ptr(i);
119 return 0; /* purify: deadcode */
122 #endif