Debian Codegraph @

storage/innobase/include/mem0mem.ic

raw
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 include/mem0mem.ic
29 The memory management
30
31 Created 6/8/1994 Heikki Tuuri
32 *************************************************************************/
33
34 #include "ut0new.h"
35
36 /** Creates a memory heap block where data can be allocated.
37 @return own: memory heap block, NULL if did not succeed (only possible
38 for MEM_HEAP_BTR_SEARCH type heaps)
39 @param[in] heap memory heap or NULL if first block should be created
40 @param[in] n number od bytes needed for user data
41 @param[in] file_name file name where created
42 @param[in] line line where created
43 @param[in] type type of heap MEM_HEAP_DYNAMIC or MEM_HEAP_BUFFER */
44 mem_block_t *mem_heap_create_block(mem_heap_t *heap, ulint n,
45 IF_DEBUG(const char *file_name, ulint line, )
46 ulint type);
47
48 /** Frees a block from a memory heap. */
49 void mem_heap_block_free(mem_heap_t *heap, /*!< in: heap */
50 mem_block_t *block); /*!< in: block to free */
51
52 #ifndef UNIV_HOTBACKUP
53 #ifndef UNIV_LIBRARY
54 /** Frees the free_block field from a memory heap. */
55 void mem_heap_free_block_free(mem_heap_t *heap); /*!< in: heap */
56 #endif /* !UNIV_LIBRARY */
57 #endif /* !UNIV_HOTBACKUP */
58
59 /** Adds a new block to a memory heap.
60 @param[in] heap memory heap
61 @param[in] n number of bytes needed
62 @return created block, NULL if did not succeed (only possible for
63 MEM_HEAP_BTR_SEARCH type heaps) */
64 mem_block_t *mem_heap_add_block(mem_heap_t *heap, ulint n);
65
66 static inline void mem_block_set_len(mem_block_t *block, ulint len) {
67 ut_ad(len > 0);
68
69 block->len = len;
70 }
71
72 static inline ulint mem_block_get_len(mem_block_t *block) {
73 return (block->len);
74 }
75
76 static inline void mem_block_set_type(mem_block_t *block, ulint type) {
77 ut_ad((type == MEM_HEAP_DYNAMIC) || (type == MEM_HEAP_BUFFER) ||
78 (type == MEM_HEAP_BUFFER + MEM_HEAP_BTR_SEARCH));
79
80 block->type = type;
81 }
82
83 static inline ulint mem_block_get_type(mem_block_t *block) {
84 return (block->type);
85 }
86
87 static inline void mem_block_set_free(mem_block_t *block, ulint free) {
88 ut_ad(free >= block->start);
89 ut_ad(free <= mem_block_get_len(block));
90
91 block->free = free;
92 }
93
94 static inline ulint mem_block_get_free(mem_block_t *block) {
95 return (block->free);
96 }
97
98 static inline void mem_block_set_start(mem_block_t *block, ulint start) {
99 ut_ad(start > 0);
100
101 block->start = start;
102 }
103
104 static inline ulint mem_block_get_start(mem_block_t *block) {
105 return (block->start);
106 }
107
108 /** Checks that an object is a memory heap (or a block of it)
109 @param[in] block Memory heap to check */
110 static inline void mem_block_validate(const mem_block_t *block) {
111 if (block->magic_n != MEM_BLOCK_MAGIC_N) {
112 ib::fatal error(UT_LOCATION_HERE);
113 error << "Memory block is invalid (correctness field value"
114 " should be "
115 << MEM_BLOCK_MAGIC_N << ", but it is " << block->magic_n
116 << ") in a block of size " << block->len;
117 ut_d(error << ", allocated at " << block->file_name << ":" << block->line);
118 }
119 }
120
121 /** Allocates and zero-fills n bytes of memory from a memory heap.
122 @param[in] heap memory heap
123 @param[in] n number of bytes; if the heap is allowed to grow into
124 the buffer pool, this must be <= MEM_MAX_ALLOC_IN_BUF
125 @return allocated, zero-filled storage */
126 static inline void *mem_heap_zalloc(mem_heap_t *heap, ulint n) {
127 ut_ad(heap);
128 ut_ad(!(heap->type & MEM_HEAP_BTR_SEARCH));
129 return (memset(mem_heap_alloc(heap, n), 0, n));
130 }
131
132 /** Allocates n bytes of memory from a memory heap.
133 @param[in] heap memory heap
134 @param[in] n number of bytes; if the heap is allowed to grow into
135 the buffer pool, this must be <= MEM_MAX_ALLOC_IN_BUF
136 @return allocated storage, NULL if did not succeed (only possible for
137 MEM_HEAP_BTR_SEARCH type heaps) */
138 static inline void *mem_heap_alloc(mem_heap_t *heap, ulint n) {
139 mem_block_t *block;
140 byte *buf;
141 ulint free;
142
143 ut_d(mem_block_validate(heap));
144
145 block = UT_LIST_GET_LAST(heap->base);
146
147 ut_ad(!(block->type & MEM_HEAP_BUFFER) || (n <= MEM_MAX_ALLOC_IN_BUF));
148
149 /* Check if there is enough space in block. If not, create a new
150 block to the heap */
151
152 if (mem_block_get_len(block) <
153 mem_block_get_free(block) + MEM_SPACE_NEEDED(n)) {
154 block = mem_heap_add_block(heap, n);
155
156 if (block == nullptr) {
157 return (nullptr);
158 }
159 }
160
161 free = mem_block_get_free(block);
162
163 buf = (byte *)block + free + MEM_NO_MANS_LAND;
164
165 mem_block_set_free(block, free + MEM_SPACE_NEEDED(n));
166
167 ut_ad(mem_block_get_len(block) >= mem_block_get_free(block));
168
169 UNIV_MEM_ALLOC(buf - MEM_NO_MANS_LAND, MEM_SPACE_NEEDED(n));
170
171 #ifdef UNIV_DEBUG
172 byte *start_no_mans_land = buf - MEM_NO_MANS_LAND;
173 byte *end_no_mans_land =
174 start_no_mans_land + MEM_SPACE_NEEDED(n) - MEM_NO_MANS_LAND;
175 memset(start_no_mans_land, MEM_NO_MANS_LAND_BEFORE_BYTE, MEM_NO_MANS_LAND);
176 UNIV_MEM_FREE(start_no_mans_land, MEM_NO_MANS_LAND);
177
178 memset(end_no_mans_land, MEM_NO_MANS_LAND_AFTER_BYTE, MEM_NO_MANS_LAND);
179 UNIV_MEM_FREE(buf + n, MEM_SPACE_NEEDED(n) - n - MEM_NO_MANS_LAND);
180 #endif
181
182 return (buf);
183 }
184
185 #ifdef UNIV_DEBUG
186 static inline void validate_no_mans_land(byte *no_mans_land_begin,
187 byte mem_no_mans_land_byte) {
188 UNIV_MEM_VALID(no_mans_land_begin, MEM_NO_MANS_LAND);
189 for (byte *no_mans_land_it = no_mans_land_begin;
190 no_mans_land_it < no_mans_land_begin + MEM_NO_MANS_LAND;
191 ++no_mans_land_it) {
192 ut_a(*no_mans_land_it == mem_no_mans_land_byte);
193 }
194 }
195 #endif
196
197 /** Returns a pointer to the heap top.
198 @param[in] heap memory heap
199 @return pointer to the heap top */
200 static inline byte *mem_heap_get_heap_top(mem_heap_t *heap) {
201 mem_block_t *block;
202 byte *buf;
203
204 ut_d(mem_block_validate(heap));
205
206 block = UT_LIST_GET_LAST(heap->base);
207
208 buf = (byte *)block + mem_block_get_free(block);
209
210 return (buf);
211 }
212
213 /** Frees the space in a memory heap exceeding the pointer given.
214 The pointer must have been acquired from mem_heap_get_heap_top.
215 The first memory block of the heap is not freed.
216 @param[in] heap heap from which to free
217 @param[in] old_top pointer to old top of heap */
218 static inline void mem_heap_free_heap_top(mem_heap_t *heap, byte *old_top) {
219 mem_block_t *block;
220 mem_block_t *prev_block;
221
222 ut_d(mem_heap_validate(heap));
223
224 block = UT_LIST_GET_LAST(heap->base);
225
226 while (block != nullptr) {
227 if (((byte *)block + mem_block_get_free(block) >= old_top) &&
228 ((byte *)block <= old_top)) {
229 /* Found the right block */
230
231 break;
232 }
233
234 /* Store prev_block value before freeing the current block
235 (the current block will be erased in freeing) */
236
237 prev_block = UT_LIST_GET_PREV(list, block);
238
239 mem_heap_block_free(heap, block);
240
241 block = prev_block;
242 }
243
244 ut_ad(block);
245
246 if (old_top == (byte *)block + mem_block_get_free(block)) {
247 return;
248 }
249
250 #ifdef UNIV_DEBUG
251 validate_no_mans_land(old_top, MEM_NO_MANS_LAND_BEFORE_BYTE);
252 validate_no_mans_land(
253 (byte *)block + mem_block_get_free(block) - MEM_NO_MANS_LAND,
254 MEM_NO_MANS_LAND_AFTER_BYTE);
255 #endif
256 /* Set the free field of block */
257 mem_block_set_free(block, old_top - (byte *)block);
258
259 ut_ad(mem_block_get_start(block) <= mem_block_get_free(block));
260 UNIV_MEM_FREE(old_top, (byte *)block + block->len - old_top);
261
262 /* If free == start, we may free the block if it is not the first
263 one */
264
265 if ((heap != block) &&
266 (mem_block_get_free(block) == mem_block_get_start(block))) {
267 mem_heap_block_free(heap, block);
268 }
269 }
270
271 /** Empties a memory heap.
272 The first memory block of the heap is not freed.
273 @param[in] heap heap to empty */
274 static inline void mem_heap_empty(mem_heap_t *heap) {
275 mem_heap_free_heap_top(heap, (byte *)heap + mem_block_get_start(heap));
276 #ifndef UNIV_HOTBACKUP
277 #ifndef UNIV_LIBRARY
278 mem_heap_free_block_free(heap);
279 #endif /* !UNIV_LIBRARY */
280 #endif /* !UNIV_HOTBACKUP */
281 }
282
283 /** Returns a pointer to the topmost element in a memory heap.
284 The size of the element must be given.
285 @param[in] heap memory heap
286 @param[in] n size of the topmost element
287 @return pointer to the topmost element */
288 static inline void *mem_heap_get_top(mem_heap_t *heap, ulint n) {
289 mem_block_t *block;
290 byte *buf;
291
292 ut_d(mem_block_validate(heap));
293
294 block = UT_LIST_GET_LAST(heap->base);
295
296 buf = (byte *)block + mem_block_get_free(block) - MEM_SPACE_NEEDED(n) +
297 MEM_NO_MANS_LAND;
298
299 return ((void *)buf);
300 }
301
302 /** Checks if a given chunk of memory is the topmost element stored in the
303 heap. If this is the case, then calling mem_heap_free_top() would free
304 that element from the heap.
305 @param[in] heap memory heap
306 @param[in] buf presumed topmost element
307 @param[in] buf_sz size of buf in bytes
308 @return true if topmost */
309 static inline bool mem_heap_is_top(mem_heap_t *heap, const void *buf,
310 ulint buf_sz) {
311 const byte *first_free_byte;
312 const byte *presumed_start_of_buf;
313
314 ut_d(mem_block_validate(heap));
315
316 first_free_byte = mem_heap_get_heap_top(heap);
317
318 presumed_start_of_buf =
319 first_free_byte - MEM_SPACE_NEEDED(buf_sz) + MEM_NO_MANS_LAND;
320
321 return (presumed_start_of_buf == buf);
322 }
323
324 /** Allocate a new chunk of memory from a memory heap, possibly discarding
325 the topmost element. If the memory chunk specified with (top, top_sz)
326 is the topmost element, then it will be discarded, otherwise it will
327 be left untouched and this function will be equivallent to
328 mem_heap_alloc().
329 @return allocated storage, NULL if did not succeed (only possible for
330 MEM_HEAP_BTR_SEARCH type heaps) */
331 static inline void *mem_heap_replace(
332 mem_heap_t *heap, /*!< in/out: memory heap */
333 const void *top, /*!< in: chunk to discard if possible */
334 ulint top_sz, /*!< in: size of top in bytes */
335 ulint new_sz) /*!< in: desired size of the new chunk */
336 {
337 if (mem_heap_is_top(heap, top, top_sz)) {
338 mem_heap_free_top(heap, top_sz);
339 }
340
341 return (mem_heap_alloc(heap, new_sz));
342 }
343
344 /** Allocate a new chunk of memory from a memory heap, possibly discarding
345 the topmost element and then copy the specified data to it. If the memory
346 chunk specified with (top, top_sz) is the topmost element, then it will be
347 discarded, otherwise it will be left untouched and this function will be
348 equivallent to mem_heap_dup().
349 @return allocated storage, NULL if did not succeed (only possible for
350 MEM_HEAP_BTR_SEARCH type heaps) */
351 static inline void *mem_heap_dup_replace(
352 mem_heap_t *heap, /*!< in/out: memory heap */
353 const void *top, /*!< in: chunk to discard if possible */
354 ulint top_sz, /*!< in: size of top in bytes */
355 const void *data, /*!< in: new data to duplicate */
356 ulint data_sz) /*!< in: size of data in bytes */
357 {
358 void *p = mem_heap_replace(heap, top, top_sz, data_sz);
359
360 memcpy(p, data, data_sz);
361
362 return (p);
363 }
364
365 /** Allocate a new chunk of memory from a memory heap, possibly discarding
366 the topmost element and then copy the specified string to it. If the memory
367 chunk specified with (top, top_sz) is the topmost element, then it will be
368 discarded, otherwise it will be left untouched and this function will be
369 equivallent to mem_heap_strdup().
370 @return allocated string, NULL if did not succeed (only possible for
371 MEM_HEAP_BTR_SEARCH type heaps) */
372 static inline char *mem_heap_strdup_replace(
373 mem_heap_t *heap, /*!< in/out: memory heap */
374 const void *top, /*!< in: chunk to discard if possible */
375 ulint top_sz, /*!< in: size of top in bytes */
376 const char *str) /*!< in: new data to duplicate */
377 {
378 return (reinterpret_cast<char *>(
379 mem_heap_dup_replace(heap, top, top_sz, str, strlen(str) + 1)));
380 }
381
382 /** Frees the topmost element in a memory heap. The size of the element must be
383 given. */
384 static inline void mem_heap_free_top(
385 mem_heap_t *heap, /*!< in: memory heap */
386 ulint n) /*!< in: size of the topmost element */
387 {
388 mem_block_t *block;
389
390 ut_d(mem_block_validate(heap));
391
392 block = UT_LIST_GET_LAST(heap->base);
393
394 /* Subtract the free field of block */
395 mem_block_set_free(block, mem_block_get_free(block) - MEM_SPACE_NEEDED(n));
396 UNIV_MEM_ASSERT_W(
397 (byte *)block + mem_block_get_free(block) + MEM_NO_MANS_LAND, n);
398 #ifdef UNIV_DEBUG
399 validate_no_mans_land((byte *)block + mem_block_get_free(block),
400 MEM_NO_MANS_LAND_BEFORE_BYTE);
401 validate_no_mans_land((byte *)block + mem_block_get_free(block) +
402 MEM_SPACE_NEEDED(n) - MEM_NO_MANS_LAND,
403 MEM_NO_MANS_LAND_AFTER_BYTE);
404 #endif
405
406 /* If free == start, we may free the block if it is not the first
407 one */
408
409 if ((heap != block) &&
410 (mem_block_get_free(block) == mem_block_get_start(block))) {
411 mem_heap_block_free(heap, block);
412 } else {
413 UNIV_MEM_FREE((byte *)block + mem_block_get_free(block),
414 MEM_SPACE_NEEDED(n));
415 }
416 }
417
418 static inline mem_heap_t *mem_heap_create(ulint size,
419 ut::Location loc [[maybe_unused]],
420 ulint type) {
421 mem_block_t *block;
422
423 if (!size) {
424 size = MEM_BLOCK_START_SIZE;
425 }
426
427 block = mem_heap_create_block(nullptr, size,
428 IF_DEBUG(loc.filename, loc.line, ) type);
429
430 if (block == nullptr) {
431 return (nullptr);
432 }
433
434 UT_LIST_INIT(block->base);
435
436 /* Add the created block itself as the first block in the list */
437 UT_LIST_ADD_FIRST(block->base, block);
438
439 return (block);
440 }
441
442 /** Frees the space occupied by a memory heap.
443 NOTE: Use the corresponding macro instead of this function.
444 @param[in] heap Heap to be freed */
445 static inline void mem_heap_free(mem_heap_t *heap) {
446 mem_block_t *block;
447 mem_block_t *prev_block;
448
449 ut_d(mem_block_validate(heap));
450
451 block = UT_LIST_GET_LAST(heap->base);
452
453 #ifndef UNIV_HOTBACKUP
454 #ifndef UNIV_LIBRARY
455 mem_heap_free_block_free(heap);
456 #endif /* !UNIV_LIBRARY */
457 #endif /* !UNIV_HOTBACKUP */
458
459 while (block != nullptr) {
460 /* Store the contents of info before freeing current block
461 (it is erased in freeing) */
462
463 prev_block = UT_LIST_GET_PREV(list, block);
464
465 mem_heap_block_free(heap, block);
466
467 block = prev_block;
468 }
469 }
470
471 /** Returns the space in bytes occupied by a memory heap. */
472 static inline size_t mem_heap_get_size(mem_heap_t *heap) /*!< in: heap */
473 {
474 size_t size = 0;
475
476 ut_d(mem_block_validate(heap));
477
478 size = heap->total_size;
479
480 #ifndef UNIV_LIBRARY
481 if (heap->free_block_ptr != nullptr && heap->free_block_ptr->load()) {
482 size += UNIV_PAGE_SIZE;
483 }
484 #endif /* !UNIV_LIBRARY */
485
486 return (size);
487 }
488
489 /** Duplicates a NUL-terminated string.
490 @return own: a copy of the string, must be deallocated with ut_free */
491 static inline char *mem_strdup(const char *str) /*!< in: string to be copied */
492 {
493 ulint len = strlen(str) + 1;
494 return (static_cast<char *>(
495 memcpy(ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, len), str, len)));
496 }
497
498 /** Makes a NUL-terminated copy of a nonterminated string.
499 @return own: a copy of the string, must be deallocated with ut_free */
500 static inline char *mem_strdupl(const char *str, /*!< in: string to be copied */
501 ulint len) /*!< in: length of str, in bytes */
502 {
503 if (static_cast<long>(len) < 0) {
504 return nullptr; // GCC bug #91397.
505 }
506 char *s = static_cast<char *>(
507 ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, len + 1));
508 s[len] = 0;
509 if (len > 0) {
510 memcpy(s, str, len);
511 }
512 return s;
513 }
514
515 /** Makes a NUL-terminated copy of a nonterminated string,
516 allocated from a memory heap.
517 @return own: a copy of the string */
518 static inline char *mem_heap_strdupl(
519 mem_heap_t *heap, /*!< in: memory heap where string is allocated */
520 const char *str, /*!< in: string to be copied */
521 ulint len) /*!< in: length of str, in bytes */
522 {
523 char *s = (char *)mem_heap_alloc(heap, len + 1);
524 s[len] = 0;
525 if (len > 0) {
526 memcpy(s, str, len);
527 }
528 return s;
529 }