#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
-#include <loc/libloc.h>
-#include "libloc-private.h"
-#include "stringpool.h"
+#include <libloc/libloc.h>
+#include <libloc/format.h>
+#include <libloc/private.h>
+#include <libloc/stringpool.h>
+
+#define LOC_STRINGPOOL_BLOCK_SIZE (512 * 1024)
struct loc_stringpool {
struct loc_ctx* ctx;
-
int refcount;
- char* data;
- char* pos;
- ssize_t max_length;
+ // Reference to any mapped data
+ const char* data;
+ ssize_t length;
+
+ // Reference to own storage
+ char* blocks;
+ size_t size;
};
-static int loc_stringpool_allocate(struct loc_stringpool* pool, size_t length) {
- // Drop old data
- if (pool->data)
- free(pool->data);
+static int loc_stringpool_grow(struct loc_stringpool* pool, const size_t size) {
+ DEBUG(pool->ctx, "Growing string pool by %zu byte(s)\n", size);
- pool->max_length = length;
- DEBUG(pool->ctx, "Allocating pool of %zu bytes\n", pool->max_length);
+ // Increment size
+ pool->size += size;
- if (pool->max_length > 0) {
- pool->data = malloc(pool->max_length);
- if (!pool->data)
- return -ENOMEM;
+ // Reallocate blocks
+ pool->blocks = realloc(pool->blocks, pool->size);
+ if (!pool->blocks) {
+ ERROR(pool->ctx, "Could not grow string pool: %m\n");
+ return 1;
}
- pool->pos = pool->data;
+ // Update data pointer
+ pool->data = pool->blocks;
return 0;
}
-LOC_EXPORT int loc_stringpool_new(struct loc_ctx* ctx, struct loc_stringpool** pool, size_t max_length) {
- struct loc_stringpool* p = calloc(1, sizeof(*p));
- if (!p)
- return -ENOMEM;
+static off_t loc_stringpool_append(struct loc_stringpool* pool, const char* string) {
+ if (!string) {
+ errno = EINVAL;
+ return -1;
+ }
- p->ctx = loc_ref(ctx);
- p->refcount = 1;
+ DEBUG(pool->ctx, "Appending '%s' to string pool at %p\n", string, pool);
+
+ // How much space to we need?
+ const size_t length = strlen(string) + 1;
- // Allocate the data section
- int r = loc_stringpool_allocate(p, max_length);
- if (r) {
- loc_stringpool_unref(p);
- return r;
+ // Make sure we have enough space
+ if (pool->length + length > pool->size) {
+ int r = loc_stringpool_grow(pool, LOC_STRINGPOOL_BLOCK_SIZE);
+ if (r)
+ return r;
}
- DEBUG(p->ctx, "String pool allocated at %p\n", p);
- DEBUG(p->ctx, " Maximum size: %zu bytes\n", p->max_length);
- *pool = p;
+ off_t offset = pool->length;
- return 0;
-}
+ // Copy the string
+ memcpy(pool->blocks + offset, string, length);
-LOC_EXPORT struct loc_stringpool* loc_stringpool_ref(struct loc_stringpool* pool) {
- pool->refcount++;
+ // Update the length of the pool
+ pool->length += length;
- return pool;
+ return offset;
}
static void loc_stringpool_free(struct loc_stringpool* pool) {
DEBUG(pool->ctx, "Releasing string pool %p\n", pool);
- loc_unref(pool->ctx);
-
- if (pool->data)
- free(pool->data);
+ // Free any data
+ if (pool->blocks)
+ free(pool->blocks);
+ loc_unref(pool->ctx);
free(pool);
}
-LOC_EXPORT struct loc_stringpool* loc_stringpool_unref(struct loc_stringpool* pool) {
- if (--pool->refcount > 0)
- return NULL;
+int loc_stringpool_new(struct loc_ctx* ctx, struct loc_stringpool** pool) {
+ struct loc_stringpool* p = calloc(1, sizeof(*p));
+ if (!p)
+ return 1;
- loc_stringpool_free(pool);
+ p->ctx = loc_ref(ctx);
+ p->refcount = 1;
- return NULL;
+ *pool = p;
+
+ return 0;
}
-static off_t loc_stringpool_get_offset(struct loc_stringpool* pool, const char* pos) {
- if (pos < pool->data)
- return -EFAULT;
+int loc_stringpool_open(struct loc_ctx* ctx, struct loc_stringpool** pool,
+ const char* data, const size_t length) {
+ struct loc_stringpool* p = NULL;
- if (pos > (pool->data + pool->max_length))
- return -EFAULT;
+ // Allocate a new stringpool
+ int r = loc_stringpool_new(ctx, &p);
+ if (r)
+ goto ERROR;
- return pos - pool->data;
-}
+ // Store data and length
+ p->data = data;
+ p->length = length;
+
+ DEBUG(p->ctx, "Opened string pool at %p (%zu bytes)\n", p->data, p->length);
+
+ *pool = p;
+ return 0;
-static off_t loc_stringpool_get_next_offset(struct loc_stringpool* pool, off_t offset) {
- const char* string = loc_stringpool_get(pool, offset);
+ERROR:
+ if (p)
+ loc_stringpool_free(p);
- return offset + strlen(string) + 1;
+ return r;
}
-static size_t loc_stringpool_space_left(struct loc_stringpool* pool) {
- return pool->max_length - loc_stringpool_get_size(pool);
+struct loc_stringpool* loc_stringpool_ref(struct loc_stringpool* pool) {
+ pool->refcount++;
+
+ return pool;
}
-LOC_EXPORT const char* loc_stringpool_get(struct loc_stringpool* pool, off_t offset) {
- if (offset >= (ssize_t)pool->max_length)
+struct loc_stringpool* loc_stringpool_unref(struct loc_stringpool* pool) {
+ if (--pool->refcount > 0)
return NULL;
- const char* string = pool->data + offset;
+ loc_stringpool_free(pool);
+
+ return NULL;
+}
- // If the string is empty, we have reached the end
- if (!*string)
+const char* loc_stringpool_get(struct loc_stringpool* pool, off_t offset) {
+ // Check boundaries
+ if (offset < 0 || offset >= pool->length) {
+ errno = ERANGE;
return NULL;
+ }
- return string;
+ // Return any data that we have in memory
+ return pool->data + offset;
}
-LOC_EXPORT size_t loc_stringpool_get_size(struct loc_stringpool* pool) {
- return loc_stringpool_get_offset(pool, pool->pos);
+size_t loc_stringpool_get_size(struct loc_stringpool* pool) {
+ return pool->length;
}
static off_t loc_stringpool_find(struct loc_stringpool* pool, const char* s) {
- if (!s || !*s)
- return -EINVAL;
+ if (!s || !*s) {
+ errno = EINVAL;
+ return -1;
+ }
off_t offset = 0;
- while (offset < pool->max_length) {
+ while (offset < pool->length) {
const char* string = loc_stringpool_get(pool, offset);
+
+ // Error!
if (!string)
- break;
+ return 1;
- int r = strcmp(s, string);
- if (r == 0)
+ // Is this a match?
+ if (strcmp(s, string) == 0)
return offset;
- offset = loc_stringpool_get_next_offset(pool, offset);
+ // Shift offset
+ offset += strlen(string) + 1;
}
- return -ENOENT;
+ // Nothing found
+ errno = ENOENT;
+ return -1;
}
-static off_t loc_stringpool_append(struct loc_stringpool* pool, const char* string) {
- if (!string || !*string)
- return -EINVAL;
-
- DEBUG(pool->ctx, "Appending '%s' to string pool at %p\n", string, pool);
-
- // Check if we have enough space left
- size_t l = strlen(string) + 1;
- if (l > loc_stringpool_space_left(pool)) {
- DEBUG(pool->ctx, "Not enough space to append '%s'\n", string);
- DEBUG(pool->ctx, " Need %zu bytes but only have %zu\n", l, loc_stringpool_space_left(pool));
- return -ENOSPC;
- }
-
- off_t offset = loc_stringpool_get_offset(pool, pool->pos);
-
- // Copy string byte by byte
- while (*string && loc_stringpool_space_left(pool) > 1) {
- *pool->pos++ = *string++;
- }
-
- // Terminate the string
- *pool->pos++ = '\0';
-
- return offset;
-}
-
-LOC_EXPORT off_t loc_stringpool_add(struct loc_stringpool* pool, const char* string) {
+off_t loc_stringpool_add(struct loc_stringpool* pool, const char* string) {
off_t offset = loc_stringpool_find(pool, string);
if (offset >= 0) {
- DEBUG(pool->ctx, "Found '%s' at position %jd\n", string, offset);
+ DEBUG(pool->ctx, "Found '%s' at position %jd\n", string, (intmax_t)offset);
return offset;
}
return loc_stringpool_append(pool, string);
}
-LOC_EXPORT void loc_stringpool_dump(struct loc_stringpool* pool) {
+void loc_stringpool_dump(struct loc_stringpool* pool) {
off_t offset = 0;
- while (offset < pool->max_length) {
+ while (offset < pool->length) {
const char* string = loc_stringpool_get(pool, offset);
if (!string)
- break;
-
- printf("%jd (%zu): %s\n", offset, strlen(string), string);
-
- offset = loc_stringpool_get_next_offset(pool, offset);
- }
-}
+ return;
-LOC_EXPORT int loc_stringpool_read(struct loc_stringpool* pool, FILE* f, off_t offset, size_t length) {
- // Allocate enough space
- int r = loc_stringpool_allocate(pool, length);
- if (r)
- return r;
-
- // Seek to the right offset
- r = fseek(f, offset, SEEK_SET);
- if (r)
- return r;
+ printf("%jd (%zu): %s\n", (intmax_t)offset, strlen(string), string);
- size_t bytes_read = fread(pool->data, sizeof(*pool->data), length, f);
- if (bytes_read < length) {
- ERROR(pool->ctx, "Could not read pool. Only read %zu bytes\n", bytes_read);
- return 1;
+ // Shift offset
+ offset += strlen(string) + 1;
}
-
- DEBUG(pool->ctx, "Read pool of %zu bytes\n", length);
-
- return 0;
}
-LOC_EXPORT size_t loc_stringpool_write(struct loc_stringpool* pool, FILE* f) {
+size_t loc_stringpool_write(struct loc_stringpool* pool, FILE* f) {
size_t size = loc_stringpool_get_size(pool);
return fwrite(pool->data, sizeof(*pool->data), size, f);