Browse Source

Copy term data on creation; fix all mem errors.

Stefano Cossu 3 years ago
parent
commit
fee5f0e9a3
8 changed files with 35 additions and 73 deletions
  1. 2 0
      TODO.md
  2. 2 5
      include/term.h
  3. 0 2
      src/environment.c
  4. 1 0
      src/graph.c
  5. 8 61
      src/term.c
  6. 2 0
      test/assets/triples.h
  7. 1 2
      test/test_graph.c
  8. 19 3
      test/test_term.c

+ 2 - 0
TODO.md

@@ -20,6 +20,8 @@
     - *D* Namespace module
     - *D* Tests (basic)
     - *D* Subclass term types
+- *D* Namespaced IRIs
+- *P* Relative IRIs
 - *P* Turtle serialization / deserialization
 - *P* Extended tests
     - *P* C API

+ 2 - 5
include/term.h

@@ -191,7 +191,7 @@ LSUP_iriref_new (const char *data, LSUP_NSMap *nsm)
  * @param data[in] The literal string.
  *
  * @param datatype[in] Data type URI string. If NULL, the default data type
- * (xsd:string) is used.
+ * (xsd:string) is used. The new term takes ownership of the pointer.
  *
  * @return same as #LSUP_term_new().
  */
@@ -264,9 +264,6 @@ inline bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2)
 { return LSUP_term_hash (term1) == LSUP_term_hash (term2); }
 
 
-void
-LSUP_term_done (LSUP_Term *term);
-
 void
 LSUP_term_free (LSUP_Term *term);
 
@@ -275,7 +272,7 @@ LSUP_term_free (LSUP_Term *term);
  *
  * @param[in] iri IRI reference handle.
  *
- * @return A pointer to the namespace map assiciated with the IRI. It is
+ * @return A pointer to the namespace map associated with the IRI. It is
  *  freed at program shutdown.
  */
 LSUP_NSMap *

+ 0 - 2
src/environment.c

@@ -77,8 +77,6 @@ LSUP_init (void)
 
         // Default literal datatype key.
         LSUP_default_datatype = LSUP_iriref_new (DEFAULT_DTYPE, NULL);
-        LSUP_default_dtype_key  = LSUP_term_hash (LSUP_default_datatype);
-        LSUP_tcache_add (LSUP_default_dtype_key, LSUP_default_datatype);
 
         // Default permanent store path.
         char *mdb_path = getenv ("LSUP_MDB_STORE_PATH");

+ 1 - 0
src/graph.c

@@ -226,6 +226,7 @@ LSUP_graph_set_uri (LSUP_Graph *gr, LSUP_Term *uri)
         return LSUP_VALUE_ERR;
     }
 
+    LSUP_term_free (gr->uri);
     gr->uri = uri;
 
     return LSUP_OK;

+ 8 - 61
src/term.c

@@ -137,6 +137,7 @@ LSUP_term_serialize (const LSUP_Term *term)
         ) != LSUP_OK) return NULL;
 
         tmp_term = LSUP_iriref_new (fq_uri, NULL);
+        free (fq_uri);
 
     } else if (term->type == LSUP_TERM_LT_LITERAL) {
         // For LT literals with empty lang tag, convert to a normal xsd:string.
@@ -294,15 +295,13 @@ term_init (
             return LSUP_VALUE_ERR;
         }
 
-        if (term->datatype != LSUP_default_datatype) {
-            uint32_t dtype_hash = LSUP_term_hash (term->datatype );
+        uint32_t dtype_hash = LSUP_term_hash (term->datatype);
 
-            LSUP_Term *tmp = (LSUP_Term *) LSUP_tcache_get (dtype_hash);
-            if (!tmp) LSUP_tcache_add (dtype_hash, term->datatype);
-            else if (term->datatype != tmp) {
-                free (term->datatype);
-                term->datatype = tmp;
-            }
+        LSUP_Term *tmp = (LSUP_Term *) LSUP_tcache_get (dtype_hash);
+        if (!tmp) LSUP_tcache_add (dtype_hash, term->datatype);
+        else if (term->datatype != tmp) {
+            LSUP_term_free (term->datatype);
+            term->datatype = tmp;
         }
 
         log_trace ("Datatype address: %p", term->datatype);
@@ -334,62 +333,10 @@ LSUP_term_hash (const LSUP_Term *term)
 }
 
 
-/* DEPRECATED
-bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2)
-{
-    LSUP_TermType type1, type2;
-    char *data1, *data2;
-
-    // Normalize IRI data before comparing.
-    if (term1->type == LSUP_TERM_NS_IRIREF) {
-        type1 = LSUP_TERM_IRIREF;
-        if (UNLIKELY (LSUP_nsmap_normalize_uri (
-            term1->iri_info->nsm, term1->data, &data1
-        ) != LSUP_OK)) return LSUP_ERROR;
-    } else {
-        type1 = term1->type;
-        data1 = term1->data;
-    }
-
-    if (term2->type == LSUP_TERM_NS_IRIREF) {
-        type2 = LSUP_TERM_IRIREF;
-        if (UNLIKELY (LSUP_nsmap_normalize_uri (
-            term2->iri_info->nsm, term2->data, &data2
-        ) != LSUP_OK)) return LSUP_ERROR;
-    } else {
-        type2 = term2->type;
-        data2 = term2->data;
-    }
-
-    if (type1 != type2) return false;
-
-    int cmp = strcmp (data1, data2);
-    if (term1->type == LSUP_TERM_NS_IRIREF) free (data1);
-    if (term2->type == LSUP_TERM_NS_IRIREF) free (data2);
-
-    if (cmp != 0) return false;
-
-    if (term1->type == LSUP_TERM_LITERAL)
-        return term1->datatype == term2->datatype;
-
-    if (term1->type == LSUP_TERM_LT_LITERAL)
-        return strncmp (term1->lang, term2->lang, sizeof (term1->lang)) == 0;
-
-    return true;
-}
-*/
-
-
-void LSUP_term_done (LSUP_Term *term)
-{
-    free (term->data);
-    term->data = NULL;
-}
-
-
 void LSUP_term_free (LSUP_Term *term)
 {
     if (LIKELY (term != NULL)) {
+        if (LSUP_IS_IRI (term)) free (term->iri_info);
         free (term->data);
         free (term);
     }

+ 2 - 0
test/assets/triples.h

@@ -67,6 +67,8 @@ LSUP_Triple *create_triples()
 
 void free_triples (LSUP_Triple *trp)
 {
+    LSUP_nsmap_free (LSUP_iriref_nsm (trp[4].p));
+
     // Last three triples are second pointers.
     for (int i=0; i < NUM_TRP - 3; i++) {
         LSUP_term_free (trp[i].s);

+ 1 - 2
test/test_graph.c

@@ -7,8 +7,7 @@
 static int
 _graph_new (LSUP_store_type type)
 {
-    LSUP_Graph *gr;
-    gr = LSUP_graph_new (LSUP_iriref_new (NULL, NULL), type);
+    LSUP_Graph *gr = LSUP_graph_new (LSUP_iriref_new (NULL, NULL), type);
     ASSERT (gr != NULL, "Error creating graph!");
 
     EXPECT_PASS (LSUP_graph_set_uri (gr, LSUP_iriref_new ("urn:gr:1", NULL)));

+ 19 - 3
test/test_term.c

@@ -16,9 +16,15 @@ static int test_iriref()
     LSUP_Term *uri1 = LSUP_iriref_new (uri1_data, NULL);
     ASSERT (uri1, "IRI is NULL!");
     ASSERT (LSUP_iriref_nsm (uri1) == NULL, "Wrong NSMap!");
-    EXPECT_STR_EQ (LSUP_iriref_prefix (uri1), "http://example.org");
-    EXPECT_STR_EQ (LSUP_iriref_path (uri1), "/term#12345");
-    EXPECT_STR_EQ (LSUP_iriref_frag (uri1), "12345");
+    char *pfx = LSUP_iriref_prefix (uri1);
+    char *path = LSUP_iriref_path (uri1);
+    char *frag = LSUP_iriref_frag (uri1);
+    EXPECT_STR_EQ (pfx, "http://example.org");
+    EXPECT_STR_EQ (path, "/term#12345");
+    EXPECT_STR_EQ (frag, "12345");
+    free (pfx);
+    free (path);
+    free (frag);
 
     LSUP_Term *uri2 = LSUP_iriref_new (uri2_data, nsm1);
     ASSERT (uri2, "IRI is NULL!");
@@ -45,6 +51,14 @@ static int test_iriref()
     ASSERT (!LSUP_term_equals (uri3, uri4), "IRIs shouldn't match!");
     ASSERT (!LSUP_term_equals (uri4, uri5), "IRIs shouldn't match!");
 
+    LSUP_term_free (uri1);
+    LSUP_term_free (uri2);
+    LSUP_term_free (uri3);
+    LSUP_term_free (uri4);
+    LSUP_term_free (uri5);
+    LSUP_nsmap_free (nsm1);
+    LSUP_nsmap_free (nsm2);
+
     return 0;
 }
 
@@ -195,6 +209,8 @@ static int test_term_to_key()
     LSUP_term_free (tlit);
     LSUP_term_free (llit1);
     LSUP_term_free (llit2);
+    LSUP_term_free (llit3);
+    LSUP_nsmap_free (nsm);
 
     return 0;
 }