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