mempool_test.c revision 70e5a7403f0e0a3bd292b8287c5fed5772c15270
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC")
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * Copyright (C) 1999-2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * copyright notice and this permission notice appear in all copies.
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews *
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * PERFORMANCE OF THIS SOFTWARE.
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews */
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews/* $Id: mempool_test.c,v 1.17 2007/06/19 23:46:59 tbox Exp $ */
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <config.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <isc/mem.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews#include <isc/util.h>
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrewsisc_mem_t *mctx;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrewsint
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrewsmain(int argc, char *argv[]) {
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews void *items1[50];
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews void *items2[50];
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews void *tmp;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_mempool_t *mp1, *mp2;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews unsigned int i, j;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_mutex_t lock;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews UNUSED(argc);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews UNUSED(argv);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_mem_debugging = ISC_MEM_DEBUGRECORD;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews mctx = NULL;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews mp1 = NULL;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews RUNTIME_CHECK(isc_mempool_create(mctx, 24, &mp1) == ISC_R_SUCCESS);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews mp2 = NULL;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews RUNTIME_CHECK(isc_mempool_create(mctx, 31, &mp2) == ISC_R_SUCCESS);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_mempool_associatelock(mp1, &lock);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_mempool_associatelock(mp2, &lock);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_mem_stats(mctx, stderr);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_mempool_setfreemax(mp1, 10);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_mempool_setfillcount(mp1, 10);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_mempool_setmaxalloc(mp1, 30);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews /*
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * Allocate 30 items from the pool. This is our max.
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews */
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews for (i = 0; i < 30; i++) {
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews items1[i] = isc_mempool_get(mp1);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews RUNTIME_CHECK(items1[i] != NULL);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews }
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews /*
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * Try to allocate one more. This should fail.
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews */
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews tmp = isc_mempool_get(mp1);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews RUNTIME_CHECK(tmp == NULL);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews /*
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * Free the first 11 items. Verify that there are 10 free items on
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews * the free list (which is our max).
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews */
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews for (i = 0; i < 11; i++) {
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews isc_mempool_put(mp1, items1[i]);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews items1[i] = NULL;
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews }
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews RUNTIME_CHECK(isc_mempool_getfreecount(mp1) == 10);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews RUNTIME_CHECK(isc_mempool_getallocated(mp1) == 19);
b976c39c07f7672bd1293e878b3306c7decf8afeMark Andrews
isc_mem_stats(mctx, stderr);
/*
* Now, beat up on mp2 for a while. Allocate 50 items, then free
* them, then allocate 50 more, etc.
*/
isc_mempool_setfreemax(mp2, 25);
isc_mempool_setfillcount(mp2, 25);
for (j = 0; j < 5000; j++) {
for (i = 0; i < 50; i++) {
items2[i] = isc_mempool_get(mp2);
RUNTIME_CHECK(items2[i] != NULL);
}
for (i = 0; i < 50; i++) {
isc_mempool_put(mp2, items2[i]);
items2[i] = NULL;
}
}
/*
* Free all the other items and blow away this pool.
*/
for (i = 11; i < 30; i++) {
isc_mempool_put(mp1, items1[i]);
items1[i] = NULL;
}
isc_mempool_destroy(&mp1);
isc_mem_stats(mctx, stderr);
isc_mempool_destroy(&mp2);
isc_mem_stats(mctx, stderr);
isc_mem_destroy(&mctx);
DESTROYLOCK(&lock);
return (0);
}