File: my_alloc-t.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (253 lines) | stat: -rw-r--r-- 8,012 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
   Copyright (c) 2015, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <gtest/gtest.h>
#include <stddef.h>
#include <sys/types.h>
#include <memory>
#include <string>

#include "my_alloc.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "mysys_err.h"

extern "C" void mock_error_handler_hook(uint err, const char *str, myf MyFlags);

/**
  An alternative error_handler for non-server unit tests since it does
  not rely on THD.  It sets the global error handler function.
*/
class Mock_global_error_handler {
 public:
  Mock_global_error_handler(uint expected_error)
      : m_expected_error(expected_error), m_handle_called(0) {
    current = this;
    m_old_error_handler_hook = error_handler_hook;
    error_handler_hook = mock_error_handler_hook;
  }

  virtual ~Mock_global_error_handler() {
    if (m_expected_error == 0) {
      EXPECT_EQ(0, m_handle_called);
    } else {
      EXPECT_GT(m_handle_called, 0);
    }
    error_handler_hook = m_old_error_handler_hook;
    current = nullptr;
  }

  void error_handler(uint err) {
    EXPECT_EQ(m_expected_error, err);
    ++m_handle_called;
  }

  int handle_called() const { return m_handle_called; }

  static Mock_global_error_handler *current;

 private:
  uint m_expected_error;
  int m_handle_called;

  ErrorHandlerFunctionPointer m_old_error_handler_hook;
};

Mock_global_error_handler *Mock_global_error_handler::current = nullptr;

/*
  Error handler function.
*/
extern "C" void mock_error_handler_hook(uint err, const char *, myf) {
  if (Mock_global_error_handler::current)
    Mock_global_error_handler::current->error_handler(err);
}

namespace my_alloc_unittest {

const size_t num_iterations = 1ULL;

class MyAllocTest : public ::testing::TestWithParam<size_t> {
 protected:
  void SetUp() override {
    ::new ((void *)&m_root) MEM_ROOT(PSI_NOT_INSTRUMENTED, 1024);
  }
  void TearDown() override { m_root.Clear(); }
  size_t m_num_objects;
  MEM_ROOT m_root;
};

class MyPreAllocTest : public ::testing::Test {
 protected:
  void SetUp() override {
    ::new ((void *)&m_prealloc_root) MEM_ROOT(PSI_NOT_INSTRUMENTED, 1024);
  }
  void TearDown() override { m_prealloc_root.Clear(); }
  size_t m_num_objects;
  MEM_ROOT m_prealloc_root;
};

size_t test_values[] = {100, 1000, 10000, 100000};

INSTANTIATE_TEST_SUITE_P(MyAlloc, MyAllocTest,
                         ::testing::ValuesIn(test_values));

TEST_P(MyAllocTest, NoMemoryLimit) {
  m_num_objects = GetParam();
  for (size_t ix = 0; ix < num_iterations; ++ix) {
    for (size_t objcount = 0; objcount < m_num_objects; ++objcount)
      EXPECT_NE(nullptr, m_root.Alloc(8));
  }
  // Normally larger, but with Valgrind/ASan, we'll get exact-sized blocks,
  // so also allow equal.
  EXPECT_GE(m_root.allocated_size(), num_iterations * m_num_objects * 8);
}

TEST_P(MyAllocTest, WithMemoryLimit) {
  m_num_objects = GetParam();
  m_root.set_max_capacity(num_iterations * m_num_objects * 8);
  for (size_t ix = 0; ix < num_iterations; ++ix) {
    for (size_t objcount = 0; objcount < m_num_objects; ++objcount)
      EXPECT_NE(nullptr, m_root.Alloc(8));
  }
  EXPECT_EQ(m_root.allocated_size(), num_iterations * m_num_objects * 8);
}

TEST_F(MyAllocTest, CheckErrorReporting) {
  const void *null_pointer = nullptr;
  EXPECT_TRUE(m_root.Alloc(1000));
  m_root.set_max_capacity(100);
  EXPECT_EQ(null_pointer, m_root.Alloc(1000));
  m_root.set_error_for_capacity_exceeded(true);
  Mock_global_error_handler error_handler(EE_CAPACITY_EXCEEDED);
  EXPECT_TRUE(m_root.Alloc(1000));
  EXPECT_EQ(1, error_handler.handle_called());
}

TEST_F(MyAllocTest, MoveConstructorDoesNotLeak) {
  MEM_ROOT alloc1(PSI_NOT_INSTRUMENTED, 512);
  (void)alloc1.Alloc(10);
  MEM_ROOT alloc2(PSI_NOT_INSTRUMENTED, 512);
  (void)alloc2.Alloc(30);
  alloc1 = std::move(alloc2);
}

TEST_F(MyAllocTest, ExceptionalBlocksAreNotReusedForLargerAllocations) {
  MEM_ROOT alloc(PSI_NOT_INSTRUMENTED, 512);
  void *ptr = alloc.Alloc(600);
  alloc.ClearForReuse();

  if (alloc.allocated_size() == 0) {
    /*
      The MEM_ROOT was all cleared out (probably because we're running under
      Valgrind/ASAN), so we are obviously not doing any reuse. Moreover,
      the test below is unsafe in this case, since the system malloc() could
      reuse the block.
    */
    return;
  }

  // The allocated block is too small to satisfy this new, larger allocation.
  void *ptr2 = alloc.Alloc(605);
  EXPECT_NE(ptr, ptr2);
}

TEST_F(MyAllocTest, RawInterface) {
  MEM_ROOT alloc(PSI_NOT_INSTRUMENTED, 512);

  // Nothing allocated yet.
  std::pair<char *, char *> block = alloc.Peek();
  EXPECT_EQ(0, block.second - block.first);

  // Create a block.
  alloc.ForceNewBlock(16);
  block = alloc.Peek();
  EXPECT_EQ(512, block.second - block.first);

  // Write and commit some memory.
  char *store_ptr = reinterpret_cast<char *>(block.first);
  strcpy(store_ptr, "12345");
  alloc.RawCommit(6);
  block = alloc.Peek();
  EXPECT_EQ(506, block.second - block.first);

  // Get a new block.
  alloc.ForceNewBlock(512);
  block = alloc.Peek();
#if defined(HAVE_VALGRIND) || defined(HAVE_ASAN)
  EXPECT_EQ(512, block.second - block.first);
#else
  EXPECT_EQ(768, block.second - block.first);
#endif

  // The value should still be there.
  EXPECT_STREQ("12345", store_ptr);
}

TEST_F(MyAllocTest, ArrayAllocInitialization) {
  MEM_ROOT alloc(PSI_NOT_INSTRUMENTED, 512);

  // No default value means each element is value-initialized. For int, it means
  // they are set to 0.
  int *int_array1 = alloc.ArrayAlloc<int>(100);
  ASSERT_NE(nullptr, int_array1);
  for (int i = 0; i < 100; ++i) {
    EXPECT_EQ(0, int_array1[i]);
  }

  // Initialize to explicit value.
  int *int_array2 = alloc.ArrayAlloc<int>(100, 123);
  ASSERT_NE(nullptr, int_array2);
  for (int i = 0; i < 100; ++i) {
    EXPECT_EQ(123, int_array2[i]);
  }

  // Initialize from rvalue. (Verifies that a bug, which made it only initialize
  // the first element correctly, is fixed.)
  std::string *string_array1 = alloc.ArrayAlloc<std::string>(
      10, std::string("abcdefghijklmnopqrstuvwxyz"));
  ASSERT_NE(nullptr, string_array1);
  for (int i = 0; i < 10; ++i) {
    EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", string_array1[i]);
  }
  destroy_array(string_array1, 10);

  // Should be allowed to create an array of a class which is not
  // copy-constructible.
  auto uptr_array1 = alloc.ArrayAlloc<std::unique_ptr<int>>(10);
  ASSERT_NE(nullptr, uptr_array1);
  auto uptr_array2 = alloc.ArrayAlloc<std::unique_ptr<int>>(10, nullptr);
  ASSERT_NE(nullptr, uptr_array2);
  for (int i = 0; i < 10; ++i) {
    EXPECT_EQ(nullptr, uptr_array1[i]);
    EXPECT_EQ(nullptr, uptr_array2[i]);
  }

  // This should give a compiler error, because it attempts to copy a value of a
  // non-copyable type:
  //
  // alloc.ArrayAlloc<std::unique_ptr<int>>(3, std::make_unique<int>(123));
}

}  // namespace my_alloc_unittest