+static int loc_stringpool_deallocate(struct loc_stringpool* pool) {
+ if (pool->data) {
+ int r = munmap(pool->data, pool->max_length);
+ if (r) {
+ ERROR(pool->ctx, "Could not unmap data at %p: %s\n",
+ pool->data, strerror(errno));
+
+ return r;
+ }
+ }
+
+ return 0;
+}
+
+static int loc_stringpool_allocate(struct loc_stringpool* pool, size_t length) {
+ // Drop old data
+ int r = loc_stringpool_deallocate(pool);
+ if (r)
+ return r;
+
+ pool->max_length = length;
+
+ // Align to page size
+ while (pool->max_length % sysconf(_SC_PAGE_SIZE) > 0)
+ pool->max_length++;
+
+ DEBUG(pool->ctx, "Allocating pool of %zu bytes\n", pool->max_length);
+
+ // Allocate some memory
+ pool->data = pool->pos = mmap(NULL, pool->max_length,
+ PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+
+ if (pool->data == MAP_FAILED) {
+ DEBUG(pool->ctx, "%s\n", strerror(errno));
+ return -errno;
+ }
+
+ DEBUG(pool->ctx, "Allocated pool at %p\n", pool->data);
+
+ return 0;
+}