1 /*
2    Copyright (c) 2006, 2010, 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 #ifndef WOPOOL_HPP
26 #define WOPOOL_HPP
27 
28 #include "Pool.hpp"
29 
30 struct WOPage
31 {
32   STATIC_CONST( WOPAGE_WORDS = GLOBAL_PAGE_SIZE_WORDS - 2 );
33 
34   Uint32 m_type_id;
35   Uint32 m_ref_count;
36   Uint32 m_data[WOPAGE_WORDS];
37 };
38 
39 /**
40  * Write Once Pool
41  */
42 struct WOPool
43 {
44   Record_info m_record_info;
45   WOPage* m_memroot;
46   WOPage* m_current_page;
47   Pool_context m_ctx;
48   Uint32 m_current_page_no;
49   Uint16 m_current_pos;
50   Uint16 m_current_ref_count;
51 public:
52   WOPool();
53 
54   void init(const Record_info& ri, const Pool_context& pc);
55   bool seize(Ptr<void>&);
56   void release(Ptr<void>);
57   void * getPtr(Uint32 i);
58 
59 private:
60   bool seize_new_page(Ptr<void>&);
61   void release_not_current(Ptr<void>);
62 
63   void handle_invalid_release(Ptr<void>) ATTRIBUTE_NORETURN;
64   void handle_invalid_get_ptr(Uint32 i) ATTRIBUTE_NORETURN;
65   void handle_inconsistent_release(Ptr<void>) ATTRIBUTE_NORETURN;
66 };
67 
68 inline
69 bool
seize(Ptr<void> & ptr)70 WOPool::seize(Ptr<void>& ptr)
71 {
72   Uint32 pos = m_current_pos;
73   Uint32 size = m_record_info.m_size;
74   WOPage *pageP = m_current_page;
75   if (likely(pos + size < WOPage::WOPAGE_WORDS))
76   {
77     ptr.i = (m_current_page_no << POOL_RECORD_BITS) + pos;
78     ptr.p = (pageP->m_data + pos);
79     pageP->m_data[pos+m_record_info.m_offset_magic] = ~(Uint32)m_record_info.m_type_id;
80     m_current_pos = pos + size;
81     m_current_ref_count++;
82     return true;
83   }
84 
85   return seize_new_page(ptr);
86 }
87 
88 inline
89 void
release(Ptr<void> ptr)90 WOPool::release(Ptr<void> ptr)
91 {
92   Uint32 cur_page = m_current_page_no;
93   Uint32 ptr_page = ptr.i >> POOL_RECORD_BITS;
94   Uint32 *magic_ptr = (((Uint32*)ptr.p)+m_record_info.m_offset_magic);
95   Uint32 magic_val = *magic_ptr;
96 
97   if (likely(magic_val == ~(Uint32)m_record_info.m_type_id))
98   {
99     * magic_ptr = 0;
100     if (cur_page == ptr_page)
101     {
102       if (m_current_ref_count == 1)
103       {
104 	m_current_pos = 0;
105       }
106       m_current_ref_count--;
107       return;
108     }
109     return release_not_current(ptr);
110   }
111   handle_invalid_release(ptr);
112 }
113 
114 inline
115 void*
getPtr(Uint32 i)116 WOPool::getPtr(Uint32 i)
117 {
118   Uint32 page_no = i >> POOL_RECORD_BITS;
119   Uint32 page_idx = i & POOL_RECORD_MASK;
120   WOPage * page = m_memroot + page_no;
121   Uint32 * record = page->m_data + page_idx;
122   Uint32 magic_val = * (record + m_record_info.m_offset_magic);
123   if (likely(magic_val == ~(Uint32)m_record_info.m_type_id))
124   {
125     return record;
126   }
127   handle_invalid_get_ptr(i);
128   return 0;                                     /* purify: deadcode */
129 }
130 
131 #endif
132