07d71f34 |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: reffd.h,v 1.1 1999/07/26 23:33:01 mdw Exp $ |
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 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: reffd.h,v $ |
32 | * Revision 1.1 1999/07/26 23:33:01 mdw |
33 | * Infrastructure for the new design. |
34 | * |
35 | */ |
36 | |
37 | #ifndef REFFD_H |
38 | #define REFFD_H |
39 | |
40 | #ifdef __cplusplus |
41 | extern "C" { |
42 | #endif |
43 | |
44 | /*----- Header files ------------------------------------------------------*/ |
45 | |
46 | #include <unistd.h> |
47 | |
48 | #include <mLib/sub.h> |
49 | |
50 | /*----- Data structures ---------------------------------------------------*/ |
51 | |
52 | typedef struct reffd { |
53 | int fd; |
54 | unsigned ref; |
55 | void (*proc)(void */*p*/); |
56 | void *p; |
57 | } reffd; |
58 | |
59 | /*----- Functions provided ------------------------------------------------*/ |
60 | |
61 | /* --- @reffd_init@ --- * |
62 | * |
63 | * Arguments: @int fd@ = file descriptor |
64 | * |
65 | * Returns: Reference-counted file descriptor object. |
66 | * |
67 | * Use: Creates a refcounted file descriptor. |
68 | */ |
69 | |
70 | extern reffd *reffd_init(int /*fd*/); |
71 | |
72 | /* --- @reffd_handler@ --- * |
73 | * |
74 | * Arguments: @reffd *r@ = pointer to reference counted filehandle |
75 | * @void (*proc)(void *p)@ = procedure to call |
76 | * @void *p@ |
77 | * |
78 | * Returns: --- |
79 | * |
80 | * Use: Sets the reference counted file descriptor to call @proc@ |
81 | * when it is no longer required. |
82 | */ |
83 | |
84 | extern void reffd_handler(reffd */*r*/, void (*/*proc*/)(void */*p*/), |
85 | void */*p*/); |
86 | |
87 | /* --- @reffd_inc@ --- * |
88 | * |
89 | * Arguments: @reffd *r@ = pointer to reference counted filehandle |
90 | * |
91 | * Returns: --- |
92 | * |
93 | * Use: Increments the reference count for a file descriptor. |
94 | */ |
95 | |
96 | #define REFFD_INC(r) do { (r)->ref++; } while (0) |
97 | |
98 | extern void reffd_inc(reffd */*r*/); |
99 | |
100 | /* --- @reffd_dec@ --- * |
101 | * |
102 | * Arguments: @reffd *r@ = pointer to reference counted filehandle |
103 | * |
104 | * Returns: --- |
105 | * |
106 | * Use: Decrements the reference count for a file descriptor. |
107 | */ |
108 | |
109 | #define REFFD_DEC(r) do { \ |
110 | reffd *_r = (r); \ |
111 | _r->ref--; \ |
112 | if (_r->ref == 0) { \ |
113 | close(_r->fd); \ |
114 | if (_r->proc) \ |
115 | _r->proc(_r->p); \ |
116 | DESTROY(_r); \ |
117 | } \ |
118 | } while (0) |
119 | |
120 | extern void reffd_dec(reffd */*r*/); |
121 | |
122 | /*----- That's all, folks -------------------------------------------------*/ |
123 | |
124 | #ifdef __cplusplus |
125 | } |
126 | #endif |
127 | |
128 | #endif |