浏览代码

Fix memory leaks; minor tweaks.

Stefano Cossu 4 年之前
父节点
当前提交
b4eafeb23e
共有 4 个文件被更改,包括 43 次插入41 次删除
  1. 3 4
      include/buffer.h
  2. 2 1
      include/store_mdb.h
  3. 22 26
      src/store_mdb.c
  4. 16 10
      test/test_store_mdb.c

+ 3 - 4
include/buffer.h

@@ -20,11 +20,10 @@ int LSUP_buffer_copy(LSUP_Buffer *dest, const LSUP_Buffer *src);
 void LSUP_buffer_done(LSUP_Buffer *buf);
 
 
-/**
- * Return whether two buffers are equal.
+/** @brief Return whether two buffers are equal.
  *
- * This is faster than LSUP_buffer_cmp because it returns immediately if the
- * sizes of the buffers differ.
+ * This may be faster than LSUP_buffer_cmp because it returns immediately if
+ * the sizes of the buffers differ.
  */
 inline bool LSUP_buffer_eq(const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
 {

+ 2 - 1
include/store_mdb.h

@@ -69,7 +69,8 @@ LSUP_rc LSUP_store_setup(char **path/*, bool clear*/);
  *  triples inserted without a context specified. If NULL, the store operates
  *  in triple mode.
  */
-LSUP_MDBStore *LSUP_store_new(const char *path, LSUP_Buffer *default_ctx);
+LSUP_MDBStore *
+LSUP_store_new(const char *path, const LSUP_Buffer *default_ctx);
 
 
 /** @brief Close a store and free its handle.

+ 22 - 26
src/store_mdb.c

@@ -254,7 +254,7 @@ LSUP_store_setup(char **path/*, bool clear*/) // TODO clear
 
 
 LSUP_MDBStore *
-LSUP_store_new(const char *path, LSUP_Buffer *default_ctx)
+LSUP_store_new(const char *path, const LSUP_Buffer *default_ctx)
 {
     int rc;
     LSUP_MDBStore *store;
@@ -272,14 +272,8 @@ LSUP_store_new(const char *path, LSUP_Buffer *default_ctx)
     // Set map size.
     size_t mapsize;
     char *env_mapsize = getenv("LSUP_MDB_MAPSIZE");
-
-    if (env_mapsize == NULL) {
-        mapsize = DEFAULT_MAPSIZE;
-    } else {
-        sscanf(env_mapsize, "%lu", &mapsize);
-    }
-    TRACE("mapsize rc: %d", rc);
-    if(rc != MDB_SUCCESS) return NULL;
+    if (env_mapsize == NULL) mapsize = DEFAULT_MAPSIZE;
+    else sscanf(env_mapsize, "%lu", &mapsize);
 
     rc = mdb_env_set_maxdbs(store->env, N_DB);
     if(rc != MDB_SUCCESS) return NULL;
@@ -308,6 +302,23 @@ LSUP_store_new(const char *path, LSUP_Buffer *default_ctx)
 }
 
 
+void
+LSUP_store_free(LSUP_MDBStore *store)
+{
+    if (store->state & LSSTORE_OPEN) {
+        TRACE(STR, "Closing MDB env.\n");
+        mdb_env_close(store->env);
+    }
+
+    if (store->default_ctx) {
+        LSUP_buffer_done(store->default_ctx);
+        free(store->default_ctx);
+    }
+
+    free(store);
+}
+
+
 LSUP_rc
 LSUP_store_stats(LSUP_MDBStore *store)
 {
@@ -346,10 +357,9 @@ LSUP_store_add(
     // Serialize and hash.
     LSUP_Key ck = NULL_KEY;
 
-    // Assign default store context if existing and ctx is NULL.
-    if (store->default_ctx && !sc) sc = store->default_ctx;
+    if (store->default_ctx != NULL) {
+        if (sc == NULL) sc = store->default_ctx;
 
-    if (sc) {
         ck = LSUP_sterm_to_key(sc);
 
         // Insert t:st for context.
@@ -615,20 +625,6 @@ _remove_abort:
 }
 
 
-void
-LSUP_store_free(LSUP_MDBStore *store)
-{
-    if (store->state & LSSTORE_OPEN) {
-        TRACE(STR, "Closing MDB env.\n");
-        mdb_env_close(store->env);
-    }
-
-    if (store->default_ctx != NULL) LSUP_buffer_done(store->default_ctx);
-
-    free(store);
-}
-
-
 /* * * Static functions. * * */
 
 /* TODO

+ 16 - 10
test/test_store_mdb.c

@@ -1,6 +1,3 @@
-#include <stdio.h>
-#include <stdlib.h>
-
 #include "test.h"
 #include "store_mdb.h"
 #include "assets.h"
@@ -13,10 +10,12 @@ static void rmdb() {
     char data_path[32], lock_path[32];
     sprintf(data_path, "%s/data.mdb", path);
     sprintf(lock_path, "%s/lock.mdb", path);
+
     printf("Removing %s\n", data_path);
     remove(data_path);
     printf("Removing %s\n", lock_path);
     remove(lock_path);
+    printf("Removing %s\n", path);
     remove(path);
 }
 
@@ -71,8 +70,12 @@ static int test_quad_store()
     rmdb();
     EXPECT_PASS(LSUP_store_setup(&path));
 
+    LSUP_Term *ctx1 = LSUP_uri_new("urn:c:1");
+    LSUP_SerTerm sc1_s;
+    LSUP_term_serialize(ctx1, &sc1_s);
+
     LSUP_MDBStore *store;
-    store = LSUP_store_new(path, NULL); // triple store.
+    store = LSUP_store_new(path, &sc1_s); // triple store.
     ASSERT(store != NULL, "Error initializing store!");
 
     LSUP_Triple *trp = create_triples();
@@ -90,16 +93,14 @@ static int test_quad_store()
         }
     }
 
-    LSUP_Term *ctx1 = LSUP_uri_new("urn:c:1");
-    LSUP_SerTerm sc1_s;
-    LSUP_term_serialize(ctx1, &sc1_s);
-
-    EXPECT_PASS(LSUP_store_add(store, &sc1_s, ser_trp, NUM_TRP));
+    // Use store default context.
+    EXPECT_PASS(LSUP_store_add(store, NULL, ser_trp, NUM_TRP));
 
     LSUP_Term *ctx2 = LSUP_uri_new("urn:c:2");
     LSUP_SerTerm sc2_s;
     LSUP_term_serialize(ctx2, &sc2_s);
 
+    // Use specific context.
     EXPECT_PASS(LSUP_store_add(store, &sc2_s, ser_trp, NUM_TRP));
 
     for (int i = 0; i < NUM_TRP; i++) {
@@ -108,9 +109,14 @@ static int test_quad_store()
         LSUP_buffer_done(ser_trp[i].o);
     }
 
-    LSUP_store_free(store);
+    LSUP_term_free(ctx1);
+    LSUP_term_free(ctx2);
+    LSUP_buffer_done(&sc1_s);
+    LSUP_buffer_done(&sc2_s);
     free_triples(trp);
 
+    LSUP_store_free(store);
+
     return 0;
 }