[augeas-devel] [PATCH 1 of 4] Split ref counting macros into separate header

David Lutterkort dlutter at redhat.com
Fri May 9 01:33:49 UTC 2008


# HG changeset patch
# User David Lutterkort <dlutter at redhat.com>
# Date 1210295923 25200
# Node ID 8b5aea1f3c6ed94c5ff3fd3117cc25e4da5d1cd2
# Parent  74365c49d25fccf5da90d346187e070145656f1d
Split ref counting macros into separate header

diff -r 74365c49d25f -r 8b5aea1f3c6e src/ref.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ref.h	Thu May 08 18:18:43 2008 -0700
@@ -0,0 +1,75 @@
+/*
+ * ref.h: reference counting macros
+ *
+ * Copyright (C) 2007 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: David Lutterkort <dlutter at redhat.com>
+ */
+
+#ifndef REF_H_
+#define REF_H_
+
+/* Reference counting for pointers to structs with a REF field of type ref_t
+ *
+ * When a pointer to such a struct is passed into a function that stores
+ * it, the function can either "receive ownership", meaning it does not
+ * increment the reference count, or it can "take ownership", meaning it
+ * increments the reference count. In the first case, the reference is now
+ * owned by wherever the function stored it, and not the caller anymore; in
+ * the second case, the caller and whereever the reference was stored both
+ * own the reference.
+ */
+// FIXME: This is not threadsafe; incr/decr ref needs to be protected
+
+#define REF_MAX UINT_MAX
+
+typedef unsigned int ref_t;
+
+#define make_ref(var)                                                   \
+    do {                                                                \
+        CALLOC(var, 1);                                                 \
+        if (var) var->ref = 1;                                          \
+    } while(0)
+
+#define ref(s) (((s) == NULL || (s)->ref == REF_MAX) ? (s) : ((s)->ref++, (s)))
+
+#define unref(s, t)                                                     \
+    do {                                                                \
+        if ((s) != NULL && (s)->ref != REF_MAX) {                      \
+            assert((s)->ref > 0);                                       \
+            if (--(s)->ref == 0) {                                      \
+                /*memset(s, 255, sizeof(*s));*/                         \
+                free_##t(s);                                            \
+                (s) = NULL;                                             \
+            }                                                           \
+        }                                                               \
+    } while(0)
+
+/* Make VAR uncollectable and pin it in memory for eternity */
+#define ref_pin(var)   (var)->ref = REF_MAX
+
+#endif
+
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 4
+ * End:
+ */
diff -r 74365c49d25f -r 8b5aea1f3c6e src/syntax.h
--- a/src/syntax.h	Thu May 08 10:55:44 2008 -0700
+++ b/src/syntax.h	Thu May 08 18:18:43 2008 -0700
@@ -27,41 +27,7 @@
 #include <limits.h>
 #include "internal.h"
 #include "lens.h"
-
-/* Reference counting for pointers to structs with a REF field
- *
- * When a pointer to such a struct is passed into a function that stores
- * it, the function can either "receive ownership", meaning it does not
- * increment the reference count, or it can "take ownership", meaning it
- * increments the reference count. In the first case, the reference is now
- * owned by wherever the function stored it, and not the caller anymore; in
- * the second case, the caller and whereever the reference was stored both
- * own the reference.
- */
-// FIXME: This is not threadsafe; incr/decr ref needs to be protected
-
-#define REF_MAX UINT_MAX
-
-#define make_ref(var)                                                   \
-    do {                                                                \
-        CALLOC(var, 1);                                                 \
-        var->ref = 1;                                                   \
-    } while(0)
-#define ref(s) (((s) == NULL || (s)->ref == REF_MAX) ? (s) : ((s)->ref++, (s)))
-#define unref(s, t)                                                     \
-    do {                                                                \
-        if ((s) != NULL && (s)->ref != REF_MAX) {                      \
-            assert((s)->ref > 0);                                       \
-            if (--(s)->ref == 0) {                                      \
-                /*memset(s, 255, sizeof(*s));*/                         \
-                free_##t(s);                                            \
-                (s) = NULL;                                             \
-            }                                                           \
-        }                                                               \
-    } while(0)
-
-/* Make VAR uncollectable and pin it in memory for eternity */
-#define ref_pin(var)   (var)->ref = REF_MAX
+#include "ref.h"
 
 struct info {
     unsigned int ref;




More information about the augeas-devel mailing list