mempool_test.c revision bf7c02293f883118784f7952944fa3e2bce98078
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1999, 2000 Internet Software Consortium.
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * copyright notice and this permission notice appear in all copies.
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt *
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
aa7b16ec2a5dacda1d65b64e0f7af434d02cbba4Tinderbox User * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt */
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt/* $Id: mempool_test.c,v 1.11 2000/08/30 01:35:37 bwelling Exp $ */
aa7b16ec2a5dacda1d65b64e0f7af434d02cbba4Tinderbox User
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt#include <config.h>
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt#include <isc/mem.h>
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt#include <isc/util.h>
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Huntisc_mem_t *mctx;
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Huntint
e45d0508c3460db87afb1f743bc5210522721bb3Evan Huntmain(int argc, char *argv[]) {
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt void *items1[50];
aa7b16ec2a5dacda1d65b64e0f7af434d02cbba4Tinderbox User void *items2[50];
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt void *tmp;
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt isc_mempool_t *mp1, *mp2;
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt unsigned int i, j;
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt isc_mutex_t lock;
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt UNUSED(argc);
aa7b16ec2a5dacda1d65b64e0f7af434d02cbba4Tinderbox User UNUSED(argv);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt isc_mem_debugging = 2;
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt mctx = NULL;
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt mp1 = NULL;
aa7b16ec2a5dacda1d65b64e0f7af434d02cbba4Tinderbox User RUNTIME_CHECK(isc_mempool_create(mctx, 24, &mp1) == ISC_R_SUCCESS);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt mp2 = NULL;
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt RUNTIME_CHECK(isc_mempool_create(mctx, 31, &mp2) == ISC_R_SUCCESS);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt isc_mempool_associatelock(mp1, &lock);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt isc_mempool_associatelock(mp2, &lock);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
aa7b16ec2a5dacda1d65b64e0f7af434d02cbba4Tinderbox User isc_mem_stats(mctx, stderr);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt isc_mempool_setfreemax(mp1, 10);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt isc_mempool_setfillcount(mp1, 10);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt isc_mempool_setmaxalloc(mp1, 30);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt /*
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt * Allocate 30 items from the pool. This is our max.
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt */
aa7b16ec2a5dacda1d65b64e0f7af434d02cbba4Tinderbox User for (i = 0 ; i < 30 ; i++) {
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt items1[i] = isc_mempool_get(mp1);
aa7b16ec2a5dacda1d65b64e0f7af434d02cbba4Tinderbox User RUNTIME_CHECK(items1[i] != NULL);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt }
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt /*
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt * Try to allocate one more. This should fail.
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt */
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt tmp = isc_mempool_get(mp1);
aa7b16ec2a5dacda1d65b64e0f7af434d02cbba4Tinderbox User RUNTIME_CHECK(tmp == NULL);
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt /*
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt * Free the first 11 items. Verify that there are 10 free items on
aa7b16ec2a5dacda1d65b64e0f7af434d02cbba4Tinderbox User * the free list (which is our max).
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt */
e45d0508c3460db87afb1f743bc5210522721bb3Evan Hunt
for (i = 0 ; i < 11 ; i++) {
isc_mempool_put(mp1, items1[i]);
items1[i] = NULL;
}
RUNTIME_CHECK(isc_mempool_getfreecount(mp1) == 10);
RUNTIME_CHECK(isc_mempool_getallocated(mp1) == 19);
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);
}