27 #include <squash/config.h>
29 #if defined(HAVE_MMAP)
30 # define _POSIX_SOURCE
33 # if defined(HAVE_MREMAP)
34 # include <sys/types.h>
35 # include <sys/mman.h>
48 squash_buffer_npot_page (
size_t value) {
49 #if defined(_POSIX_SOURCE)
50 static size_t page_size = 0;
53 long ps = sysconf (_SC_PAGE_SIZE);
54 page_size = (ps == -1) ? 8192 : ((
size_t) ps);
57 if (value < page_size)
64 if ((value & (value - 1)) != 0) {
78 squash_buffer_ensure_allocation (SquashBuffer* buffer,
size_t allocation) {
79 if (allocation > buffer->allocated) {
80 allocation = squash_buffer_npot_page (allocation);
81 buffer->allocated = allocation;
82 buffer->data = (uint8_t*) realloc (buffer->data, allocation);
94 squash_buffer_new (
size_t preallocated_len) {
95 SquashBuffer* buffer = (SquashBuffer*) malloc (
sizeof (SquashBuffer));
97 buffer->data = preallocated_len > 0 ? (uint8_t*) malloc (preallocated_len) : NULL;
99 buffer->allocated = preallocated_len;
105 squash_buffer_set_size (SquashBuffer* buffer,
size_t length) {
106 if (length > buffer->allocated)
107 squash_buffer_ensure_allocation (buffer, length);
109 buffer->length = length;
113 squash_buffer_clear (SquashBuffer* buffer) {
116 buffer->allocated = 0;
121 squash_buffer_append (SquashBuffer* buffer, uint8_t* data,
size_t data_length) {
122 const size_t start_pos = buffer->length;
124 squash_buffer_set_size (buffer, buffer->length + data_length);
126 memcpy (buffer->data + start_pos, data, data_length);
130 squash_buffer_free (SquashBuffer* buffer) {
131 if (buffer->data != NULL) {