1 /*
2  * Copyright (c) 2015 Sippy Software, Inc., http://www.sippysoft.com
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/stat.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <string.h>
33 
34 #include "rtpp_log.h"
35 #include "rtpp_types.h"
36 #include "rtpp_log_obj.h"
37 #include "rtpp_memdeb_internal.h"
38 #include "rtpp_memdeb_stats.h"
39 
40 int
rtpp_memdeb_selftest(void * pvt)41 rtpp_memdeb_selftest(void *pvt)
42 {
43     void *p;
44     int i;
45     struct memdeb_stats mds_b, mds_a;
46 
47     memset(&mds_b, '\0', sizeof(mds_b));
48     memset(&mds_a, '\0', sizeof(mds_a));
49     p = malloc(50);
50     free(p);
51     if (rtpp_memdeb_get_stats(pvt, __FILE__, __func__, &mds_b) < 1) {
52         RTPP_MEMDEB_REPORT(NULL, "MEMDEB is compiled in but is not working");
53         return (-1);
54     }
55     if (mds_b.nalloc < 1 || mds_b.nfree < 1 || \
56       mds_b.balloc < 50 ||  mds_b.bfree < 50) {
57         RTPP_MEMDEB_REPORT(NULL, "MEMDEB is compiled in but is not working");
58         return (-1);
59     }
60     for (i = 0; i < 10; i++) {
61         p = malloc(16);
62         free(p);
63     }
64     if (rtpp_memdeb_get_stats(pvt, __FILE__, __func__, &mds_a) < 2) {
65         RTPP_MEMDEB_REPORT(NULL, "MEMDEB is compiled in but is not working");
66         return (-1);
67     }
68     if (RTPP_MD_STATS_CMP(&mds_b, &mds_a) == 0) {
69         RTPP_MEMDEB_REPORT(NULL, "MEMDEB is compiled in but is not working");
70         return (-1);
71     }
72     RTPP_MD_STATS_SUB(&mds_a, &mds_b);
73     if (mds_a.nalloc != 10 || mds_a.nfree != 10 || \
74       mds_a.balloc != 160 ||  mds_a.bfree != 160) {
75         RTPP_MEMDEB_REPORT(NULL, "MEMDEB is compiled in but is not working");
76         return (-1);
77     }
78     return (0);
79 }
80