test_malloc_wrappers.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #include <gtest/gtest.h>
36 
37 #include "rosrt/malloc_wrappers.h"
38 #include "ros/atomic.h"
39 #include <ros/time.h>
40 #include <ros/console.h>
41 
42 #include <boost/thread.hpp>
43 
44 #if __unix__ && !APPLE
45 #include <dlfcn.h>
46 #endif
47 
48 using namespace rosrt;
49 using namespace ros;
50 
51 void allocateThread(const atomic<bool>& done)
52 {
53  while (!done.load())
54  {
55  void* mem = malloc(500);
56  free(mem);
57  ros::WallDuration(0.001).sleep();
58  }
59 }
60 
61 TEST(MallocWrappers, statsMainThread)
62 {
63  atomic<bool> done(false);
64  boost::thread t(boost::bind(allocateThread, boost::ref(done)));
65 
67 
68  for (uint32_t i = 1; i <= 1000; ++i)
69  {
70  void* mem = malloc(500);
71  free(mem);
72  ros::WallDuration(0.001).sleep();
73 
75  ASSERT_EQ(info.mallocs, i);
76  ASSERT_EQ(info.frees, i);
77  ASSERT_EQ(info.total_ops, i * 2);
78  }
79 
80  done.store(true);
81  t.join();
82 }
83 
85 {
87 
88  for (uint32_t i = 1; i <= 1000; ++i)
89  {
90  void* mem = malloc(500);
91  free(mem);
92  ros::WallDuration(0.001).sleep();
93 
95  if (info.mallocs != i)
96  {
97  ROS_ERROR_STREAM("mallocs is " << info.mallocs << " should be " << i);
98  failed.store(true);
99  return;
100  }
101 
102  if (info.frees != i)
103  {
104  ROS_ERROR_STREAM("mallocs is " << info.frees << " should be " << i);
105  failed.store(true);
106  return;
107  }
108  }
109 }
110 
111 TEST(MallocWrappers, statsNewThread)
112 {
113  atomic<bool> failed(false);
114  boost::thread t(boost::bind(statsThread, boost::ref(failed)));
115  t.join();
116 
117  ASSERT_FALSE(failed.load());
118 }
119 
120 // TODO: once we have a low-level dynamic library wrapper, use it and allow testing on non-unix platforms
121 #if __unix__ && !APPLE
122 TEST(MallocWrappers, sharedObjectDynamicallyOpened)
123 {
124  void* handle = dlopen("libtest_malloc_wrappers_so.so", RTLD_LAZY|RTLD_GLOBAL);
125  ASSERT_TRUE(handle);
126  void*(*alloc_func)(size_t) = (void*(*)(size_t))dlsym(handle, "myTestMalloc");
127  ASSERT_TRUE(handle);
128  void(*free_func)(void*) = (void(*)(void*))dlsym(handle, "myTestFree");
129  ASSERT_TRUE(free_func);
130 
132  void* mem = alloc_func(500);
133  free_func(mem);
134 
135  AllocInfo info = getThreadAllocInfo();
136  ASSERT_EQ(info.mallocs, 1U);
137  ASSERT_EQ(info.frees, 1U);
138 
139  dlclose(handle);
140 }
141 #endif
142 
144 {
146  void* mem = malloc(500);
147  mem = 0;
149 }
150 
151 TEST(MallocWrappersDeathTest, breakOnAllocFree)
152 {
154 
155  // TODO: Re-enable once ROS 1.1 goes out with the updated version of gtest
156  //ASSERT_DEATH_IF_SUPPORTED(doBreakOnMalloc(), "Issuing break due to break_on_alloc_or_free being set");
157 }
158 
159 int main(int argc, char** argv)
160 {
161  testing::InitGoogleTest(&argc, argv);
162  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
163  return RUN_ALL_TESTS();
164 }
165 
void setThreadBreakOnAllocOrFree(bool b)
Definition: malloc.cpp:184
AllocInfo getThreadAllocInfo()
Definition: malloc.cpp:142
void * malloc(size_t size)
Definition: malloc.cpp:245
void allocateThread(const atomic< bool > &done)
void doBreakOnMalloc()
Definition: managers.h:38
bool sleep() const
void free(void *ptr)
Definition: malloc.cpp:293
void resetThreadAllocInfo()
Definition: malloc.cpp:165
void statsThread(atomic< bool > &failed)
TEST(MallocWrappers, statsMainThread)
#define ROS_ERROR_STREAM(args)
int main(int argc, char **argv)


rosrt
Author(s): Josh Faust
autogenerated on Fri Apr 5 2019 02:16:39