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 RWPOOL_HPP
26 #define RWPOOL_HPP
27 
28 #include "Pool.hpp"
29 
30 struct RWPage
31 {
32   STATIC_CONST( RWPAGE_WORDS = GLOBAL_PAGE_SIZE_WORDS - 4 );
33 
34   Uint32 m_type_id;
35   Uint16 m_first_free;
36   Uint16 m_ref_count;
37   Uint32 m_next_page;
38   Uint32 m_prev_page;
39   Uint32 m_data[RWPAGE_WORDS];
40 };
41 
42 /**
43  * Read Write  Pool
44  */
45 struct RWPool
46 {
47   Record_info m_record_info;
48   RWPage* m_memroot;
49   RWPage* m_current_page;
50   Pool_context m_ctx;
51   Uint32 m_first_free_page;
52   Uint32 m_current_page_no;
53   Uint16 m_current_pos;
54   Uint16 m_current_first_free;
55   Uint16 m_current_ref_count;
56 public:
57   RWPool();
58 
59   void init(const Record_info& ri, const Pool_context& pc);
60   bool seize(Ptr<void>&);
61   void release(Ptr<void>);
62   void * getPtr(Uint32 i);
63   void * getPtr(const Record_info&ri, Uint32 i);
64 
65   STATIC_CONST( WORDS_PER_PAGE = RWPage::RWPAGE_WORDS );
66 
67 private:
68   void handle_invalid_release(Ptr<void>) ATTRIBUTE_NORETURN;
69   void handle_invalid_get_ptr(Uint32 i) ATTRIBUTE_NORETURN;
70 };
71 
72 inline
73 void*
getPtr(Uint32 i)74 RWPool::getPtr(Uint32 i)
75 {
76   Uint32 page_no = i >> POOL_RECORD_BITS;
77   Uint32 page_idx = i & POOL_RECORD_MASK;
78   RWPage * page = m_memroot + page_no;
79   Uint32 * record = page->m_data + page_idx;
80   Uint32 magic_val = * (record + m_record_info.m_offset_magic);
81   if (likely(magic_val == ~(Uint32)m_record_info.m_type_id))
82   {
83     return record;
84   }
85   handle_invalid_get_ptr(i);
86   return 0;                                     /* purify: deadcode */
87 }
88 
89 inline
90 void*
getPtr(const Record_info & ri,Uint32 i)91 RWPool::getPtr(const Record_info &ri, Uint32 i)
92 {
93   Uint32 page_no = i >> POOL_RECORD_BITS;
94   Uint32 page_idx = i & POOL_RECORD_MASK;
95   RWPage * page = m_memroot + page_no;
96   Uint32 * record = page->m_data + page_idx;
97   Uint32 magic_val = * (record + ri.m_offset_magic);
98   if (likely(magic_val == ~(Uint32)ri.m_type_id))
99   {
100     return record;
101   }
102   handle_invalid_get_ptr(i);
103   return 0;                                     /* purify: deadcode */
104 }
105 
106 #endif
107