t_mem.c 4.33 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10 11
 */

Automatic Updater's avatar
Automatic Updater committed
12
/* $Id: t_mem.c,v 1.15 2009/01/22 23:47:54 tbox Exp $ */
David Lawrence's avatar
David Lawrence committed
13

14 15 16 17
#include <config.h>

#include <isc/mem.h>

18
#include <tests/t_api.h>
19 20

/*
21
 * Adapted from the original mempool_test.c program.
22 23 24 25 26 27 28 29 30 31 32
 */
isc_mem_t *mctx;

#define	MP1_FREEMAX	10
#define	MP1_FILLCNT	10
#define	MP1_MAXALLOC	30

#define	MP2_FREEMAX	25
#define	MP2_FILLCNT	25

static int
David Lawrence's avatar
David Lawrence committed
33
memtest(void) {
34 35 36 37 38 39 40 41 42 43 44 45 46 47
	int		nfails;
	void		*items1[50];
	void		*items2[50];
	void		*tmp;
	isc_mempool_t	*mp1, *mp2;
	isc_result_t	isc_result;
	unsigned int	i, j;
	int		rval;


	nfails = 0;
	mctx = NULL;
	isc_result = isc_mem_create(0, 0, &mctx);
	if (isc_result != ISC_R_SUCCESS) {
David Lawrence's avatar
David Lawrence committed
48 49
		t_info("isc_mem_create failed %s\n",
		       isc_result_totext(isc_result));
50 51 52 53 54 55 56
		++nfails;
		return(nfails);
	}

	mp1 = NULL;
	isc_result = isc_mempool_create(mctx, 24, &mp1);
	if (isc_result != ISC_R_SUCCESS) {
David Lawrence's avatar
David Lawrence committed
57 58
		t_info("isc_mempool_create failed %s\n",
		       isc_result_totext(isc_result));
59 60 61 62 63 64 65
		++nfails;
		return(nfails);
	}

	mp2 = NULL;
	isc_result = isc_mempool_create(mctx, 31, &mp2);
	if (isc_result != ISC_R_SUCCESS) {
David Lawrence's avatar
David Lawrence committed
66 67
		t_info("isc_mempool_create failed %s\n",
		       isc_result_totext(isc_result));
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		++nfails;
		return(nfails);
	}

	if (T_debug)
		isc_mem_stats(mctx, stderr);

	t_info("setting freemax to %d\n", MP1_FREEMAX);
	isc_mempool_setfreemax(mp1, MP1_FREEMAX);
	t_info("setting fillcount to %d\n", MP1_FILLCNT);
	isc_mempool_setfillcount(mp1, MP1_FILLCNT);
	t_info("setting maxalloc to %d\n", MP1_MAXALLOC);
	isc_mempool_setmaxalloc(mp1, MP1_MAXALLOC);

	/*
	 * Allocate MP1_MAXALLOC items from the pool.  This is our max.
	 */
85
	for (i = 0; i < MP1_MAXALLOC; i++) {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
		items1[i] = isc_mempool_get(mp1);
		if (items1[i] == NULL) {
			t_info("isc_mempool_get unexpectedly failed\n");
			++nfails;
		}
	}

	/*
	 * Try to allocate one more.  This should fail.
	 */
	tmp = isc_mempool_get(mp1);
	if (tmp != NULL) {
		t_info("isc_mempool_get unexpectedly succeeded\n");
		++nfails;
	}

	/*
	 * Free the first 11 items.  Verify that there are 10 free items on
	 * the free list (which is our max).
	 */

107
	for (i = 0; i < 11; i++) {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
		isc_mempool_put(mp1, items1[i]);
		items1[i] = NULL;
	}

	rval = isc_mempool_getfreecount(mp1);
	if (rval != 10) {
		t_info("isc_mempool_getfreecount returned %d, expected %d\n",
				rval, MP1_FREEMAX);
		++nfails;
	}

	rval = isc_mempool_getallocated(mp1);
	if (rval != 19) {
		t_info("isc_mempool_getallocated returned %d, expected %d\n",
				rval, MP1_MAXALLOC - 11);
		++nfails;
	}
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139
	if (T_debug)
		isc_mem_stats(mctx, stderr);

	/*
	 * Now, beat up on mp2 for a while.  Allocate 50 items, then free
	 * them, then allocate 50 more, etc.
	 */

	t_info("setting freemax to %d\n", MP2_FREEMAX);
	isc_mempool_setfreemax(mp2, 25);
	t_info("setting fillcount to %d\n", MP2_FILLCNT);
	isc_mempool_setfillcount(mp2, 25);

	t_info("exercising the memory pool\n");
140 141
	for (j = 0; j < 500000; j++) {
		for (i = 0; i < 50; i++) {
142 143 144 145 146 147
			items2[i] = isc_mempool_get(mp2);
			if (items2[i] == NULL) {
				t_info("items2[%d] is unexpectedly null\n", i);
				++nfails;
			}
		}
148
		for (i = 0; i < 50; i++) {
149 150 151 152 153 154 155 156 157 158
			isc_mempool_put(mp2, items2[i]);
			items2[i] = NULL;
		}
		if (j % 50000 == 0)
			t_info("...\n");
	}

	/*
	 * Free all the other items and blow away this pool.
	 */
159
	for (i = 11; i < MP1_MAXALLOC; i++) {
160 161 162
		isc_mempool_put(mp1, items1[i]);
		items1[i] = NULL;
	}
163

164 165 166 167 168 169 170 171 172 173 174 175
	isc_mempool_destroy(&mp1);

	if (T_debug)
		isc_mem_stats(mctx, stderr);

	isc_mempool_destroy(&mp2);

	if (T_debug)
		isc_mem_stats(mctx, stderr);

	isc_mem_destroy(&mctx);

Mark Andrews's avatar
Mark Andrews committed
176
	return(nfails);
177 178
}

David Lawrence's avatar
David Lawrence committed
179
static const char *a1 =
180 181 182
		"the memory module supports the creation of memory contexts "
		"and the management of memory pools.";
static void
183
t1(void) {
184 185 186
	int	rval;
	int	result;

187
	t_assert("mem", 1, T_REQUIRED, "%s", a1);
188 189 190 191 192 193 194 195 196

	rval = memtest();

	if (rval == 0)
		result = T_PASS;
	else
		result = T_FAIL;
	t_result(result);
}
197

198
testspec_t	T_testlist[] = {
199 200
	{	(PFV) t1,	"basic memory subsystem"	},
	{	(PFV) 0,	NULL				}
201 202
};

203 204 205 206 207 208 209
#ifdef WIN32
int
main(int argc, char **argv) {
	t_settests(T_testlist);
	return (t_main(argc, argv));
}
#endif