output: Make tables reference-counted.
[pspp-builds.git] / src / output / table.c
index 9457923ab8f8c2e517fe6e2a5a74ac27705156f0..1b052888ebc0db42bd41ad7ab5b6978da8bc5f5c 100644 (file)
@@ -62,6 +62,7 @@ tab_create (int nc, int nr, int reallocable UNUSED)
   struct tab_table *t;
 
   t = pool_create_container (struct tab_table, container);
+  t->ref_cnt = 1;
   t->col_style = TAB_COL_NONE;
   t->col_group = 0;
   t->title = NULL;
@@ -86,17 +87,28 @@ tab_create (int nc, int nr, int reallocable UNUSED)
   return t;
 }
 
-/* Destroys table T. */
+/* Increases T's reference count and, if this causes T's
+   reference count to reach 0, destroys T. */
 void
 tab_destroy (struct tab_table *t)
 {
-  assert (t != NULL);
+  assert (t->ref_cnt > 0);
+  if (--t->ref_cnt > 0)
+    return;
   if (t->dim_free != NULL)
     t->dim_free (t->dim_aux);
   free (t->title);
   pool_destroy (t->container);
 }
 
+/* Increases T's reference count. */
+void
+tab_ref (struct tab_table *t)
+{
+  assert (t->ref_cnt > 0);
+  t->ref_cnt++;
+}
+
 /* Sets the width and height of a table, in columns and rows,
    respectively.  Use only to reduce the size of a table, since it
    does not change the amount of allocated memory. */