xref: /titanic_52/usr/src/uts/common/fs/zfs/sys/refcount.h (revision e4a2bec7d50838378a9a8d23992ab4a6fa8726ab)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_REFCOUNT_H
28 #define	_SYS_REFCOUNT_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/inttypes.h>
33 #include <sys/list.h>
34 #include <sys/zfs_context.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * If the reference is held only by the calling function and not any
42  * particular object, use FTAG (which is a string) for the holder_tag.
43  * Otherwise, use the object that holds the reference.
44  */
45 #define	FTAG ((void*)__func__)
46 
47 #if defined(DEBUG) || !defined(_KERNEL)
48 typedef struct reference {
49 	list_node_t ref_link;
50 	void *ref_holder;
51 	uint64_t ref_number;
52 	uint8_t *ref_removed;
53 } reference_t;
54 
55 typedef struct refcount {
56 	kmutex_t rc_mtx;
57 	list_t rc_list;
58 	list_t rc_removed;
59 	int64_t rc_count;
60 	int64_t rc_removed_count;
61 } refcount_t;
62 
63 /* Note: refcount_t should be initialized to zero before use. */
64 
65 void refcount_create(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 
75 void refcount_init(void);
76 void refcount_fini(void);
77 
78 #else /* DEBUG */
79 
80 typedef struct refcount {
81 	uint64_t rc_count;
82 } refcount_t;
83 
84 #define	refcount_create(rc) ((rc)->rc_count = 0)
85 #define	refcount_destroy(rc) ((rc)->rc_count = 0)
86 #define	refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
87 #define	refcount_is_zero(rc) ((rc)->rc_count == 0)
88 #define	refcount_count(rc) ((rc)->rc_count)
89 #define	refcount_add(rc, holder) atomic_add_64_nv(&(rc)->rc_count, 1)
90 #define	refcount_remove(rc, holder) atomic_add_64_nv(&(rc)->rc_count, -1)
91 #define	refcount_add_many(rc, number, holder) \
92 	atomic_add_64_nv(&(rc)->rc_count, number)
93 #define	refcount_remove_many(rc, number, holder) \
94 	atomic_add_64_nv(&(rc)->rc_count, -number)
95 
96 #define	refcount_init()
97 #define	refcount_fini()
98 
99 #endif /* DEBUG */
100 
101 #ifdef	__cplusplus
102 }
103 #endif
104 
105 #endif /* _SYS_REFCOUNT_H */
106