1999-06-08 02:37:12 +00:00
|
|
|
/*
|
2007-06-19 23:47:24 +00:00
|
|
|
* Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC")
|
2001-01-09 22:01:04 +00:00
|
|
|
* Copyright (C) 1999-2001 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2007-06-18 23:47:57 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
1999-06-08 02:37:12 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
1999-06-08 02:37:12 +00:00
|
|
|
*/
|
|
|
|
|
2007-06-19 23:47:24 +00:00
|
|
|
/* $Id: mempool_test.c,v 1.17 2007/06/19 23:46:59 tbox Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1999-06-08 02:37:12 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <isc/mem.h>
|
2000-04-28 02:12:16 +00:00
|
|
|
#include <isc/util.h>
|
1999-06-08 02:37:12 +00:00
|
|
|
|
|
|
|
isc_mem_t *mctx;
|
|
|
|
|
|
|
|
int
|
2000-05-08 14:38:29 +00:00
|
|
|
main(int argc, char *argv[]) {
|
1999-06-08 02:37:12 +00:00
|
|
|
void *items1[50];
|
|
|
|
void *items2[50];
|
|
|
|
void *tmp;
|
|
|
|
isc_mempool_t *mp1, *mp2;
|
|
|
|
unsigned int i, j;
|
1999-09-15 17:47:44 +00:00
|
|
|
isc_mutex_t lock;
|
1999-06-08 02:37:12 +00:00
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
1999-06-16 21:04:30 +00:00
|
|
|
|
2001-09-06 23:14:42 +00:00
|
|
|
isc_mem_debugging = ISC_MEM_DEBUGRECORD;
|
2000-07-26 19:07:36 +00:00
|
|
|
|
1999-09-15 17:47:44 +00:00
|
|
|
RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
|
|
|
|
|
1999-06-08 02:37:12 +00:00
|
|
|
mctx = NULL;
|
|
|
|
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
mp1 = NULL;
|
|
|
|
RUNTIME_CHECK(isc_mempool_create(mctx, 24, &mp1) == ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
mp2 = NULL;
|
|
|
|
RUNTIME_CHECK(isc_mempool_create(mctx, 31, &mp2) == ISC_R_SUCCESS);
|
|
|
|
|
1999-09-15 17:47:44 +00:00
|
|
|
isc_mempool_associatelock(mp1, &lock);
|
|
|
|
isc_mempool_associatelock(mp2, &lock);
|
|
|
|
|
1999-06-08 02:37:12 +00:00
|
|
|
isc_mem_stats(mctx, stderr);
|
|
|
|
|
|
|
|
isc_mempool_setfreemax(mp1, 10);
|
|
|
|
isc_mempool_setfillcount(mp1, 10);
|
|
|
|
isc_mempool_setmaxalloc(mp1, 30);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate 30 items from the pool. This is our max.
|
|
|
|
*/
|
2001-11-27 00:56:32 +00:00
|
|
|
for (i = 0; i < 30; i++) {
|
1999-06-08 02:37:12 +00:00
|
|
|
items1[i] = isc_mempool_get(mp1);
|
|
|
|
RUNTIME_CHECK(items1[i] != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to allocate one more. This should fail.
|
|
|
|
*/
|
|
|
|
tmp = isc_mempool_get(mp1);
|
|
|
|
RUNTIME_CHECK(tmp == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the first 11 items. Verify that there are 10 free items on
|
|
|
|
* the free list (which is our max).
|
|
|
|
*/
|
|
|
|
|
2001-11-27 00:56:32 +00:00
|
|
|
for (i = 0; i < 11; i++) {
|
1999-06-08 02:37:12 +00:00
|
|
|
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);
|
2001-11-27 00:56:32 +00:00
|
|
|
for (j = 0; j < 5000; j++) {
|
|
|
|
for (i = 0; i < 50; i++) {
|
1999-06-08 02:37:12 +00:00
|
|
|
items2[i] = isc_mempool_get(mp2);
|
|
|
|
RUNTIME_CHECK(items2[i] != NULL);
|
|
|
|
}
|
2001-11-27 00:56:32 +00:00
|
|
|
for (i = 0; i < 50; i++) {
|
1999-06-08 02:37:12 +00:00
|
|
|
isc_mempool_put(mp2, items2[i]);
|
|
|
|
items2[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free all the other items and blow away this pool.
|
|
|
|
*/
|
2001-11-27 00:56:32 +00:00
|
|
|
for (i = 11; i < 30; i++) {
|
1999-06-08 02:37:12 +00:00
|
|
|
isc_mempool_put(mp1, items1[i]);
|
|
|
|
items1[i] = NULL;
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-06-08 02:37:12 +00:00
|
|
|
isc_mempool_destroy(&mp1);
|
|
|
|
|
|
|
|
isc_mem_stats(mctx, stderr);
|
|
|
|
|
|
|
|
isc_mempool_destroy(&mp2);
|
|
|
|
|
|
|
|
isc_mem_stats(mctx, stderr);
|
|
|
|
|
|
|
|
isc_mem_destroy(&mctx);
|
|
|
|
|
2000-08-30 01:35:42 +00:00
|
|
|
DESTROYLOCK(&lock);
|
1999-09-15 17:47:44 +00:00
|
|
|
|
1999-06-08 02:37:12 +00:00
|
|
|
return (0);
|
|
|
|
}
|