]> git.proxmox.com Git - mirror_zfs.git/blame - include/sys/refcount.h
Remove duplicate macro in dsl_dir.h
[mirror_zfs.git] / include / sys / refcount.h
CommitLineData
34dc7c2f
BB
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/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
3dfb57a3 23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
34dc7c2f
BB
24 */
25
26#ifndef _SYS_REFCOUNT_H
27#define _SYS_REFCOUNT_H
28
34dc7c2f
BB
29#include <sys/inttypes.h>
30#include <sys/list.h>
31#include <sys/zfs_context.h>
32
33#ifdef __cplusplus
34extern "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 */
867959b5 42#define FTAG ((char *)(uintptr_t)__func__)
34dc7c2f 43
572e2857 44#ifdef ZFS_DEBUG
34dc7c2f
BB
45typedef struct reference {
46 list_node_t ref_link;
47 void *ref_holder;
48 uint64_t ref_number;
49 uint8_t *ref_removed;
50} reference_t;
51
52typedef struct refcount {
53 kmutex_t rc_mtx;
13fe0198 54 boolean_t rc_tracked;
34dc7c2f
BB
55 list_t rc_list;
56 list_t rc_removed;
d21f279a
BB
57 uint64_t rc_count;
58 uint64_t rc_removed_count;
4859fe79 59} zfs_refcount_t;
34dc7c2f 60
c13060e4
TS
61/*
62 * Note: zfs_refcount_t must be initialized with
63 * refcount_create[_untracked]()
64 */
34dc7c2f 65
c13060e4
TS
66void refcount_create(zfs_refcount_t *rc);
67void refcount_create_untracked(zfs_refcount_t *rc);
68void refcount_create_tracked(zfs_refcount_t *rc);
69void refcount_destroy(zfs_refcount_t *rc);
70void refcount_destroy_many(zfs_refcount_t *rc, uint64_t number);
71int refcount_is_zero(zfs_refcount_t *rc);
72int64_t refcount_count(zfs_refcount_t *rc);
73int64_t zfs_refcount_add(zfs_refcount_t *rc, void *holder_tag);
74int64_t refcount_remove(zfs_refcount_t *rc, void *holder_tag);
75int64_t refcount_add_many(zfs_refcount_t *rc, uint64_t number,
76 void *holder_tag);
77int64_t refcount_remove_many(zfs_refcount_t *rc, uint64_t number,
78 void *holder_tag);
79void refcount_transfer(zfs_refcount_t *dst, zfs_refcount_t *src);
80void refcount_transfer_ownership(zfs_refcount_t *, void *, void *);
81boolean_t refcount_held(zfs_refcount_t *, void *);
82boolean_t refcount_not_held(zfs_refcount_t *, void *);
34dc7c2f
BB
83
84void refcount_init(void);
85void refcount_fini(void);
86
572e2857 87#else /* ZFS_DEBUG */
34dc7c2f
BB
88
89typedef struct refcount {
90 uint64_t rc_count;
c13060e4 91} zfs_refcount_t;
34dc7c2f
BB
92
93#define refcount_create(rc) ((rc)->rc_count = 0)
13fe0198 94#define refcount_create_untracked(rc) ((rc)->rc_count = 0)
3dfb57a3 95#define refcount_create_tracked(rc) ((rc)->rc_count = 0)
34dc7c2f
BB
96#define refcount_destroy(rc) ((rc)->rc_count = 0)
97#define refcount_destroy_many(rc, number) ((rc)->rc_count = 0)
98#define refcount_is_zero(rc) ((rc)->rc_count == 0)
99#define refcount_count(rc) ((rc)->rc_count)
4859fe79 100#define zfs_refcount_add(rc, holder) atomic_inc_64_nv(&(rc)->rc_count)
bc89ac84 101#define refcount_remove(rc, holder) atomic_dec_64_nv(&(rc)->rc_count)
34dc7c2f
BB
102#define refcount_add_many(rc, number, holder) \
103 atomic_add_64_nv(&(rc)->rc_count, number)
104#define refcount_remove_many(rc, number, holder) \
105 atomic_add_64_nv(&(rc)->rc_count, -number)
428870ff
BB
106#define refcount_transfer(dst, src) { \
107 uint64_t __tmp = (src)->rc_count; \
108 atomic_add_64(&(src)->rc_count, -__tmp); \
109 atomic_add_64(&(dst)->rc_count, __tmp); \
110}
2aa34383 111#define refcount_transfer_ownership(rc, current_holder, new_holder) (void)0
3dfb57a3
DB
112#define refcount_held(rc, holder) ((rc)->rc_count > 0)
113#define refcount_not_held(rc, holder) (B_TRUE)
34dc7c2f
BB
114
115#define refcount_init()
116#define refcount_fini()
117
572e2857 118#endif /* ZFS_DEBUG */
34dc7c2f
BB
119
120#ifdef __cplusplus
121}
122#endif
123
124#endif /* _SYS_REFCOUNT_H */