1 /* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
22
23 #ifndef ARENA_POOL_HPP
24 #define ARENA_POOL_HPP
25
26 #include "Pool.hpp"
27 #include "RWPool.hpp"
28
29 struct ArenaBlock
30 {
31 Uint32 m_magic;
32 union {
33 Uint32 m_next_block;
34 Uint32 nextPool;
35 };
36
37 Uint32 m_data[1];
38
39 STATIC_CONST( HeaderSize = 2 );
40
computeBlockSizeInWordsArenaBlock41 static Uint32 computeBlockSizeInWords(Uint32 datasz) {
42 return 16 * (((datasz + 2) + 8) / 16);
43 }
44 };
45
46 struct ArenaHead
47 {
ArenaHeadArenaHead48 ArenaHead() {
49 m_first_free = ~(Uint16)0;
50 m_block_size = 0;
51 m_first_block = RNIL;
52 m_current_block = RNIL;
53 m_current_block_ptr = 0;
54 }
55
56 ArenaBlock * m_current_block_ptr;
57 Uint32 m_first_block;
58 Uint32 m_current_block;
59 Uint16 m_first_free;
60 Uint16 m_block_size;
61 };
62
63 class ArenaPool; // forward
64
65 class ArenaAllocator
66 {
67 RWPool m_pool;
68 Uint32 m_block_size;
69 friend class ArenaPool;
70 public:
ArenaAllocator()71 ArenaAllocator() {}
72 void init(Uint32 blockSize, Uint32 type_id, const Pool_context& pc);
73
74 bool seize(ArenaHead&);
75 void release(ArenaHead&);
76 };
77
78 class ArenaPool
79 {
80 public:
ArenaPool()81 ArenaPool() {}
82
83 void init(ArenaAllocator*, const Record_info& ri, const Pool_context& pc);
84
seize(Ptr<void> &)85 bool seize(Ptr<void>&) { assert(false); return false; } // Not implemented...
86
87 bool seize(ArenaHead&, Ptr<void>&);
88 void release(Ptr<void>);
89 void * getPtr(Uint32 i);
90
91 private:
92 void handle_invalid_release(Ptr<void>) ATTRIBUTE_NORETURN;
93
94 Record_info m_record_info;
95 ArenaAllocator * m_allocator;
96 };
97
98 class LocalArenaPoolImpl
99 {
100 ArenaHead & m_head;
101 ArenaPool & m_pool;
102 public:
LocalArenaPoolImpl(ArenaHead & head,ArenaPool & pool)103 LocalArenaPoolImpl(ArenaHead& head, ArenaPool & pool)
104 : m_head(head), m_pool(pool) {}
105
seize(Ptr<void> & ptr)106 bool seize(Ptr<void> & ptr) { return m_pool.seize(m_head, ptr); }
release(Ptr<void> ptr)107 void release(Ptr<void> ptr) { m_pool.release(ptr); }
getPtr(Uint32 i)108 void * getPtr(Uint32 i) { return m_pool.getPtr(i); }
109 };
110
111 inline
112 void*
getPtr(Uint32 i)113 ArenaPool::getPtr(Uint32 i)
114 {
115 return m_allocator->m_pool.getPtr(m_record_info, i);
116 }
117
118 inline
119 void
release(Ptr<void> ptr)120 ArenaPool::release(Ptr<void> ptr)
121 {
122 Uint32 * record_ptr = static_cast<Uint32*>(ptr.p);
123 Uint32 off = m_record_info.m_offset_magic;
124 Uint32 type_id = m_record_info.m_type_id;
125 Uint32 magic_val = * (record_ptr + off);
126
127 if (likely(magic_val == ~type_id))
128 {
129 * (record_ptr + off) = 0;
130 return;
131 }
132 handle_invalid_release(ptr);
133 }
134
135 #endif
136