1*af07f394Sdlg.\" $OpenBSD: srpl_rc_init.9,v 1.14 2016/11/21 07:11:13 dlg Exp $ 2a81ac0ceSdlg.\" 3a81ac0ceSdlg.\" Copyright (c) 2015 David Gwynne <dlg@openbsd.org> 4a81ac0ceSdlg.\" 5a81ac0ceSdlg.\" Permission to use, copy, modify, and distribute this software for any 6a81ac0ceSdlg.\" purpose with or without fee is hereby granted, provided that the above 7a81ac0ceSdlg.\" copyright notice and this permission notice appear in all copies. 8a81ac0ceSdlg.\" 9a81ac0ceSdlg.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10a81ac0ceSdlg.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11a81ac0ceSdlg.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12a81ac0ceSdlg.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13a81ac0ceSdlg.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14a81ac0ceSdlg.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15a81ac0ceSdlg.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16a81ac0ceSdlg.\" 17de209267Sdlg.Dd $Mdocdate: November 21 2016 $ 18a81ac0ceSdlg.Dt SRPL_RC_INIT 9 19a81ac0ceSdlg.Os 20a81ac0ceSdlg.Sh NAME 21a81ac0ceSdlg.Nm srpl_rc_init , 22ce2f93b3Smpi.Nm SRPL_HEAD , 23ce2f93b3Smpi.Nm SRPL_ENTRY , 24a81ac0ceSdlg.Nm SRPL_INIT , 25f8e0b8c2Smpi.Nm SRPL_FIRST , 26a81ac0ceSdlg.Nm SRPL_NEXT , 27f8e0b8c2Smpi.Nm SRPL_FOLLOW , 28a81ac0ceSdlg.Nm SRPL_FOREACH , 29a81ac0ceSdlg.Nm SRPL_LEAVE , 30b10ba340Sjmc.Nm SRPL_RC_INITIALIZER 31a81ac0ceSdlg.Nd singly-linked shared reference pointer list 32a81ac0ceSdlg.Sh SYNOPSIS 33a81ac0ceSdlg.In sys/srp.h 34a81ac0ceSdlg.Vt struct srpl_rc; 35a81ac0ceSdlg.Ft void 36a81ac0ceSdlg.Fo "srpl_rc_init" 37a81ac0ceSdlg.Fa "struct srpl_rc *rc" 38a81ac0ceSdlg.Fa "void (*ref)(void *, void *)" 39a81ac0ceSdlg.Fa "void (*unref)(void *, void *)" 40a81ac0ceSdlg.Fa "void *ctx" 41a81ac0ceSdlg.Fc 42ce2f93b3Smpi.Fn SRPL_HEAD "HEADNAME" "TYPE" 43ce2f93b3Smpi.Fn SRPL_ENTRY "TYPE" 44*af07f394Sdlg.Ft void 45ce2f93b3Smpi.Fn "SRPL_INIT" "SRPL_HEAD *sl" 46a81ac0ceSdlg.Ft void * 47f8e0b8c2Smpi.Fn "SRPL_FIRST" "struct srp_ref *sr" "SRPL_HEAD *sl" 48a81ac0ceSdlg.Ft void * 49ca268887Sdlg.Fn "SRPL_NEXT" "struct srp_ref *sr" "struct TYPE *listelm" "FIELDNAME" 50f8e0b8c2Smpi.Ft void * 51f8e0b8c2Smpi.Fn "SRPL_FOLLOW" "struct srp_ref *sr" "struct TYPE *listelm" "FIELDNAME" 52a81ac0ceSdlg.Fo "SRPL_FOREACH" 53a81ac0ceSdlg.Fa "VARNAME" 54ca268887Sdlg.Fa "struct srp_ref *sr" 55ce2f93b3Smpi.Fa "SRPL_HEAD *sl" 56a81ac0ceSdlg.Fa "FIELDNAME" 57a81ac0ceSdlg.Fc 58*af07f394Sdlg.Ft void 59*af07f394Sdlg.Fn "SRPL_LEAVE" "struct srp_ref *sr" 6035de6c50Sdlg.Fo "SRPL_RC_INITIALIZER" 6135de6c50Sdlg.Fa "void (*ref)(void *, void *)" 6235de6c50Sdlg.Fa "void (*unref)(void *, void *)" 6335de6c50Sdlg.Fa "void *ctx" 6435de6c50Sdlg.Fc 65a81ac0ceSdlg.Sh DESCRIPTION 666e41c69bSmpiThe SRPL list 67a81ac0ceSdlgmacros build a linked list on top of shared reference pointers. 68a81ac0ceSdlgThis allows concurrent traversal of a linked list and access to the 69a81ac0ceSdlgitems on the list. 70a81ac0ceSdlg.Pp 71a81ac0ceSdlgSRP lists are a generic type represented by a 72ce2f93b3Smpi.Vt SRPL_HEAD . 73a81ac0ceSdlgThe elements inserted into the list must be structures that contain a 74ce2f93b3Smpi.Vt SRPL_ENTRY 75a81ac0ceSdlgfield. 76a81ac0ceSdlgFurther, the elements must also support reference counting as 77a81ac0ceSdlginsertion and removal operations can cause items to be temporarily 78a81ac0ceSdlgreferenced by multiple SRPs within the list at the same time. 79a81ac0ceSdlg.Pp 80a81ac0ceSdlg.Fn srpl_rc_init 81a81ac0ceSdlginitialises the SRP list refcounts 82a81ac0ceSdlg.Fa rc 83a81ac0ceSdlgstructure so it can be used to manage the reference counts on 84a81ac0ceSdlgelements in the list. 85a81ac0ceSdlgThe insertion or removal of an element in an SRP list will increment 86a81ac0ceSdlgthe reference counts on elements within the list via calls to 87a81ac0ceSdlg.Fa ref . 88a81ac0ceSdlgAs these references are released by the SRP infrastructure, the 89a81ac0ceSdlgreference counts will be decremented by calls to 90a81ac0ceSdlg.Fa unref . 91a81ac0ceSdlg.Fa unref 92a81ac0ceSdlgis also responsible for freeing the element when the reference count 93a81ac0ceSdlgreaches 0. 94a81ac0ceSdlgBoth 95a81ac0ceSdlg.Fa ref 96a81ac0ceSdlgand 97a81ac0ceSdlg.Fa unref 98a81ac0ceSdlgwill be called with 99a81ac0ceSdlg.Fa ctx 100a81ac0ceSdlgas their first argument and a pointer to the element as their second 101a81ac0ceSdlgargument. 102a81ac0ceSdlg.Pp 103a81ac0ceSdlg.Fn SRPL_INIT 104a81ac0ceSdlginitialises the SRP list 105a81ac0ceSdlg.Fa sl 106a81ac0ceSdlgto an empty state. 107a81ac0ceSdlg.Pp 108f8e0b8c2Smpi.Fn SRPL_FIRST 109f8e0b8c2Smpiaccesses the first element in the SRP list 110f8e0b8c2Smpi.Fa sl 111f8e0b8c2Smpiand holds its reference via 112ca268887Sdlg.Fa sr . 113a81ac0ceSdlg.Pp 114a81ac0ceSdlg.Fn SRPL_NEXT 115a81ac0ceSdlgaccesses the element in the SRP list after 116f8e0b8c2Smpi.Fa listelm 117f8e0b8c2Smpiand holds its reference via 118f8e0b8c2Smpi.Fa sr . 119f8e0b8c2Smpi.\".Pp 120f8e0b8c2Smpi.\"Every call to 121f8e0b8c2Smpi.\".Fn SRPL_FIRST 122f8e0b8c2Smpi.\"must have a corresponding call to 123f8e0b8c2Smpi.\".Fn SRPL_LEAVE 124f8e0b8c2Smpi.\"to release references to the list and its elements. 125f8e0b8c2Smpi.Pp 126f8e0b8c2Smpi.Fn SRPL_FOLLOW 127f8e0b8c2Smpiaccesses the element in the SRP list after 128f8e0b8c2Smpi.Fa listelm 129de209267Sdlgand swaps the previous reference held via 130f8e0b8c2Smpi.Fa sr 131f8e0b8c2Smpifor the reference of the newly accessed item. 132a81ac0ceSdlg.Pp 133a81ac0ceSdlg.Fn SRPL_FOREACH 134a81ac0ceSdlgcreates a loop for traversing the list. 135a81ac0ceSdlgEvery call to 136a81ac0ceSdlg.Fn SRPL_FOREACH 137a81ac0ceSdlgmust have a corresponding call to 138a81ac0ceSdlg.Fn SRPL_LEAVE 139a81ac0ceSdlgto release references to the list and its elements. 140a81ac0ceSdlg.Pp 141a81ac0ceSdlg.Fn SRPL_LEAVE 142a81ac0ceSdlgreleases references to the list and its elements held by previous 143a81ac0ceSdlgcalls to 144f8e0b8c2Smpi.Fn SRPL_FIRST , 145ca268887Sdlg.Fn SRPL_NEXT , 146f8e0b8c2Smpi.Fn SRPL_FOLLOW , 147a81ac0ceSdlgor 148a81ac0ceSdlg.Fn SRPL_FOREACH . 149de209267Sdlg.Pp 150a81ac0ceSdlgAn srpl_rc declaration can be initialised with the 151a81ac0ceSdlg.Fn SRPL_RC_INITIALIZER 152a81ac0ceSdlgmacro. 153a81ac0ceSdlg.Sh CONTEXT 154a81ac0ceSdlg.Fn SRPL_INIT , 155f8e0b8c2Smpi.Fn SRPL_FIRST , 156a81ac0ceSdlg.Fn SRPL_NEXT , 157f8e0b8c2Smpi.Fn SRPL_FOLLOW , 158a81ac0ceSdlg.Fn SRPL_FOREACH , 159a81ac0ceSdlgand 160a81ac0ceSdlg.Fn SRPL_LEAVE 161a81ac0ceSdlgmay be called during autoconf, from process context, or from interrupt 162a81ac0ceSdlgcontext. 163a81ac0ceSdlg.Pp 164a81ac0ceSdlg.Fn srpl_rc_init , 165a81ac0ceSdlgmay be called during autoconf or from process context. 166a81ac0ceSdlg.Sh RETURN VALUES 167f8e0b8c2Smpi.Fn SRPL_FIRST , 168a81ac0ceSdlg.Fn SRPL_NEXT , 169a81ac0ceSdlgand 1706e41c69bSmpi.Fn SRPL_FOLLOW 171a81ac0ceSdlgreturn a pointer to elements in the SRP list, or 172a81ac0ceSdlg.Dv NULL 173a81ac0ceSdlgif there are no more elements. 174de209267Sdlg.Sh SEE ALSO 175de209267Sdlg.Xr SRPL_FIRST_LOCKED 9 176a81ac0ceSdlg.Sh HISTORY 177a81ac0ceSdlgThe srp API was originally written by 178a81ac0ceSdlg.An Jonathan Matthew Aq Mt jmatthew@openbsd.org 179a81ac0ceSdlgand 180a81ac0ceSdlg.An David Gwynne Aq Mt dlg@openbsd.org . 181a81ac0ceSdlgThe SRP list API first appeared in 182a81ac0ceSdlg.Ox 5.9 . 183