| 30 #include "util_allocator.h" |
30 #include "util_allocator.h" |
| 31 #include <errno.h> |
31 #include <errno.h> |
| 32 |
32 |
| 33 #include "cx/buffer.h" |
33 #include "cx/buffer.h" |
| 34 |
34 |
| 35 #ifdef _WIN32 |
|
| 36 #include <Windows.h> |
|
| 37 #include <sysinfoapi.h> |
|
| 38 static unsigned long system_page_size(void) { |
|
| 39 static unsigned long ps = 0; |
|
| 40 if (ps == 0) { |
|
| 41 SYSTEM_INFO sysinfo; |
|
| 42 GetSystemInfo(&sysinfo); |
|
| 43 ps = sysinfo.dwPageSize; |
|
| 44 } |
|
| 45 return ps; |
|
| 46 } |
|
| 47 #else |
|
| 48 #include <unistd.h> |
|
| 49 static unsigned long system_page_size(void) { |
|
| 50 static unsigned long ps = 0; |
|
| 51 if (ps == 0) { |
|
| 52 long sc = sysconf(_SC_PAGESIZE); |
|
| 53 if (sc < 0) { |
|
| 54 // fallback for systems which do not report a value here |
|
| 55 ps = 4096; // LCOV_EXCL_LINE |
|
| 56 } else { |
|
| 57 ps = (unsigned long) sc; |
|
| 58 } |
|
| 59 } |
|
| 60 return ps; |
|
| 61 } |
|
| 62 #endif |
|
| 63 |
|
| 64 CX_TEST(test_buffer_init_wrap_space) { |
35 CX_TEST(test_buffer_init_wrap_space) { |
| 65 CxTestingAllocator talloc; |
36 CxTestingAllocator talloc; |
| 66 cx_testing_allocator_init(&talloc); |
37 cx_testing_allocator_init(&talloc); |
| 67 CxAllocator *alloc = &talloc.base; |
38 CxAllocator *alloc = &talloc.base; |
| 68 CX_TEST_DO { |
39 CX_TEST_DO { |
| 232 // below page size, new capacity should be power of two |
203 // below page size, new capacity should be power of two |
| 233 cxBufferMinimumCapacity(&buf, 200); |
204 cxBufferMinimumCapacity(&buf, 200); |
| 234 CX_TEST_ASSERT(buf.capacity == 256); |
205 CX_TEST_ASSERT(buf.capacity == 256); |
| 235 |
206 |
| 236 // greater than page size, new capacity should be multiple of pagesize |
207 // greater than page size, new capacity should be multiple of pagesize |
| 237 cxBufferMinimumCapacity(&buf, system_page_size() + 200); |
208 cxBufferMinimumCapacity(&buf, cx_system_page_size() + 200); |
| 238 CX_TEST_ASSERT(buf.capacity == system_page_size()*2); |
209 CX_TEST_ASSERT(buf.capacity == cx_system_page_size()*2); |
| 239 |
210 |
| 240 cxBufferDestroy(&buf); |
211 cxBufferDestroy(&buf); |
| 241 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
212 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
| 242 } |
213 } |
| 243 cx_testing_allocator_destroy(&talloc); |
214 cx_testing_allocator_destroy(&talloc); |