1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 // Copyright (c) 2008, Google Inc.
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 are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // ---
32 // Author: Craig Silverstein
33 //
34 // Simple test of malloc_extension.  Includes test of C shims.
35 
36 #include "config_for_unittests.h"
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include "base/logging.h"
40 #include <gperftools/malloc_extension.h>
41 #include <gperftools/malloc_extension_c.h>
42 
main(int argc,char ** argv)43 int main(int argc, char** argv) {
44   void* a = malloc(1000);
45 
46   size_t cxx_bytes_used, c_bytes_used;
47   ASSERT_TRUE(MallocExtension::instance()->GetNumericProperty(
48       "generic.current_allocated_bytes", &cxx_bytes_used));
49   ASSERT_TRUE(MallocExtension_GetNumericProperty(
50       "generic.current_allocated_bytes", &c_bytes_used));
51   ASSERT_GT(cxx_bytes_used, 1000);
52   ASSERT_EQ(cxx_bytes_used, c_bytes_used);
53 
54   ASSERT_TRUE(MallocExtension::instance()->VerifyAllMemory());
55   ASSERT_TRUE(MallocExtension_VerifyAllMemory());
56 
57   ASSERT_EQ(MallocExtension::kOwned,
58             MallocExtension::instance()->GetOwnership(a));
59   // TODO(csilvers): this relies on undocumented behavior that
60   // GetOwnership works on stack-allocated variables.  Use a better test.
61   ASSERT_EQ(MallocExtension::kNotOwned,
62             MallocExtension::instance()->GetOwnership(&cxx_bytes_used));
63   ASSERT_EQ(MallocExtension::kNotOwned,
64             MallocExtension::instance()->GetOwnership(NULL));
65   ASSERT_GE(MallocExtension::instance()->GetAllocatedSize(a), 1000);
66   // This is just a sanity check.  If we allocated too much, tcmalloc is broken
67   ASSERT_LE(MallocExtension::instance()->GetAllocatedSize(a), 5000);
68   ASSERT_GE(MallocExtension::instance()->GetEstimatedAllocatedSize(1000), 1000);
69 
70   for (int i = 0; i < 10; ++i) {
71     void *p = malloc(i);
72     ASSERT_GE(MallocExtension::instance()->GetAllocatedSize(p),
73              MallocExtension::instance()->GetEstimatedAllocatedSize(i));
74     free(p);
75   }
76 
77   // Check the c-shim version too.
78   ASSERT_EQ(MallocExtension_kOwned, MallocExtension_GetOwnership(a));
79   ASSERT_EQ(MallocExtension_kNotOwned,
80             MallocExtension_GetOwnership(&cxx_bytes_used));
81   ASSERT_EQ(MallocExtension_kNotOwned, MallocExtension_GetOwnership(NULL));
82   ASSERT_GE(MallocExtension_GetAllocatedSize(a), 1000);
83   ASSERT_LE(MallocExtension_GetAllocatedSize(a), 5000);
84   ASSERT_GE(MallocExtension_GetEstimatedAllocatedSize(1000), 1000);
85 
86   free(a);
87 
88   // Verify that the .cc file and .h file have the same enum values.
89   ASSERT_EQ(static_cast<int>(MallocExtension::kUnknownOwnership),
90             static_cast<int>(MallocExtension_kUnknownOwnership));
91   ASSERT_EQ(static_cast<int>(MallocExtension::kOwned),
92             static_cast<int>(MallocExtension_kOwned));
93   ASSERT_EQ(static_cast<int>(MallocExtension::kNotOwned),
94             static_cast<int>(MallocExtension_kNotOwned));
95 
96   printf("DONE\n");
97   return 0;
98 }
99