| 1 | /* Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <array> |
| 26 | #include <cstring> |
| 27 | #include <memory> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include "allocator_helper.h" |
| 31 | #include "storage/temptable/include/temptable/allocator.h" |
| 32 | #include "storage/temptable/include/temptable/block.h" |
| 33 | |
| 34 | namespace temptable_test { |
| 35 | |
| 36 | /* TestItem class. */ |
| 37 | |
| 38 | class TestItem { |
| 39 | public: |
| 40 | explicit TestItem(); |
| 41 | |
| 42 | TestItem(int first, int second); |
| 43 | |
| 44 | ~TestItem(); |
| 45 | |
| 46 | int get_first() const; |
| 47 | |
| 48 | int get_second() const; |
| 49 | |
| 50 | static void reset_counters(); |
| 51 | |
| 52 | static int get_default_constructor_call_count(); |
| 53 | |
| 54 | static int get_parametrized_constructor_call_count(); |
| 55 | |
| 56 | static int get_destructor_call_count(); |
| 57 | |
| 58 | private: |
| 59 | int m_first; |
| 60 | |
| 61 | int m_second; |
| 62 | |
| 63 | static int s_default_constructor_call_count; |
| 64 | |
| 65 | static int s_parametrized_constructor_call_count; |
| 66 | |
| 67 | static int s_destructor_call_count; |
| 68 | }; |
| 69 | |
| 70 | TestItem::TestItem() : m_first(0), m_second(0) { |
| 71 | ++s_default_constructor_call_count; |
| 72 | } |
| 73 | |
| 74 | TestItem::TestItem(int first, int second) : m_first(first), m_second(second) { |
| 75 | ++s_parametrized_constructor_call_count; |
| 76 | } |
| 77 | |
| 78 | TestItem::~TestItem() { ++s_destructor_call_count; } |
| 79 | |
| 80 | int TestItem::get_first() const { return m_first; } |
| 81 | |
| 82 | int TestItem::get_second() const { return m_second; } |
| 83 | |
| 84 | void TestItem::reset_counters() { |
| 85 | s_default_constructor_call_count = 0; |
| 86 | s_parametrized_constructor_call_count = 0; |
| 87 | s_destructor_call_count = 0; |
| 88 | } |
| 89 | |
| 90 | int TestItem::get_default_constructor_call_count() { |
| 91 | return s_default_constructor_call_count; |
| 92 | } |
| 93 | |
| 94 | int TestItem::get_parametrized_constructor_call_count() { |
| 95 | return s_parametrized_constructor_call_count; |
| 96 | } |
| 97 | |
| 98 | int TestItem::get_destructor_call_count() { return s_destructor_call_count; } |
| 99 | |
| 100 | int TestItem::s_default_constructor_call_count = -1; |
| 101 | |
| 102 | int TestItem::s_parametrized_constructor_call_count = -1; |
| 103 | |
| 104 | int TestItem::s_destructor_call_count = -1; |
| 105 | |
| 106 | /* End of TestItem class. */ |
| 107 | |
| 108 | namespace { |
| 109 | |
| 110 | void init_allocator_once() { |
| 111 | static bool inited = false; |
| 112 | if (!inited) { |
| 113 | temptable::Allocator<int>::init(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } // namespace |
| 118 | |
| 119 | TEST(Allocator, BasicAlloc) { |
| 120 | Allocator_helper::set_allocator_max_ram_default(); |
| 121 | init_allocator_once(); |
| 122 | |
| 123 | using Item = int; |
| 124 | const int ITEM_COUNT = 100; |
| 125 | |
| 126 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 127 | temptable::Block shared_block; |
| 128 | temptable::Allocator<Item> allocator(&shared_block, table_resource_monitor); |
| 129 | |
| 130 | std::vector<int *> item_pointers; |
| 131 | item_pointers.assign(ITEM_COUNT, nullptr); |
| 132 | |
| 133 | /* Allocate and then deallocate in same order. */ |
| 134 | for (int i = 0; i < ITEM_COUNT; ++i) { |
| 135 | EXPECT_NO_THROW(item_pointers[i] = allocator.allocate(i + 1)); |
| 136 | } |
| 137 | for (int i = 0; i < ITEM_COUNT; ++i) { |
| 138 | EXPECT_NO_THROW(allocator.deallocate(item_pointers[i], i + 1)); |
| 139 | item_pointers[i] = nullptr; |
| 140 | } |
| 141 | |
| 142 | /* Allocate and then deallocate in reverse order. */ |
| 143 | for (int i = 0; i < ITEM_COUNT; ++i) { |
| 144 | EXPECT_NO_THROW(item_pointers[i] = allocator.allocate(i + 1)); |
| 145 | } |
| 146 | for (int i = ITEM_COUNT - 1; i >= 0; --i) { |
| 147 | EXPECT_NO_THROW(allocator.deallocate(item_pointers[i], i + 1)); |
| 148 | item_pointers[i] = nullptr; |
| 149 | } |
| 150 | |
| 151 | /* Allocate and then deallocate in quasi-random order. */ |
| 152 | for (int i = 0; i < ITEM_COUNT; ++i) { |
| 153 | EXPECT_NO_THROW(item_pointers[i] = allocator.allocate(i + 1)); |
| 154 | } |
| 155 | for (int i = 0; i < ITEM_COUNT; i += 3) { |
| 156 | EXPECT_NO_THROW(allocator.deallocate(item_pointers[i], i + 1)); |
| 157 | item_pointers[i] = nullptr; |
| 158 | } |
| 159 | for (int i = ITEM_COUNT - 1; i >= 0; --i) { |
| 160 | if (item_pointers[i]) { |
| 161 | EXPECT_NO_THROW(allocator.deallocate(item_pointers[i], i + 1)); |
| 162 | item_pointers[i] = nullptr; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | shared_block.destroy(); |
| 167 | } |
| 168 | |
| 169 | TEST(Allocator, ZeroSize) { |
| 170 | Allocator_helper::set_allocator_max_ram_default(); |
| 171 | init_allocator_once(); |
| 172 | |
| 173 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 174 | temptable::Block shared_block; |
| 175 | temptable::Allocator<int> allocator(&shared_block, table_resource_monitor); |
| 176 | |
| 177 | int *item = nullptr; |
| 178 | |
| 179 | EXPECT_NO_THROW(item = allocator.allocate(0)); |
| 180 | EXPECT_NO_THROW(allocator.deallocate(item, 0)); |
| 181 | } |
| 182 | |
| 183 | TEST(Allocator, ConstructDestroy) { |
| 184 | Allocator_helper::set_allocator_max_ram_default(); |
| 185 | init_allocator_once(); |
| 186 | |
| 187 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 188 | temptable::Block shared_block; |
| 189 | temptable::Allocator<TestItem> allocator(&shared_block, |
| 190 | table_resource_monitor); |
| 191 | |
| 192 | TestItem *item = nullptr; |
| 193 | |
| 194 | TestItem::reset_counters(); |
| 195 | |
| 196 | EXPECT_NO_THROW(item = allocator.allocate(1)); |
| 197 | EXPECT_NO_THROW(allocator.construct(item)); |
| 198 | EXPECT_EQ(item->get_first(), 0); |
| 199 | EXPECT_EQ(item->get_second(), 0); |
| 200 | EXPECT_NO_THROW(allocator.destroy(item)); |
| 201 | EXPECT_NO_THROW(allocator.deallocate(item, 1)); |
| 202 | item = nullptr; |
| 203 | |
| 204 | EXPECT_NO_THROW(item = allocator.allocate(1)); |
| 205 | EXPECT_NO_THROW(allocator.construct(item, 1, 2)); |
| 206 | EXPECT_EQ(item->get_first(), 1); |
| 207 | EXPECT_EQ(item->get_second(), 2); |
| 208 | EXPECT_NO_THROW(allocator.destroy(item)); |
| 209 | EXPECT_NO_THROW(allocator.deallocate(item, 1)); |
| 210 | item = nullptr; |
| 211 | |
| 212 | EXPECT_EQ(TestItem::get_default_constructor_call_count(), 1); |
| 213 | EXPECT_EQ(TestItem::get_parametrized_constructor_call_count(), 1); |
| 214 | EXPECT_EQ(TestItem::get_destructor_call_count(), 2); |
| 215 | |
| 216 | shared_block.destroy(); |
| 217 | } |
| 218 | |
| 219 | TEST(Allocator, Casts) { |
| 220 | Allocator_helper::set_allocator_max_ram_default(); |
| 221 | init_allocator_once(); |
| 222 | |
| 223 | using ItemType1 = char; |
| 224 | using ItemType2 = int; |
| 225 | using ItemType3 = TestItem; |
| 226 | |
| 227 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 228 | temptable::Block shared_block; |
| 229 | temptable::Allocator<ItemType1> allocator1(&shared_block, |
| 230 | table_resource_monitor); |
| 231 | temptable::Allocator<ItemType2> allocator2(allocator1); |
| 232 | temptable::Allocator<ItemType3> allocator3(allocator2); |
| 233 | |
| 234 | EXPECT_EQ(allocator1, allocator2); |
| 235 | EXPECT_EQ(allocator1, allocator3); |
| 236 | EXPECT_EQ(allocator2, allocator3); |
| 237 | |
| 238 | ItemType1 *items1[2] = {}; |
| 239 | ItemType2 *items2[2] = {}; |
| 240 | ItemType3 *items3[2] = {}; |
| 241 | |
| 242 | EXPECT_NO_THROW(items1[0] = allocator1.allocate(1)); |
| 243 | EXPECT_NO_THROW(items2[0] = allocator2.allocate(1)); |
| 244 | EXPECT_NO_THROW(items3[0] = allocator3.allocate(1)); |
| 245 | EXPECT_NO_THROW(items1[1] = allocator1.allocate(1)); |
| 246 | EXPECT_NO_THROW(items2[1] = allocator2.allocate(1)); |
| 247 | EXPECT_NO_THROW(items3[1] = allocator3.allocate(1)); |
| 248 | |
| 249 | EXPECT_NO_THROW(allocator3.deallocate(items3[0], 1)); |
| 250 | EXPECT_NO_THROW(allocator3.deallocate(items3[1], 1)); |
| 251 | EXPECT_NO_THROW(allocator2.deallocate(items2[0], 1)); |
| 252 | EXPECT_NO_THROW(allocator2.deallocate(items2[1], 1)); |
| 253 | EXPECT_NO_THROW(allocator1.deallocate(items1[0], 1)); |
| 254 | EXPECT_NO_THROW(allocator1.deallocate(items1[1], 1)); |
| 255 | |
| 256 | shared_block.destroy(); |
| 257 | } |
| 258 | |
| 259 | } // namespace temptable_test |