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