1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #define DONT_DEPRECATE_MALLOC
40 #include "test.h"
41 
42 /* Test to see if setting malloc works. */
43 
44 #include <memory.h>
45 #include <db.h>
46 
47 static int malloc_counter=0;
48 static int realloc_counter=0;
49 static int free_counter=0;
50 
51 static void *
bmalloc(size_t s)52 bmalloc (size_t s)
53 {
54     malloc_counter++;
55     return malloc(s);
56 }
57 
58 static void
bfree(void * p)59 bfree (void*p)
60 {
61     free_counter++;
62     free(p);
63 }
64 
65 static void*
brealloc(void * p,size_t s)66 brealloc (void*p, size_t s)
67 {
68     realloc_counter++;
69     return realloc(p,s);
70 }
71 
72 static void
test1(void)73 test1 (void)
74 {
75     DB_ENV *env=0;
76     int r;
77     r = db_env_create(&env, 0);            assert(r==0);
78     r = env->close(env, 0);                assert(r==0);
79     assert(malloc_counter==0);
80     assert(free_counter==0);
81     assert(realloc_counter==0);
82 
83     db_env_set_func_malloc(bmalloc);
84     r = db_env_create(&env, 0);            assert(r==0);
85     r = env->close(env, 0);                assert(r==0);
86     assert(malloc_counter>0);
87     assert(free_counter==0);
88     assert(realloc_counter==0);
89 
90     malloc_counter = realloc_counter = free_counter = 0;
91 
92     db_env_set_func_free(bfree);
93     db_env_set_func_malloc(NULL);
94     r = db_env_create(&env, 0);            assert(r==0);
95     r = env->close(env, 0);                assert(r==0);
96     assert(malloc_counter==0);
97     assert(free_counter>=0);
98     assert(realloc_counter==0);
99 
100     db_env_set_func_malloc(bmalloc);
101     db_env_set_func_realloc(brealloc);
102     db_env_set_func_free(bfree);
103 
104     // toku_malloc isn't affected by calling the BDB set_fun_malloc calls.
105     malloc_counter = realloc_counter = free_counter = 0;
106 
107     {
108 	void *x = toku_malloc(5); assert(x);	assert(malloc_counter==1 && free_counter==0 && realloc_counter==0);
109 	x = toku_realloc(x, 6);   assert(x);    assert(malloc_counter==1 && free_counter==0 && realloc_counter==1);
110 	toku_free(x);	                        assert(malloc_counter==1 && free_counter==1 && realloc_counter==1);
111     }
112 
113     db_env_set_func_malloc(NULL);
114     db_env_set_func_realloc(NULL);
115     db_env_set_func_free(NULL);
116 }
117 
118 int
test_main(int argc,char * const argv[])119 test_main (int argc __attribute__((__unused__)), char *const argv[] __attribute__((__unused__)))
120 {
121     test1();
122     return 0;
123 }
124