/* * Authors: Petr Spacek * * Copyright (C) 2012 Red Hat * see file 'COPYING' for use and warranty information * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; version 2 or later * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include void logger(const char *format, ...) { va_list args; va_start(args, format); vfprintf(stderr, format, args); va_end(args); } #define CHECK(op) \ do { \ logger("%4u: %s = ", __LINE__, #op); \ result = (op); \ logger("%s\n", dns_result_totext(result)); \ if (result != ISC_R_SUCCESS) \ goto cleanup; \ } while (0) #define PRINT(op) \ do { \ logger("%4u: %s\n", __LINE__, #op); \ (op); \ } while (0) void wait() { fprintf(stderr, "\nPress ENTER to continue ...\n"); getchar(); } #define BLOCK_SIZE 1024 #define BLOCK_COUNT 1024*1024 void *blocks[BLOCK_COUNT] = {}; int main (int argc, char ** argv) { isc_result_t result = ISC_R_SUCCESS; isc_mem_t *mctx = NULL; size_t inuse; CHECK(isc_mem_create2(0, 0, &mctx, ISC_MEMFLAG_DEFAULT)); inuse = isc_mem_inuse(mctx); printf("inuse = %lu\n", inuse); isc_mem_stats(mctx, stderr); logger("Real memory consumption at this point is near to 0.\n"); wait(); logger("getting blocks"); for (int i = 1; i <= BLOCK_COUNT; i++) { if (i % 1024 == 0) logger("\rgetting block %d/%d", i, BLOCK_COUNT); blocks[i-1] = isc_mem_get(mctx, BLOCK_SIZE); } logger(": done => got %lu bytes\n", BLOCK_COUNT*BLOCK_SIZE); printf("block[BLOCK_COUNT-1] = %p\n", blocks[BLOCK_COUNT-1]); inuse = isc_mem_inuse(mctx); printf("inuse = %lu\n", inuse); isc_mem_stats(mctx, stderr); logger("Real memory consumption at this point is ~ 1 GB.\n"); wait(); logger("putting blocks"); for (int i = 1; i <= BLOCK_COUNT; i++) { if (i % 1024 == 0) logger("\rputting block %d/%d", i, BLOCK_COUNT); isc_mem_put(mctx, blocks[i-1], BLOCK_SIZE); } logger(": done => put %lu bytes\n", BLOCK_COUNT*BLOCK_SIZE); inuse = isc_mem_inuse(mctx); printf("inuse = %lu\n", inuse); isc_mem_stats(mctx, stderr); logger("Real memory consumption at this point is still ~ 1 GB !\n"); wait(); logger("destroying memory context\n"); isc_mem_destroy(&mctx); logger("Real memory consumption at this point is finally near 0.\n"); wait(); cleanup: (void) argc; (void) argv; return 0; }