| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 1994, 2014, Oracle and/or its affiliates. All Rights Reserved. |
| 4 | Copyright (c) 2017, 2021, MariaDB Corporation. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU General Public License as published by the Free Software |
| 8 | Foundation; version 2 of the License. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License along with |
| 15 | this program; if not, write to the Free Software Foundation, Inc., |
| 16 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA |
| 17 | |
| 18 | *****************************************************************************/ |
| 19 | |
| 20 | /********************************************************************//** |
| 21 | @file mem/mem0mem.cc |
| 22 | The memory management |
| 23 | |
| 24 | Created 6/9/1994 Heikki Tuuri |
| 25 | *************************************************************************/ |
| 26 | |
| 27 | #include "mem0mem.h"cxx . . $ `<file>/storage/innobase/include/mem0mem.h`/ |
| 28 | #include "buf0buf.h"cxx . . $ `<file>/storage/innobase/include/buf0buf.h`/ |
| 29 | #include "buf0lru.h"cxx . . $ `<file>/storage/innobase/include/buf0lru.h`/ |
| 30 | #include "srv0srv.h"cxx . . $ `<file>/storage/innobase/include/srv0srv.h`/ |
| 31 | #include <stdarg.h> |
| 32 | |
| 33 | /**********************************************************************//** |
| 34 | Concatenate two strings and return the result, using a memory heap. |
| 35 | @return own: the result */ |
| 36 | char* |
| 37 | mem_heap_strcatcxx . . $ mem_heap_strcat(69d14ad9bd07f363).( |
| 38 | /*============*/ |
| 39 | mem_heap_tcxx . . $ mem_heap_t#* heap, /*!< in: memory heap where string is allocated */ |
| 40 | const char* s1, /*!< in: string 1 */ |
| 41 | const char* s2) /*!< in: string 2 */ |
| 42 | { |
| 43 | char* s; |
| 44 | ulintcxx . . $ ulint# s1_len = strlen(s1); |
| 45 | ulintcxx . . $ ulint# s2_len = strlen(s2); |
| 46 | |
| 47 | s = static_cast<char*>(mem_heap_alloccxx . . $ mem_heap_alloc(3f50e4c8b9ca77c0).(heap, s1_len + s2_len + 1)); |
| 48 | |
| 49 | memcpy(s, s1, s1_len); |
| 50 | memcpy(s + s1_len, s2, s2_len); |
| 51 | |
| 52 | s[s1_len + s2_len] = '\0'; |
| 53 | |
| 54 | return(s); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /****************************************************************//** |
| 59 | Helper function for mem_heap_printf. |
| 60 | @return length of formatted string, including terminating NUL */ |
| 61 | static |
| 62 | ulintcxx . . $ ulint# |
| 63 | mem_heap_printf_lowcxx . . $ mem_heap_printf_low(188d729aee829a2c).( |
| 64 | /*================*/ |
| 65 | char* buf, /*!< in/out: buffer to store formatted string |
| 66 | in, or NULL to just calculate length */ |
| 67 | const char* format, /*!< in: format string */ |
| 68 | va_list ap) /*!< in: arguments */ |
| 69 | { |
| 70 | ulintcxx . . $ ulint# len = 0; |
| 71 | |
| 72 | while (*format) { |
| 73 | |
| 74 | /* Does this format specifier have the 'l' length modifier. */ |
| 75 | iboolcxx . . $ `storage/innobase/include/univ.i:355:9`! is_long = FALSEcxx . . $ `include/my_global.h:477:9`!; |
| 76 | |
| 77 | /* Length of one parameter. */ |
| 78 | size_t plen; |
| 79 | |
| 80 | if (*format++ != '%') { |
| 81 | /* Non-format character. */ |
| 82 | |
| 83 | len++; |
| 84 | |
| 85 | if (buf) { |
| 86 | *buf++ = *(format - 1); |
| 87 | } |
| 88 | |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | if (*format == 'l') { |
| 93 | is_long = TRUEcxx . . $ `include/my_global.h:476:9`!; |
| 94 | format++; |
| 95 | } |
| 96 | |
| 97 | switch (*format++) { |
| 98 | case 's': |
| 99 | /* string */ |
| 100 | { |
| 101 | char* s = va_argcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:53:9`!(ap, char*); |
| 102 | |
| 103 | /* "%ls" is a non-sensical format specifier. */ |
| 104 | ut_acxx . . $ `storage/innobase/include/ut0dbg.h:50:9`!(!is_long); |
| 105 | |
| 106 | plen = strlen(s); |
| 107 | len += plen; |
| 108 | |
| 109 | if (buf) { |
| 110 | memcpy(buf, s, plen); |
| 111 | buf += plen; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | break; |
| 116 | |
| 117 | case 'u': |
| 118 | /* unsigned int */ |
| 119 | { |
| 120 | char tmp[32]; |
| 121 | unsigned long val; |
| 122 | |
| 123 | /* We only support 'long' values for now. */ |
| 124 | ut_acxx . . $ `storage/innobase/include/ut0dbg.h:50:9`!(is_long); |
| 125 | |
| 126 | val = va_argcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:53:9`!(ap, unsigned long); |
| 127 | |
| 128 | plen = size_t(snprintf(tmp, sizeof(tmp), |
| 129 | "%lu", val)); |
| 130 | len += plen; |
| 131 | |
| 132 | if (buf) { |
| 133 | memcpy(buf, tmp, plen); |
| 134 | buf += plen; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | break; |
| 139 | |
| 140 | case '%': |
| 141 | |
| 142 | /* "%l%" is a non-sensical format specifier. */ |
| 143 | ut_acxx . . $ `storage/innobase/include/ut0dbg.h:50:9`!(!is_long); |
| 144 | |
| 145 | len++; |
| 146 | |
| 147 | if (buf) { |
| 148 | *buf++ = '%'; |
| 149 | } |
| 150 | |
| 151 | break; |
| 152 | |
| 153 | default: |
| 154 | ut_errorcxx . . $ `storage/innobase/include/ut0dbg.h:58:9`!; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* For the NUL character. */ |
| 159 | len++; |
| 160 | |
| 161 | if (buf) { |
| 162 | *buf = '\0'; |
| 163 | } |
| 164 | |
| 165 | return(len); |
| 166 | } |
| 167 | |
| 168 | /****************************************************************//** |
| 169 | A simple sprintf replacement that dynamically allocates the space for the |
| 170 | formatted string from the given heap. This supports a very limited set of |
| 171 | the printf syntax: types 's' and 'u' and length modifier 'l' (which is |
| 172 | required for the 'u' type). |
| 173 | @return heap-allocated formatted string */ |
| 174 | char* |
| 175 | mem_heap_printfcxx . . $ mem_heap_printf(8e5f2cda99e9e381).( |
| 176 | /*============*/ |
| 177 | mem_heap_tcxx . . $ mem_heap_t#* heap, /*!< in: memory heap */ |
| 178 | const char* format, /*!< in: format string */ |
| 179 | ...) |
| 180 | { |
| 181 | va_list ap; |
| 182 | char* str; |
| 183 | ulintcxx . . $ ulint# len; |
| 184 | |
| 185 | /* Calculate length of string */ |
| 186 | len = 0; |
| 187 | va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`!(ap, format); |
| 188 | len = mem_heap_printf_lowcxx . . $ mem_heap_printf_low(188d729aee829a2c).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, format, ap); |
| 189 | va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`!(ap); |
| 190 | |
| 191 | /* Now create it for real. */ |
| 192 | str = static_cast<char*>(mem_heap_alloccxx . . $ mem_heap_alloc(3f50e4c8b9ca77c0).(heap, len)); |
| 193 | va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:50:9`!(ap, format); |
| 194 | mem_heap_printf_lowcxx . . $ mem_heap_printf_low(188d729aee829a2c).(str, format, ap); |
| 195 | va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stdarg.h:52:9`!(ap); |
| 196 | |
| 197 | return(str); |
| 198 | } |
| 199 | |
| 200 | #ifdef UNIV_DEBUG |
| 201 | /** Validates the contents of a memory heap. |
| 202 | Checks a memory heap for consistency, prints the contents if any error |
| 203 | is detected. A fatal error is logged if an error is detected. |
| 204 | @param[in] heap Memory heap to validate. */ |
| 205 | void |
| 206 | mem_heap_validate( |
| 207 | const mem_heap_t* heap) |
| 208 | { |
| 209 | ulint size = 0; |
| 210 | |
| 211 | for (const mem_block_t* block = heap; |
| 212 | block != NULL; |
| 213 | block = UT_LIST_GET_NEXT(list, block)) { |
| 214 | |
| 215 | switch (block->type) { |
| 216 | case MEM_HEAP_DYNAMIC: |
| 217 | break; |
| 218 | case MEM_HEAP_BUFFER: |
| 219 | ut_ad(block->len <= srv_page_size); |
| 220 | break; |
| 221 | default: |
| 222 | ut_error; |
| 223 | } |
| 224 | |
| 225 | size += block->len; |
| 226 | } |
| 227 | |
| 228 | ut_ad(size == heap->total_size); |
| 229 | } |
| 230 | |
| 231 | /** Copy the tail of a string. |
| 232 | @param[in,out] dst destination buffer |
| 233 | @param[in] src string whose tail to copy |
| 234 | @param[in] size size of dst buffer, in bytes, including NUL terminator |
| 235 | @return strlen(src) */ |
| 236 | static void ut_strlcpy_rev(char* dst, const char* src, ulint size) |
| 237 | { |
| 238 | size_t src_size = strlen(src), n = std::min(src_size, size - 1); |
| 239 | memcpy(dst, src + src_size - n, n + 1); |
| 240 | } |
| 241 | #endif /* UNIV_DEBUG */ |
| 242 | |
| 243 | /***************************************************************//** |
| 244 | Creates a memory heap block where data can be allocated. |
| 245 | @return own: memory heap block */ |
| 246 | mem_block_tcxx . . $ mem_block_t#* |
| 247 | mem_heap_create_block_funccxx . . $ mem_heap_create_block_func(c7cb44e8d63d2b65).( |
| 248 | /*=======================*/ |
| 249 | mem_heap_tcxx . . $ mem_heap_t#* heap, /*!< in: memory heap or NULL if first block |
| 250 | should be created */ |
| 251 | ulintcxx . . $ ulint# n, /*!< in: number of bytes needed for user data */ |
| 252 | #ifdef UNIV_DEBUG |
| 253 | const char* file_name,/*!< in: file name where created */ |
| 254 | unsigned line, /*!< in: line where created */ |
| 255 | #endif /* UNIV_DEBUG */ |
| 256 | ulintcxx . . $ ulint# type) /*!< in: type of heap: MEM_HEAP_DYNAMIC or |
| 257 | MEM_HEAP_BUFFER */ |
| 258 | { |
| 259 | buf_block_tcxx . . $ buf_block_t#* buf_block; |
| 260 | mem_block_tcxx . . $ mem_block_t#* block; |
| 261 | ulintcxx . . $ ulint# len; |
| 262 | |
| 263 | ut_adcxx . . $ `storage/innobase/include/ut0dbg.h:62:9`!(type == MEM_HEAP_DYNAMIC || type == MEM_HEAP_BUFFER); |
| 264 | |
| 265 | if (heap != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 266 | ut_dcxx . . $ `storage/innobase/include/ut0dbg.h:68:9`!(mem_heap_validate(heap)); |
| 267 | } |
| 268 | |
| 269 | /* In dynamic allocation, calculate the size: block header + data. */ |
| 270 | len = MEM_BLOCK_HEADER_SIZEcxx . . $ `storage/innobase/include/mem0mem.h:303:9`! + MEM_SPACE_NEEDEDcxx . . $ `storage/innobase/include/mem0mem.h:68:9`!(n); |
| 271 | |
| 272 | if (type == MEM_HEAP_DYNAMICcxx . . $ `storage/innobase/include/mem0mem.h:49:9`! || len < srv_page_sizecxx . . $ srv_page_size. / 2) { |
| 273 | |
| 274 | ut_adcxx . . $ `storage/innobase/include/ut0dbg.h:62:9`!(type == MEM_HEAP_DYNAMIC || n <= MEM_MAX_ALLOC_IN_BUF); |
| 275 | |
| 276 | block = static_cast<mem_block_tcxx . . $ mem_block_t#*>(ut_malloc_nokeycxx . . $ `storage/innobase/include/ut0new.h:1028:9`!(len)); |
| 277 | buf_block = nullptr; |
| 278 | } else { |
| 279 | len = srv_page_sizecxx . . $ srv_page_size.; |
| 280 | |
| 281 | buf_block = buf_block_alloccxx . . $ `storage/innobase/include/buf0lru.h:92:9`!(); |
| 282 | |
| 283 | block = (mem_block_tcxx . . $ mem_block_t#*) buf_block->pagecxx . . $ buf_block_t#page..framecxx . . $ buf_page_t#frame.; |
| 284 | } |
| 285 | |
| 286 | if (block == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 287 | ibcxx . . $ ib/::fatalcxx . . $ ib/fatal#() <<cxx . . $ ib/logger#`operator<<`(dcbc6b5678e2764). "Unable to allocate memory of size " |
| 288 | <<cxx . . $ ib/logger#`operator<<`(8dc2c095a0caceff). len <<cxx . . $ ib/logger#`operator<<`(dcbc6b5678e2764). "."; |
| 289 | } |
| 290 | |
| 291 | block->buf_blockcxx . . $ mem_block_info_t#buf_block. = buf_block; |
| 292 | |
| 293 | ut_dcxx . . $ `storage/innobase/include/ut0dbg.h:68:9`!(ut_strlcpy_rev(block->file_name, file_name, |
| 294 | sizeof(block->file_name))); |
| 295 | ut_dcxx . . $ `storage/innobase/include/ut0dbg.h:68:9`!(block->line = line); |
| 296 | |
| 297 | mem_block_set_lencxx . . $ mem_block_set_len(4218aa3e1a6cdba4).(block, len); |
| 298 | mem_block_set_typecxx . . $ mem_block_set_type(4218aa3e1a6cdba4).(block, type); |
| 299 | mem_block_set_freecxx . . $ mem_block_set_free(4218aa3e1a6cdba4).(block, MEM_BLOCK_HEADER_SIZEcxx . . $ `storage/innobase/include/mem0mem.h:303:9`!); |
| 300 | mem_block_set_startcxx . . $ mem_block_set_start(4218aa3e1a6cdba4).(block, MEM_BLOCK_HEADER_SIZEcxx . . $ `storage/innobase/include/mem0mem.h:303:9`!); |
| 301 | |
| 302 | if (UNIV_UNLIKELYcxx . . $ `storage/innobase/include/univ.i:439:9`!(heap == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)) { |
| 303 | /* This is the first block of the heap. The field |
| 304 | total_size should be initialized here */ |
| 305 | block->total_sizecxx . . $ mem_block_info_t#total_size. = len; |
| 306 | } else { |
| 307 | /* Not the first allocation for the heap. This block's |
| 308 | total_length field should be set to undefined. */ |
| 309 | ut_dcxx . . $ `storage/innobase/include/ut0dbg.h:68:9`!(block->total_size = ULINT_UNDEFINED); |
| 310 | MEM_UNDEFINEDcxx . . $ `include/my_valgrind.h:67:10`!(&block->total_size, sizeof block->total_size); |
| 311 | |
| 312 | heap->total_sizecxx . . $ mem_block_info_t#total_size. += len; |
| 313 | } |
| 314 | |
| 315 | /* Poison all available memory. Individual chunks will be unpoisoned on |
| 316 | every mem_heap_alloc() call. */ |
| 317 | compile_time_assertcxx . . $ `include/my_global.h:377:9`!(MEM_BLOCK_HEADER_SIZEcxx . . $ `storage/innobase/include/mem0mem.h:303:9`! >= sizeof *block); |
| 318 | MEM_NOACCESScxx . . $ `include/my_valgrind.h:70:10`!(block + 1, len - sizeof *block); |
| 319 | |
| 320 | ut_adcxx . . $ `storage/innobase/include/ut0dbg.h:62:9`!((ulint)MEM_BLOCK_HEADER_SIZE < len); |
| 321 | |
| 322 | return(block); |
| 323 | } |
| 324 | |
| 325 | /***************************************************************//** |
| 326 | Adds a new block to a memory heap. |
| 327 | @return created block */ |
| 328 | mem_block_tcxx . . $ mem_block_t#* |
| 329 | mem_heap_add_blockcxx . . $ mem_heap_add_block(8f3def19ff5191ab).( |
| 330 | /*===============*/ |
| 331 | mem_heap_tcxx . . $ mem_heap_t#* heap, /*!< in: memory heap */ |
| 332 | ulintcxx . . $ ulint# n) /*!< in: number of bytes user needs */ |
| 333 | { |
| 334 | mem_block_tcxx . . $ mem_block_t#* block; |
| 335 | mem_block_tcxx . . $ mem_block_t#* new_block; |
| 336 | ulintcxx . . $ ulint# new_size; |
| 337 | |
| 338 | block = UT_LIST_GET_LASTcxx . . $ `storage/innobase/include/ut0lst.h:427:9`!(heap->base); |
| 339 | |
| 340 | /* We have to allocate a new block. The size is always at least |
| 341 | doubled until the standard size is reached. After that the size |
| 342 | stays the same, except in cases where the caller needs more space. */ |
| 343 | |
| 344 | new_size = 2 * mem_block_get_lencxx . . $ mem_block_get_len(ad7b671f85d77495).(block); |
| 345 | |
| 346 | if (heap->typecxx . . $ mem_block_info_t#type. != MEM_HEAP_DYNAMICcxx . . $ `storage/innobase/include/mem0mem.h:49:9`!) { |
| 347 | /* From the buffer pool we allocate buffer frames */ |
| 348 | ut_acxx . . $ `storage/innobase/include/ut0dbg.h:50:9`!(n <= MEM_MAX_ALLOC_IN_BUFcxx . . $ `storage/innobase/include/mem0mem.h:64:9`!); |
| 349 | |
| 350 | if (new_size > MEM_MAX_ALLOC_IN_BUFcxx . . $ `storage/innobase/include/mem0mem.h:64:9`!) { |
| 351 | new_size = MEM_MAX_ALLOC_IN_BUFcxx . . $ `storage/innobase/include/mem0mem.h:64:9`!; |
| 352 | } |
| 353 | } else if (new_size > MEM_BLOCK_STANDARD_SIZEcxx . . $ `storage/innobase/include/mem0mem.h:59:9`!) { |
| 354 | |
| 355 | new_size = MEM_BLOCK_STANDARD_SIZEcxx . . $ `storage/innobase/include/mem0mem.h:59:9`!; |
| 356 | } |
| 357 | |
| 358 | if (new_size < n) { |
| 359 | new_size = n; |
| 360 | } |
| 361 | |
| 362 | new_block = mem_heap_create_blockcxx . . $ `storage/innobase/include/mem0mem.inl:35:10`!(heap, new_size, heap->type, |
| 363 | heap->file_name, heap->line); |
| 364 | if (new_block == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 365 | |
| 366 | return(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 367 | } |
| 368 | |
| 369 | /* Add the new block as the last block */ |
| 370 | |
| 371 | UT_LIST_INSERT_AFTERcxx . . $ `storage/innobase/include/ut0lst.h:278:9`!(heap->base, block, new_block); |
| 372 | |
| 373 | return(new_block); |
| 374 | } |
| 375 | |
| 376 | /******************************************************************//** |
| 377 | Frees a block from a memory heap. */ |
| 378 | void |
| 379 | mem_heap_block_freecxx . . $ mem_heap_block_free(5c4f14d388e95635).( |
| 380 | /*================*/ |
| 381 | mem_heap_tcxx . . $ mem_heap_t#* heap, /*!< in: heap */ |
| 382 | mem_block_tcxx . . $ mem_block_t#* block) /*!< in: block to free */ |
| 383 | { |
| 384 | ulintcxx . . $ ulint# type; |
| 385 | ulintcxx . . $ ulint# len; |
| 386 | |
| 387 | UT_LIST_REMOVEcxx . . $ `storage/innobase/include/ut0lst.h:394:9`!(heap->base, block); |
| 388 | |
| 389 | ut_adcxx . . $ `storage/innobase/include/ut0dbg.h:62:9`!(heap->total_size >= block->len); |
| 390 | heap->total_sizecxx . . $ mem_block_info_t#total_size. -= block->lencxx . . $ mem_block_info_t#len.; |
| 391 | |
| 392 | type = heap->typecxx . . $ mem_block_info_t#type.; |
| 393 | len = block->lencxx . . $ mem_block_info_t#len.; |
| 394 | |
| 395 | if (type == MEM_HEAP_DYNAMICcxx . . $ `storage/innobase/include/mem0mem.h:49:9`! || len < srv_page_sizecxx . . $ srv_page_size. / 2) { |
| 396 | ut_adcxx . . $ `storage/innobase/include/ut0dbg.h:62:9`!(!block->buf_block); |
| 397 | ut_freecxx . . $ `storage/innobase/include/ut0new.h:1044:9`!(block); |
| 398 | } else { |
| 399 | ut_adcxx . . $ `storage/innobase/include/ut0dbg.h:62:9`!(type & MEM_HEAP_BUFFER); |
| 400 | buf_block_freecxx . . $ `storage/innobase/include/buf0buf.h:148:10`!(block->buf_block); |
| 401 | } |
| 402 | } |