| 1 | /*	$OpenBSD: refcnt.h,v 1.16 2025/08/05 12:52:20 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); |
| 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 | unsigned int	refcnt_read(const struct refcnt *); |
| 44 | |
| 45 | #define refcnt_shared(_r) (refcnt_read((_r)) > 1) |
| 46 | |
| 47 | /* sorted alphabetically, keep in sync with dev/dt/dt_prov_static.c */ |
| 48 | #define DT_REFCNT_IDX_ETHMULTI	1 |
| 49 | #define DT_REFCNT_IDX_IFADDR	2 |
| 50 | #define DT_REFCNT_IDX_IFMADDR	3 |
| 51 | #define DT_REFCNT_IDX_INPCB	4 |
| 52 | #define DT_REFCNT_IDX_RTENTRY	5 |
| 53 | #define DT_REFCNT_IDX_SOCKET	6 |
| 54 | #define DT_REFCNT_IDX_SYNCACHE	7 |
| 55 | #define DT_REFCNT_IDX_TDB	8 |
| 56 | |
| 57 | #endif /* _KERNEL */ |
| 58 | |
| 59 | #endif /* _SYS_REFCNT_H_ */ |