| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 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 mem/memory.cc |
| 29 |
The memory management |
| 30 |
|
| 31 |
Created 6/9/1994 Heikki Tuuri |
| 32 |
*************************************************************************/ |
| 33 |
|
| 34 |
/* NOTE: The functions in this file should only use functions from |
| 35 |
other files in library. The code in this file is used to make a library for |
| 36 |
external tools. */ |
| 37 |
|
| 38 |
#include "ha_prototypes.h" |
| 39 |
#include "mem0mem.h" |
| 40 |
|
| 41 |
#ifndef UNIV_LIBRARY |
| 42 |
#include "buf0buf.h" |
| 43 |
#include "srv0srv.h" |
| 44 |
#endif /* UNIV_LIBRARY */ |
| 45 |
#include <stdarg.h> |
| 46 |
|
| 47 |
/** Duplicates a NUL-terminated string, allocated from a memory heap. |
| 48 |
@param[in] heap memory heap where string is allocated |
| 49 |
@param[in] str string to be copied |
| 50 |
@return own: a copy of the string */ |
| 51 |
char *mem_heap_strdup(mem_heap_t *heap, const char *str) { |
| 52 |
return (static_cast<char *>(mem_heap_dup(heap, str, strlen(str) + 1))); |
| 53 |
} |
| 54 |
|
| 55 |
/** Duplicate a block of data, allocated from a memory heap. |
| 56 |
@return own: a copy of the data */ |
| 57 |
void *mem_heap_dup( |
| 58 |
mem_heap_t *heap, /*!< in: memory heap where copy is allocated */ |
| 59 |
const void *data, /*!< in: data to be copied */ |
| 60 |
ulint len) /*!< in: length of data, in bytes */ |
| 61 |
{ |
| 62 |
return (memcpy(mem_heap_alloc(heap, len), data, len)); |
| 63 |
} |
| 64 |
|
| 65 |
/** Concatenate two strings and return the result, using a memory heap. |
| 66 |
@return own: the result */ |
| 67 |
char *mem_heap_strcat( |
| 68 |
mem_heap_t *heap, /*!< in: memory heap where string is allocated */ |
| 69 |
const char *s1, /*!< in: string 1 */ |
| 70 |
const char *s2) /*!< in: string 2 */ |
| 71 |
{ |
| 72 |
char *s; |
| 73 |
ulint s1_len = strlen(s1); |
| 74 |
ulint s2_len = strlen(s2); |
| 75 |
|
| 76 |
s = static_cast<char *>(mem_heap_alloc(heap, s1_len + s2_len + 1)); |
| 77 |
|
| 78 |
memcpy(s, s1, s1_len); |
| 79 |
memcpy(s + s1_len, s2, s2_len); |
| 80 |
|
| 81 |
s[s1_len + s2_len] = '\0'; |
| 82 |
|
| 83 |
return (s); |
| 84 |
} |
| 85 |
|
| 86 |
/** Helper function for mem_heap_printf. |
| 87 |
@return length of formatted string, including terminating NUL */ |
| 88 |
static ulint mem_heap_printf_low( |
| 89 |
char *buf, /*!< in/out: buffer to store formatted string |
| 90 |
in, or NULL to just calculate length */ |
| 91 |
const char *format, /*!< in: format string */ |
| 92 |
va_list ap) /*!< in: arguments */ |
| 93 |
{ |
| 94 |
ulint len = 0; |
| 95 |
|
| 96 |
while (*format) { |
| 97 |
/* Does this format specifier have the 'l' length modifier. */ |
| 98 |
bool is_long = false; |
| 99 |
|
| 100 |
/* Length of one parameter. */ |
| 101 |
size_t plen; |
| 102 |
|
| 103 |
if (*format++ != '%') { |
| 104 |
/* Non-format character. */ |
| 105 |
|
| 106 |
len++; |
| 107 |
|
| 108 |
if (buf) { |
| 109 |
*buf++ = *(format - 1); |
| 110 |
} |
| 111 |
|
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
if (*format == 'l') { |
| 116 |
is_long = true; |
| 117 |
format++; |
| 118 |
} |
| 119 |
|
| 120 |
switch (*format++) { |
| 121 |
case 's': |
| 122 |
/* string */ |
| 123 |
{ |
| 124 |
char *s = va_arg(ap, char *); |
| 125 |
|
| 126 |
/* "%ls" is a non-sensical format specifier. */ |
| 127 |
ut_a(!is_long); |
| 128 |
|
| 129 |
plen = strlen(s); |
| 130 |
len += plen; |
| 131 |
|
| 132 |
if (buf) { |
| 133 |
memcpy(buf, s, plen); |
| 134 |
buf += plen; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
break; |
| 139 |
|
| 140 |
case 'u': |
| 141 |
/* unsigned int */ |
| 142 |
{ |
| 143 |
char tmp[32]; |
| 144 |
unsigned long val; |
| 145 |
|
| 146 |
/* We only support 'long' values for now. */ |
| 147 |
ut_a(is_long); |
| 148 |
|
| 149 |
val = va_arg(ap, unsigned long); |
| 150 |
|
| 151 |
plen = sprintf(tmp, "%lu", val); |
| 152 |
len += plen; |
| 153 |
|
| 154 |
if (buf) { |
| 155 |
memcpy(buf, tmp, plen); |
| 156 |
buf += plen; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
break; |
| 161 |
|
| 162 |
case '%': |
| 163 |
|
| 164 |
/* "%l%" is a non-sensical format specifier. */ |
| 165 |
ut_a(!is_long); |
| 166 |
|
| 167 |
len++; |
| 168 |
|
| 169 |
if (buf) { |
| 170 |
*buf++ = '%'; |
| 171 |
} |
| 172 |
|
| 173 |
break; |
| 174 |
|
| 175 |
default: |
| 176 |
ut_error; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/* For the NUL character. */ |
| 181 |
len++; |
| 182 |
|
| 183 |
if (buf) { |
| 184 |
*buf = '\0'; |
| 185 |
} |
| 186 |
|
| 187 |
return (len); |
| 188 |
} |
| 189 |
|
| 190 |
/** A simple sprintf replacement that dynamically allocates the space for the |
| 191 |
formatted string from the given heap. This supports a very limited set of |
| 192 |
the printf syntax: types 's' and 'u' and length modifier 'l' (which is |
| 193 |
required for the 'u' type). |
| 194 |
@return heap-allocated formatted string */ |
| 195 |
char *mem_heap_printf(mem_heap_t *heap, /*!< in: memory heap */ |
| 196 |
const char *format, /*!< in: format string */ |
| 197 |
...) { |
| 198 |
va_list ap; |
| 199 |
char *str; |
| 200 |
ulint len; |
| 201 |
|
| 202 |
/* Calculate length of string */ |
| 203 |
len = 0; |
| 204 |
va_start(ap, format); |
| 205 |
len = mem_heap_printf_low(nullptr, format, ap); |
| 206 |
va_end(ap); |
| 207 |
|
| 208 |
/* Now create it for real. */ |
| 209 |
str = static_cast<char *>(mem_heap_alloc(heap, len)); |
| 210 |
va_start(ap, format); |
| 211 |
mem_heap_printf_low(str, format, ap); |
| 212 |
va_end(ap); |
| 213 |
|
| 214 |
return (str); |
| 215 |
} |
| 216 |
|
| 217 |
#ifdef UNIV_DEBUG |
| 218 |
|
| 219 |
/** Validates the contents of a memory heap. |
| 220 |
Checks a memory heap for consistency, prints the contents if any error |
| 221 |
is detected. A fatal error is logged if an error is detected. |
| 222 |
@param[in] heap Memory heap to validate. */ |
| 223 |
void mem_heap_validate(const mem_heap_t *heap) { |
| 224 |
ulint size = 0; |
| 225 |
|
| 226 |
for (const mem_block_t *block = heap; block != nullptr; |
| 227 |
block = UT_LIST_GET_NEXT(list, block)) { |
| 228 |
mem_block_validate(block); |
| 229 |
|
| 230 |
switch (block->type) { |
| 231 |
case MEM_HEAP_DYNAMIC: |
| 232 |
break; |
| 233 |
case MEM_HEAP_BUFFER: |
| 234 |
case MEM_HEAP_BUFFER | MEM_HEAP_BTR_SEARCH: |
| 235 |
ut_ad(block->len <= UNIV_PAGE_SIZE); |
| 236 |
break; |
| 237 |
default: |
| 238 |
ut_error; |
| 239 |
} |
| 240 |
|
| 241 |
size += block->len; |
| 242 |
} |
| 243 |
|
| 244 |
ut_ad(size == heap->total_size); |
| 245 |
} |
| 246 |
#endif /* UNIV_DEBUG */ |
| 247 |
|
| 248 |
mem_block_t *mem_heap_create_block(mem_heap_t *heap, ulint n, |
| 249 |
IF_DEBUG(const char *file_name, ulint line, ) |
| 250 |
ulint type) { |
| 251 |
#ifndef UNIV_LIBRARY |
| 252 |
buf_block_t *buf_block = nullptr; |
| 253 |
#endif /* !UNIV_LIBRARY */ |
| 254 |
mem_block_t *block; |
| 255 |
ulint len; |
| 256 |
|
| 257 |
ut_ad((type == MEM_HEAP_DYNAMIC) || (type == MEM_HEAP_BUFFER) || |
| 258 |
(type == MEM_HEAP_BUFFER + MEM_HEAP_BTR_SEARCH)); |
| 259 |
|
| 260 |
if (heap != nullptr) { |
| 261 |
mem_block_validate(heap); |
| 262 |
ut_d(mem_heap_validate(heap)); |
| 263 |
} |
| 264 |
|
| 265 |
/* In dynamic allocation, calculate the size: block header + data. */ |
| 266 |
len = MEM_BLOCK_HEADER_SIZE + MEM_SPACE_NEEDED(n); |
| 267 |
|
| 268 |
#if !defined(UNIV_LIBRARY) && !defined(UNIV_HOTBACKUP) |
| 269 |
if (type == MEM_HEAP_DYNAMIC || len < UNIV_PAGE_SIZE / 2) { |
| 270 |
ut_ad(type == MEM_HEAP_DYNAMIC || n <= MEM_MAX_ALLOC_IN_BUF); |
| 271 |
|
| 272 |
block = static_cast<mem_block_t *>( |
| 273 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, len)); |
| 274 |
} else { |
| 275 |
len = UNIV_PAGE_SIZE; |
| 276 |
|
| 277 |
if ((type & MEM_HEAP_BTR_SEARCH) && heap) { |
| 278 |
/* We cannot allocate the block from the buffer pool, but must get the |
| 279 |
free block from free block field of the heap base block. This is because |
| 280 |
we hold the X latch on AHI, and getting a block by eviction from LRU might |
| 281 |
require it too. See btr_search_check_free_space_in_heap. |
| 282 |
It is safe to do load()->if(!=null)->store(null) as the methods that do |
| 283 |
such store of nullptr value are synchronized. The if statement is |
| 284 |
important, because we can suffer from ABA problem if the value read is |
| 285 |
nullptr, as it could be replaced with non-null by any concurrent |
| 286 |
btr_search_check_free_space_in_heap, which is the only not synchronized |
| 287 |
modify access to heap. */ |
| 288 |
ut_ad(heap->free_block_ptr != nullptr); |
| 289 |
buf_block = static_cast<buf_block_t *>(heap->free_block_ptr->load()); |
| 290 |
|
| 291 |
if (!buf_block) { |
| 292 |
return nullptr; |
| 293 |
} |
| 294 |
heap->free_block_ptr->store(nullptr); |
| 295 |
} else { |
| 296 |
buf_block = buf_block_alloc(nullptr); |
| 297 |
} |
| 298 |
|
| 299 |
block = (mem_block_t *)buf_block->frame; |
| 300 |
} |
| 301 |
|
| 302 |
if (block == nullptr) { |
| 303 |
#ifdef UNIV_NO_ERR_MSGS |
| 304 |
ib::fatal(UT_LOCATION_HERE) |
| 305 |
#else |
| 306 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_1274) |
| 307 |
#endif /* !UNIV_NO_ERR_MSGS */ |
| 308 |
<< "Unable to allocate memory of size " << len << "."; |
| 309 |
} |
| 310 |
|
| 311 |
/* Make only the header part of the block accessible. If it is a block |
| 312 |
from the buffer pool, the len will be UNIV_PAGE_SIZE already. */ |
| 313 |
UNIV_MEM_FREE(block, len); |
| 314 |
UNIV_MEM_ALLOC(block, MEM_BLOCK_HEADER_SIZE); |
| 315 |
|
| 316 |
block->buf_block = buf_block; |
| 317 |
block->free_block_ptr = nullptr; |
| 318 |
|
| 319 |
#else /* !UNIV_LIBRARY && !UNIV_HOTBACKUP */ |
| 320 |
len = MEM_BLOCK_HEADER_SIZE + MEM_SPACE_NEEDED(n); |
| 321 |
block = static_cast<mem_block_t *>( |
| 322 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, len)); |
| 323 |
ut_a(block); |
| 324 |
block->free_block_ptr = nullptr; |
| 325 |
#endif /* !UNIV_LIBRARY && !UNIV_HOTBACKUP */ |
| 326 |
|
| 327 |
ut_d(ut_strlcpy_rev(block->file_name, file_name, sizeof(block->file_name))); |
| 328 |
ut_d(block->line = line); |
| 329 |
|
| 330 |
block->magic_n = MEM_BLOCK_MAGIC_N; |
| 331 |
mem_block_set_len(block, len); |
| 332 |
mem_block_set_type(block, type); |
| 333 |
mem_block_set_start(block, MEM_BLOCK_HEADER_SIZE); |
| 334 |
mem_block_set_free(block, MEM_BLOCK_HEADER_SIZE); |
| 335 |
|
| 336 |
if (UNIV_UNLIKELY(heap == nullptr)) { |
| 337 |
/* This is the first block of the heap. The field |
| 338 |
total_size should be initialized here */ |
| 339 |
block->total_size = len; |
| 340 |
} else { |
| 341 |
/* Not the first allocation for the heap. This block's |
| 342 |
total_length field should be set to undefined and never |
| 343 |
actually used. */ |
| 344 |
ut_d(block->total_size = ULINT_UNDEFINED); |
| 345 |
UNIV_MEM_FREE(&block->total_size, sizeof block->total_size); |
| 346 |
|
| 347 |
heap->total_size += len; |
| 348 |
} |
| 349 |
|
| 350 |
ut_ad((ulint)MEM_BLOCK_HEADER_SIZE < len); |
| 351 |
|
| 352 |
return (block); |
| 353 |
} |
| 354 |
|
| 355 |
/** Adds a new block to a memory heap. |
| 356 |
@return created block, NULL if did not succeed (only possible for |
| 357 |
MEM_HEAP_BTR_SEARCH type heaps) */ |
| 358 |
mem_block_t *mem_heap_add_block(mem_heap_t *heap, /*!< in: memory heap */ |
| 359 |
ulint n) /*!< in: number of bytes user needs */ |
| 360 |
{ |
| 361 |
mem_block_t *block; |
| 362 |
mem_block_t *new_block; |
| 363 |
ulint new_size; |
| 364 |
|
| 365 |
ut_d(mem_block_validate(heap)); |
| 366 |
|
| 367 |
block = UT_LIST_GET_LAST(heap->base); |
| 368 |
|
| 369 |
/* We have to allocate a new block. The size is always at least |
| 370 |
doubled until the standard size is reached. After that the size |
| 371 |
stays the same, except in cases where the caller needs more space. */ |
| 372 |
|
| 373 |
new_size = 2 * mem_block_get_len(block); |
| 374 |
|
| 375 |
if (heap->type != MEM_HEAP_DYNAMIC) { |
| 376 |
/* From the buffer pool we allocate buffer frames */ |
| 377 |
ut_a(n <= MEM_MAX_ALLOC_IN_BUF); |
| 378 |
|
| 379 |
if (new_size > MEM_MAX_ALLOC_IN_BUF) { |
| 380 |
new_size = MEM_MAX_ALLOC_IN_BUF; |
| 381 |
} |
| 382 |
} else if (new_size > MEM_BLOCK_STANDARD_SIZE) { |
| 383 |
new_size = MEM_BLOCK_STANDARD_SIZE; |
| 384 |
} |
| 385 |
|
| 386 |
if (new_size < n) { |
| 387 |
new_size = n; |
| 388 |
} |
| 389 |
|
| 390 |
new_block = mem_heap_create_block( |
| 391 |
heap, new_size, IF_DEBUG(heap->file_name, heap->line, ) heap->type); |
| 392 |
if (new_block == nullptr) { |
| 393 |
return (nullptr); |
| 394 |
} |
| 395 |
|
| 396 |
/* Add the new block as the last block */ |
| 397 |
|
| 398 |
UT_LIST_INSERT_AFTER(heap->base, block, new_block); |
| 399 |
|
| 400 |
return (new_block); |
| 401 |
} |
| 402 |
|
| 403 |
/** Frees a block from a memory heap. */ |
| 404 |
void mem_heap_block_free(mem_heap_t *heap, /*!< in: heap */ |
| 405 |
mem_block_t *block) /*!< in: block to free */ |
| 406 |
{ |
| 407 |
#ifndef UNIV_LIBRARY |
| 408 |
buf_block_t *buf_block; |
| 409 |
|
| 410 |
buf_block = static_cast<buf_block_t *>(block->buf_block); |
| 411 |
#endif /* !UNIV_LIBRARY */ |
| 412 |
|
| 413 |
mem_block_validate(block); |
| 414 |
|
| 415 |
UT_LIST_REMOVE(heap->base, block); |
| 416 |
|
| 417 |
ut_ad(heap->total_size >= block->len); |
| 418 |
heap->total_size -= block->len; |
| 419 |
|
| 420 |
#ifndef UNIV_LIBRARY |
| 421 |
ulint type = heap->type; |
| 422 |
ulint len = block->len; |
| 423 |
#endif /* !UNIV_LIBRARY */ |
| 424 |
|
| 425 |
block->magic_n = MEM_FREED_BLOCK_MAGIC_N; |
| 426 |
|
| 427 |
#ifdef UNIV_DEBUG |
| 428 |
if (mem_block_get_start(block) != mem_block_get_free(block)) { |
| 429 |
validate_no_mans_land((byte *)block + mem_block_get_start(block), |
| 430 |
MEM_NO_MANS_LAND_BEFORE_BYTE); |
| 431 |
validate_no_mans_land( |
| 432 |
(byte *)block + mem_block_get_free(block) - MEM_NO_MANS_LAND, |
| 433 |
MEM_NO_MANS_LAND_AFTER_BYTE); |
| 434 |
} |
| 435 |
#endif |
| 436 |
|
| 437 |
#if !defined(UNIV_LIBRARY) && !defined(UNIV_HOTBACKUP) |
| 438 |
if (type == MEM_HEAP_DYNAMIC || len < UNIV_PAGE_SIZE / 2) { |
| 439 |
ut_ad(!buf_block); |
| 440 |
ut::free(block); |
| 441 |
} else { |
| 442 |
ut_ad(type & MEM_HEAP_BUFFER); |
| 443 |
|
| 444 |
/* Make memory available again for buffer pool, as we set parts |
| 445 |
of block to "free" state in heap allocator. */ |
| 446 |
UNIV_MEM_ALLOC(block, UNIV_PAGE_SIZE); |
| 447 |
buf_block_free(buf_block); |
| 448 |
} |
| 449 |
#else /* !UNIV_LIBRARY && !UNIV_HOTBACKUP */ |
| 450 |
ut::free(block); |
| 451 |
#endif /* !UNIV_LIBRARY && !UNIV_HOTBACKUP */ |
| 452 |
} |
| 453 |
|
| 454 |
#ifndef UNIV_HOTBACKUP |
| 455 |
#ifndef UNIV_LIBRARY |
| 456 |
/** Frees the free_block field from a memory heap. */ |
| 457 |
void mem_heap_free_block_free(mem_heap_t *heap) /*!< in: heap */ |
| 458 |
{ |
| 459 |
if (heap->free_block_ptr == nullptr) { |
| 460 |
return; |
| 461 |
} |
| 462 |
|
| 463 |
/* It is safe to do load()->if(!=null)->store(null) as the methods that do |
| 464 |
such store of nullptr value are synchronized. The if statement is important, |
| 465 |
because we can suffer from ABA problem if the value read is nullptr, as it |
| 466 |
could be replaced with non-null by any concurrent |
| 467 |
btr_search_check_free_space_in_heap, which is the only not synchronized |
| 468 |
modify access to heap. */ |
| 469 |
const auto block = static_cast<buf_block_t *>(heap->free_block_ptr->load()); |
| 470 |
if (block) { |
| 471 |
#ifdef UNIV_DEBUG_VALGRIND |
| 472 |
const auto frame = block->frame; |
| 473 |
/* Make memory available again for the buffer pool, since |
| 474 |
we previously set parts of the block to "free" state in |
| 475 |
heap allocator. */ |
| 476 |
UNIV_MEM_ALLOC(frame, UNIV_PAGE_SIZE); |
| 477 |
#endif |
| 478 |
|
| 479 |
heap->free_block_ptr->store(nullptr); |
| 480 |
buf_block_free(block); |
| 481 |
} |
| 482 |
} |
| 483 |
#endif /* !UNIV_LIBRARY */ |
| 484 |
#endif /* !UNIV_HOTBACKUP */ |