| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2006, 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/buf0buddy.ic |
| 29 |
Binary buddy allocator for compressed pages |
| 30 |
|
| 31 |
Created December 2006 by Marko Makela |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "buf0buddy.h" |
| 35 |
#include "buf0buf.h" |
| 36 |
|
| 37 |
/** Allocate a block. |
| 38 |
@param[in,out] buf_pool buffer pool instance |
| 39 |
@param[in] i index of buf_pool->zip_free[] |
| 40 |
or BUF_BUDDY_SIZES |
| 41 |
@return allocated block, never NULL */ |
| 42 |
void *buf_buddy_alloc_low(buf_pool_t *buf_pool, ulint i) MY_ATTRIBUTE((malloc)); |
| 43 |
|
| 44 |
/** Deallocate a block. |
| 45 |
@param[in] buf_pool buffer pool instance |
| 46 |
@param[in] buf block to be freed, must not be pointed to |
| 47 |
by the buffer pool |
| 48 |
@param[in] i index of buf_pool->zip_free[], |
| 49 |
or BUF_BUDDY_SIZES |
| 50 |
@param[in] has_zip_free whether has zip_free_mutex */ |
| 51 |
void buf_buddy_free_low(buf_pool_t *buf_pool, void *buf, ulint i, |
| 52 |
bool has_zip_free); |
| 53 |
|
| 54 |
/** Get the index of buf_pool->zip_free[] for a given block size. |
| 55 |
@return index of buf_pool->zip_free[], or BUF_BUDDY_SIZES */ |
| 56 |
static inline ulint buf_buddy_get_slot(ulint size) /*!< in: block size */ |
| 57 |
{ |
| 58 |
ulint i; |
| 59 |
ulint s; |
| 60 |
|
| 61 |
ut_ad(size >= UNIV_ZIP_SIZE_MIN); |
| 62 |
|
| 63 |
for (i = 0, s = BUF_BUDDY_LOW; s < size; i++, s <<= 1) { |
| 64 |
} |
| 65 |
|
| 66 |
ut_ad(i <= BUF_BUDDY_SIZES); |
| 67 |
return (i); |
| 68 |
} |
| 69 |
|
| 70 |
/** Allocate a block. This function should only be used for allocating |
| 71 |
compressed page frames. The thread calling this function must hold |
| 72 |
buf_pool->LRU_list_mutex and must not hold buf_pool->zip_mutex or any |
| 73 |
block->mutex. |
| 74 |
@param[in,out] buf_pool buffer pool in which the page resides |
| 75 |
@param[in] size compressed page size, between |
| 76 |
UNIV_ZIP_SIZE_MIN and UNIV_PAGE_SIZE |
| 77 |
@return allocated block, never NULL */ |
| 78 |
static inline byte *buf_buddy_alloc(buf_pool_t *buf_pool, ulint size) { |
| 79 |
ut_ad(ut_is_2pow(size)); |
| 80 |
ut_ad(size >= UNIV_ZIP_SIZE_MIN); |
| 81 |
ut_ad(size <= UNIV_PAGE_SIZE); |
| 82 |
|
| 83 |
return (static_cast<byte *>( |
| 84 |
buf_buddy_alloc_low(buf_pool, buf_buddy_get_slot(size)))); |
| 85 |
} |
| 86 |
|
| 87 |
/** Deallocate a block. |
| 88 |
@param[in,out] buf_pool buffer pool in which the block resides |
| 89 |
@param[in] buf block to be freed, must not be pointed to |
| 90 |
by the buffer pool |
| 91 |
@param[in] size block size, up to UNIV_PAGE_SIZE */ |
| 92 |
static inline void buf_buddy_free(buf_pool_t *buf_pool, void *buf, ulint size) { |
| 93 |
ut_ad(ut_is_2pow(size)); |
| 94 |
ut_ad(size >= UNIV_ZIP_SIZE_MIN); |
| 95 |
ut_ad(size <= UNIV_PAGE_SIZE); |
| 96 |
|
| 97 |
buf_buddy_free_low(buf_pool, buf, buf_buddy_get_slot(size), false); |
| 98 |
} |