07d71f34 |
1 | /* -*-c-*- |
2 | * |
07d71f34 |
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 | |
07d71f34 |
27 | /*----- Header files ------------------------------------------------------*/ |
28 | |
29 | #include <stdio.h> |
30 | #include <stdlib.h> |
31 | #include <string.h> |
32 | |
33 | #include <sys/types.h> |
34 | #include <unistd.h> |
35 | |
36 | #include <mLib/sub.h> |
37 | |
38 | #include "reffd.h" |
39 | |
40 | /*----- Main code ---------------------------------------------------------*/ |
41 | |
42 | /* --- @reffd_init@ --- * |
43 | * |
44 | * Arguments: @int fd@ = file descriptor |
45 | * |
46 | * Returns: Reference-counted file descriptor object. |
47 | * |
48 | * Use: Creates a refcounted file descriptor. |
49 | */ |
50 | |
51 | reffd *reffd_init(int fd) |
52 | { |
53 | reffd *r = CREATE(reffd); |
54 | r->fd = fd; |
55 | r->ref = 1; |
56 | r->proc = 0; |
57 | r->p = 0; |
58 | return (r); |
59 | } |
60 | |
61 | /* --- @reffd_handler@ --- * |
62 | * |
63 | * Arguments: @reffd *r@ = pointer to reference counted filehandle |
64 | * @void (*proc)(void *p)@ = procedure to call |
65 | * @void *p@ |
66 | * |
67 | * Returns: --- |
68 | * |
69 | * Use: Sets the reference counted file descriptor to call @proc@ |
70 | * when it is no longer required. |
71 | */ |
72 | |
73 | void reffd_handler(reffd *r, void (*proc)(void *p), void *p) |
74 | { |
75 | r->proc = proc; |
76 | r->p = p; |
77 | } |
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 | void reffd_inc(reffd *r) { REFFD_INC(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 | void reffd_dec(reffd *r) { REFFD_DEC(r); } |
100 | |
101 | /*----- That's all, folks -------------------------------------------------*/ |