move libkcompat inside ust
[ust.git] / include / ust / kcompat / kref.h
diff --git a/include/ust/kcompat/kref.h b/include/ust/kcompat/kref.h
new file mode 100644 (file)
index 0000000..f0604ef
--- /dev/null
@@ -0,0 +1,46 @@
+#ifndef _KCOMPAT_KREF_H
+#define _KCOMPAT_KREF_H
+
+/*
+ * Kernel sourcecode compatible reference counting implementation
+ *
+ * Copyright (C) 2009 Novell Inc.
+ *
+ * Author: Jan Blunck <jblunck@suse.de>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License version 2.1 as
+ * published by the Free  Software Foundation.
+ */
+
+#include <assert.h>
+#include <urcu/uatomic_arch.h>
+
+struct kref {
+       long refcount; /* ATOMIC */
+};
+
+static inline void kref_set(struct kref *ref, int val)
+{
+       uatomic_set(&ref->refcount, val);
+}
+
+static inline void kref_init(struct kref *ref)
+{
+       kref_set(ref, 1);
+}
+
+static inline void kref_get(struct kref *ref)
+{
+       long result = uatomic_add_return(&ref->refcount, 1);
+       assert(result != 0);
+}
+
+static inline void kref_put(struct kref *ref, void (*release)(struct kref *))
+{
+       long res = uatomic_sub_return(&ref->refcount, 1);
+       if (res == 0)
+               release(ref);
+}
+
+#endif /* _KCOMPAT_KREF_H */
This page took 0.022355 seconds and 4 git commands to generate.