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