[augeas-devel] augeas: master - Use a proper function for make_ref
David Lutterkort
lutter at fedoraproject.org
Thu Oct 1 00:23:17 UTC 2009
Gitweb: http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=ce18a1ba007cd308c8de16ad56d215a8738d58ed
Commit: ce18a1ba007cd308c8de16ad56d215a8738d58ed
Parent: de5176539277cb7513cddf2ada51c9683555f29f
Author: David Lutterkort <lutter at redhat.com>
AuthorDate: Thu Sep 24 16:55:02 2009 -0700
Committer: David Lutterkort <lutter at redhat.com>
CommitterDate: Wed Sep 30 12:10:55 2009 -0700
Use a proper function for make_ref
That allows us to require that callers check the return value
---
src/Makefile.am | 4 ++--
src/ref.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
src/ref.h | 10 +++++-----
3 files changed, 53 insertions(+), 7 deletions(-)
diff --git a/src/Makefile.am b/src/Makefile.am
index 68d13cb..09d88ae 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -20,7 +20,7 @@ include_HEADERS = augeas.h fa.h
libaugeas_la_SOURCES = augeas.h augeas.c pathx.c \
internal.h internal.c \
- memory.h memory.c ref.h \
+ memory.h memory.c ref.h ref.c \
syntax.c syntax.h parser.y builtin.c lens.c lens.h regexp.c regexp.h \
transform.h transform.c ast.c get.c put.c list.h \
info.c info.h
@@ -43,7 +43,7 @@ augtool_LDADD = libaugeas.la $(READLINE_LIBS) $(GNULIB)
augparse_SOURCES = augparse.c
augparse_LDADD = libaugeas.la $(GNULIB)
-libfa_la_SOURCES = fa.c fa.h hash.c hash.h memory.c memory.h ref.h
+libfa_la_SOURCES = fa.c fa.h hash.c hash.h memory.c memory.h ref.h ref.c
libfa_la_LIBADD = $(GNULIB)
libfa_la_LDFLAGS = $(FA_VERSION_SCRIPT) -version-info $(LIBFA_VERSION_INFO)
diff --git a/src/ref.c b/src/ref.c
new file mode 100644
index 0000000..41bba49
--- /dev/null
+++ b/src/ref.c
@@ -0,0 +1,46 @@
+/*
+ * ref.c: reference counting
+ *
+ * Copyright (C) 2009 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 <lutter at redhat.com>
+ */
+
+#include <config.h>
+
+#include "ref.h"
+#include <stdlib.h>
+
+int ref_make_ref(void *ptrptr, size_t size, size_t ref_ofs) {
+ *(void**) ptrptr = calloc(1, size);
+ if (*(void **)ptrptr == NULL) {
+ return -1;
+ } else {
+ void *ptr = *(void **)ptrptr;
+ *((ref_t *) ((char*) ptr + ref_ofs)) = 1;
+ return 0;
+ }
+}
+
+/*
+ * Local variables:
+ * indent-tabs-mode: nil
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ */
diff --git a/src/ref.h b/src/ref.h
index 009cb9f..269c06c 100644
--- a/src/ref.h
+++ b/src/ref.h
@@ -24,6 +24,7 @@
#define REF_H_
#include <limits.h>
+#include <stddef.h>
/* Reference counting for pointers to structs with a REF field of type ref_t
*
@@ -41,11 +42,10 @@
typedef unsigned int ref_t;
-#define make_ref(var) \
- do { \
- CALLOC(var, 1); \
- if (var) var->ref = 1; \
- } while(0)
+int ref_make_ref(void *ptrptr, size_t size, size_t ref_ofs);
+
+#define make_ref(var) \
+ ref_make_ref(&(var), sizeof(*(var)), offsetof(typeof(*(var)), ref))
#define ref(s) (((s) == NULL || (s)->ref == REF_MAX) ? (s) : ((s)->ref++, (s)))
More information about the augeas-devel
mailing list