src/buffer.c

changeset 1539
2cbdb482d325
parent 1520
03d703c3cfe9
child 1542
197450c2b0b3
equal deleted inserted replaced
1538:fd948e5af26e 1539:2cbdb482d325
30 30
31 #include <stdio.h> 31 #include <stdio.h>
32 #include <string.h> 32 #include <string.h>
33 #include <errno.h> 33 #include <errno.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 static int buffer_copy_on_write(CxBuffer* buffer) { 35 static int buffer_copy_on_write(CxBuffer* buffer) {
65 if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0; 36 if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0;
66 void *newspace = cxMalloc(buffer->allocator, buffer->capacity); 37 void *newspace = cxMalloc(buffer->allocator, buffer->capacity);
67 if (NULL == newspace) return -1; // LCOV_EXCL_LINE 38 if (NULL == newspace) return -1; // LCOV_EXCL_LINE
68 memcpy(newspace, buffer->space, buffer->size); 39 memcpy(newspace, buffer->space, buffer->size);
260 return -1; // LCOV_EXCL_LINE 231 return -1; // LCOV_EXCL_LINE
261 } 232 }
262 } 233 }
263 234
264 static size_t cx_buffer_calculate_minimum_capacity(size_t mincap) { 235 static size_t cx_buffer_calculate_minimum_capacity(size_t mincap) {
265 unsigned long pagesize = system_page_size(); 236 unsigned long pagesize = cx_system_page_size();
266 // if page size is larger than 64 KB - for some reason - truncate to 64 KB 237 // if page size is larger than 64 KB - for some reason - truncate to 64 KB
267 if (pagesize > 65536) pagesize = 65536; 238 if (pagesize > 65536) pagesize = 65536;
268 if (mincap < pagesize) { 239 if (mincap < pagesize) {
269 // when smaller as one page, map to the next power of two 240 // when smaller as one page, map to the next power of two
270 mincap--; 241 mincap--;

mercurial