aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <greg@kroah.com>2004-03-14 22:39:18 -0800
committerGreg Kroah-Hartman <greg@kroah.com>2004-03-14 22:39:18 -0800
commit04e52ea83772c5e47c0f36328c10f21e744d57ba (patch)
treefbdff73665d259e700925301d47a8503699b7ed2 /lib
parenta538fe37e9f04a5b9eebc3f22ce190717d854296 (diff)
downloadhistory-04e52ea83772c5e47c0f36328c10f21e744d57ba.tar.gz
kref: add kref structure to kernel tree.
Based on the kobject structure, but much smaller and simpler to use.
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile3
-rw-r--r--lib/kref.c60
2 files changed, 63 insertions, 0 deletions
diff --git a/lib/Makefile b/lib/Makefile
index c489f79b9dfa3a..961bac2e6166f6 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -8,6 +8,9 @@ lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \
kobject.o idr.o div64.o parser.o int_sqrt.o \
bitmap.o extable.o
+# hack for now till some static code uses krefs, then it can move up above...
+obj-y += kref.o
+
lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
diff --git a/lib/kref.c b/lib/kref.c
new file mode 100644
index 00000000000000..ee141adbf2e558
--- /dev/null
+++ b/lib/kref.c
@@ -0,0 +1,60 @@
+/*
+ * kref.c - library routines for handling generic reference counted objects
+ *
+ * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
+ * Copyright (C) 2004 IBM Corp.
+ *
+ * based on lib/kobject.c which was:
+ * Copyright (C) 2002-2003 Patrick Mochel <mochel@osdl.org>
+ *
+ * This file is released under the GPLv2.
+ *
+ */
+
+/* #define DEBUG */
+
+#include <linux/kref.h>
+#include <linux/module.h>
+
+/**
+ * kref_init - initialize object.
+ * @kref: object in question.
+ * @release: pointer to a function that will clean up the object
+ * when the last reference to the object is released.
+ * This pointer is required.
+ */
+void kref_init(struct kref *kref, void (*release)(struct kref *kref))
+{
+ WARN_ON(release == NULL);
+ atomic_set(&kref->refcount,1);
+ kref->release = release;
+}
+
+/**
+ * kref_get - increment refcount for object.
+ * @kref: object.
+ */
+struct kref *kref_get(struct kref *kref)
+{
+ WARN_ON(!atomic_read(&kref->refcount));
+ atomic_inc(&kref->refcount);
+ return kref;
+}
+
+/**
+ * kref_put - decrement refcount for object.
+ * @kref: object.
+ *
+ * Decrement the refcount, and if 0, call kref->release().
+ */
+void kref_put(struct kref *kref)
+{
+ if (atomic_dec_and_test(&kref->refcount)) {
+ pr_debug("kref cleaning up\n");
+ kref->release(kref);
+ }
+}
+
+EXPORT_SYMBOL(kref_init);
+EXPORT_SYMBOL(kref_get);
+EXPORT_SYMBOL(kref_put);