5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff/*
bd911976d51f102751848568ccf56592fd5f6d77Tinderbox User * Copyright (C) 1999-2001, 2004, 2007, 2009, 2013, 2016 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff */
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/* $Id: t_mem.c,v 1.15 2009/01/22 23:47:54 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <config.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff#include <isc/mem.h>
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews#include <tests/t_api.h>
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff
2cc1d2536d5834fbe20281068b8bd34dd1ee5337David Lawrence/*
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews * Adapted from the original mempool_test.c program.
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence */
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafssonisc_mem_t *mctx;
b68f341a48d140912f73e79489386f55cf03e50eAndreas Gustafsson
0f7045b0c437b158c61c195c319d2762882ece83Andreas Gustafsson#define MP1_FREEMAX 10
0f7045b0c437b158c61c195c319d2762882ece83Andreas Gustafsson#define MP1_FILLCNT 10
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#define MP1_MAXALLOC 30
364a82f7c25b62967678027043425201a5e5171aBob Halley
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff#define MP2_FREEMAX 25
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff#define MP2_FILLCNT 25
2cc1d2536d5834fbe20281068b8bd34dd1ee5337David Lawrence
c1807114ce574896694e3f449c73b098fe91730fMark Andrewsstatic int
c1807114ce574896694e3f449c73b098fe91730fMark Andrewsmemtest(void) {
c1807114ce574896694e3f449c73b098fe91730fMark Andrews int nfails;
c1807114ce574896694e3f449c73b098fe91730fMark Andrews void *items1[50];
c1807114ce574896694e3f449c73b098fe91730fMark Andrews void *items2[50];
c1807114ce574896694e3f449c73b098fe91730fMark Andrews void *tmp;
c1807114ce574896694e3f449c73b098fe91730fMark Andrews isc_mempool_t *mp1, *mp2;
c1807114ce574896694e3f449c73b098fe91730fMark Andrews isc_result_t isc_result;
c1807114ce574896694e3f449c73b098fe91730fMark Andrews unsigned int i, j;
c1807114ce574896694e3f449c73b098fe91730fMark Andrews int rval;
c1807114ce574896694e3f449c73b098fe91730fMark Andrews
c1807114ce574896694e3f449c73b098fe91730fMark Andrews
c1807114ce574896694e3f449c73b098fe91730fMark Andrews nfails = 0;
c1807114ce574896694e3f449c73b098fe91730fMark Andrews mctx = NULL;
c1807114ce574896694e3f449c73b098fe91730fMark Andrews isc_result = isc_mem_create(0, 0, &mctx);
56f6fd83625392a3055483e6a55c6d4b666fecd3Bob Halley if (isc_result != ISC_R_SUCCESS) {
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff t_info("isc_mem_create failed %s\n",
56f6fd83625392a3055483e6a55c6d4b666fecd3Bob Halley isc_result_totext(isc_result));
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff ++nfails;
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff return(nfails);
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley }
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley mp1 = NULL;
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley isc_result = isc_mempool_create(mctx, 24, &mp1);
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley if (isc_result != ISC_R_SUCCESS) {
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley t_info("isc_mempool_create failed %s\n",
5c02d3183f46ba8621a4bdecc71facda5f8e0717Michael Graff isc_result_totext(isc_result));
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley ++nfails;
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley return(nfails);
c1807114ce574896694e3f449c73b098fe91730fMark Andrews }
c1807114ce574896694e3f449c73b098fe91730fMark Andrews
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson mp2 = NULL;
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley isc_result = isc_mempool_create(mctx, 31, &mp2);
c1807114ce574896694e3f449c73b098fe91730fMark Andrews if (isc_result != ISC_R_SUCCESS) {
c1807114ce574896694e3f449c73b098fe91730fMark Andrews t_info("isc_mempool_create failed %s\n",
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley isc_result_totext(isc_result));
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley ++nfails;
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley return(nfails);
c1807114ce574896694e3f449c73b098fe91730fMark Andrews }
c1807114ce574896694e3f449c73b098fe91730fMark Andrews
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson if (T_debug)
3724ec85fb720159dd4542d9c590725bb7effb6fBob Halley isc_mem_stats(mctx, stderr);
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉
c1807114ce574896694e3f449c73b098fe91730fMark Andrews t_info("setting freemax to %d\n", MP1_FREEMAX);
c1807114ce574896694e3f449c73b098fe91730fMark Andrews isc_mempool_setfreemax(mp1, MP1_FREEMAX);
c1807114ce574896694e3f449c73b098fe91730fMark Andrews t_info("setting fillcount to %d\n", MP1_FILLCNT);
c1807114ce574896694e3f449c73b098fe91730fMark Andrews isc_mempool_setfillcount(mp1, MP1_FILLCNT);
c1807114ce574896694e3f449c73b098fe91730fMark Andrews t_info("setting maxalloc to %d\n", MP1_MAXALLOC);
c1807114ce574896694e3f449c73b098fe91730fMark Andrews isc_mempool_setmaxalloc(mp1, MP1_MAXALLOC);
c1807114ce574896694e3f449c73b098fe91730fMark Andrews
c1807114ce574896694e3f449c73b098fe91730fMark Andrews /*
c1807114ce574896694e3f449c73b098fe91730fMark Andrews * Allocate MP1_MAXALLOC items from the pool. This is our max.
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉 */
ec3f1d35170225c74d11c27bb184e250d150b209Tatuya JINMEI 神明達哉 for (i = 0; i < MP1_MAXALLOC; i++) {
c1807114ce574896694e3f449c73b098fe91730fMark Andrews items1[i] = isc_mempool_get(mp1);
c1807114ce574896694e3f449c73b098fe91730fMark Andrews if (items1[i] == NULL) {
54570ba7ffb85ad8449d86bd0e14fb6fb2e271c4James Brister t_info("isc_mempool_get unexpectedly failed\n");
54570ba7ffb85ad8449d86bd0e14fb6fb2e271c4James Brister ++nfails;
54570ba7ffb85ad8449d86bd0e14fb6fb2e271c4James Brister }
54570ba7ffb85ad8449d86bd0e14fb6fb2e271c4James Brister }
54570ba7ffb85ad8449d86bd0e14fb6fb2e271c4James Brister
54570ba7ffb85ad8449d86bd0e14fb6fb2e271c4James Brister /*
54570ba7ffb85ad8449d86bd0e14fb6fb2e271c4James Brister * Try to allocate one more. This should fail.
54570ba7ffb85ad8449d86bd0e14fb6fb2e271c4James Brister */
54570ba7ffb85ad8449d86bd0e14fb6fb2e271c4James Brister tmp = isc_mempool_get(mp1);
33911b409bea2e0576cb1a58e0f3408f63187976Andreas Gustafsson if (tmp != NULL) {
33911b409bea2e0576cb1a58e0f3408f63187976Andreas Gustafsson t_info("isc_mempool_get unexpectedly succeeded\n");
33911b409bea2e0576cb1a58e0f3408f63187976Andreas Gustafsson ++nfails;
33911b409bea2e0576cb1a58e0f3408f63187976Andreas Gustafsson }
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson /*
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson * Free the first 11 items. Verify that there are 10 free items on
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson * the free list (which is our max).
33911b409bea2e0576cb1a58e0f3408f63187976Andreas Gustafsson */
33911b409bea2e0576cb1a58e0f3408f63187976Andreas Gustafsson
4ed0c326e6c344589db02738358f98d96c6299d2Andreas Gustafsson for (i = 0; i < 11; i++) {
4ed0c326e6c344589db02738358f98d96c6299d2Andreas Gustafsson isc_mempool_put(mp1, items1[i]);
bb25076ffc6fd068c081c3c41b9782f715edd883Andreas Gustafsson items1[i] = NULL;
bb25076ffc6fd068c081c3c41b9782f715edd883Andreas Gustafsson }
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson
4ed0c326e6c344589db02738358f98d96c6299d2Andreas Gustafsson rval = isc_mempool_getfreecount(mp1);
4ed0c326e6c344589db02738358f98d96c6299d2Andreas Gustafsson if (rval != 10) {
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews t_info("isc_mempool_getfreecount returned %d, expected %d\n",
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews rval, MP1_FREEMAX);
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews ++nfails;
35af0018ddee51e2a9551d7aec343933498d0a79Andreas Gustafsson }
35af0018ddee51e2a9551d7aec343933498d0a79Andreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence rval = isc_mempool_getallocated(mp1);
35af0018ddee51e2a9551d7aec343933498d0a79Andreas Gustafsson if (rval != 19) {
35af0018ddee51e2a9551d7aec343933498d0a79Andreas Gustafsson t_info("isc_mempool_getallocated returned %d, expected %d\n",
bb25076ffc6fd068c081c3c41b9782f715edd883Andreas Gustafsson rval, MP1_MAXALLOC - 11);
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews ++nfails;
806c235ecf533b98d068b3f8df9d7abbe1e30cf9Mark Andrews }
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews if (T_debug)
806c235ecf533b98d068b3f8df9d7abbe1e30cf9Mark Andrews isc_mem_stats(mctx, stderr);
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews /*
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * Now, beat up on mp2 for a while. Allocate 50 items, then free
15683369e0113bd08863a705a8a1785c093455d3Mark Andrews * them, then allocate 50 more, etc.
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews */
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews t_info("setting freemax to %d\n", MP2_FREEMAX);
15683369e0113bd08863a705a8a1785c093455d3Mark Andrews isc_mempool_setfreemax(mp2, 25);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews t_info("setting fillcount to %d\n", MP2_FILLCNT);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews isc_mempool_setfillcount(mp2, 25);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews t_info("exercising the memory pool\n");
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews for (j = 0; j < 500000; j++) {
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews for (i = 0; i < 50; i++) {
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews items2[i] = isc_mempool_get(mp2);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews if (items2[i] == NULL) {
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews t_info("items2[%d] is unexpectedly null\n", i);
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews ++nfails;
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews }
35af0018ddee51e2a9551d7aec343933498d0a79Andreas Gustafsson }
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews for (i = 0; i < 50; i++) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_mempool_put(mp2, items2[i]);
4ed0c326e6c344589db02738358f98d96c6299d2Andreas Gustafsson items2[i] = NULL;
bb25076ffc6fd068c081c3c41b9782f715edd883Andreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (j % 50000 == 0)
35af0018ddee51e2a9551d7aec343933498d0a79Andreas Gustafsson t_info("...\n");
35af0018ddee51e2a9551d7aec343933498d0a79Andreas Gustafsson }
35af0018ddee51e2a9551d7aec343933498d0a79Andreas Gustafsson
35af0018ddee51e2a9551d7aec343933498d0a79Andreas Gustafsson /*
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Free all the other items and blow away this pool.
bb25076ffc6fd068c081c3c41b9782f715edd883Andreas Gustafsson */
4ed0c326e6c344589db02738358f98d96c6299d2Andreas Gustafsson for (i = 11; i < MP1_MAXALLOC; i++) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_mempool_put(mp1, items1[i]);
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence items1[i] = NULL;
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence }
4ed0c326e6c344589db02738358f98d96c6299d2Andreas Gustafsson
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence isc_mempool_destroy(&mp1);
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence if (T_debug)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_mem_stats(mctx, stderr);
91fbf6ef97974c3a9be56f637db69e5b2288647dAndreas Gustafsson
91fbf6ef97974c3a9be56f637db69e5b2288647dAndreas Gustafsson isc_mempool_destroy(&mp2);
91fbf6ef97974c3a9be56f637db69e5b2288647dAndreas Gustafsson
4ed0c326e6c344589db02738358f98d96c6299d2Andreas Gustafsson if (T_debug)
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews isc_mem_stats(mctx, stderr);
ff89d6add5f529ffd2119708ef6d70aca69f45b1Mark Andrews
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson isc_mem_destroy(&mctx);
28e509b145c3d241d1d2d9102fea38afad95bbd4Andreas Gustafsson
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson return(0);
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson}
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson
0b8c40c8d841de7e539f03fbc8e8815f06d49425Mark Andrewsstatic const char *a1 =
0b8c40c8d841de7e539f03fbc8e8815f06d49425Mark Andrews "the memory module supports the creation of memory contexts "
0b8c40c8d841de7e539f03fbc8e8815f06d49425Mark Andrews "and the management of memory pools.";
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafssonstatic void
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafssont1(void) {
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson int rval;
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence int result;
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence t_assert("mem", 1, T_REQUIRED, "%s", a1);
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence rval = memtest();
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence if (rval == 0)
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson result = T_PASS;
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson else
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson result = T_FAIL;
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson t_result(result);
527ea00c176abc167a6daf978e06f52c7e70aa06Andreas Gustafsson}
56f6fd83625392a3055483e6a55c6d4b666fecd3Bob Halley
0f0ed7ea47ce9911e9249fba14ecfef8d6054008Mark Andrewstestspec_t T_testlist[] = {
c54091c8ed26b98ceabcb3f82237b847e328769aMark Andrews { (PFV) t1, "basic memory subsystem" },
c54091c8ed26b98ceabcb3f82237b847e328769aMark Andrews { (PFV) 0, NULL }
56f6fd83625392a3055483e6a55c6d4b666fecd3Bob Halley};
c54091c8ed26b98ceabcb3f82237b847e328769aMark Andrews
8138bb315d89b63fe1946d9341e3aa36d7ff6aebMark Andrews#ifdef WIN32
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceint
56f6fd83625392a3055483e6a55c6d4b666fecd3Bob Halleymain(int argc, char **argv) {
56f6fd83625392a3055483e6a55c6d4b666fecd3Bob Halley t_settests(T_testlist);
c54091c8ed26b98ceabcb3f82237b847e328769aMark Andrews return (t_main(argc, argv));
c54091c8ed26b98ceabcb3f82237b847e328769aMark Andrews}
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews#endif
c54091c8ed26b98ceabcb3f82237b847e328769aMark Andrews