xref: /openbsd/sys/sys/refcnt.h (revision a1744ce2)
1 /*	$OpenBSD: refcnt.h,v 1.12 2023/08/28 14:50:02 bluhm Exp $ */
2 
3 /*
4  * Copyright (c) 2015 David Gwynne <dlg@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef _SYS_REFCNT_H_
20 #define _SYS_REFCNT_H_
21 
22 /*
23  * Locks used to protect struct members in this file:
24  *	I	immutable after creation
25  *	a	atomic operations
26  */
27 
28 struct refcnt {
29 	unsigned int	r_refs;		/* [a] reference counter */
30 	int		r_traceidx;	/* [I] index for dt(4) tracing  */
31 };
32 
33 #define REFCNT_INITIALIZER()		{ .r_refs = 1, .r_traceidx = 0 }
34 
35 #ifdef _KERNEL
36 
37 void	refcnt_init(struct refcnt *);
38 void	refcnt_init_trace(struct refcnt *, int id);
39 void	refcnt_take(struct refcnt *);
40 int	refcnt_rele(struct refcnt *);
41 void	refcnt_rele_wake(struct refcnt *);
42 void	refcnt_finalize(struct refcnt *, const char *);
43 int	refcnt_shared(struct refcnt *);
44 unsigned int	refcnt_read(struct refcnt *);
45 
46 /* sorted alphabetically, keep in sync with dev/dt/dt_prov_static.c */
47 #define DT_REFCNT_IDX_ETHMULTI	1
48 #define DT_REFCNT_IDX_IFADDR	2
49 #define DT_REFCNT_IDX_IFMADDR	3
50 #define DT_REFCNT_IDX_INPCB	4
51 #define DT_REFCNT_IDX_RTENTRY	5
52 #define DT_REFCNT_IDX_SYNCACHE	6
53 #define DT_REFCNT_IDX_TDB	7
54 
55 #endif /* _KERNEL */
56 
57 #endif /* _SYS_REFCNT_H_ */
58