build: Rewrite the build system to be nicer.
[fwd] / reffd.h
1 /* -*-c-*-
2 *
3 * Reference counted file descriptors
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the `fw' port forwarder.
11 *
12 * `fw' is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * `fw' is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with `fw'; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #ifndef REFFD_H
28 #define REFFD_H
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /*----- Header files ------------------------------------------------------*/
35
36 #include <unistd.h>
37
38 #include <mLib/sub.h>
39
40 /*----- Data structures ---------------------------------------------------*/
41
42 typedef struct reffd {
43 int fd;
44 unsigned ref;
45 void (*proc)(void */*p*/);
46 void *p;
47 } reffd;
48
49 /*----- Functions provided ------------------------------------------------*/
50
51 /* --- @reffd_init@ --- *
52 *
53 * Arguments: @int fd@ = file descriptor
54 *
55 * Returns: Reference-counted file descriptor object.
56 *
57 * Use: Creates a refcounted file descriptor.
58 */
59
60 extern reffd *reffd_init(int /*fd*/);
61
62 /* --- @reffd_handler@ --- *
63 *
64 * Arguments: @reffd *r@ = pointer to reference counted filehandle
65 * @void (*proc)(void *p)@ = procedure to call
66 * @void *p@
67 *
68 * Returns: ---
69 *
70 * Use: Sets the reference counted file descriptor to call @proc@
71 * when it is no longer required.
72 */
73
74 extern void reffd_handler(reffd */*r*/, void (*/*proc*/)(void */*p*/),
75 void */*p*/);
76
77 /* --- @reffd_inc@ --- *
78 *
79 * Arguments: @reffd *r@ = pointer to reference counted filehandle
80 *
81 * Returns: ---
82 *
83 * Use: Increments the reference count for a file descriptor.
84 */
85
86 #define REFFD_INC(r) do { (r)->ref++; } while (0)
87
88 extern void reffd_inc(reffd */*r*/);
89
90 /* --- @reffd_dec@ --- *
91 *
92 * Arguments: @reffd *r@ = pointer to reference counted filehandle
93 *
94 * Returns: ---
95 *
96 * Use: Decrements the reference count for a file descriptor.
97 */
98
99 #define REFFD_DEC(r) do { \
100 reffd *_r = (r); \
101 _r->ref--; \
102 if (_r->ref == 0) { \
103 close(_r->fd); \
104 if (_r->proc) \
105 _r->proc(_r->p); \
106 DESTROY(_r); \
107 } \
108 } while (0)
109
110 extern void reffd_dec(reffd */*r*/);
111
112 /*----- That's all, folks -------------------------------------------------*/
113
114 #ifdef __cplusplus
115 }
116 #endif
117
118 #endif