1/*****************************************************************************
2
3Copyright (c) 2006, 2018, Oracle and/or its affiliates. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24
25*****************************************************************************/
26
27/** @file include/buf0buddy.ic
28 Binary buddy allocator for compressed pages
29
30 Created December 2006 by Marko Makela
31 *******************************************************/
32
33#ifdef UNIV_MATERIALIZE
34#undef UNIV_INLINE
35#define UNIV_INLINE
36#endif
37
38#include "buf0buddy.h"
39#include "buf0buf.h"
40
41/** Allocate a block.
42@param[in,out]	buf_pool	buffer pool instance
43@param[in]	i		index of buf_pool->zip_free[]
44                                or BUF_BUDDY_SIZES
45@return allocated block, never NULL */
46void *buf_buddy_alloc_low(buf_pool_t *buf_pool, ulint i) MY_ATTRIBUTE((malloc));
47
48/** Deallocate a block.
49@param[in]	buf_pool	buffer pool instance
50@param[in]	buf		block to be freed, must not be pointed to
51                                by the buffer pool
52@param[in]	i		index of buf_pool->zip_free[],
53                                or BUF_BUDDY_SIZES
54@param[in]	has_zip_free	whether has zip_free_mutex */
55void buf_buddy_free_low(buf_pool_t *buf_pool, void *buf, ulint i,
56                        bool has_zip_free);
57
58/** Get the index of buf_pool->zip_free[] for a given block size.
59 @return index of buf_pool->zip_free[], or BUF_BUDDY_SIZES */
60UNIV_INLINE
61ulint buf_buddy_get_slot(ulint size) /*!< in: block size */
62{
63  ulint i;
64  ulint s;
65
66  ut_ad(size >= UNIV_ZIP_SIZE_MIN);
67
68  for (i = 0, s = BUF_BUDDY_LOW; s < size; i++, s <<= 1) {
69  }
70
71  ut_ad(i <= BUF_BUDDY_SIZES);
72  return (i);
73}
74
75/** Allocate a block. This function should only be used for allocating
76compressed page frames. The thread calling this function must hold
77buf_pool->LRU_list_mutex and must not hold buf_pool->zip_mutex or any
78block->mutex.
79@param[in,out]	buf_pool	buffer pool in which the page resides
80@param[in]	size		compressed page size, between
81                                UNIV_ZIP_SIZE_MIN and UNIV_PAGE_SIZE
82@return allocated block, never NULL */
83UNIV_INLINE
84byte *buf_buddy_alloc(buf_pool_t *buf_pool, ulint size) {
85  ut_ad(ut_is_2pow(size));
86  ut_ad(size >= UNIV_ZIP_SIZE_MIN);
87  ut_ad(size <= UNIV_PAGE_SIZE);
88
89  return (static_cast<byte *>(
90      buf_buddy_alloc_low(buf_pool, buf_buddy_get_slot(size))));
91}
92
93/** Deallocate a block.
94@param[in,out]	buf_pool	buffer pool in which the block resides
95@param[in]	buf		block to be freed, must not be pointed to
96                                by the buffer pool
97@param[in]	size		block size, up to UNIV_PAGE_SIZE */
98UNIV_INLINE
99void buf_buddy_free(buf_pool_t *buf_pool, void *buf, ulint size) {
100  ut_ad(ut_is_2pow(size));
101  ut_ad(size >= UNIV_ZIP_SIZE_MIN);
102  ut_ad(size <= UNIV_PAGE_SIZE);
103
104  buf_buddy_free_low(buf_pool, buf, buf_buddy_get_slot(size), false);
105}
106
107#ifdef UNIV_MATERIALIZE
108#undef UNIV_INLINE
109#define UNIV_INLINE UNIV_INLINE_ORIGINAL
110#endif
111