Add userspace equivalent of kref
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Mon, 24 Apr 2017 16:48:33 +0000 (18:48 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Mon, 24 Apr 2017 16:48:33 +0000 (18:48 +0200)
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
src/core/uref.h [new file with mode: 0644]

diff --git a/src/core/uref.h b/src/core/uref.h
new file mode 100644 (file)
index 0000000..a68e7b3
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef FAULTD_UREF_H
+#define FAULTD_UREF_H
+
+/* This is not thread safe! */
+struct uref {
+       unsigned refcnt;
+       void (*release)(struct uref *);
+};
+
+static inline void uref_init(struct uref *uref, void (*release)(struct uref *))
+{
+       uref->refcnt = 1;
+       uref->release = release;
+}
+
+static inline void uref_set_release(struct uref *uref,
+                                   void (*release)(struct uref *))
+{
+       assert(uref->refcnt > 0);
+       uref->release = release;
+}
+
+static inline void uref_get(struct uref *uref)
+{
+       assert(uref->refcnt > 0);
+       uref->refcnt++;
+}
+
+static inline void uref_sub(struct uref *uref, unsigned count)
+{
+       assert(uref->refcnt >= count);
+       uref->refcnt -= count;
+       if (uref->refcnt == 0 && uref->release)
+               uref->release(uref);
+}
+
+static inline void uref_put(struct uref *uref)
+{
+       uref_sub(uref, 1);
+}
+
+#endif /* FAULTD_UREF_H */