xref: /xnu-8796.141.3/tests/intrusive_shared_ptr_src/test_policy.h (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #ifndef TESTS_INTRUSIVE_SHARED_PTR_TEST_POLICY_H
2*1b191cb5SApple OSS Distributions #define TESTS_INTRUSIVE_SHARED_PTR_TEST_POLICY_H
3*1b191cb5SApple OSS Distributions 
4*1b191cb5SApple OSS Distributions #include <libkern/c++/intrusive_shared_ptr.h>
5*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
6*1b191cb5SApple OSS Distributions 
7*1b191cb5SApple OSS Distributions struct test_policy {
8*1b191cb5SApple OSS Distributions 	static inline int retain_count = 0;
9*1b191cb5SApple OSS Distributions 
10*1b191cb5SApple OSS Distributions 	template <typename T>
11*1b191cb5SApple OSS Distributions 	static void
retaintest_policy12*1b191cb5SApple OSS Distributions 	retain(T&)
13*1b191cb5SApple OSS Distributions 	{
14*1b191cb5SApple OSS Distributions 		++retain_count;
15*1b191cb5SApple OSS Distributions 	}
16*1b191cb5SApple OSS Distributions 	template <typename T>
17*1b191cb5SApple OSS Distributions 	static void
releasetest_policy18*1b191cb5SApple OSS Distributions 	release(T&)
19*1b191cb5SApple OSS Distributions 	{
20*1b191cb5SApple OSS Distributions 		--retain_count;
21*1b191cb5SApple OSS Distributions 	}
22*1b191cb5SApple OSS Distributions };
23*1b191cb5SApple OSS Distributions 
24*1b191cb5SApple OSS Distributions struct tracking_policy {
25*1b191cb5SApple OSS Distributions 	static inline int retains = 0;
26*1b191cb5SApple OSS Distributions 	static inline int releases = 0;
27*1b191cb5SApple OSS Distributions 	static inline int refcount = 0;
28*1b191cb5SApple OSS Distributions 	static inline bool hit_zero = false;
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions 	static void
resettracking_policy31*1b191cb5SApple OSS Distributions 	reset()
32*1b191cb5SApple OSS Distributions 	{
33*1b191cb5SApple OSS Distributions 		retains = 0;
34*1b191cb5SApple OSS Distributions 		releases = 0;
35*1b191cb5SApple OSS Distributions 		refcount = 0;
36*1b191cb5SApple OSS Distributions 		hit_zero = false;
37*1b191cb5SApple OSS Distributions 	}
38*1b191cb5SApple OSS Distributions 
39*1b191cb5SApple OSS Distributions 	template <typename T>
40*1b191cb5SApple OSS Distributions 	static void
retaintracking_policy41*1b191cb5SApple OSS Distributions 	retain(T&)
42*1b191cb5SApple OSS Distributions 	{
43*1b191cb5SApple OSS Distributions 		++retains;
44*1b191cb5SApple OSS Distributions 		++refcount;
45*1b191cb5SApple OSS Distributions 	}
46*1b191cb5SApple OSS Distributions 	template <typename T>
47*1b191cb5SApple OSS Distributions 	static void
releasetracking_policy48*1b191cb5SApple OSS Distributions 	release(T&)
49*1b191cb5SApple OSS Distributions 	{
50*1b191cb5SApple OSS Distributions 		++releases;
51*1b191cb5SApple OSS Distributions 		--refcount;
52*1b191cb5SApple OSS Distributions 		if (refcount == 0) {
53*1b191cb5SApple OSS Distributions 			hit_zero = true;
54*1b191cb5SApple OSS Distributions 		}
55*1b191cb5SApple OSS Distributions 	}
56*1b191cb5SApple OSS Distributions };
57*1b191cb5SApple OSS Distributions 
58*1b191cb5SApple OSS Distributions template <int>
59*1b191cb5SApple OSS Distributions struct dummy_policy {
60*1b191cb5SApple OSS Distributions 	template <typename T>
61*1b191cb5SApple OSS Distributions 	static void
retaindummy_policy62*1b191cb5SApple OSS Distributions 	retain(T&)
63*1b191cb5SApple OSS Distributions 	{
64*1b191cb5SApple OSS Distributions 	}
65*1b191cb5SApple OSS Distributions 	template <typename T>
66*1b191cb5SApple OSS Distributions 	static void
releasedummy_policy67*1b191cb5SApple OSS Distributions 	release(T&)
68*1b191cb5SApple OSS Distributions 	{
69*1b191cb5SApple OSS Distributions 	}
70*1b191cb5SApple OSS Distributions };
71*1b191cb5SApple OSS Distributions 
72*1b191cb5SApple OSS Distributions template <typename T>
73*1b191cb5SApple OSS Distributions using tracked_shared_ptr = libkern::intrusive_shared_ptr<T, tracking_policy>;
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions template <typename T>
76*1b191cb5SApple OSS Distributions using test_shared_ptr = libkern::intrusive_shared_ptr<T, test_policy>;
77*1b191cb5SApple OSS Distributions 
78*1b191cb5SApple OSS Distributions #define CHECK(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
79*1b191cb5SApple OSS Distributions 
80*1b191cb5SApple OSS Distributions #endif // !TESTS_INTRUSIVE_SHARED_PTR_TEST_POLICY_H
81