]> git.proxmox.com Git - mirror_zfs.git/blame - include/sys/refcount.h
Fix coverity defects: CID 147692, 147693, 147694
[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.
34dc7c2f
BB
23 */
24
25#ifndef _SYS_REFCOUNT_H
26#define _SYS_REFCOUNT_H
27
34dc7c2f
BB
28#include <sys/inttypes.h>
29#include <sys/list.h>
30#include <sys/zfs_context.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/*
37 * If the reference is held only by the calling function and not any
38 * particular object, use FTAG (which is a string) for the holder_tag.
39 * Otherwise, use the object that holds the reference.
40 */
41#define FTAG ((char *)__func__)
42
572e2857 43#ifdef ZFS_DEBUG
34dc7c2f
BB
44typedef struct reference {
45 list_node_t ref_link;
46 void *ref_holder;
47 uint64_t ref_number;
48 uint8_t *ref_removed;
49} reference_t;
50
51typedef struct refcount {
52 kmutex_t rc_mtx;
13fe0198 53 boolean_t rc_tracked;
34dc7c2f
BB
54 list_t rc_list;
55 list_t rc_removed;
d21f279a
BB
56 uint64_t rc_count;
57 uint64_t rc_removed_count;
34dc7c2f
BB
58} refcount_t;
59
13fe0198 60/* Note: refcount_t must be initialized with refcount_create[_untracked]() */
34dc7c2f
BB
61
62void refcount_create(refcount_t *rc);
13fe0198 63void refcount_create_untracked(refcount_t *rc);
34dc7c2f
BB
64void refcount_destroy(refcount_t *rc);
65void refcount_destroy_many(refcount_t *rc, uint64_t number);
66int refcount_is_zero(refcount_t *rc);
67int64_t refcount_count(refcount_t *rc);
68int64_t refcount_add(refcount_t *rc, void *holder_tag);
69int64_t refcount_remove(refcount_t *rc, void *holder_tag);
70int64_t refcount_add_many(refcount_t *rc, uint64_t number, void *holder_tag);
71int64_t refcount_remove_many(refcount_t *rc, uint64_t number, void *holder_tag);
572e2857 72void refcount_transfer(refcount_t *dst, refcount_t *src);
d3c2ae1c 73void refcount_transfer_ownership(refcount_t *, void *, void *);
34dc7c2f
BB
74
75void refcount_init(void);
76void refcount_fini(void);
77
572e2857 78#else /* ZFS_DEBUG */
34dc7c2f
BB
79
80typedef struct refcount {
81 uint64_t rc_count;
82} refcount_t;
83
84#define refcount_create(rc) ((rc)->rc_count = 0)
13fe0198 85#define refcount_create_untracked(rc) ((rc)->rc_count = 0)
34dc7c2f
BB
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)
bc89ac84
JJS
90#define refcount_add(rc, holder) atomic_inc_64_nv(&(rc)->rc_count)
91#define refcount_remove(rc, holder) atomic_dec_64_nv(&(rc)->rc_count)
34dc7c2f
BB
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)
428870ff
BB
96#define refcount_transfer(dst, src) { \
97 uint64_t __tmp = (src)->rc_count; \
98 atomic_add_64(&(src)->rc_count, -__tmp); \
99 atomic_add_64(&(dst)->rc_count, __tmp); \
100}
2aa34383 101#define refcount_transfer_ownership(rc, current_holder, new_holder) (void)0
34dc7c2f
BB
102
103#define refcount_init()
104#define refcount_fini()
105
572e2857 106#endif /* ZFS_DEBUG */
34dc7c2f
BB
107
108#ifdef __cplusplus
109}
110#endif
111
112#endif /* _SYS_REFCOUNT_H */