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 #include "ArenaPool.hpp"
24 #include <ndbd_exit_codes.h>
25 #include <NdbOut.hpp>
26 
27 static
28 Uint32
computeBlockSize(Uint32 blockSz,Uint32 wpp)29 computeBlockSize(Uint32 blockSz, Uint32 wpp)
30 {
31   Uint32 minspill = wpp % blockSz;
32   Uint32 minspill_bs = blockSz;
33 
34   for (Uint32 i = 16; i<blockSz/4; i += 16)
35   {
36     Uint32 spillsz = wpp % (blockSz - i);
37     if (spillsz == 0)
38     {
39       return blockSz - i;
40     }
41     else if (spillsz < minspill)
42     {
43       minspill = spillsz;
44       minspill_bs = blockSz -i;
45     }
46   }
47   ndbout_c("blockSz: %u, wpp: %u -> %u (%u)",
48            blockSz, wpp, minspill_bs, minspill);
49   return minspill_bs;
50 }
51 
52 void
init(Uint32 sz,Uint32 type_id,const Pool_context & pc)53 ArenaAllocator::init(Uint32 sz, Uint32 type_id, const Pool_context& pc)
54 {
55   Uint32 blocksz = ArenaBlock::computeBlockSizeInWords(sz);
56   Uint32 wpp = m_pool.WORDS_PER_PAGE;
57 
58   Uint32 bs = computeBlockSize(blocksz, wpp);
59   Record_info ri;
60   ri.m_size = 4 * bs;
61   {
62     ArenaBlock tmp;
63     const char * off_base = (char*)&tmp;
64     const char * off_next = (char*)&tmp.nextPool;
65     const char * off_magic = (char*)&tmp.m_magic;
66 
67     ri.m_offset_next_pool = Uint32(off_next - off_base);
68     ri.m_offset_magic = Uint32(off_magic - off_base);
69   }
70   ri.m_type_id = type_id;
71   m_pool.init(ri, pc);
72   m_block_size = bs - ArenaBlock::HeaderSize;
73 }
74 
75 bool
seize(ArenaHead & ah)76 ArenaAllocator::seize(ArenaHead& ah)
77 {
78   Ptr<void> tmp;
79   if (m_pool.seize(tmp))
80   {
81     ah.m_first_block = tmp.i;
82     ah.m_current_block = tmp.i;
83     ah.m_block_size = m_block_size;
84     ah.m_current_block_ptr = static_cast<ArenaBlock*>(tmp.p);
85     ah.m_current_block_ptr->m_next_block = RNIL;
86     return true;
87   }
88   return false;
89 }
90 
91 void
release(ArenaHead & ah)92 ArenaAllocator::release(ArenaHead& ah)
93 {
94   Ptr<void> curr;
95   curr.i = ah.m_first_block;
96   while (curr.i != RNIL)
97   {
98     curr.p = m_pool.getPtr(curr.i);
99     Uint32 next = static_cast<ArenaBlock*>(curr.p)->m_next_block;
100     m_pool.release(curr);
101     curr.i = next;
102   }
103 
104   new (&ah) ArenaHead();
105 }
106 
107 void
init(ArenaAllocator * alloc,const Record_info & ri,const Pool_context &)108 ArenaPool::init(ArenaAllocator * alloc,
109                 const Record_info& ri, const Pool_context&)
110 {
111   m_record_info = ri;
112 #if SIZEOF_CHARP == 4
113   m_record_info.m_size = ((ri.m_size + 3) >> 2); // Align to word boundary
114 #else
115   m_record_info.m_size = ((ri.m_size + 7) >> 3) << 1; // align 8-byte
116 #endif
117   m_record_info.m_offset_magic = ((ri.m_offset_magic + 3) >> 2);
118   m_record_info.m_offset_next_pool = ((ri.m_offset_next_pool + 3) >> 2);
119   m_allocator = alloc;
120 }
121 
122 bool
seize(ArenaHead & ah,Ptr<void> & ptr)123 ArenaPool::seize(ArenaHead & ah, Ptr<void>& ptr)
124 {
125   Uint32 pos = ah.m_first_free;
126   Uint32 bs = ah.m_block_size;
127   Uint32 ptrI = ah.m_current_block;
128   ArenaBlock * block = ah.m_current_block_ptr;
129 
130   Uint32 sz = m_record_info.m_size;
131   Uint32 off = m_record_info.m_offset_magic;
132 
133   if (0)
134     ndbout_c("pos: %u sz: %u (sum: %u) bs: %u",
135              pos, sz, (pos + sz), bs);
136 
137   if (pos + sz <= bs)
138   {
139     /**
140      * Alloc in this block
141      */
142     ptr.i =
143       ((ptrI >> POOL_RECORD_BITS) << POOL_RECORD_BITS) +
144       (ptrI & POOL_RECORD_MASK) + pos + ArenaBlock::HeaderSize;
145     ptr.p = block->m_data + pos;
146     block->m_data[pos+off] = ~(Uint32)m_record_info.m_type_id;
147 
148     ah.m_first_free = pos + sz;
149     return true;
150   }
151   else
152   {
153     Ptr<void> tmp;
154     if (m_allocator->m_pool.seize(tmp))
155     {
156       ah.m_first_free = 0;
157       ah.m_current_block = tmp.i;
158       ah.m_current_block_ptr->m_next_block = tmp.i;
159       ah.m_current_block_ptr = static_cast<ArenaBlock*>(tmp.p);
160       ah.m_current_block_ptr->m_next_block = RNIL;
161       bool ret = seize(ah, ptr);
162       (void)ret;
163       assert(ret == true);
164       return true;
165     }
166   }
167   return false;
168 }
169 
170 void
handle_invalid_release(Ptr<void> ptr)171 ArenaPool::handle_invalid_release(Ptr<void> ptr)
172 {
173   char buf[255];
174 
175   //Uint32 pos = ptr.i & POOL_RECORD_MASK;
176   //Uint32 pageI = ptr.i >> POOL_RECORD_BITS;
177   Uint32 * record_ptr_p = (Uint32*)ptr.p;
178 
179   Uint32 magic = * (record_ptr_p + m_record_info.m_offset_magic);
180   BaseString::snprintf(buf, sizeof(buf),
181                        "Invalid memory release: ptr (%x %p) magic: (%.8x %.8x)",
182                        ptr.i, ptr.p, magic, m_record_info.m_type_id);
183 
184   m_allocator->m_pool.m_ctx.handleAbort(NDBD_EXIT_PRGERR, buf);
185 }
186