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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
26 #ifndef _SYS_REFCOUNT_H
27 #define _SYS_REFCOUNT_H
29 #include <sys/inttypes.h>
31 #include <sys/zfs_context.h>
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.
42 #define FTAG ((char *)(uintptr_t)__func__)
45 typedef struct reference
{
52 typedef struct refcount
{
58 uint64_t rc_removed_count
;
61 /* Note: refcount_t must be initialized with refcount_create[_untracked]() */
63 void refcount_create(refcount_t
*rc
);
64 void refcount_create_untracked(refcount_t
*rc
);
65 void refcount_create_tracked(refcount_t
*rc
);
66 void refcount_destroy(refcount_t
*rc
);
67 void refcount_destroy_many(refcount_t
*rc
, uint64_t number
);
68 int refcount_is_zero(refcount_t
*rc
);
69 int64_t refcount_count(refcount_t
*rc
);
70 int64_t refcount_add(refcount_t
*rc
, void *holder_tag
);
71 int64_t refcount_remove(refcount_t
*rc
, void *holder_tag
);
72 int64_t refcount_add_many(refcount_t
*rc
, uint64_t number
, void *holder_tag
);
73 int64_t refcount_remove_many(refcount_t
*rc
, uint64_t number
, void *holder_tag
);
74 void refcount_transfer(refcount_t
*dst
, refcount_t
*src
);
75 void refcount_transfer_ownership(refcount_t
*, void *, void *);
76 boolean_t
refcount_held(refcount_t
*, void *);
77 boolean_t
refcount_not_held(refcount_t
*, void *);
79 void refcount_init(void);
80 void refcount_fini(void);
84 typedef struct refcount
{
88 #define refcount_create(rc) ((rc)->rc_count = 0)
89 #define refcount_create_untracked(rc) ((rc)->rc_count = 0)
90 #define refcount_create_tracked(rc) ((rc)->rc_count = 0)
91 #define refcount_destroy(rc) ((rc)->rc_count = 0)
92 #define refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
93 #define refcount_is_zero(rc) ((rc)->rc_count == 0)
94 #define refcount_count(rc) ((rc)->rc_count)
95 #define refcount_add(rc, holder) atomic_inc_64_nv(&(rc)->rc_count)
96 #define refcount_remove(rc, holder) atomic_dec_64_nv(&(rc)->rc_count)
97 #define refcount_add_many(rc, number, holder) \
98 atomic_add_64_nv(&(rc)->rc_count, number)
99 #define refcount_remove_many(rc, number, holder) \
100 atomic_add_64_nv(&(rc)->rc_count, -number)
101 #define refcount_transfer(dst, src) { \
102 uint64_t __tmp = (src)->rc_count; \
103 atomic_add_64(&(src)->rc_count, -__tmp); \
104 atomic_add_64(&(dst)->rc_count, __tmp); \
106 #define refcount_transfer_ownership(rc, current_holder, new_holder) (void)0
107 #define refcount_held(rc, holder) ((rc)->rc_count > 0)
108 #define refcount_not_held(rc, holder) (B_TRUE)
110 #define refcount_init()
111 #define refcount_fini()
113 #endif /* ZFS_DEBUG */
119 #endif /* _SYS_REFCOUNT_H */