| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2013, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/ut0pool.h |
| 29 |
Object pool. |
| 30 |
|
| 31 |
Created 2012-Feb-26 Sunny Bains |
| 32 |
***********************************************************************/ |
| 33 |
|
| 34 |
#ifndef ut0pool_h |
| 35 |
#define ut0pool_h |
| 36 |
|
| 37 |
#include <functional> |
| 38 |
#include <queue> |
| 39 |
#include <vector> |
| 40 |
|
| 41 |
#include "ut0new.h" |
| 42 |
|
| 43 |
/** Allocate the memory for the object in blocks. We keep the objects sorted |
| 44 |
on pointer so that they are closer together in case they have to be iterated |
| 45 |
over in a list. */ |
| 46 |
template <typename Type, typename Factory, typename LockStrategy> |
| 47 |
struct Pool { |
| 48 |
typedef Type value_type; |
| 49 |
|
| 50 |
// FIXME: Add an assertion to check alignment and offset is |
| 51 |
// as we expect it. Also, sizeof(void*) can be 8, can we improve on this. |
| 52 |
struct Element { |
| 53 |
Pool *m_pool; |
| 54 |
value_type m_type; |
| 55 |
}; |
| 56 |
|
| 57 |
/** Constructor |
| 58 |
@param size size of the memory block */ |
| 59 |
Pool(size_t size) : m_end(), m_start(), m_size(size), m_last() { |
| 60 |
ut_a(size >= sizeof(Element)); |
| 61 |
|
| 62 |
m_lock_strategy.create(); |
| 63 |
|
| 64 |
ut_a(m_start == nullptr); |
| 65 |
|
| 66 |
m_start = reinterpret_cast<Element *>( |
| 67 |
ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, m_size)); |
| 68 |
|
| 69 |
m_last = m_start; |
| 70 |
|
| 71 |
m_end = &m_start[m_size / sizeof(*m_start)]; |
| 72 |
|
| 73 |
/* Note: Initialise only a small subset, even though we have |
| 74 |
allocated all the memory. This is required only because PFS |
| 75 |
(MTR) results change if we instantiate too many mutexes up |
| 76 |
front. */ |
| 77 |
|
| 78 |
init(std::min(size_t(16), size_t(m_end - m_start))); |
| 79 |
|
| 80 |
ut_ad(m_pqueue.size() <= size_t(m_last - m_start)); |
| 81 |
} |
| 82 |
|
| 83 |
/** Destructor */ |
| 84 |
~Pool() { |
| 85 |
m_lock_strategy.destroy(); |
| 86 |
|
| 87 |
for (Element *elem = m_start; elem != m_last; ++elem) { |
| 88 |
ut_ad(elem->m_pool == this); |
| 89 |
Factory::destroy(&elem->m_type); |
| 90 |
} |
| 91 |
|
| 92 |
ut::free(m_start); |
| 93 |
m_end = m_last = m_start = nullptr; |
| 94 |
m_size = 0; |
| 95 |
} |
| 96 |
|
| 97 |
/** Get an object from the pool. |
| 98 |
@return a free instance or NULL if exhausted. */ |
| 99 |
Type *get() { |
| 100 |
Element *elem; |
| 101 |
|
| 102 |
m_lock_strategy.enter(); |
| 103 |
|
| 104 |
if (!m_pqueue.empty()) { |
| 105 |
elem = m_pqueue.top(); |
| 106 |
m_pqueue.pop(); |
| 107 |
|
| 108 |
} else if (m_last < m_end) { |
| 109 |
/* Initialise the remaining elements. */ |
| 110 |
init(m_end - m_last); |
| 111 |
|
| 112 |
ut_ad(!m_pqueue.empty()); |
| 113 |
|
| 114 |
elem = m_pqueue.top(); |
| 115 |
m_pqueue.pop(); |
| 116 |
} else { |
| 117 |
elem = nullptr; |
| 118 |
} |
| 119 |
|
| 120 |
m_lock_strategy.exit(); |
| 121 |
|
| 122 |
return (elem != nullptr ? &elem->m_type : nullptr); |
| 123 |
} |
| 124 |
|
| 125 |
/** Add the object to the pool. |
| 126 |
@param ptr object to free */ |
| 127 |
static void mem_free(value_type *ptr) { |
| 128 |
Element *elem; |
| 129 |
byte *p = reinterpret_cast<byte *>(ptr + 1); |
| 130 |
|
| 131 |
elem = reinterpret_cast<Element *>(p - sizeof(*elem)); |
| 132 |
|
| 133 |
elem->m_pool->put(elem); |
| 134 |
} |
| 135 |
|
| 136 |
protected: |
| 137 |
// Disable copying |
| 138 |
Pool(const Pool &); |
| 139 |
Pool &operator=(const Pool &); |
| 140 |
|
| 141 |
private: |
| 142 |
/* We only need to compare on pointer address. */ |
| 143 |
typedef std::priority_queue<Element *, |
| 144 |
std::vector<Element *, ut::allocator<Element *>>, |
| 145 |
std::greater<Element *>> |
| 146 |
pqueue_t; |
| 147 |
|
| 148 |
/** Release the object to the free pool |
| 149 |
@param elem element to free */ |
| 150 |
void put(Element *elem) { |
| 151 |
m_lock_strategy.enter(); |
| 152 |
|
| 153 |
ut_ad(elem >= m_start && elem < m_last); |
| 154 |
|
| 155 |
ut_ad(Factory::debug(&elem->m_type)); |
| 156 |
|
| 157 |
m_pqueue.push(elem); |
| 158 |
|
| 159 |
m_lock_strategy.exit(); |
| 160 |
} |
| 161 |
|
| 162 |
/** Initialise the elements. |
| 163 |
@param n_elems Number of elements to initialise */ |
| 164 |
void init(size_t n_elems) { |
| 165 |
ut_ad(size_t(m_end - m_last) >= n_elems); |
| 166 |
|
| 167 |
for (size_t i = 0; i < n_elems; ++i, ++m_last) { |
| 168 |
m_last->m_pool = this; |
| 169 |
Factory::init(&m_last->m_type); |
| 170 |
m_pqueue.push(m_last); |
| 171 |
} |
| 172 |
|
| 173 |
ut_ad(m_last <= m_end); |
| 174 |
} |
| 175 |
|
| 176 |
private: |
| 177 |
/** Pointer to the last element */ |
| 178 |
Element *m_end; |
| 179 |
|
| 180 |
/** Pointer to the first element */ |
| 181 |
Element *m_start; |
| 182 |
|
| 183 |
/** Size of the block in bytes */ |
| 184 |
size_t m_size; |
| 185 |
|
| 186 |
/** Upper limit of used space */ |
| 187 |
Element *m_last; |
| 188 |
|
| 189 |
/** Priority queue ordered on the pointer addresses. */ |
| 190 |
pqueue_t m_pqueue; |
| 191 |
|
| 192 |
/** Lock strategy to use */ |
| 193 |
LockStrategy m_lock_strategy; |
| 194 |
}; |
| 195 |
|
| 196 |
template <typename Pool, typename LockStrategy> |
| 197 |
struct PoolManager { |
| 198 |
typedef Pool PoolType; |
| 199 |
typedef typename PoolType::value_type value_type; |
| 200 |
|
| 201 |
PoolManager(size_t size) : m_size(size) { create(); } |
| 202 |
|
| 203 |
~PoolManager() { |
| 204 |
destroy(); |
| 205 |
|
| 206 |
ut_a(m_pools.empty()); |
| 207 |
} |
| 208 |
|
| 209 |
/** Get an element from one of the pools. |
| 210 |
@return instance or NULL if pool is empty. */ |
| 211 |
value_type *get() { |
| 212 |
size_t index = 0; |
| 213 |
size_t delay = 1; |
| 214 |
value_type *ptr = nullptr; |
| 215 |
|
| 216 |
do { |
| 217 |
m_lock_strategy.enter(); |
| 218 |
|
| 219 |
ut_ad(!m_pools.empty()); |
| 220 |
|
| 221 |
size_t n_pools = m_pools.size(); |
| 222 |
|
| 223 |
PoolType *pool = m_pools[index % n_pools]; |
| 224 |
|
| 225 |
m_lock_strategy.exit(); |
| 226 |
|
| 227 |
ptr = pool->get(); |
| 228 |
|
| 229 |
if (ptr == nullptr && (index / n_pools) > 2) { |
| 230 |
if (!add_pool(n_pools)) { |
| 231 |
ib::error(ER_IB_MSG_FAILED_TO_ALLOCATE_WAIT, m_size, delay); |
| 232 |
|
| 233 |
/* There is nothing much we can do |
| 234 |
except crash and burn, however lets |
| 235 |
be a little optimistic and wait for |
| 236 |
a resource to be freed. */ |
| 237 |
std::this_thread::sleep_for(std::chrono::seconds(delay)); |
| 238 |
|
| 239 |
if (delay < 32) { |
| 240 |
delay <<= 1; |
| 241 |
} |
| 242 |
|
| 243 |
} else { |
| 244 |
delay = 1; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
++index; |
| 249 |
|
| 250 |
} while (ptr == nullptr); |
| 251 |
|
| 252 |
return (ptr); |
| 253 |
} |
| 254 |
|
| 255 |
static void mem_free(value_type *ptr) { PoolType::mem_free(ptr); } |
| 256 |
|
| 257 |
private: |
| 258 |
/** Add a new pool |
| 259 |
@param n_pools Number of pools that existed when the add pool was |
| 260 |
called. |
| 261 |
@return true on success */ |
| 262 |
bool add_pool(size_t n_pools) { |
| 263 |
bool added = false; |
| 264 |
|
| 265 |
m_lock_strategy.enter(); |
| 266 |
|
| 267 |
if (n_pools < m_pools.size()) { |
| 268 |
/* Some other thread already added a pool. */ |
| 269 |
added = true; |
| 270 |
} else { |
| 271 |
PoolType *pool; |
| 272 |
|
| 273 |
ut_ad(n_pools == m_pools.size()); |
| 274 |
|
| 275 |
pool = ut::new_withkey<PoolType>(UT_NEW_THIS_FILE_PSI_KEY, m_size); |
| 276 |
|
| 277 |
if (pool != nullptr) { |
| 278 |
ut_ad(n_pools <= m_pools.size()); |
| 279 |
|
| 280 |
m_pools.push_back(pool); |
| 281 |
|
| 282 |
added = true; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
ut_ad(n_pools < m_pools.size() || !added); |
| 287 |
|
| 288 |
m_lock_strategy.exit(); |
| 289 |
|
| 290 |
return (added); |
| 291 |
} |
| 292 |
|
| 293 |
/** Create the pool manager. */ |
| 294 |
void create() { |
| 295 |
ut_a(m_size > sizeof(value_type)); |
| 296 |
m_lock_strategy.create(); |
| 297 |
|
| 298 |
add_pool(0); |
| 299 |
} |
| 300 |
|
| 301 |
/** Release the resources. */ |
| 302 |
void destroy() { |
| 303 |
typename Pools::iterator it; |
| 304 |
typename Pools::iterator end = m_pools.end(); |
| 305 |
|
| 306 |
for (it = m_pools.begin(); it != end; ++it) { |
| 307 |
PoolType *pool = *it; |
| 308 |
|
| 309 |
ut::delete_(pool); |
| 310 |
} |
| 311 |
|
| 312 |
m_pools.clear(); |
| 313 |
|
| 314 |
m_lock_strategy.destroy(); |
| 315 |
} |
| 316 |
|
| 317 |
private: |
| 318 |
// Disable copying |
| 319 |
PoolManager(const PoolManager &); |
| 320 |
PoolManager &operator=(const PoolManager &); |
| 321 |
|
| 322 |
typedef std::vector<PoolType *, ut::allocator<PoolType *>> Pools; |
| 323 |
|
| 324 |
/** Size of each block */ |
| 325 |
size_t m_size; |
| 326 |
|
| 327 |
/** Pools managed this manager */ |
| 328 |
Pools m_pools; |
| 329 |
|
| 330 |
/** Lock strategy to use */ |
| 331 |
LockStrategy m_lock_strategy; |
| 332 |
}; |
| 333 |
|
| 334 |
#endif /* ut0pool_h */ |