forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_alloc.cc
281 lines (237 loc) · 8.69 KB
/
my_alloc.cc
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
Without limiting anything contained in the foregoing, this file,
which is part of C Driver for MySQL (Connector/C), is also subject to the
Universal FOSS Exception, version 1.0, a copy of which can be found at
http://oss.oracle.com/licenses/universal-foss-exception.
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 */
/**
* @file mysys/my_alloc.cc
* Implementation of MEM_ROOT.
*
* This file follows Google coding style.
*/
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include "my_alloc.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_pointer_arithmetic.h"
#include "my_sys.h"
#include "mysql/service_mysql_alloc.h"
#include "mysys_err.h"
#include "template_utils.h"
// For instrumented code: Always use malloc(); never reuse a chunk.
// This gives a lot more memory chunks, each with a red-zone around them.
#if defined(HAVE_VALGRIND) || defined(HAVE_ASAN)
#define MEM_ROOT_SINGLE_CHUNKS 1
#else
#define MEM_ROOT_SINGLE_CHUNKS 0
#endif
MEM_ROOT::Block *MEM_ROOT::AllocBlock(size_t length) {
DBUG_ENTER("MEM_ROOT::AllocBlock");
if (m_max_capacity != 0 && (m_allocated_size > m_max_capacity ||
length > m_max_capacity - m_allocated_size)) {
if (m_error_for_capacity_exceeded) {
my_error(EE_CAPACITY_EXCEEDED, MYF(0),
static_cast<ulonglong>(m_max_capacity));
// NOTE: No early return; we will abort the query at the next safe point.
} else {
DBUG_RETURN(nullptr);
}
}
Block *new_block = static_cast<Block *>(
my_malloc(m_psi_key, length + ALIGN_SIZE(sizeof(Block)),
MYF(MY_WME | ME_FATALERROR)));
if (new_block == nullptr) {
if (m_error_handler) (m_error_handler)();
DBUG_RETURN(nullptr);
}
m_allocated_size += length;
// Make the default block size 50% larger next time.
// This ensures O(1) total mallocs (assuming Clear() is not called).
m_block_size += m_block_size / 2;
DBUG_RETURN(new_block);
}
void *MEM_ROOT::AllocSlow(size_t length) {
DBUG_ENTER("MEM_ROOT::alloc");
DBUG_PRINT("enter", ("root: %p", this));
// We need to allocate a new block to satisfy this allocation;
// otherwise, the fast path in Alloc() would not have sent us here.
// We plan to allocate a block of <block_size> bytes; see if that
// would be enough or not.
if (length >= m_block_size || MEM_ROOT_SINGLE_CHUNKS) {
// The next block we'd allocate would _not_ be big enough
// (or we're in Valgrind/ASAN mode, and want everything in single chunks).
// Allocate an entirely new block, not disturbing anything;
// since the new block isn't going to be used for the next allocation
// anyway, we can just as well keep the previous one.
Block *new_block = AllocBlock(length);
if (new_block == nullptr) DBUG_RETURN(nullptr);
if (m_current_block == nullptr) {
// This is the only block, so it has to be the current block, too.
// However, it will be full, so we won't be allocating from it
// unless ClearForReuse() is called.
new_block->prev = nullptr;
m_current_block = new_block;
m_current_free_end = pointer_cast<char *>(new_block) +
ALIGN_SIZE(sizeof(*new_block)) + length;
m_current_free_start = m_current_free_end;
} else {
// Insert the new block in the second-to-last position.
new_block->prev = m_current_block->prev;
m_current_block->prev = new_block;
}
DBUG_RETURN(pointer_cast<char *>(new_block) +
ALIGN_SIZE(sizeof(*new_block)));
} else {
// The normal case: Throw away the current block, allocate a new block,
// and use that to satisfy the new allocation.
const size_t new_block_size = m_block_size;
Block *new_block = AllocBlock(new_block_size); // Will modify block_size.
if (new_block == nullptr) DBUG_RETURN(nullptr);
new_block->prev = m_current_block;
m_current_block = new_block;
char *new_mem =
pointer_cast<char *>(new_block) + ALIGN_SIZE(sizeof(*new_block));
m_current_free_start = new_mem + length;
m_current_free_end = new_mem + new_block_size;
DBUG_RETURN(new_mem);
}
}
void MEM_ROOT::Clear() {
DBUG_ENTER("MEM_ROOT::Clear()");
DBUG_PRINT("enter", ("root: %p", this));
// Already cleared, or memset() to zero, so just ignore.
if (m_current_block == nullptr) DBUG_VOID_RETURN;
Block *start = m_current_block;
m_current_block = nullptr;
m_block_size = m_orig_block_size;
m_current_free_start = &s_dummy_target;
m_current_free_end = &s_dummy_target;
m_allocated_size = 0;
FreeBlocks(start);
DBUG_VOID_RETURN;
}
void MEM_ROOT::ClearForReuse() {
DBUG_ENTER("MEM_ROOT::ClearForReuse()");
if (MEM_ROOT_SINGLE_CHUNKS) {
Clear();
DBUG_VOID_RETURN;
}
// Already cleared, or memset() to zero, so just ignore.
if (m_current_block == nullptr) DBUG_VOID_RETURN;
// Keep the last block, which is usually the biggest one.
m_current_free_start = pointer_cast<char *>(m_current_block) +
ALIGN_SIZE(sizeof(*m_current_block));
Block *start = m_current_block->prev;
m_current_block->prev = nullptr;
m_allocated_size = m_current_free_end - m_current_free_start;
FreeBlocks(start);
DBUG_VOID_RETURN;
}
void MEM_ROOT::FreeBlocks(Block *start) {
// The MEM_ROOT might be allocated on itself, so make sure we don't
// touch it after we've started freeing.
for (Block *block = start; block != nullptr;) {
Block *prev = block->prev;
my_free(block);
block = prev;
}
}
void MEM_ROOT::Claim() {
DBUG_ENTER("MEM_ROOT::Claim()");
DBUG_PRINT("enter", ("root: %p", this));
for (Block *block = m_current_block; block != nullptr; block = block->prev) {
my_claim(block);
}
DBUG_VOID_RETURN;
}
/*
* Allocate many pointers at the same time.
*
* DESCRIPTION
* ptr1, ptr2, etc all point into big allocated memory area.
*
* SYNOPSIS
* multi_alloc_root()
* root Memory root
* ptr1, length1 Multiple arguments terminated by a NULL pointer
* ptr2, length2 ...
* ...
* NULL
*
* RETURN VALUE
* A pointer to the beginning of the allocated memory block
* in case of success or NULL if out of memory.
*/
void *multi_alloc_root(MEM_ROOT *root, ...) {
va_list args;
char **ptr, *start, *res;
size_t tot_length, length;
DBUG_ENTER("multi_alloc_root");
va_start(args, root);
tot_length = 0;
while ((ptr = va_arg(args, char **))) {
length = va_arg(args, uint);
tot_length += ALIGN_SIZE(length);
}
va_end(args);
if (!(start = static_cast<char *>(root->Alloc(tot_length))))
DBUG_RETURN(0); /* purecov: inspected */
va_start(args, root);
res = start;
while ((ptr = va_arg(args, char **))) {
*ptr = res;
length = va_arg(args, uint);
res += ALIGN_SIZE(length);
}
va_end(args);
DBUG_RETURN((void *)start);
}
char *strdup_root(MEM_ROOT *root, const char *str) {
return strmake_root(root, str, strlen(str));
}
char *safe_strdup_root(MEM_ROOT *root, const char *str) {
return str ? strdup_root(root, str) : 0;
}
void free_root(MEM_ROOT *root, myf flags) {
if (root != nullptr) {
if ((flags & MY_MARK_BLOCKS_FREE) || (flags & MY_KEEP_PREALLOC))
root->ClearForReuse();
else
root->Clear();
}
}
char *strmake_root(MEM_ROOT *root, const char *str, size_t len) {
char *pos;
if ((pos = static_cast<char *>(root->Alloc(len + 1)))) {
if (len > 0) memcpy(pos, str, len);
pos[len] = 0;
}
return pos;
}
void *memdup_root(MEM_ROOT *root, const void *str, size_t len) {
char *pos;
if ((pos = static_cast<char *>(root->Alloc(len)))) {
memcpy(pos, str, len);
}
return pos;
}
char MEM_ROOT::s_dummy_target;