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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/*-
* Copyright (c) 2008-2012 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
static void __wt_buf_clear(WT_ITEM *);
/*
* __wt_buf_clear --
* Clear a buffer.
*/
static void
__wt_buf_clear(WT_ITEM *buf)
{
buf->data = NULL;
buf->size = 0;
buf->mem = NULL;
buf->memsize = 0;
/* Note: don't clear the flags, the buffer remains marked in-use. */
}
/*
* __wt_buf_grow --
* Grow a buffer that's currently in-use.
*/
int
__wt_buf_grow(WT_SESSION_IMPL *session, WT_ITEM *buf, size_t size)
{
size_t offset;
int set_data;
WT_ASSERT(session, size <= UINT32_MAX);
if (size > buf->memsize) {
/*
* Grow the buffer's memory: if the data reference is not set
* or references the buffer's memory, maintain it.
*/
WT_ASSERT(session, buf->mem == NULL || buf->memsize > 0);
if (buf->data == NULL) {
offset = 0;
set_data = 1;
} else if (buf->data >= buf->mem &&
(uint8_t *)buf->data < (uint8_t *)buf->mem + buf->memsize) {
offset = WT_PTRDIFF(buf->data, buf->mem);
set_data = 1;
} else {
offset = 0;
set_data = 0;
}
if (F_ISSET(buf, WT_ITEM_ALIGNED))
WT_RET(__wt_realloc_aligned(
session, &buf->memsize, size, &buf->mem));
else
WT_RET(__wt_realloc(
session, &buf->memsize, size, &buf->mem));
if (set_data)
buf->data = (uint8_t *)buf->mem + offset;
}
return (0);
}
/*
* __wt_buf_init --
* Initialize a buffer at a specific size.
*/
int
__wt_buf_init(WT_SESSION_IMPL *session, WT_ITEM *buf, size_t size)
{
WT_RET(__wt_buf_grow(session, buf, size));
buf->data = buf->mem;
buf->size = 0;
return (0);
}
/*
* __wt_buf_initsize --
* Initialize a buffer at a specific size, and set the data length.
*/
int
__wt_buf_initsize(WT_SESSION_IMPL *session, WT_ITEM *buf, size_t size)
{
WT_RET(__wt_buf_init(session, buf, size));
buf->size = WT_STORE_SIZE(size); /* Set the data length. */
return (0);
}
/*
* __wt_buf_set --
* Set the contents of the buffer.
*/
int
__wt_buf_set(
WT_SESSION_IMPL *session, WT_ITEM *buf, const void *data, size_t size)
{
/* Ensure the buffer is large enough. */
WT_RET(__wt_buf_initsize(session, buf, size));
memcpy(buf->mem, data, size);
return (0);
}
/*
* __wt_buf_set_printable --
* Set the contents of the buffer to a printable representation of a
* byte string.
*/
int
__wt_buf_set_printable(
WT_SESSION_IMPL *session, WT_ITEM *buf, const void *from_arg, size_t size)
{
return (__wt_raw_to_esc_hex(session, from_arg, size, buf));
}
/*
* __wt_buf_steal --
* Steal a buffer for another purpose.
*/
void *
__wt_buf_steal(WT_SESSION_IMPL *session, WT_ITEM *buf, uint32_t *sizep)
{
void *retp;
/*
* Sometimes we steal a buffer for a different purpose, for example,
* we've read in an overflow item, and now it's going to become a key
* on an in-memory page, eventually freed when the page is discarded.
*
* First, correct for the possibility the data field doesn't point to
* the start of memory (if we only have a single memory reference, it
* must point to the start of the memory chunk, otherwise freeing the
* memory isn't going to work out). This is possibly a common case:
* it happens when we read in overflow items and we want to skip over
* the page header, so buf->data references a location past buf->mem.
*/
if (buf->data != buf->mem) {
WT_ASSERT(session,
buf->data > buf->mem &&
(uint8_t *)buf->data <
(uint8_t *)buf->mem + buf->memsize &&
(uint8_t *)buf->data + buf->size <=
(uint8_t *)buf->mem + buf->memsize);
memmove(buf->mem, buf->data, buf->size);
}
/* Second, give our caller the buffer's memory. */
retp = buf->mem;
if (sizep != NULL)
*sizep = buf->size;
/* Third, discard the buffer's memory. */
__wt_buf_clear(buf);
return (retp);
}
/*
* __wt_buf_swap --
* Swap a pair of buffers.
*/
void
__wt_buf_swap(WT_ITEM *a, WT_ITEM *b)
{
WT_ITEM tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/*
* __wt_buf_free --
* Free a buffer.
*/
void
__wt_buf_free(WT_SESSION_IMPL *session, WT_ITEM *buf)
{
__wt_free(session, buf->mem);
__wt_buf_clear(buf);
}
/*
* __wt_buf_fmt --
* Grow a buffer to accommodate a formatted string.
*/
int
__wt_buf_fmt(WT_SESSION_IMPL *session, WT_ITEM *buf, const char *fmt, ...)
WT_GCC_FUNC_ATTRIBUTE((format (printf, 3, 4)))
{
va_list ap;
size_t len;
for (;;) {
va_start(ap, fmt);
len = (size_t)vsnprintf(buf->mem, buf->memsize, fmt, ap);
va_end(ap);
/* Check if there was enough space. */
if (len < buf->memsize) {
buf->data = buf->mem;
buf->size = WT_STORE_SIZE(len);
return (0);
}
/*
* If not, double the size of the buffer: we're dealing with
* strings, and we don't expect these numbers to get huge.
*/
WT_RET(__wt_buf_grow(
session, buf, WT_MAX(len + 1, buf->memsize * 2)));
}
}
/*
* __wt_buf_catfmt --
* Grow a buffer to append a formatted string.
*/
int
__wt_buf_catfmt(WT_SESSION_IMPL *session, WT_ITEM *buf, const char *fmt, ...)
WT_GCC_FUNC_ATTRIBUTE((format (printf, 3, 4)))
{
va_list ap;
size_t len, space;
char *p;
for (;;) {
va_start(ap, fmt);
p = (char *)((uint8_t *)buf->mem + buf->size);
WT_ASSERT(session, buf->memsize >= buf->size);
space = buf->memsize - buf->size;
len = (size_t)vsnprintf(p, (size_t)space, fmt, ap);
va_end(ap);
/* Check if there was enough space. */
if (len < space) {
buf->size += WT_STORE_SIZE(len);
return (0);
}
/*
* If not, double the size of the buffer: we're dealing with
* strings, and we don't expect these numbers to get huge.
*/
WT_RET(__wt_buf_grow(session, buf,
WT_MAX(buf->size + len + 1, buf->memsize * 2)));
}
}
/*
* __wt_scr_alloc --
* Scratch buffer allocation function.
*/
int
__wt_scr_alloc(WT_SESSION_IMPL *session, uint32_t size, WT_ITEM **scratchp)
{
WT_ITEM *buf, **p, *best, **slot;
size_t allocated;
u_int i;
int ret;
/* Don't risk the caller not catching the error. */
*scratchp = NULL;
/*
* Each WT_SESSION_IMPL has an array of scratch buffers available for
* use by any function. We use WT_ITEM structures for scratch memory
* because we already have functions that do variable-length allocation
* on a WT_ITEM. Scratch buffers are allocated only by a single thread
* of control, so no locking is necessary.
*
* Walk the array, looking for a buffer we can use.
*/
for (i = 0, best = NULL, slot = NULL,
p = session->scratch; i < session->scratch_alloc; ++i, ++p) {
/* If we find an empty slot, remember it. */
if ((buf = *p) == NULL) {
if (slot == NULL)
slot = p;
continue;
}
if (F_ISSET(buf, WT_ITEM_INUSE))
continue;
/*
* If we find a buffer that's not in-use, check its size: we
* want the the smallest buffer larger than the requested size,
* or the largest buffer if none are large enough.
*/
if (best == NULL ||
(best->memsize < size && buf->memsize > best->memsize) ||
(buf->memsize >= size && buf->memsize < best->memsize))
best = buf;
/* If we find a perfect match, use it. */
if (best->memsize == size)
break;
}
/*
* If we didn't find a free buffer, extend the array and use the first
* slot we allocated.
*/
if (best == NULL && slot == NULL) {
allocated = session->scratch_alloc * sizeof(WT_ITEM *);
WT_ERR(__wt_realloc(session, &allocated,
(session->scratch_alloc + 10) * sizeof(WT_ITEM *),
&session->scratch));
slot = session->scratch + session->scratch_alloc;
session->scratch_alloc += 10;
}
/*
* If slot is non-NULL, we found an empty slot, try and allocate a
* buffer.
*/
if (best == NULL) {
WT_ASSERT(session, slot != NULL);
WT_ERR(__wt_calloc_def(session, 1, slot));
best = *slot;
/* Scratch buffers must be aligned. */
F_SET(best, WT_ITEM_ALIGNED);
}
/* Grow the buffer as necessary and return. */
WT_ERR(__wt_buf_init(session, best, size));
F_SET(best, WT_ITEM_INUSE);
*scratchp = best;
return (0);
err: WT_RET_MSG(session, ret,
"session unable to allocate a scratch buffer");
}
/*
* __wt_scr_free --
* Release a scratch buffer.
*/
void
__wt_scr_free(WT_ITEM **bufp)
{
if (*bufp == NULL)
return;
F_CLR(*bufp, WT_ITEM_INUSE);
*bufp = NULL;
}
/*
* __wt_scr_discard --
* Free all memory associated with the scratch buffers.
*/
void
__wt_scr_discard(WT_SESSION_IMPL *session)
{
WT_ITEM **bufp;
u_int i;
for (i = 0,
bufp = session->scratch; i < session->scratch_alloc; ++i, ++bufp) {
if (*bufp == NULL)
continue;
#if 0
if (F_ISSET(*bufp, WT_ITEM_INUSE))
__wt_errx(session,
"scratch buffer allocated and never discarded");
#endif
__wt_buf_free(session, *bufp);
__wt_free(session, *bufp);
}
__wt_free(session, session->scratch);
}
/*
* __wt_scr_alloc_ext --
* Allocate a scratch buffer, and return the memory reference.
*/
void *
__wt_scr_alloc_ext(WT_SESSION *wt_session, size_t size)
{
WT_ITEM *buf;
WT_SESSION_IMPL *session;
session = (WT_SESSION_IMPL *)wt_session;
return (__wt_scr_alloc(
session, (uint32_t)size, &buf) == 0 ? buf->mem : NULL);
}
/*
* __wt_scr_free_ext --
* Free a scratch buffer based on the memory reference.
*/
void
__wt_scr_free_ext(WT_SESSION *wt_session, void *p)
{
WT_ITEM **bufp;
WT_SESSION_IMPL *session;
u_int i;
session = (WT_SESSION_IMPL *)wt_session;
for (i = 0,
bufp = session->scratch; i < session->scratch_alloc; ++i, ++bufp)
if (*bufp != NULL && (*bufp)->mem == p) {
/*
* Do NOT call __wt_scr_free() here, it clears the
* caller's pointer, which would truncate the list.
*/
F_CLR(*bufp, WT_ITEM_INUSE);
return;
}
__wt_errx(session, "extension free'd non-existent scratch buffer");
}
|