]> git.ipfire.org Git - location/libloc.git/commitdiff
Make AS independent from stringpool
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 30 Dec 2017 15:26:14 +0000 (15:26 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 30 Dec 2017 15:26:14 +0000 (15:26 +0000)
Strings are written to the string pool when a AS is written to
disk.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/as.c
src/loc/as.h
src/python/as.c
src/writer.c

index 818df412ce26d8c75bf5856414f574017bf708e3..f1f01ac9c7f2e8e968a7c0f5ae24f3710796c140 100644 (file)
--- a/src/as.c
+++ b/src/as.c
        Lesser General Public License for more details.
 */
 
-#include <arpa/inet.h>
+#include <endian.h>
 #include <errno.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <loc/libloc.h>
 #include <loc/as.h>
@@ -29,22 +30,20 @@ struct loc_as {
        struct loc_ctx* ctx;
        int refcount;
 
-       struct loc_stringpool* pool;
-
        uint32_t number;
-       off_t name;
+       char* name;
 };
 
-LOC_EXPORT int loc_as_new(struct loc_ctx* ctx, struct loc_stringpool* pool, struct loc_as** as, uint32_t number) {
+LOC_EXPORT int loc_as_new(struct loc_ctx* ctx, struct loc_as** as, uint32_t number) {
        struct loc_as* a = calloc(1, sizeof(*a));
        if (!a)
                return -ENOMEM;
 
        a->ctx = loc_ref(ctx);
        a->refcount = 1;
-       a->pool = loc_stringpool_ref(pool);
 
        a->number = number;
+       a->name = NULL;
 
        DEBUG(a->ctx, "AS%u allocated at %p\n", a->number, a);
        *as = a;
@@ -61,9 +60,10 @@ LOC_EXPORT struct loc_as* loc_as_ref(struct loc_as* as) {
 static void loc_as_free(struct loc_as* as) {
        DEBUG(as->ctx, "Releasing AS%u %p\n", as->number, as);
 
-       loc_stringpool_unref(as->pool);
-       loc_unref(as->ctx);
+       if (as->name)
+               free(as->name);
 
+       loc_unref(as->ctx);
        free(as);
 }
 
@@ -81,19 +81,12 @@ LOC_EXPORT uint32_t loc_as_get_number(struct loc_as* as) {
 }
 
 LOC_EXPORT const char* loc_as_get_name(struct loc_as* as) {
-       if (as->name)
-               return loc_stringpool_get(as->pool, as->name);
-
-       return NULL;
+       return as->name;
 }
 
 LOC_EXPORT int loc_as_set_name(struct loc_as* as, const char* name) {
-       // Add the string to the string pool
-       off_t offset = loc_stringpool_add(as->pool, name);
-       if (offset < 0)
-               return offset;
+       as->name = strdup(name);
 
-       as->name = offset;
        return 0;
 }
 
@@ -109,20 +102,29 @@ LOC_EXPORT int loc_as_cmp(struct loc_as* as1, struct loc_as* as2) {
 
 int loc_as_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool,
                struct loc_as** as, const struct loc_database_as_v0* dbobj) {
-       uint32_t number = ntohl(dbobj->number);
+       uint32_t number = be32toh(dbobj->number);
 
-       int r = loc_as_new(ctx, pool, as, number);
+       int r = loc_as_new(ctx, as, number);
        if (r)
                return r;
 
-       (*as)->name = ntohl(dbobj->name);
+       const char* name = loc_stringpool_get(pool, be32toh(dbobj->name));
+       r = loc_as_set_name(*as, name);
+       if (r) {
+               loc_as_unref(*as);
+               return r;
+       }
 
        return 0;
 }
 
-int loc_as_to_database_v0(struct loc_as* as, struct loc_database_as_v0* dbobj) {
-       dbobj->number = htonl(as->number);
-       dbobj->name   = htonl(as->name);
+int loc_as_to_database_v0(struct loc_as* as, struct loc_stringpool* pool,
+               struct loc_database_as_v0* dbobj) {
+       dbobj->number = htobe32(as->number);
+
+       // Save the name string in the string pool
+       off_t name = loc_stringpool_add(pool, as->name ? as->name : "");
+       dbobj->name = htobe32(name);
 
        return 0;
 }
index 14e9cf3b1f7651a4537ff6b2c88fa2b068f66903..b16de84b665b076c5ff0aed166ddbb4299e95917 100644 (file)
@@ -24,7 +24,7 @@
 #include <loc/stringpool.h>
 
 struct loc_as;
-int loc_as_new(struct loc_ctx* ctx, struct loc_stringpool* pool, struct loc_as** as, uint32_t number);
+int loc_as_new(struct loc_ctx* ctx, struct loc_as** as, uint32_t number);
 struct loc_as* loc_as_ref(struct loc_as* as);
 struct loc_as* loc_as_unref(struct loc_as* as);
 
@@ -37,6 +37,7 @@ int loc_as_cmp(struct loc_as* as1, struct loc_as* as2);
 
 int loc_as_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool,
                struct loc_as** as, const struct loc_database_as_v0* dbobj);
-int loc_as_to_database_v0(struct loc_as* as, struct loc_database_as_v0* dbobj);
+int loc_as_to_database_v0(struct loc_as* as, struct loc_stringpool* pool,
+               struct loc_database_as_v0* dbobj);
 
 #endif
index ec1573b26bb8039f03dd967583f64e53d8a697f4..f6e9cabe4169728ad0bf15924a9f2536c26c4279 100644 (file)
@@ -51,7 +51,7 @@ static int AS_init(ASObject* self, PyObject* args, PyObject* kwargs) {
                return -1;
 
        // Create the AS object
-       int r = loc_as_new(loc_ctx, NULL, &self->as, number);
+       int r = loc_as_new(loc_ctx, &self->as, number);
        if (r)
                return -1;
 
index 6427d47e94e63b32654c8fb132f62aabba00e9a4..44b783af31f327d4799379063bb91d1c26d4175c 100644 (file)
@@ -134,7 +134,7 @@ static int __loc_as_cmp(const void* as1, const void* as2) {
 }
 
 LOC_EXPORT int loc_writer_add_as(struct loc_writer* writer, struct loc_as** as, uint32_t number) {
-       int r = loc_as_new(writer->ctx, writer->pool, as, number);
+       int r = loc_as_new(writer->ctx, as, number);
        if (r)
                return r;
 
@@ -208,7 +208,7 @@ static int loc_database_write_as_section(struct loc_writer* writer,
        struct loc_database_as_v0 as;
        for (unsigned int i = 0; i < writer->as_count; i++) {
                // Convert AS into database format
-               loc_as_to_database_v0(writer->as[i], &as);
+               loc_as_to_database_v0(writer->as[i], writer->pool, &as);
 
                // Write to disk
                *offset += fwrite(&as, 1, sizeof(as), f);