mem_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 #include <config.h>
00018 #include <stdlib.h>
00019 #include <unistd.h>
00020 #include <fcntl.h>
00021 
00022 #include <atf-c.h>
00023 
00024 #include "isctest.h"
00025 
00026 #include <isc/mem.h>
00027 #include <isc/result.h>
00028 
00029 static void *
00030 default_memalloc(void *arg, size_t size) {
00031         UNUSED(arg);
00032         if (size == 0U)
00033                 size = 1;
00034         return (malloc(size));
00035 }
00036 
00037 static void
00038 default_memfree(void *arg, void *ptr) {
00039         UNUSED(arg);
00040         free(ptr);
00041 }
00042 
00043 ATF_TC(isc_mem_total);
00044 ATF_TC_HEAD(isc_mem_total, tc) {
00045         atf_tc_set_md_var(tc, "descr", "test TotalUse calculation");
00046 }
00047 
00048 ATF_TC_BODY(isc_mem_total, tc) {
00049         isc_result_t result;
00050         isc_mem_t *mctx2 = NULL;
00051         size_t before, after;
00052         ssize_t diff;
00053         int i;
00054 
00055         result = isc_test_begin(NULL, ISC_TRUE);
00056         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00057 
00058         /* Local alloc, free */
00059         mctx2 = NULL;
00060         result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
00061                                   NULL, &mctx2, 0);
00062         if (result != ISC_R_SUCCESS)
00063                 goto out;
00064 
00065         before = isc_mem_total(mctx2);
00066 
00067         for (i = 0; i < 100000; i++) {
00068                 void *ptr;
00069 
00070                 ptr = isc_mem_allocate(mctx2, 2048);
00071                 isc_mem_free(mctx2, ptr);
00072         }
00073 
00074         after = isc_mem_total(mctx2);
00075         diff = after - before;
00076 
00077         printf("total_before=%lu, total_after=%lu, total_diff=%lu\n",
00078                (unsigned long)before, (unsigned long)after,
00079                (unsigned long)diff);
00080         /* 2048 +8 bytes extra for size_info */
00081         ATF_CHECK_EQ(diff, (2048 + 8) * 100000);
00082 
00083         /* ISC_MEMFLAG_INTERNAL */
00084 
00085         before = isc_mem_total(mctx);
00086 
00087         for (i = 0; i < 100000; i++) {
00088                 void *ptr;
00089 
00090                 ptr = isc_mem_allocate(mctx, 2048);
00091                 isc_mem_free(mctx, ptr);
00092         }
00093 
00094         after = isc_mem_total(mctx);
00095         diff = after - before;
00096 
00097         printf("total_before=%lu, total_after=%lu, total_diff=%lu\n",
00098                (unsigned long)before, (unsigned long)after,
00099                (unsigned long)diff);
00100         /* 2048 +8 bytes extra for size_info */
00101         ATF_CHECK_EQ(diff, (2048 + 8) * 100000);
00102 
00103  out:
00104         if (mctx2 != NULL)
00105                 isc_mem_destroy(&mctx2);
00106 
00107         isc_test_end();
00108 }
00109 
00110 ATF_TC(isc_mem_inuse);
00111 ATF_TC_HEAD(isc_mem_inuse, tc) {
00112         atf_tc_set_md_var(tc, "descr", "test InUse calculation");
00113 }
00114 
00115 ATF_TC_BODY(isc_mem_inuse, tc) {
00116         isc_result_t result;
00117         isc_mem_t *mctx2 = NULL;
00118         size_t before, during, after;
00119         ssize_t diff;
00120         void *ptr;
00121 
00122         result = isc_test_begin(NULL, ISC_TRUE);
00123         ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
00124 
00125         mctx2 = NULL;
00126         result = isc_mem_createx2(0, 0, default_memalloc, default_memfree,
00127                                   NULL, &mctx2, 0);
00128         if (result != ISC_R_SUCCESS)
00129                 goto out;
00130 
00131         before = isc_mem_inuse(mctx2);
00132         ptr = isc_mem_allocate(mctx2, 1024000);
00133         during = isc_mem_inuse(mctx2);
00134         isc_mem_free(mctx2, ptr);
00135         after = isc_mem_inuse(mctx2);
00136 
00137         diff = after - before;
00138 
00139         printf("inuse_before=%lu, inuse_during=%lu, inuse_after=%lu\n",
00140                (unsigned long)before, (unsigned long)during,
00141                (unsigned long)after);
00142         ATF_REQUIRE_EQ(diff, 0);
00143 
00144  out:
00145         if (mctx2 != NULL)
00146                 isc_mem_destroy(&mctx2);
00147 
00148         isc_test_end();
00149 }
00150 
00151 /*
00152  * Main
00153  */
00154 ATF_TP_ADD_TCS(tp) {
00155         ATF_TP_ADD_TC(tp, isc_mem_total);
00156         ATF_TP_ADD_TC(tp, isc_mem_inuse);
00157 
00158         return (atf_no_error());
00159 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha