| 1 | /* Copyright (c) 2016, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify it under |
| 4 | the terms of the GNU General Public License, version 2.0, as published by the |
| 5 | 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, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 18 | for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License along with |
| 21 | this program; if not, write to the Free Software Foundation, Inc., |
| 22 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <thread> |
| 26 | |
| 27 | #include "storage/temptable/include/temptable/allocator.h" |
| 28 | #include "storage/temptable/include/temptable/block.h" |
| 29 | #include "storage/temptable/include/temptable/storage.h" |
| 30 | |
| 31 | namespace temptable_storage_unittest { |
| 32 | |
| 33 | TEST(StorageTest, Iterate) { |
| 34 | std::thread t([]() { |
| 35 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 36 | temptable::Block shared_block; |
| 37 | temptable::Allocator<uint8_t> allocator(&shared_block, |
| 38 | table_resource_monitor); |
| 39 | { |
| 40 | temptable::Storage storage(&allocator); |
| 41 | |
| 42 | storage.element_size(sizeof(uint64_t)); |
| 43 | |
| 44 | for (uint64_t i = 0; i < 10000; ++i) { |
| 45 | *static_cast<uint64_t *>(storage.allocate_back()) = i; |
| 46 | } |
| 47 | |
| 48 | uint64_t i = 0; |
| 49 | for (auto it = storage.begin(); it != storage.end(); ++it, ++i) { |
| 50 | EXPECT_EQ(i, *static_cast<uint64_t *>(*it)); |
| 51 | } |
| 52 | |
| 53 | i = storage.size(); |
| 54 | auto it = storage.end(); |
| 55 | for (; it != storage.begin();) { |
| 56 | --it; |
| 57 | --i; |
| 58 | EXPECT_EQ(i, *static_cast<uint64_t *>(*it)); |
| 59 | } |
| 60 | EXPECT_EQ(0u, i); |
| 61 | } |
| 62 | shared_block.destroy(); |
| 63 | }); |
| 64 | t.join(); |
| 65 | } |
| 66 | |
| 67 | TEST(StorageTest, AllocatorRebind) { |
| 68 | // Bug in VS2019 error C3409 if we do the same as above. |
| 69 | // Turns out it is the rebind which confuses the compiler. |
| 70 | auto thread_function = []() { |
| 71 | temptable::TableResourceMonitor table_resource_monitor(16 * 1024 * 1024); |
| 72 | temptable::Block shared_block; |
| 73 | temptable::Allocator<uint8_t> alloc(&shared_block, table_resource_monitor); |
| 74 | uint8_t *shared_eater = alloc.allocate( |
| 75 | 1048576); // Make sure to consume the initial shared block. |
| 76 | uint8_t *ptr = alloc.allocate(100); |
| 77 | |
| 78 | decltype(alloc)::rebind<uint32_t>::other rebound_alloc(alloc); |
| 79 | |
| 80 | alloc.deallocate(ptr, 100); |
| 81 | |
| 82 | uint32_t *ptr2 = rebound_alloc.allocate(50); |
| 83 | rebound_alloc.deallocate(ptr2, 50); |
| 84 | |
| 85 | alloc.deallocate(shared_eater, 1048576); |
| 86 | |
| 87 | shared_block.destroy(); |
| 88 | }; |
| 89 | std::thread t(thread_function); |
| 90 | t.join(); |
| 91 | } |
| 92 | |
| 93 | } /* namespace temptable_storage_unittest */ |