ust: continue work
[ust.git] / share / kref.c
CommitLineData
a3b1d95b
PMF
1/*
2 * kref.c - library routines for handling generic reference counted objects
3 *
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 IBM Corp.
6 *
7 * based on lib/kobject.c which was:
8 * Copyright (C) 2002-2003 Patrick Mochel <mochel@osdl.org>
9 *
10 * This file is released under the GPLv2.
11 *
12 */
13
59b161cd
PMF
14//#include "<kernelcompat.h>"
15#include <kref.h>
16//ust// #include <linux/module.h>
17#include "usterr.h"
18#include "compiler.h"
a3b1d95b
PMF
19
20/**
21 * kref_set - initialize object and set refcount to requested number.
22 * @kref: object in question.
23 * @num: initial reference counter
24 */
25void kref_set(struct kref *kref, int num)
26{
27 atomic_set(&kref->refcount, num);
28 smp_mb();
29}
30
31/**
32 * kref_init - initialize object.
33 * @kref: object in question.
34 */
35void kref_init(struct kref *kref)
36{
37 kref_set(kref, 1);
38}
39
40/**
41 * kref_get - increment refcount for object.
42 * @kref: object.
43 */
44void kref_get(struct kref *kref)
45{
46 WARN_ON(!atomic_read(&kref->refcount));
47 atomic_inc(&kref->refcount);
48 smp_mb__after_atomic_inc();
49}
50
51/**
52 * kref_put - decrement refcount for object.
53 * @kref: object.
54 * @release: pointer to the function that will clean up the object when the
55 * last reference to the object is released.
56 * This pointer is required, and it is not acceptable to pass kfree
57 * in as this function.
58 *
59 * Decrement the refcount, and if 0, call release().
60 * Return 1 if the object was removed, otherwise return 0. Beware, if this
61 * function returns 0, you still can not count on the kref from remaining in
62 * memory. Only use the return value if you want to see if the kref is now
63 * gone, not present.
64 */
65int kref_put(struct kref *kref, void (*release)(struct kref *kref))
66{
67 WARN_ON(release == NULL);
59b161cd 68//ust// WARN_ON(release == (void (*)(struct kref *))kfree);
a3b1d95b
PMF
69
70 if (atomic_dec_and_test(&kref->refcount)) {
71 release(kref);
72 return 1;
73 }
74 return 0;
75}
76
59b161cd
PMF
77//ust// EXPORT_SYMBOL(kref_set);
78//ust// EXPORT_SYMBOL(kref_init);
79//ust// EXPORT_SYMBOL(kref_get);
80//ust// EXPORT_SYMBOL(kref_put);
This page took 0.024452 seconds and 4 git commands to generate.