| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 2016, 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 | #include <string.h> |
| 28 | #include <cassert> |
| 29 | #include <iostream> |
| 30 | #include <map> |
| 31 | #include <set> |
| 32 | |
| 33 | #include "lot0buf.h" |
| 34 | #include "mach0data.h" |
| 35 | #include "ut0byte.h" |
| 36 | #include "ut0ut.h" |
| 37 | |
| 38 | #ifdef UNIV_DEBUG |
| 39 | #define Fname(x) const char *fname = x; |
| 40 | #define LOG(x) \ |
| 41 | { std::cout << fname << ":" << x << std::endl; } |
| 42 | #else |
| 43 | #define Fname(x) |
| 44 | #define LOG(x) |
| 45 | #endif /* UNIV_DEBUG */ |
| 46 | |
| 47 | const ulint buf_pool_size = GB1; |
| 48 | |
| 49 | class buf_pool_t { |
| 50 | public: |
| 51 | buf_pool_t() { |
| 52 | Fname("buf_pool_t::buf_pool_t"); |
| 53 | ulint page_count; |
| 54 | LOG("Requested buffer pool size = " << MB100); |
| 55 | ulint mem_size = ut_2pow_round(buf_pool_size, UNIV_PAGE_SIZE); |
| 56 | LOG("Will be allocating = " << mem_size); |
| 57 | page_count = mem_size / UNIV_PAGE_SIZE; |
| 58 | LOG("Number of pages = " << page_count); |
| 59 | m_frame_array_raw = new (std::nothrow) byte[mem_size]; |
| 60 | m_frames = (byte *)ut_align(m_frame_array_raw, UNIV_PAGE_SIZE); |
| 61 | LOG("Raw ptr = " << (void *)m_frame_array_raw); |
| 62 | LOG("Aligned ptr = " << (void *)m_frames); |
| 63 | if (m_frame_array_raw != m_frames) { |
| 64 | --page_count; |
| 65 | } |
| 66 | m_cur_free_frame = m_frames; |
| 67 | m_frame_end = m_frames + page_count * UNIV_PAGE_SIZE; |
| 68 | } |
| 69 | |
| 70 | /** Get the buffer block, given the page number. It is an error to |
| 71 | look for a page number that is not there in the buffer pool. |
| 72 | @param[in] page_no the page number to look for in the buffer pool. */ |
| 73 | buf_block_t *get(page_no_t page_no) { |
| 74 | auto it = m_buf_pool.find(page_no); |
| 75 | assert(it != m_buf_pool.end()); |
| 76 | |
| 77 | /* Free page must not be accessed. */ |
| 78 | ut_ad(m_free_pages.find(it->second) == m_free_pages.end()); |
| 79 | return (it->second); |
| 80 | } |
| 81 | |
| 82 | /** Allocate a new page. */ |
| 83 | buf_block_t *alloc() { |
| 84 | if (!m_free_pages.empty()) { |
| 85 | auto it = m_free_pages.begin(); |
| 86 | buf_block_t *tmp = *it; |
| 87 | m_free_pages.erase(it); |
| 88 | return (tmp); |
| 89 | } |
| 90 | return (alloc(max_page_no())); |
| 91 | } |
| 92 | |
| 93 | void reset() { |
| 94 | m_free_pages.clear(); |
| 95 | m_buf_pool.clear(); |
| 96 | m_cur_free_frame = m_frames; |
| 97 | } |
| 98 | |
| 99 | void dealloc(buf_block_t *block) { m_free_pages.insert(block); } |
| 100 | |
| 101 | private: |
| 102 | buf_block_t *alloc(page_no_t page_no) { |
| 103 | Fname("buf_pool_t::alloc"); |
| 104 | buf_block_t *block = new (std::nothrow) buf_block_t; |
| 105 | assert(block != nullptr); |
| 106 | /* Ensure that the page_no is not already allocated. */ |
| 107 | assert(m_buf_pool.find(page_no) == m_buf_pool.end()); |
| 108 | /* Allocate the new page. */ |
| 109 | block->m_frame = alloc_frame(); |
| 110 | assert(block->m_frame != nullptr); |
| 111 | /* Initialize the page with zeroes */ |
| 112 | memset(block->m_frame, 0x00, UNIV_PAGE_SIZE); |
| 113 | /* Set the page number */ |
| 114 | block->set_page_no(page_no); |
| 115 | m_buf_pool.insert(std::pair<page_no_t, buf_block_t *>(page_no, block)); |
| 116 | LOG("block ptr=" << (void *)block); |
| 117 | LOG("frame ptr=" << (void *)block->m_frame); |
| 118 | LOG("page_no=" << page_no); |
| 119 | return (block); |
| 120 | } |
| 121 | |
| 122 | page_no_t max_page_no() const { return (m_buf_pool.size()); } |
| 123 | |
| 124 | byte *alloc_frame() { |
| 125 | byte *tmp = m_cur_free_frame; |
| 126 | ut_a(m_cur_free_frame != m_frame_end); |
| 127 | m_cur_free_frame += UNIV_PAGE_SIZE; |
| 128 | return (tmp); |
| 129 | } |
| 130 | |
| 131 | /** Copy constructor is disabled. */ |
| 132 | buf_pool_t(const buf_pool_t &other); |
| 133 | |
| 134 | /** The map between the page number, and the actual page. */ |
| 135 | std::map<page_no_t, buf_block_t *> m_buf_pool; |
| 136 | |
| 137 | /** One big chunk containing all frames (unaligned). */ |
| 138 | byte *m_frame_array_raw; |
| 139 | |
| 140 | /** First frame in the array, aligned to page size. */ |
| 141 | byte *m_frames; |
| 142 | |
| 143 | /** End of frame in the array, aligned to page size. */ |
| 144 | byte *m_frame_end; |
| 145 | |
| 146 | /** Current free frame */ |
| 147 | byte *m_cur_free_frame; |
| 148 | |
| 149 | /** Free pages. */ |
| 150 | std::set<buf_block_t *> m_free_pages; |
| 151 | }; |
| 152 | |
| 153 | static buf_pool_t g_buf_pool; |
| 154 | |
| 155 | buf_block_t *buf_page_get(page_no_t page_no) { |
| 156 | return (g_buf_pool.get(page_no)); |
| 157 | } |
| 158 | |
| 159 | buf_block_t *btr_page_alloc() { return (g_buf_pool.alloc()); } |
| 160 | |
| 161 | void btr_page_free(buf_block_t *block) { return (g_buf_pool.dealloc(block)); } |
| 162 | |
| 163 | void buf_pool_reset() { g_buf_pool.reset(); } |