Coda Distributed File System
memory.h
Go to the documentation of this file.
1/* BLURB gpl
2
3 Coda File System
4 Release 7
5
6 Copyright (c) 2019 Carnegie Mellon University
7 Additional copyrights listed below
8
9This code is distributed "AS IS" without warranty of any kind under
10the terms of the GNU General Public Licence Version 2, as shown in the
11file LICENSE. The technical and financial contributors to Coda are
12listed in the file CREDITS.
13
14 Additional copyrights
15 none currently
16
17#*/
18
19#ifndef _CODA_MEMORY_H_
20#define _CODA_MEMORY_H_ 1
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#include <valgrind/memcheck.h>
27
28#ifdef __cplusplus
29}
30#endif
31
32#include "gtest/gtest.h"
33
34#define PRINT_FAILURE_MESSAGE \
35 const ::testing::TestInfo *const test_info = \
36 ::testing::UnitTest::GetInstance()->current_test_info(); \
37 const ::testing::TestResult *res = test_info->result(); \
38 for (int i = 0; i < res->total_part_count(); i++) { \
39 const testing::TestPartResult part_res = res->GetTestPartResult(i); \
40 std::cout << part_res.file_name() << ":" << part_res.line_number() \
41 << " Failure" << std::endl; \
42 std::cout << part_res.summary() << std::endl; \
43 }
44
45#define TEST_FOR_MEMORY_LEAKS_SETUP \
46 unsigned long base_definite, base_dubious, base_reachable, \
47 base_suppressed; \
48 unsigned long leaked, dubious, reachable, suppressed; \
49 VALGRIND_DO_LEAK_CHECK; \
50 VALGRIND_COUNT_LEAKS(base_definite, base_dubious, base_reachable, \
51 base_suppressed);
52
53#define TEST_FOR_MEMORY_LEAKS_CHECK \
54 VALGRIND_DO_LEAK_CHECK; \
55 VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed); \
56 EXPECT_EQ(base_definite, leaked) << "Memory Leak found"; \
57 EXPECT_EQ(base_dubious, dubious) << "Memory Leak found"; \
58 EXPECT_EQ(base_reachable, reachable) << "Memory Leak found"; \
59 EXPECT_EQ(base_suppressed, suppressed) << "Memory Leak found";
60
61#define RVM_LAUNCH_IN_INSTANCE(function) \
62 ASSERT_EXIT(({ \
63 TEST_FOR_MEMORY_LEAKS_SETUP \
64 function(); \
65 VALGRIND_DO_LEAK_CHECK; \
66 VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, \
67 suppressed); \
68 TEST_FOR_MEMORY_LEAKS_CHECK \
69 ::testing::Test::TearDownTestCase(); \
70 if (::testing::Test::HasFailure()) { \
71 PRINT_FAILURE_MESSAGE \
72 } \
73 exit(::testing::Test::HasFailure() ? 1 : 0); \
74 }), \
75 ::testing::ExitedWithCode(0), ".*");
76
77/* Google test wrapper to support memory leaks testing for forked
78 * processes. Tests that require RVM should fork/clone since RVM
79 * is NOT reentrant. */
80#define RVM_TEST(test_case_name, test) \
81 static void test_case_name##_##test##_RvmTest(); \
82 TEST(test_case_name, test) \
83 { \
84 RVM_LAUNCH_IN_INSTANCE(test_case_name##_##test##_RvmTest) \
85 } \
86 static void test_case_name##_##test##_RvmTest()
87
88#define RVM_TEST_F(test_case_name, test) \
89 static void test_case_name##_##test##_RvmTest(); \
90 TEST_F(test_case_name, test) \
91 { \
92 RVM_LAUNCH_IN_INSTANCE(test_case_name##_##test##_RvmTest) \
93 } \
94 static void test_case_name##_##test##_RvmTest()
95
96#endif /* _CODA_MEMORY_H_ */