]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/stringpool.c
network: Add function to return a reverse pointer for networks
[people/ms/libloc.git] / src / stringpool.c
index 4a0a46b06027120b21116428f389ac548b18216c..9986a619485f83f10961cde304c21b4a6e6e3412 100644 (file)
 #include <sys/mman.h>
 #include <unistd.h>
 
-#include <loc/libloc.h>
-#include <loc/format.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_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));
+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);
 
-                       return r;
-               }
+       // Increment size
+       pool->size += size;
+
+       // Reallocate blocks
+       pool->blocks = realloc(pool->blocks, pool->size);
+       if (!pool->blocks) {
+               ERROR(pool->ctx, "Could not grow string pool: %m\n");
+               return 1;
        }
 
+       // Update data pointer
+       pool->data = pool->blocks;
+
        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;
+static off_t loc_stringpool_append(struct loc_stringpool* pool, const char* string) {
+       if (!string) {
+               errno = EINVAL;
+               return -1;
+       }
 
-       pool->max_length = length;
+       DEBUG(pool->ctx, "Appending '%s' to string pool at %p\n", string, pool);
 
-       // Align to page size
-       while (pool->max_length % sysconf(_SC_PAGE_SIZE) > 0)
-               pool->max_length++;
+       // How much space to we need?
+       const size_t length = strlen(string) + 1;
 
-       DEBUG(pool->ctx, "Allocating pool of %zu bytes\n", pool->max_length);
+       // 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;
+       }
 
-       // Allocate some memory
-       pool->data = pool->pos = mmap(NULL, pool->max_length,
-               PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+       off_t offset = pool->length;
 
-       if (pool->data == MAP_FAILED) {
-               DEBUG(pool->ctx, "%s\n", strerror(errno));
-               return -errno;
-       }
+       // Copy the string
+       memcpy(pool->blocks + offset, string, length);
 
-       DEBUG(pool->ctx, "Allocated pool at %p\n", pool->data);
+       // Update the length of the pool
+       pool->length += length;
 
-       return 0;
+       return offset;
 }
 
-LOC_EXPORT int loc_stringpool_new(struct loc_ctx* ctx, struct loc_stringpool** pool, size_t max_length) {
+static void loc_stringpool_free(struct loc_stringpool* pool) {
+       DEBUG(pool->ctx, "Releasing string pool %p\n", pool);
+
+       // Free any data
+       if (pool->blocks)
+               free(pool->blocks);
+
+       loc_unref(pool->ctx);
+       free(pool);
+}
+
+int loc_stringpool_new(struct loc_ctx* ctx, struct loc_stringpool** pool) {
        struct loc_stringpool* p = calloc(1, sizeof(*p));
        if (!p)
-               return -ENOMEM;
+               return 1;
 
        p->ctx = loc_ref(ctx);
        p->refcount = 1;
 
-       // Allocate the data section
-       if (max_length > 0) {
-               int r = loc_stringpool_allocate(p, max_length);
-               if (r) {
-                       loc_stringpool_unref(p);
-                       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;
 
        return 0;
 }
 
-LOC_EXPORT struct loc_stringpool* loc_stringpool_ref(struct loc_stringpool* pool) {
-       pool->refcount++;
+int loc_stringpool_open(struct loc_ctx* ctx, struct loc_stringpool** pool,
+               const char* data, const size_t length) {
+       struct loc_stringpool* p = NULL;
 
-       return pool;
-}
+       // Allocate a new stringpool
+       int r = loc_stringpool_new(ctx, &p);
+       if (r)
+               goto ERROR;
 
-static void loc_stringpool_free(struct loc_stringpool* pool) {
-       DEBUG(pool->ctx, "Releasing string pool %p\n", pool);
+       // Store data and length
+       p->data   = data;
+       p->length = length;
 
-       loc_stringpool_deallocate(pool);
-       loc_unref(pool->ctx);
-       free(pool);
-}
+       DEBUG(p->ctx, "Opened string pool at %p (%zu bytes)\n", p->data, p->length);
 
-LOC_EXPORT struct loc_stringpool* loc_stringpool_unref(struct loc_stringpool* pool) {
-       if (--pool->refcount > 0)
-               return NULL;
+       *pool = p;
+       return 0;
 
-       loc_stringpool_free(pool);
+ERROR:
+       if (p)
+               loc_stringpool_free(p);
 
-       return NULL;
+       return r;
 }
 
-static off_t loc_stringpool_get_offset(struct loc_stringpool* pool, const char* pos) {
-       if (pos < pool->data)
-               return -EFAULT;
-
-       if (pos > (pool->data + pool->max_length))
-               return -EFAULT;
+struct loc_stringpool* loc_stringpool_ref(struct loc_stringpool* pool) {
+       pool->refcount++;
 
-       return pos - pool->data;
+       return pool;
 }
 
-static off_t loc_stringpool_get_next_offset(struct loc_stringpool* pool, off_t offset) {
-       const char* string = loc_stringpool_get(pool, offset);
+struct loc_stringpool* loc_stringpool_unref(struct loc_stringpool* pool) {
+       if (--pool->refcount > 0)
+               return NULL;
 
-       return offset + strlen(string) + 1;
-}
+       loc_stringpool_free(pool);
 
-static size_t loc_stringpool_space_left(struct loc_stringpool* pool) {
-       return pool->max_length - loc_stringpool_get_size(pool);
+       return NULL;
 }
 
-LOC_EXPORT const char* loc_stringpool_get(struct loc_stringpool* pool, off_t offset) {
-       if (offset >= (ssize_t)pool->max_length)
-               return NULL;
-
-       const char* string = pool->data + offset;
-
-       // 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;
+                       return;
 
-               printf("%jd (%zu): %s\n", offset, strlen(string), string);
+               printf("%jd (%zu): %s\n", (intmax_t)offset, strlen(string), string);
 
-               offset = loc_stringpool_get_next_offset(pool, offset);
+               // Shift offset
+               offset += strlen(string) + 1;
        }
 }
 
-LOC_EXPORT int loc_stringpool_read(struct loc_stringpool* pool, FILE* f, off_t offset, size_t length) {
-       DEBUG(pool->ctx, "Reading string pool from %zu (%zu bytes)\n", offset, length);
-
-       pool->data = pool->pos = mmap(NULL, length, PROT_READ,
-               MAP_PRIVATE, fileno(f), offset);
-       pool->max_length = length;
-
-       if (pool->data == MAP_FAILED)
-               return -errno;
-
-       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);