| 1 | /* Copyright (c) 2013, 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 | /* See http://code.google.com/p/googletest/wiki/Primer */ |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <stddef.h> |
| 28 | |
| 29 | #include "sql/handler.h" |
| 30 | #include "storage/innobase/include/mem0mem.h" |
| 31 | #include "storage/innobase/include/os0event.h" |
| 32 | #include "storage/innobase/include/srv0conc.h" |
| 33 | #include "storage/innobase/include/srv0srv.h" |
| 34 | #include "storage/innobase/include/univ.i" |
| 35 | |
| 36 | namespace innodb_mem0mem_unittest { |
| 37 | |
| 38 | class mem0mem : public ::testing::Test { |
| 39 | protected: |
| 40 | static void SetUpTestCase() { |
| 41 | srv_max_n_threads = srv_sync_array_size + 1; |
| 42 | os_event_global_init(); |
| 43 | sync_check_init(srv_max_n_threads); |
| 44 | } |
| 45 | static void TearDownTestCase() { |
| 46 | sync_check_close(); |
| 47 | os_event_global_destroy(); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | /* test mem_heap_is_top() */ |
| 52 | TEST_F(mem0mem, memheapistop) { |
| 53 | mem_heap_t *heap; |
| 54 | const char *str = "aabbccddeeff"; |
| 55 | size_t str_len = strlen(str); |
| 56 | char *str_in_heap; |
| 57 | void *dummy; |
| 58 | |
| 59 | #define INITIAL_HEAP_SIZE 512 |
| 60 | |
| 61 | heap = mem_heap_create(INITIAL_HEAP_SIZE, UT_LOCATION_HERE); |
| 62 | |
| 63 | str_in_heap = mem_heap_strdup(heap, str); |
| 64 | |
| 65 | EXPECT_TRUE(mem_heap_is_top(heap, str_in_heap, str_len + 1)); |
| 66 | |
| 67 | /* Check with a random pointer to make sure that mem_heap_is_top() |
| 68 | does not return true unconditionally. */ |
| 69 | EXPECT_FALSE(mem_heap_is_top(heap, "foo", 4)); |
| 70 | |
| 71 | /* Allocate another chunk and check that our string is not at the |
| 72 | top anymore. */ |
| 73 | dummy = mem_heap_alloc(heap, 32); |
| 74 | ut_a(dummy != nullptr); |
| 75 | EXPECT_FALSE(mem_heap_is_top(heap, str_in_heap, str_len + 1)); |
| 76 | |
| 77 | /* Cause the heap to allocate a second block and retest. */ |
| 78 | dummy = mem_heap_alloc(heap, INITIAL_HEAP_SIZE + 1); |
| 79 | str_in_heap = mem_heap_strdup(heap, str); |
| 80 | EXPECT_TRUE(mem_heap_is_top(heap, str_in_heap, str_len + 1)); |
| 81 | |
| 82 | /* Allocate another chunk, free it, and then confirm that our string |
| 83 | is still the topmost element. */ |
| 84 | const ulint x = 64; |
| 85 | dummy = mem_heap_alloc(heap, x); |
| 86 | EXPECT_FALSE(mem_heap_is_top(heap, str_in_heap, str_len + 1)); |
| 87 | mem_heap_free_top(heap, x); |
| 88 | EXPECT_TRUE(mem_heap_is_top(heap, str_in_heap, str_len + 1)); |
| 89 | |
| 90 | mem_heap_free(heap); |
| 91 | } |
| 92 | |
| 93 | /* test mem_heap_replace() */ |
| 94 | TEST_F(mem0mem, memheapreplace) { |
| 95 | mem_heap_t *heap; |
| 96 | void *p1; |
| 97 | const ulint p1_size = 16; |
| 98 | void *p2; |
| 99 | const ulint p2_size = 32; |
| 100 | void *p3; |
| 101 | const ulint p3_size = 64; |
| 102 | void *p4; |
| 103 | const ulint p4_size = 128; |
| 104 | void *p5; |
| 105 | const ulint p5_size = 256; |
| 106 | |
| 107 | heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 108 | |
| 109 | p1 = mem_heap_alloc(heap, p1_size); |
| 110 | p2 = mem_heap_alloc(heap, p2_size); |
| 111 | p3 = mem_heap_replace(heap, p1, p1_size, p3_size); |
| 112 | |
| 113 | EXPECT_NE(p2, p3); |
| 114 | EXPECT_TRUE(mem_heap_is_top(heap, p3, p3_size)); |
| 115 | |
| 116 | p4 = mem_heap_replace(heap, p3, p3_size, p4_size); |
| 117 | |
| 118 | EXPECT_EQ(p3, p4); |
| 119 | EXPECT_TRUE(mem_heap_is_top(heap, p4, p4_size)); |
| 120 | |
| 121 | p5 = mem_heap_replace(heap, p4, p4_size - 5, p5_size); |
| 122 | |
| 123 | #ifdef UNIV_MEM_DEBUG |
| 124 | /* In UNIV_MEM_DEBUG we need to specify the correct size of the |
| 125 | old top in order for it to get replaced. */ |
| 126 | EXPECT_NE(p4, p5); |
| 127 | #else /* UNIV_MEM_DEBUG */ |
| 128 | EXPECT_EQ(p4, p5); |
| 129 | #endif /* UNIV_MEM_DEBUG */ |
| 130 | EXPECT_TRUE(mem_heap_is_top(heap, p5, p5_size)); |
| 131 | |
| 132 | mem_heap_free(heap); |
| 133 | } |
| 134 | |
| 135 | } // namespace innodb_mem0mem_unittest |