]> git.proxmox.com Git - mirror_zfs-debian.git/blob - include/sys/refcount.h
New upstream version 0.7.2
[mirror_zfs-debian.git] / include / sys / refcount.h
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 */
25
26 #ifndef _SYS_REFCOUNT_H
27 #define _SYS_REFCOUNT_H
28
29 #include <sys/inttypes.h>
30 #include <sys/list.h>
31 #include <sys/zfs_context.h>
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /*
38 * If the reference is held only by the calling function and not any
39 * particular object, use FTAG (which is a string) for the holder_tag.
40 * Otherwise, use the object that holds the reference.
41 */
42 #define FTAG ((char *)__func__)
43
44 /*
45 * Starting with 4.11, torvalds/linux@f405df5, the linux kernel defines a
46 * refcount_t type of its own. The macro below effectively changes references
47 * in the ZFS code from refcount_t to zfs_refcount_t at compile time, so that
48 * existing code need not be altered, reducing conflicts when landing openZFS
49 * patches.
50 */
51
52 #define refcount_t zfs_refcount_t
53 #define refcount_add zfs_refcount_add
54
55 #ifdef ZFS_DEBUG
56 typedef struct reference {
57 list_node_t ref_link;
58 void *ref_holder;
59 uint64_t ref_number;
60 uint8_t *ref_removed;
61 } reference_t;
62
63 typedef struct refcount {
64 kmutex_t rc_mtx;
65 boolean_t rc_tracked;
66 list_t rc_list;
67 list_t rc_removed;
68 uint64_t rc_count;
69 uint64_t rc_removed_count;
70 } zfs_refcount_t;
71
72 /* Note: refcount_t must be initialized with refcount_create[_untracked]() */
73
74 void refcount_create(refcount_t *rc);
75 void refcount_create_untracked(refcount_t *rc);
76 void refcount_create_tracked(refcount_t *rc);
77 void refcount_destroy(refcount_t *rc);
78 void refcount_destroy_many(refcount_t *rc, uint64_t number);
79 int refcount_is_zero(refcount_t *rc);
80 int64_t refcount_count(refcount_t *rc);
81 int64_t zfs_refcount_add(refcount_t *rc, void *holder_tag);
82 int64_t refcount_remove(refcount_t *rc, void *holder_tag);
83 int64_t refcount_add_many(refcount_t *rc, uint64_t number, void *holder_tag);
84 int64_t refcount_remove_many(refcount_t *rc, uint64_t number, void *holder_tag);
85 void refcount_transfer(refcount_t *dst, refcount_t *src);
86 void refcount_transfer_ownership(refcount_t *, void *, void *);
87 boolean_t refcount_held(refcount_t *, void *);
88 boolean_t refcount_not_held(refcount_t *, void *);
89
90 void refcount_init(void);
91 void refcount_fini(void);
92
93 #else /* ZFS_DEBUG */
94
95 typedef struct refcount {
96 uint64_t rc_count;
97 } refcount_t;
98
99 #define refcount_create(rc) ((rc)->rc_count = 0)
100 #define refcount_create_untracked(rc) ((rc)->rc_count = 0)
101 #define refcount_create_tracked(rc) ((rc)->rc_count = 0)
102 #define refcount_destroy(rc) ((rc)->rc_count = 0)
103 #define refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
104 #define refcount_is_zero(rc) ((rc)->rc_count == 0)
105 #define refcount_count(rc) ((rc)->rc_count)
106 #define zfs_refcount_add(rc, holder) atomic_inc_64_nv(&(rc)->rc_count)
107 #define refcount_remove(rc, holder) atomic_dec_64_nv(&(rc)->rc_count)
108 #define refcount_add_many(rc, number, holder) \
109 atomic_add_64_nv(&(rc)->rc_count, number)
110 #define refcount_remove_many(rc, number, holder) \
111 atomic_add_64_nv(&(rc)->rc_count, -number)
112 #define refcount_transfer(dst, src) { \
113 uint64_t __tmp = (src)->rc_count; \
114 atomic_add_64(&(src)->rc_count, -__tmp); \
115 atomic_add_64(&(dst)->rc_count, __tmp); \
116 }
117 #define refcount_transfer_ownership(rc, current_holder, new_holder) (void)0
118 #define refcount_held(rc, holder) ((rc)->rc_count > 0)
119 #define refcount_not_held(rc, holder) (B_TRUE)
120
121 #define refcount_init()
122 #define refcount_fini()
123
124 #endif /* ZFS_DEBUG */
125
126 #ifdef __cplusplus
127 }
128 #endif
129
130 #endif /* _SYS_REFCOUNT_H */