27 */ |
27 */ |
28 |
28 |
29 #include "cx/buffer.h" |
29 #include "cx/buffer.h" |
30 |
30 |
31 #include <stdio.h> |
31 #include <stdio.h> |
32 #include <unistd.h> |
|
33 #include <string.h> |
32 #include <string.h> |
34 #include <errno.h> |
33 #include <errno.h> |
|
34 |
|
35 #ifdef _WIN32 |
|
36 #include <Windows.h> |
|
37 #include <sysinfoapi.h> |
|
38 static unsigned long system_page_size() { |
|
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 #define SYSTEM_PAGE_SIZE system_page_size() |
|
48 #else |
|
49 #include <unistd.h> |
|
50 #define SYSTEM_PAGE_SIZE sysconf(_SC_PAGESIZE) |
|
51 #endif |
35 |
52 |
36 static int buffer_copy_on_write(CxBuffer* buffer) { |
53 static int buffer_copy_on_write(CxBuffer* buffer) { |
37 if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0; |
54 if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0; |
38 void *newspace = cxMalloc(buffer->allocator, buffer->capacity); |
55 void *newspace = cxMalloc(buffer->allocator, buffer->capacity); |
39 if (NULL == newspace) return -1; |
56 if (NULL == newspace) return -1; |
189 ) { |
206 ) { |
190 if (newcap <= buffer->capacity) { |
207 if (newcap <= buffer->capacity) { |
191 return 0; |
208 return 0; |
192 } |
209 } |
193 |
210 |
194 unsigned long pagesize = sysconf(_SC_PAGESIZE); |
211 unsigned long pagesize = SYSTEM_PAGE_SIZE; |
195 // if page size is larger than 64 KB - for some reason - truncate to 64 KB |
212 // if page size is larger than 64 KB - for some reason - truncate to 64 KB |
196 if (pagesize > 65536) pagesize = 65536; |
213 if (pagesize > 65536) pagesize = 65536; |
197 if (newcap < pagesize) { |
214 if (newcap < pagesize) { |
198 // when smaller as one page, map to the next power of two |
215 // when smaller as one page, map to the next power of two |
199 newcap--; |
216 newcap--; |