Code

utils llist: Let llist_get() increment the reference count.
authorSebastian Harl <sh@tokkee.org>
Fri, 7 Mar 2014 13:52:59 +0000 (14:52 +0100)
committerSebastian Harl <sh@tokkee.org>
Fri, 7 Mar 2014 13:52:59 +0000 (14:52 +0100)
This feels more consistent.

src/frontend/connection.c
src/frontend/parser.c
src/include/utils/llist.h
src/utils/llist.c
t/frontend/parser_test.c
t/utils/llist_test.c

index b48de2c893ccbc9d9c65de335fecbb66c3421733..fdbb26fe60518a0b1834d4c809a3f27915f54d74 100644 (file)
@@ -301,8 +301,10 @@ command_handle(sdb_conn_t *conn)
                                        node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
                        }
 
-                       if (node)
+                       if (node) {
                                status = sdb_fe_exec(conn, node);
+                               sdb_object_deref(SDB_OBJ(node));
+                       }
 
                        sdb_llist_destroy(parsetree);
                        break;
index e8c0cdef314c7b186110f578ec3ab84f6f9cf908..6504a66ea7a408b00522ab3ae94639f52e484df0 100644 (file)
@@ -121,6 +121,7 @@ sdb_fe_parse_matcher(const char *expr, int len)
                m = NULL;
 
        sdb_llist_destroy(yyextra.parsetree);
+       sdb_object_deref(SDB_OBJ(node));
        return m;
 } /* sdb_fe_parse_matcher */
 
index 3eefae5e19b894adf457a5d195e7840a11f79757..93561906b6056a272f468c9c87187469004b8246 100644 (file)
@@ -121,7 +121,9 @@ sdb_llist_insert_sorted(sdb_llist_t *list,
 
 /*
  * sdb_llist_get:
- * Returns the i-th element of the list or NULL in case of an error.
+ * Returns the i-th element of the list or NULL in case of an error. The
+ * reference count of the element is incremented before returning it to share
+ * ownership between the list and the caller.
  */
 sdb_object_t *
 sdb_llist_get(sdb_llist_t *list, size_t i);
index 2dc7781cccab7f2ca8decd373461ae7e42999d0b..f009bcdac43cd30b3ccd51c0d79f953018f16e04 100644 (file)
@@ -321,6 +321,7 @@ sdb_llist_get(sdb_llist_t *list, size_t i)
                /* iterate */;
 
        assert(elem);
+       sdb_object_ref(elem->obj);
        return elem->obj;
 } /* sdb_llist_get */
 
index 64580b9e70412aebc8800f599da6bc52f6f5d960..acd8ee69f079c5571be0b4786ef8ec9c647436fe 100644 (file)
@@ -91,6 +91,7 @@ START_TEST(test_parse)
                                        "sdb_fe_parse(LIST)->cmd = %i; expected: %d "
                                        "(CONNECTION_LIST)", SDB_CONN_NODE(obj)->cmd,
                                        CONNECTION_LIST);
+                       sdb_object_deref(obj);
                }
 
                sdb_llist_destroy(check);
index 5197bd11cf05079efcbc5e9b9c1dcaab0aeab4dc..9539c910bc9acbe3c73b8ba62d4f9c47e695380b 100644 (file)
@@ -179,9 +179,10 @@ START_TEST(test_sdb_llist_get)
                fail_unless(check == &golden_data[i],
                                "sdb_llist_get() = NULL; expected: %p",
                                &golden_data[i]);
-               fail_unless(check->ref_cnt == 2,
-                               "sdb_llist_get() changed reference count; got: %i; "
-                               "expected: 2", check->ref_cnt);
+               fail_unless(check->ref_cnt == 3,
+                               "sdb_llist_get() didn't increment reference count; got: %i; "
+                               "expected: 3", check->ref_cnt);
+               sdb_object_deref(check);
        }
 }
 END_TEST