src/buffer.c

changeset 1501
e60860db189f
parent 1319
aa1f580f8f59
equal deleted inserted replaced
1500:d20037235c9c 1501:e60860db189f
42 GetSystemInfo(&sysinfo); 42 GetSystemInfo(&sysinfo);
43 ps = sysinfo.dwPageSize; 43 ps = sysinfo.dwPageSize;
44 } 44 }
45 return ps; 45 return ps;
46 } 46 }
47 #define SYSTEM_PAGE_SIZE system_page_size()
48 #else 47 #else
49 #include <unistd.h> 48 #include <unistd.h>
50 #define SYSTEM_PAGE_SIZE sysconf(_SC_PAGESIZE) 49 static unsigned long system_page_size() {
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 }
51 #endif 62 #endif
52 63
53 static int buffer_copy_on_write(CxBuffer* buffer) { 64 static int buffer_copy_on_write(CxBuffer* buffer) {
54 if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0; 65 if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0;
55 void *newspace = cxMalloc(buffer->allocator, buffer->capacity); 66 void *newspace = cxMalloc(buffer->allocator, buffer->capacity);
56 if (NULL == newspace) return -1; 67 if (NULL == newspace) return -1; // LCOV_EXCL_LINE
57 memcpy(newspace, buffer->space, buffer->size); 68 memcpy(newspace, buffer->space, buffer->size);
58 buffer->space = newspace; 69 buffer->space = newspace;
59 buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE; 70 buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE;
60 buffer->flags |= CX_BUFFER_FREE_CONTENTS; 71 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
61 return 0; 72 return 0;
76 } 87 }
77 buffer->allocator = allocator; 88 buffer->allocator = allocator;
78 buffer->flags = flags; 89 buffer->flags = flags;
79 if (!space) { 90 if (!space) {
80 buffer->bytes = cxMalloc(allocator, capacity); 91 buffer->bytes = cxMalloc(allocator, capacity);
81 if (buffer->bytes == NULL) { 92 if (buffer->bytes == NULL) return -1; // LCOV_EXCL_LINE
82 return -1; // LCOV_EXCL_LINE
83 }
84 buffer->flags |= CX_BUFFER_FREE_CONTENTS; 93 buffer->flags |= CX_BUFFER_FREE_CONTENTS;
85 } else { 94 } else {
86 buffer->bytes = space; 95 buffer->bytes = space;
87 } 96 }
88 buffer->capacity = capacity; 97 buffer->capacity = capacity;
120 ) { 129 ) {
121 if (allocator == NULL) { 130 if (allocator == NULL) {
122 allocator = cxDefaultAllocator; 131 allocator = cxDefaultAllocator;
123 } 132 }
124 CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); 133 CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer));
125 if (buf == NULL) return NULL; 134 if (buf == NULL) return NULL; // LCOV_EXCL_LINE
126 if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { 135 if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) {
127 return buf; 136 return buf;
128 } else { 137 } else {
129 // LCOV_EXCL_START 138 // LCOV_EXCL_START
130 cxFree(allocator, buf); 139 cxFree(allocator, buf);
206 ) { 215 ) {
207 if (newcap <= buffer->capacity) { 216 if (newcap <= buffer->capacity) {
208 return 0; 217 return 0;
209 } 218 }
210 219
211 unsigned long pagesize = SYSTEM_PAGE_SIZE; 220 unsigned long pagesize = system_page_size();
212 // if page size is larger than 64 KB - for some reason - truncate to 64 KB 221 // if page size is larger than 64 KB - for some reason - truncate to 64 KB
213 if (pagesize > 65536) pagesize = 65536; 222 if (pagesize > 65536) pagesize = 65536;
214 if (newcap < pagesize) { 223 if (newcap < pagesize) {
215 // when smaller as one page, map to the next power of two 224 // when smaller as one page, map to the next power of two
216 newcap--; 225 newcap--;

mercurial