1919
2020using umf_test::test;
2121
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+
2327#define ALIGN_1024 1024
2428
2529TEST_F (test, proxyLib_basic) {
26-
27- ::free (::malloc(SIZE_64));
28-
2930 // a check to verify we are running the proxy library
3031 void *ptr = (void *)0x01 ;
3132
@@ -41,46 +42,162 @@ TEST_F(test, proxyLib_basic) {
4142}
4243
4344TEST_F (test, proxyLib_realloc_size0) {
44- // realloc(ptr, 0) == free (ptr)
45+ // realloc(ptr, 0) == free(ptr)
4546 // realloc(ptr, 0) returns NULL
46- ASSERT_EQ (::realloc (::malloc (SIZE_64 ), 0 ), nullptr );
47+ ASSERT_EQ (::realloc (::malloc (SIZE_EQ ), 0 ), nullptr );
4748}
4849
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)
5856
57+ TEST_F (test, proxyLib_size_threshold_aligned_alloc) {
5958#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);
6361#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);
6564#endif
6665
67- ASSERT_EQ ((int )(size == 0 || size >= SIZE_64), 1 );
66+ ASSERT_NE (ptr_LT, nullptr );
67+ ASSERT_NE (ptr_EQ, nullptr );
6868
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+ #ifndef _WIN32 /* the size threshold works only on Linux for now */
74+ // umfPoolByPtr(ptr_size_LT) == nullptr
75+ ASSERT_EQ (umfPoolByPtr (ptr_LT), nullptr );
76+ #endif
77+ // umfPoolByPtr(ptr_size_EQ) != nullptr
78+ ASSERT_NE (umfPoolByPtr (ptr_EQ), nullptr );
7179
72- TEST_F (test, proxyLib_aligned_alloc) {
7380#ifdef _WIN32
74- void *ptr = _aligned_malloc (SIZE_64, ALIGN_1024);
81+ _aligned_free (ptr_LT);
82+ _aligned_free (ptr_EQ);
7583#else
76- void *ptr = ::aligned_alloc (ALIGN_1024, SIZE_64);
84+ ::free (ptr_LT);
85+ ::free (ptr_EQ);
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+ #ifndef _WIN32 /* the size threshold works only on Linux for now */
97+ // umfPoolByPtr(ptr_size_LT) == nullptr
98+ ASSERT_EQ (umfPoolByPtr (ptr_LT), nullptr );
99+ #endif
100+ // umfPoolByPtr(ptr_size_EQ) != nullptr
101+ ASSERT_NE (umfPoolByPtr (ptr_EQ), nullptr );
102+
103+ ::free (ptr_LT);
104+ ::free (ptr_EQ);
105+ }
106+
107+ TEST_F (test, proxyLib_size_threshold_calloc) {
108+ void *ptr_LT = calloc (SIZE_LT, 1 );
109+ void *ptr_EQ = calloc (SIZE_EQ, 1 );
110+
111+ ASSERT_NE (ptr_LT, nullptr );
112+ ASSERT_NE (ptr_EQ, nullptr );
113+
114+ #ifndef _WIN32 /* the size threshold works only on Linux for now */
115+ // umfPoolByPtr(ptr_size_LT) == nullptr
116+ ASSERT_EQ (umfPoolByPtr (ptr_LT), nullptr );
117+ #endif
118+ // umfPoolByPtr(ptr_size_EQ) != nullptr
119+ ASSERT_NE (umfPoolByPtr (ptr_EQ), nullptr );
120+
121+ ::free (ptr_LT);
122+ ::free (ptr_EQ);
123+ }
124+
125+ TEST_F (test, proxyLib_size_threshold_realloc_up) {
126+ void *ptr_LT = malloc (SIZE_LT);
127+ void *ptr_EQ = malloc (SIZE_EQ);
128+
129+ ASSERT_NE (ptr_LT, nullptr );
130+ ASSERT_NE (ptr_EQ, nullptr );
131+
132+ void *ptr_LT_r = realloc (ptr_LT, 2 * SIZE_LT);
133+ void *ptr_EQ_r = realloc (ptr_EQ, 2 * SIZE_EQ);
134+
135+ ASSERT_NE (ptr_LT_r, nullptr );
136+ ASSERT_NE (ptr_EQ_r, nullptr );
137+
138+ #ifndef _WIN32 /* the size threshold works only on Linux for now */
139+ // umfPoolByPtr(ptr_size_LT) == nullptr
140+ ASSERT_EQ (umfPoolByPtr (ptr_LT_r), nullptr );
77141#endif
142+ // umfPoolByPtr(ptr_size_EQ) != nullptr
143+ ASSERT_NE (umfPoolByPtr (ptr_EQ_r), nullptr );
78144
79- ASSERT_EQ ((int )(IS_ALIGNED ((uintptr_t )ptr, ALIGN_1024)), 1 );
145+ ::free (ptr_LT_r);
146+ ::free (ptr_EQ_r);
147+ }
148+
149+ TEST_F (test, proxyLib_size_threshold_realloc_down) {
150+ void *ptr_LT = malloc (SIZE_LT);
151+ void *ptr_EQ = malloc (SIZE_EQ);
152+
153+ ASSERT_NE (ptr_LT, nullptr );
154+ ASSERT_NE (ptr_EQ, nullptr );
155+
156+ void *ptr_LT_r = realloc (ptr_LT, SIZE_LT / 2 );
157+ void *ptr_EQ_r = realloc (ptr_EQ, SIZE_EQ / 2 );
158+
159+ ASSERT_NE (ptr_LT_r, nullptr );
160+ ASSERT_NE (ptr_EQ_r, nullptr );
161+
162+ #ifndef _WIN32 /* the size threshold works only on Linux for now */
163+ // umfPoolByPtr(ptr_size_LT) == nullptr
164+ ASSERT_EQ (umfPoolByPtr (ptr_LT_r), nullptr );
165+ #endif
166+ // umfPoolByPtr(ptr_size_EQ) != nullptr
167+ ASSERT_NE (umfPoolByPtr (ptr_EQ_r), nullptr );
168+
169+ ::free (ptr_LT_r);
170+ ::free (ptr_EQ_r);
171+ }
172+
173+ TEST_F (test, proxyLib_size_threshold_malloc_usable_size) {
174+
175+ void *ptr_LT = ::malloc (SIZE_LT);
176+ void *ptr_EQ = ::malloc (SIZE_EQ);
177+
178+ ASSERT_NE (ptr_LT, nullptr );
179+ ASSERT_NE (ptr_EQ, nullptr );
180+
181+ if (ptr_LT == nullptr || ptr_EQ == nullptr ) {
182+ // Fix for the following CodeQL's warning on Windows:
183+ // 'ptr' could be '0': this does not adhere to the specification for the function '_msize'.
184+ return ;
185+ }
80186
81187#ifdef _WIN32
82- _aligned_free (ptr);
188+ size_t size_LT = _msize (ptr_LT);
189+ size_t size_EQ = _msize (ptr_EQ);
190+ #elif __APPLE__
191+ size_t size_LT = ::malloc_size (ptr_LT);
192+ size_t size_EQ = ::malloc_size (ptr_EQ);
83193#else
84- ::free (ptr);
194+ size_t size_LT = ::malloc_usable_size (ptr_LT);
195+ size_t size_EQ = ::malloc_usable_size (ptr_EQ);
85196#endif
197+
198+ ASSERT_EQ ((int )(size_LT == 0 || size_LT >= SIZE_LT), 1 );
199+ ASSERT_EQ ((int )(size_EQ == 0 || size_EQ >= SIZE_EQ), 1 );
200+
201+ ::free (ptr_LT);
202+ ::free (ptr_EQ);
86203}
0 commit comments