-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfduserdata.3.html
229 lines (168 loc) · 4.23 KB
/
fduserdata.3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
Content-type: text/html; charset=UTF-8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of FDUSERDATA</TITLE>
</HEAD><BODY>
<H1>FDUSERDATA</H1>
Section: Library Functions Manual (3)<BR>Updated: January 2023<BR><A HREF="#index">Index</A>
<A HREF="/#/man/index">Return to Main Contents</A><HR>
<A NAME="lbAB"> </A>
<H2>NAME</H2>
<P>
fduserdata_create, fduserdata_destroy, fduserdata_destroy_cb,
fduserdata_new, fduserdata_get, fduserdata_put, fduserdata_del -
associate file descriptors with user defined data
<A NAME="lbAC"> </A>
<H2>SYNOPSIS</H2>
<P>
#include *fduserdata.h*
<P>
FDUSERDATA *fduserdata_create(int size);
<P>
void fduserdata_destroy(FDUSERDATA *fdtable);
<P>
typedef void (*fduserdata_destr_cb_t)(int fd, void *data, void *arg);
<P>
<P>
void fduserdata_destroy_cb(FDUSERDATA *fdtable, fduserdata_destr_cb_t
callback, void *arg);
<P>
void *fduserdata_new(FDUSERDATA *fdtable, int
fd, type);
<P>
void *fduserdata_get(FDUSERDATA *fdtable, int
fd);
<P>
void fduserdata_put(void *data);
<P>
int fduserdata_del(void *data);
<A NAME="lbAD"> </A>
<H2>DESCRIPTION</H2>
<P>
This library permits one to associate file descriptors with user defined
data, more precisely it manages a data structure whose searching key is
a file descriptor.
<P>
fduserdata_create and fduserdata_destroy are the
constructor and destructor of the data structure, respectively.
The data structure has been implemented as a hash table, the argument
size of fduserdata_create is the size of the hash
array.
When size is zero the hash array has its default size (64).
<P>
fduserdata_destroy_cb is an alternative destructor which calls
the function callback for each element still in the data
structure.
<P>
fduserdata_new creates a new element.
It is a macro: type is the type of the user data.
<P>
fduserdata_get search the user data associated to the
fd.
<P>
Both fduserdata_new and fduserdata_get lock the
access to the element, so that fduserdata is thread safe.
fduserdata_put unlocks the element and makes it available for
further requests.
<P>
fduserdata_del can be used instead of fduserdata_put
to delete the element.
<A NAME="lbAE"> </A>
<H2>RETURN VALUE</H2>
<P>
fduserdata_create returns the descriptor of the data structure
(NULL in case of error).
<P>
fduserdata_new returns the element of type type just
created (NULL in case of error).
<P>
fduserdata_get returns the element or NULL if no data
corresponds to the file descriptor fd.
<P>
fduserdata_del On success, zero is returned.
On error, -1 is returned.
<P>
On error, errno is set appropriately.
<A NAME="lbAF"> </A>
<H2>EXAMPLE</H2>
<P>
fduserdata uses a trivial hash table, the optional arg is the size of
the hash table: default value = 64
<DL COMPACT>
<DT><DD>
<PRE>
FDUSERDATA table = fduserdata_create(0);
struct mydata {
// fd data fields ...
};
</PRE>
</DL>
<P>
create a struct mydata for the file descriptor fd.
<DL COMPACT>
<DT><DD>
<PRE>
struct mydata *data = fduserdata_new(table, fd, struct mydata);
</PRE>
</DL>
<P>
....
set user defined data (data-*fields)
<DL COMPACT>
<DT><DD>
<PRE>
fduserdata_put(data);
</PRE>
</DL>
<P>
search for data there is mutual exclusion between new/put, get/put (or
new/del, get/del) so do not insert time consuming or blocking ops.
<DL COMPACT>
<DT><DD>
<PRE>
struct mydata *fddata = fduserdata_get(table, fd);
if (fddata) {
</PRE>
</DL>
<P>
...
read/update user defined data (data->fields) (use fduserdata_del instead
of fduserdata_put to delete the element)
<DL COMPACT>
<DT><DD>
<PRE>
fduserdata_put(data);
}
</PRE>
</DL>
<P>
at the end...
when table is no longer required
<DL COMPACT>
<DT><DD>
<PRE>
fduserdata_destroy(table);
</PRE>
</DL>
<A NAME="lbAG"> </A>
<H2>AUTHOR</H2>
<P>
VirtualSquare.
Project leader: Renzo Davoli.
<P>
<HR>
<A NAME="index"> </A><H2>Index</H2>
<DL>
<DT><A HREF="#lbAB">NAME</A><DD>
<DT><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT><A HREF="#lbAF">EXAMPLE</A><DD>
<DT><A HREF="#lbAG">AUTHOR</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 15:22:07 GMT, November 27, 2023
</BODY>
</HTML>