1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 2212296SLin.Ling@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23789Sahrens */ 24789Sahrens 25789Sahrens #ifndef _SYS_REFCOUNT_H 26789Sahrens #define _SYS_REFCOUNT_H 27789Sahrens 28789Sahrens #include <sys/inttypes.h> 29789Sahrens #include <sys/list.h> 30789Sahrens #include <sys/zfs_context.h> 31789Sahrens 32789Sahrens #ifdef __cplusplus 33789Sahrens extern "C" { 34789Sahrens #endif 35789Sahrens 36789Sahrens /* 37789Sahrens * If the reference is held only by the calling function and not any 38789Sahrens * particular object, use FTAG (which is a string) for the holder_tag. 39789Sahrens * Otherwise, use the object that holds the reference. 40789Sahrens */ 411544Seschrock #define FTAG ((char *)__func__) 42789Sahrens 43*12684STom.Erickson@Sun.COM #ifdef ZFS_DEBUG 44789Sahrens typedef struct reference { 45789Sahrens list_node_t ref_link; 46789Sahrens void *ref_holder; 47789Sahrens uint64_t ref_number; 48789Sahrens uint8_t *ref_removed; 49789Sahrens } reference_t; 50789Sahrens 51789Sahrens typedef struct refcount { 52789Sahrens kmutex_t rc_mtx; 53789Sahrens list_t rc_list; 54789Sahrens list_t rc_removed; 55789Sahrens int64_t rc_count; 56789Sahrens int64_t rc_removed_count; 57789Sahrens } refcount_t; 58789Sahrens 594787Sahrens /* Note: refcount_t must be initialized with refcount_create() */ 60789Sahrens 61789Sahrens void refcount_create(refcount_t *rc); 62789Sahrens void refcount_destroy(refcount_t *rc); 63789Sahrens void refcount_destroy_many(refcount_t *rc, uint64_t number); 64789Sahrens int refcount_is_zero(refcount_t *rc); 65789Sahrens int64_t refcount_count(refcount_t *rc); 66789Sahrens int64_t refcount_add(refcount_t *rc, void *holder_tag); 67789Sahrens int64_t refcount_remove(refcount_t *rc, void *holder_tag); 68789Sahrens int64_t refcount_add_many(refcount_t *rc, uint64_t number, void *holder_tag); 69789Sahrens int64_t refcount_remove_many(refcount_t *rc, uint64_t number, void *holder_tag); 70*12684STom.Erickson@Sun.COM void refcount_transfer(refcount_t *dst, refcount_t *src); 71789Sahrens 72789Sahrens void refcount_init(void); 73789Sahrens void refcount_fini(void); 74789Sahrens 75*12684STom.Erickson@Sun.COM #else /* ZFS_DEBUG */ 76789Sahrens 77789Sahrens typedef struct refcount { 78789Sahrens uint64_t rc_count; 79789Sahrens } refcount_t; 80789Sahrens 81789Sahrens #define refcount_create(rc) ((rc)->rc_count = 0) 82789Sahrens #define refcount_destroy(rc) ((rc)->rc_count = 0) 83789Sahrens #define refcount_destroy_many(rc, number) ((rc)->rc_count = 0) 84789Sahrens #define refcount_is_zero(rc) ((rc)->rc_count == 0) 85789Sahrens #define refcount_count(rc) ((rc)->rc_count) 86789Sahrens #define refcount_add(rc, holder) atomic_add_64_nv(&(rc)->rc_count, 1) 87789Sahrens #define refcount_remove(rc, holder) atomic_add_64_nv(&(rc)->rc_count, -1) 88789Sahrens #define refcount_add_many(rc, number, holder) \ 89789Sahrens atomic_add_64_nv(&(rc)->rc_count, number) 90789Sahrens #define refcount_remove_many(rc, number, holder) \ 91789Sahrens atomic_add_64_nv(&(rc)->rc_count, -number) 9212296SLin.Ling@Sun.COM #define refcount_transfer(dst, src) { \ 9312296SLin.Ling@Sun.COM uint64_t __tmp = (src)->rc_count; \ 9412296SLin.Ling@Sun.COM atomic_add_64(&(src)->rc_count, -__tmp); \ 9512296SLin.Ling@Sun.COM atomic_add_64(&(dst)->rc_count, __tmp); \ 9612296SLin.Ling@Sun.COM } 97789Sahrens 98789Sahrens #define refcount_init() 99789Sahrens #define refcount_fini() 100789Sahrens 101*12684STom.Erickson@Sun.COM #endif /* ZFS_DEBUG */ 102789Sahrens 103789Sahrens #ifdef __cplusplus 104789Sahrens } 105789Sahrens #endif 106789Sahrens 107789Sahrens #endif /* _SYS_REFCOUNT_H */ 108