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