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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #ifndef _SYS_REFCOUNT_H
27 #define _SYS_REFCOUNT_H
29 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 #include_next <sys/refcount.h>
35 #include <sys/zfs_context.h>
42 * If the reference is held only by the calling function and not any
43 * particular object, use FTAG (which is a string) for the holder_tag.
44 * Otherwise, use the object that holds the reference.
46 #define FTAG ((char *)__func__)
48 #if defined(DEBUG) || !defined(_KERNEL)
49 typedef struct reference {
56 typedef struct refcount {
61 int64_t rc_removed_count;
64 /* Note: refcount_t must be initialized with refcount_create() */
66 void refcount_create(refcount_t *rc);
67 void refcount_destroy(refcount_t *rc);
68 void refcount_destroy_many(refcount_t *rc, uint64_t number);
69 int refcount_is_zero(refcount_t *rc);
70 int64_t refcount_count(refcount_t *rc);
71 int64_t refcount_add(refcount_t *rc, void *holder_tag);
72 int64_t refcount_remove(refcount_t *rc, void *holder_tag);
73 int64_t refcount_add_many(refcount_t *rc, uint64_t number, void *holder_tag);
74 int64_t refcount_remove_many(refcount_t *rc, uint64_t number, void *holder_tag);
76 void refcount_sysinit(void);
77 void refcount_fini(void);
81 typedef struct refcount {
85 #define refcount_create(rc) ((rc)->rc_count = 0)
86 #define refcount_destroy(rc) ((rc)->rc_count = 0)
87 #define refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
88 #define refcount_is_zero(rc) ((rc)->rc_count == 0)
89 #define refcount_count(rc) ((rc)->rc_count)
90 #define refcount_add(rc, holder) atomic_add_64_nv(&(rc)->rc_count, 1)
91 #define refcount_remove(rc, holder) atomic_add_64_nv(&(rc)->rc_count, -1)
92 #define refcount_add_many(rc, number, holder) \
93 atomic_add_64_nv(&(rc)->rc_count, number)
94 #define refcount_remove_many(rc, number, holder) \
95 atomic_add_64_nv(&(rc)->rc_count, -number)
97 #define refcount_sysinit()
98 #define refcount_fini()
106 #endif /* _SYS_REFCOUNT_H */