summaryrefslogtreecommitdiff
path: root/os_posix/os_alloc.c
blob: da50c63b2f3b5a5aaa909233f87a3a373c794e0a (plain)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2008 WiredTiger Software.
 *	All rights reserved.
 *
 * $Id$
 */

#include "wt_internal.h"

#ifdef HAVE_DIAGNOSTIC
static void __wt_mtrack(
    ENV *env, const void *, const void *, const char *, int);
#endif

/*
 * __wt_calloc_func --
 *	ANSI calloc function.
 */
int
__wt_calloc_func(ENV *env, u_int32_t number, u_int32_t size, void *retp
#ifdef HAVE_DIAGNOSTIC
    , const char *file, int line
#endif
    )
{
	void *p;

	/*
	 * !!!
	 * This function MUST handle a NULL ENV structure reference.
	 */
	WT_ASSERT(env, number != 0 && size != 0);

	if (env != NULL && env->ienv != NULL && env->ienv->stats != NULL)
		WT_STAT_INCR(env->ienv->stats, MEMALLOC);

	if ((p = calloc(number, (size_t)size)) == NULL) {
		__wt_api_env_err(env, errno, "memory allocation");
		return (WT_ERROR);
	}
	*(void **)retp = p;

#ifdef	HAVE_DIAGNOSTIC
	__wt_mtrack(env, NULL, p, file, line);
#endif
	return (0);
}

/*
 * __wt_realloc_func --
 *	ANSI realloc function.
 */
int
__wt_realloc_func(ENV *env,
    u_int32_t *bytes_allocated_ret, u_int32_t bytes_to_allocate, void *retp
#ifdef HAVE_DIAGNOSTIC
    , const char *file, int line
#endif
    )
{
	void *p;
	u_int32_t bytes_allocated;

	/*
	 * !!!
	 * This function MUST handle a NULL ENV structure reference.
	 */
	WT_ASSERT(env, bytes_to_allocate != 0);

	if (env != NULL && env->ienv != NULL && env->ienv->stats != NULL)
		WT_STAT_INCR(env->ienv->stats, MEMALLOC);

	p = *(void **)retp;

	/*
	 * Sometimes we're allocating memory and we don't care about the
	 * final length -- bytes_allocated_ret may be NULL.
	 */
	bytes_allocated =
	    bytes_allocated_ret == NULL ? 0 : *bytes_allocated_ret;
	WT_ASSERT(env, bytes_allocated < bytes_to_allocate);

	if ((p = realloc(p, (size_t)bytes_to_allocate)) == NULL) {
		__wt_api_env_err(env, errno, "memory allocation");
		return (WT_ERROR);
	}

	/*
	 * Clear the allocated memory -- an application might: allocate memory,
	 * write secret stuff into it, free the memory, we re-allocate the
	 * memory, then use it for a database page or log record and write it
	 * to disk.  That would result in the secret stuff being protected by
	 * the WiredTiger permission mechanisms, potentially inappropriate for
	 * the secret stuff.
	 */
	memset((u_int8_t *)
	    p + bytes_allocated, 0, bytes_to_allocate - bytes_allocated);

	/* Update caller's bytes allocated value. */
	if (bytes_allocated_ret != NULL)
		*bytes_allocated_ret = bytes_to_allocate;

#ifdef	HAVE_DIAGNOSTIC
	__wt_mtrack(env, *(void **)retp, p, file, line);
#endif

	*(void **)retp = p;
	return (0);
}

/*
 * __wt_strdup_func --
 *	ANSI strdup function.
 */
int
__wt_strdup_func(ENV *env, const char *str, void *retp
#ifdef HAVE_DIAGNOSTIC
    , const char *file, int line
#endif
    )
{
	size_t len;
	void *p;

	/*
	 * !!!
	 * This function MUST handle a NULL ENV structure reference.
	 */
	if (env != NULL && env->ienv != NULL && env->ienv->stats != NULL)
		WT_STAT_INCR(env->ienv->stats, MEMALLOC);

	len = strlen(str) + 1;
#ifdef HAVE_DIAGNOSTIC
	WT_RET(__wt_calloc_func(env, len, 1, &p, file, line));
#else
	WT_RET(__wt_calloc_func(env, len, 1, &p));
#endif

	memcpy(p, str, len);

	*(void **)retp = p;
	return (0);
}

/*
 * __wt_free_func --
 *	ANSI free function.
 */
void
__wt_free_func(ENV *env, void *p_arg
#ifdef HAVE_DIAGNOSTIC
    , u_int32_t len
#endif
    )
{
	void *p;

	/*
	 * !!!
	 * This function MUST handle a NULL ENV structure reference.
	 */
	if (env != NULL && env->ienv != NULL && env->ienv->stats != NULL)
		WT_STAT_INCR(env->ienv->stats, MEMFREE);

	p = *(void **)p_arg;
	if (p == NULL)			/* ANSI C free semantics */
		return;

	/*
	 * If there's a serialization bug we might race with another thread.
	 * Avoid the race by clearing the location atomically.
	 */
	*(void **)p_arg = NULL;

#ifdef HAVE_DIAGNOSTIC
	/*
	 * If we know how long the object is, overwrite it with an easily
	 * recognizable value for debugging.
	 */
	if (len != 0)
		memset(p, WT_DEBUG_BYTE, len);

	__wt_mtrack(env, p, NULL, NULL, 0);
#endif

	free(p);
}

#ifdef HAVE_DIAGNOSTIC
/*
 * __wt_mtrack_alloc --
 *	Allocate memory tracking structures.
 */
int
__wt_mtrack_alloc(ENV *env)
{
	IENV *ienv;
	WT_MTRACK *p;

	ienv = env->ienv;

	/*
	 * Use a temporary variable -- assigning memory to ienv->mtrack turns
	 * on memory object tracking, and we need to set up the rest of the
	 * structure first.
	 */
	WT_RET(__wt_calloc(env, 1, sizeof(WT_MTRACK), &p));
	WT_RET(__wt_calloc(env, 1000, sizeof(WT_MEM), &p->list));
	p->next = p->list;
	p->slots = 1000;
	ienv->mtrack = p;
	return (0);
}

/*
 * __wt_mtrack_free --
 *	Free memory tracking structures.
 */
void
__wt_mtrack_free(ENV *env)
{
	IENV *ienv;
	WT_MTRACK *p;

	ienv = env->ienv;

	/*
	 * Clear ienv->mtrack (to turn off memory object tracking) before the
	 * free.
	 */
	if ((p = ienv->mtrack) == NULL)
		return;
	ienv->mtrack = NULL;

	__wt_free(env, p->list, 0);
	__wt_free(env, p, 0);
}

/*
 * __wt_mtrack_free --
 *	Track memory allocations and frees.
 */
static void
__wt_mtrack(ENV *env, const void *f, const void *a, const char *file, int line)
{
	WT_MEM *mp, *t, *mp_end;
	WT_MTRACK *mtrack;
	int slot_check;

	if (env == NULL ||
	    env->ienv == NULL || (mtrack = env->ienv->mtrack) == NULL)
		return;

	/*
	 * Remove freed memory from the list.  If it's a free/alloc pair (that
	 * is, if __wt_realloc was called), re-use the slot.
	 */
	if (f != NULL) {
		if ((mp = mtrack->next) > mtrack->list)
			do {
				if ((--mp)->addr == f)
					goto enter;
			} while (mp > mtrack->list);

		__wt_api_env_errx(env, "mtrack: %p: not found", f);
	}

	if (a == NULL)
		return;

	/*
	 * Add allocated memory to the list.
	 *
	 * First, see if there's a slot close by we can re-use (the assumption
	 * is that when memory is allocated and quickly freed we re-use the
	 * slots instead of leaving lots of free spots in the array.
	 */
	if ((mp = mtrack->next) > mtrack->list)
		for (slot_check = 0; slot_check < 10; ++slot_check) {
			if ((--mp)->addr == NULL)
				goto enter;
			if (mp == mtrack->list)
				break;
		}

	mp_end = mtrack->list + mtrack->slots;

	/* If there's an empty slot, use it. */
	if (mtrack->next < mp_end)
		goto next;

	/* Try to compress the array. */
	for (mp = mtrack->list, t = NULL;; ++mp, ++t) {
		while (mp < mp_end && mp->addr != NULL)
			++mp;
		if (mp == mp_end)
			break;
		if (t == NULL)
			t = mp + 1;
		while (t < mp_end && t->addr == NULL)
			++t;
		if (t == mp_end)
			break;
		*mp++ = *t;
		t->addr = NULL;
	}
	mtrack->next = mp;

	/* If there's an empty slot, use it. */
	if (mtrack->next < mp_end)
		goto next;

	/* Re-allocate the array and use the next empty slot. */
	if ((mtrack->list = realloc(mtrack->list,
	    mtrack->slots * 2 * sizeof(WT_MEM))) == NULL)
		return;
	mtrack->next = mtrack->list + mtrack->slots;
	mtrack->slots *= 2;

next:	mp = mtrack->next++;
enter:	mp->addr = a;
	mp->file = file;
	mp->line = line;
}

/*
 * __wt_mtrack_dump --
 *	Complain about any memory allocated but never freed.
 */
void
__wt_mtrack_dump(ENV *env)
{
	WT_MTRACK *mtrack;
	WT_MEM *mp;

	if ((mtrack = env->ienv->mtrack) == NULL)
		return;

	for (mp = mtrack->list; mp < mtrack->next; ++mp)
		if (mp->addr != NULL)
			__wt_api_env_errx(env,
			    "mtrack: %p {%s/%d}: never freed",
			        mp->addr, mp->file, mp->line);
}
#endif