19
19
20
20
using umf_test::test;
21
21
22
+ #define SIZE_63 63
22
23
#define SIZE_64 64
23
24
#define ALIGN_1024 1024
24
25
25
26
TEST_F (test, proxyLib_basic) {
26
-
27
- ::free (::malloc(SIZE_64));
28
-
29
27
// a check to verify we are running the proxy library
30
28
void *ptr = (void *)0x01 ;
31
29
@@ -41,46 +39,129 @@ TEST_F(test, proxyLib_basic) {
41
39
}
42
40
43
41
TEST_F (test, proxyLib_realloc_size0) {
44
- // realloc(ptr, 0) == free (ptr)
42
+ // realloc(ptr, 0) == free(ptr)
45
43
// realloc(ptr, 0) returns NULL
46
44
ASSERT_EQ (::realloc (::malloc (SIZE_64), 0 ), nullptr );
47
45
}
48
46
49
- TEST_F (test, proxyLib_malloc_usable_size) {
50
-
51
- void *ptr = ::malloc (SIZE_64);
52
- ASSERT_NE (ptr, nullptr );
53
- if (ptr == nullptr ) {
54
- // Fix for the following CodeQL's warning on Windows:
55
- // 'ptr' could be '0': this does not adhere to the specification for the function '_msize'.
56
- return ;
57
- }
47
+ // The proxyLib_size_threshold_* tests test the size threshold of the proxy library.
48
+ // The size threshold is set to 64 bytes in this test, so all allocations of:
49
+ // 1) size < 64 go through the default system allocator (umfPoolByPtr(ptr_size < 64) == nullptr)
50
+ // 2) size >= 64 go through the proxy lib allocator (umfPoolByPtr(ptr_size >= 64) != nullptr)
58
51
52
+ TEST_F (test, proxyLib_size_threshold_aligned_alloc) {
59
53
#ifdef _WIN32
60
- size_t size = _msize (ptr);
61
- #elif __APPLE__
62
- size_t size = ::malloc_size (ptr);
54
+ void *ptr_63 = _aligned_malloc (SIZE_63, ALIGN_1024);
55
+ void *ptr_64 = _aligned_malloc (SIZE_64, ALIGN_1024);
63
56
#else
64
- size_t size = ::malloc_usable_size (ptr);
57
+ void *ptr_63 = ::aligned_alloc (ALIGN_1024, SIZE_63);
58
+ void *ptr_64 = ::aligned_alloc (ALIGN_1024, SIZE_64);
65
59
#endif
66
60
67
- ASSERT_EQ ((int )(size == 0 || size >= SIZE_64), 1 );
61
+ // verify alignment
62
+ ASSERT_EQ ((int )(IS_ALIGNED ((uintptr_t )ptr_63, ALIGN_1024)), 1 );
63
+ ASSERT_EQ ((int )(IS_ALIGNED ((uintptr_t )ptr_64, ALIGN_1024)), 1 );
68
64
69
- ::free (ptr);
70
- }
65
+ // umfPoolByPtr(ptr_size_63) == nullptr
66
+ ASSERT_EQ (umfPoolByPtr (ptr_63), nullptr );
67
+ // umfPoolByPtr(ptr_size_64) != nullptr
68
+ ASSERT_NE (umfPoolByPtr (ptr_64), nullptr );
71
69
72
- TEST_F (test, proxyLib_aligned_alloc) {
73
70
#ifdef _WIN32
74
- void *ptr = _aligned_malloc (SIZE_64, ALIGN_1024);
71
+ _aligned_free (ptr_63);
72
+ _aligned_free (ptr_64);
75
73
#else
76
- void *ptr = ::aligned_alloc (ALIGN_1024, SIZE_64);
74
+ ::free (ptr_63);
75
+ ::free (ptr_64);
77
76
#endif
77
+ }
78
+
79
+ TEST_F (test, proxyLib_size_threshold_malloc) {
80
+ void *ptr_63 = malloc (SIZE_63);
81
+ void *ptr_64 = malloc (SIZE_64);
82
+
83
+ // umfPoolByPtr(ptr_size_63) == nullptr
84
+ ASSERT_EQ (umfPoolByPtr (ptr_63), nullptr );
85
+ // umfPoolByPtr(ptr_size_64) != nullptr
86
+ ASSERT_NE (umfPoolByPtr (ptr_64), nullptr );
87
+
88
+ ::free (ptr_63);
89
+ ::free (ptr_64);
90
+ }
91
+
92
+ TEST_F (test, proxyLib_size_threshold_calloc) {
93
+ void *ptr_63 = calloc (SIZE_63, 1 );
94
+ void *ptr_64 = calloc (SIZE_64, 1 );
95
+
96
+ // umfPoolByPtr(ptr_size_63) == nullptr
97
+ ASSERT_EQ (umfPoolByPtr (ptr_63), nullptr );
98
+ // umfPoolByPtr(ptr_size_64) != nullptr
99
+ ASSERT_NE (umfPoolByPtr (ptr_64), nullptr );
100
+
101
+ ::free (ptr_63);
102
+ ::free (ptr_64);
103
+ }
78
104
79
- ASSERT_EQ ((int )(IS_ALIGNED ((uintptr_t )ptr, ALIGN_1024)), 1 );
105
+ TEST_F (test, proxyLib_size_threshold_realloc_up) {
106
+ void *ptr_63 = malloc (SIZE_63);
107
+ void *ptr_64 = malloc (SIZE_64);
108
+
109
+ ptr_63 = realloc (ptr_63, 2 * SIZE_63);
110
+ ptr_64 = realloc (ptr_64, 2 * SIZE_64);
111
+
112
+ // umfPoolByPtr(ptr_size_63) == nullptr
113
+ ASSERT_EQ (umfPoolByPtr (ptr_63), nullptr );
114
+ // umfPoolByPtr(ptr_size_64) != nullptr
115
+ ASSERT_NE (umfPoolByPtr (ptr_64), nullptr );
116
+
117
+ ::free (ptr_63);
118
+ ::free (ptr_64);
119
+ }
120
+
121
+ TEST_F (test, proxyLib_size_threshold_realloc_down) {
122
+ void *ptr_63 = malloc (SIZE_63);
123
+ void *ptr_64 = malloc (SIZE_64);
124
+
125
+ ptr_63 = realloc (ptr_63, SIZE_63 / 2 );
126
+ ptr_64 = realloc (ptr_64, SIZE_64 / 2 );
127
+
128
+ // umfPoolByPtr(ptr_size_63) == nullptr
129
+ ASSERT_EQ (umfPoolByPtr (ptr_63), nullptr );
130
+ // umfPoolByPtr(ptr_size_64) != nullptr
131
+ ASSERT_NE (umfPoolByPtr (ptr_64), nullptr );
132
+
133
+ ::free (ptr_63);
134
+ ::free (ptr_64);
135
+ }
136
+
137
+ TEST_F (test, proxyLib_size_threshold_malloc_usable_size) {
138
+
139
+ void *ptr_63 = ::malloc (SIZE_63);
140
+ void *ptr_64 = ::malloc (SIZE_64);
141
+
142
+ ASSERT_NE (ptr_63, nullptr );
143
+ ASSERT_NE (ptr_64, nullptr );
144
+
145
+ if (ptr_63 == nullptr || ptr_64 == nullptr ) {
146
+ // Fix for the following CodeQL's warning on Windows:
147
+ // 'ptr' could be '0': this does not adhere to the specification for the function '_msize'.
148
+ return ;
149
+ }
80
150
81
151
#ifdef _WIN32
82
- _aligned_free (ptr);
152
+ size_t size_63 = _msize (ptr_63);
153
+ size_t size_64 = _msize (ptr_64);
154
+ #elif __APPLE__
155
+ size_t size_63 = ::malloc_size (ptr_63);
156
+ size_t size_64 = ::malloc_size (ptr_64);
83
157
#else
84
- ::free (ptr);
158
+ size_t size_63 = ::malloc_usable_size (ptr_63);
159
+ size_t size_64 = ::malloc_usable_size (ptr_64);
85
160
#endif
161
+
162
+ ASSERT_EQ ((int )(size_63 == 0 || size_63 >= SIZE_63), 1 );
163
+ ASSERT_EQ ((int )(size_64 == 0 || size_64 >= SIZE_64), 1 );
164
+
165
+ ::free (ptr_63);
166
+ ::free (ptr_64);
86
167
}
0 commit comments