forked from shenango/caladan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathref.h
51 lines (45 loc) · 916 Bytes
/
ref.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* ref.h - generic support for reference counts
*
* This implementation is inspired by the following paper:
* Kroah-Hartman, Greg, kobjects and krefs. Linux Symposium 2004
*
* This version doesn't use atomics.
*/
#pragma once
#include <base/stddef.h>
struct ref {
int cnt;
};
/**
* ref_init - initializes the reference count to one
* @ref: the kref
*/
static inline void
ref_init(struct ref *ref)
{
ref->cnt = 1;
}
/**
* ref_get - atomically increments the reference count
* @ref: the kref
*/
static inline void
ref_get(struct ref *ref)
{
assert(ref->cnt > 0);
ref->cnt++;
}
/**
* ref_put - atomically decrements the reference count, releasing the object
* when it reaches zero
* @ref: the ref
* @release: a pointer to the release function
*/
static inline void
ref_put(struct ref *ref, void (*release)(struct ref *ref))
{
assert(release);
if (--ref->cnt == 0)
release(ref);
}