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,150 @@ 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
+ ASSERT_NE (ptr_63, nullptr );
62
+ ASSERT_NE (ptr_64, nullptr );
68
63
69
- ::free (ptr);
70
- }
64
+ // verify alignment
65
+ ASSERT_EQ ((int )(IS_ALIGNED ((uintptr_t )ptr_63, ALIGN_1024)), 1 );
66
+ ASSERT_EQ ((int )(IS_ALIGNED ((uintptr_t )ptr_64, ALIGN_1024)), 1 );
67
+
68
+ // umfPoolByPtr(ptr_size_63) == nullptr
69
+ ASSERT_EQ (umfPoolByPtr (ptr_63), nullptr );
70
+ // umfPoolByPtr(ptr_size_64) != nullptr
71
+ ASSERT_NE (umfPoolByPtr (ptr_64), nullptr );
71
72
72
- TEST_F (test, proxyLib_aligned_alloc) {
73
73
#ifdef _WIN32
74
- void *ptr = _aligned_malloc (SIZE_64, ALIGN_1024);
74
+ _aligned_free (ptr_63);
75
+ _aligned_free (ptr_64);
75
76
#else
76
- void *ptr = ::aligned_alloc (ALIGN_1024, SIZE_64);
77
+ ::free (ptr_63);
78
+ ::free (ptr_64);
77
79
#endif
80
+ }
81
+
82
+ TEST_F (test, proxyLib_size_threshold_malloc) {
83
+ void *ptr_63 = malloc (SIZE_63);
84
+ void *ptr_64 = malloc (SIZE_64);
85
+
86
+ ASSERT_NE (ptr_63, nullptr );
87
+ ASSERT_NE (ptr_64, nullptr );
88
+
89
+ // umfPoolByPtr(ptr_size_63) == nullptr
90
+ ASSERT_EQ (umfPoolByPtr (ptr_63), nullptr );
91
+ // umfPoolByPtr(ptr_size_64) != nullptr
92
+ ASSERT_NE (umfPoolByPtr (ptr_64), nullptr );
93
+
94
+ ::free (ptr_63);
95
+ ::free (ptr_64);
96
+ }
97
+
98
+ TEST_F (test, proxyLib_size_threshold_calloc) {
99
+ void *ptr_63 = calloc (SIZE_63, 1 );
100
+ void *ptr_64 = calloc (SIZE_64, 1 );
101
+
102
+ ASSERT_NE (ptr_63, nullptr );
103
+ ASSERT_NE (ptr_64, nullptr );
104
+
105
+ // umfPoolByPtr(ptr_size_63) == nullptr
106
+ ASSERT_EQ (umfPoolByPtr (ptr_63), nullptr );
107
+ // umfPoolByPtr(ptr_size_64) != nullptr
108
+ ASSERT_NE (umfPoolByPtr (ptr_64), nullptr );
109
+
110
+ ::free (ptr_63);
111
+ ::free (ptr_64);
112
+ }
78
113
79
- ASSERT_EQ ((int )(IS_ALIGNED ((uintptr_t )ptr, ALIGN_1024)), 1 );
114
+ TEST_F (test, proxyLib_size_threshold_realloc_up) {
115
+ void *ptr_63 = malloc (SIZE_63);
116
+ void *ptr_64 = malloc (SIZE_64);
117
+
118
+ ASSERT_NE (ptr_63, nullptr );
119
+ ASSERT_NE (ptr_64, nullptr );
120
+
121
+ ptr_63 = realloc (ptr_63, 2 * SIZE_63);
122
+ ptr_64 = realloc (ptr_64, 2 * SIZE_64);
123
+
124
+ ASSERT_NE (ptr_63, nullptr );
125
+ ASSERT_NE (ptr_64, nullptr );
126
+
127
+ // umfPoolByPtr(ptr_size_63) == nullptr
128
+ ASSERT_EQ (umfPoolByPtr (ptr_63), nullptr );
129
+ // umfPoolByPtr(ptr_size_64) != nullptr
130
+ ASSERT_NE (umfPoolByPtr (ptr_64), nullptr );
131
+
132
+ ::free (ptr_63);
133
+ ::free (ptr_64);
134
+ }
135
+
136
+ TEST_F (test, proxyLib_size_threshold_realloc_down) {
137
+ void *ptr_63 = malloc (SIZE_63);
138
+ void *ptr_64 = malloc (SIZE_64);
139
+
140
+ ASSERT_NE (ptr_63, nullptr );
141
+ ASSERT_NE (ptr_64, nullptr );
142
+
143
+ ptr_63 = realloc (ptr_63, SIZE_63 / 2 );
144
+ ptr_64 = realloc (ptr_64, SIZE_64 / 2 );
145
+
146
+ ASSERT_NE (ptr_63, nullptr );
147
+ ASSERT_NE (ptr_64, nullptr );
148
+
149
+ // umfPoolByPtr(ptr_size_63) == nullptr
150
+ ASSERT_EQ (umfPoolByPtr (ptr_63), nullptr );
151
+ // umfPoolByPtr(ptr_size_64) != nullptr
152
+ ASSERT_NE (umfPoolByPtr (ptr_64), nullptr );
153
+
154
+ ::free (ptr_63);
155
+ ::free (ptr_64);
156
+ }
157
+
158
+ TEST_F (test, proxyLib_size_threshold_malloc_usable_size) {
159
+
160
+ void *ptr_63 = ::malloc (SIZE_63);
161
+ void *ptr_64 = ::malloc (SIZE_64);
162
+
163
+ ASSERT_NE (ptr_63, nullptr );
164
+ ASSERT_NE (ptr_64, nullptr );
165
+
166
+ if (ptr_63 == nullptr || ptr_64 == nullptr ) {
167
+ // Fix for the following CodeQL's warning on Windows:
168
+ // 'ptr' could be '0': this does not adhere to the specification for the function '_msize'.
169
+ return ;
170
+ }
80
171
81
172
#ifdef _WIN32
82
- _aligned_free (ptr);
173
+ size_t size_63 = _msize (ptr_63);
174
+ size_t size_64 = _msize (ptr_64);
175
+ #elif __APPLE__
176
+ size_t size_63 = ::malloc_size (ptr_63);
177
+ size_t size_64 = ::malloc_size (ptr_64);
83
178
#else
84
- ::free (ptr);
179
+ size_t size_63 = ::malloc_usable_size (ptr_63);
180
+ size_t size_64 = ::malloc_usable_size (ptr_64);
85
181
#endif
182
+
183
+ ASSERT_EQ ((int )(size_63 == 0 || size_63 >= SIZE_63), 1 );
184
+ ASSERT_EQ ((int )(size_64 == 0 || size_64 >= SIZE_64), 1 );
185
+
186
+ ::free (ptr_63);
187
+ ::free (ptr_64);
86
188
}
0 commit comments