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