Commit | Line | Data |
---|---|---|
5a693d30 MD |
1 | /* |
2 | * Copyright (C) 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #define _LGPL_SOURCE | |
20 | #include <common/common.h> | |
21 | ||
22 | #include "stream-fd.h" | |
23 | ||
24 | struct stream_fd *stream_fd_create(int fd) | |
25 | { | |
26 | struct stream_fd *sf; | |
27 | ||
28 | sf = zmalloc(sizeof(*sf)); | |
29 | if (!sf) { | |
30 | goto end; | |
31 | } | |
32 | urcu_ref_init(&sf->ref); | |
33 | sf->fd = fd; | |
34 | end: | |
35 | return sf; | |
36 | } | |
37 | ||
38 | void stream_fd_get(struct stream_fd *sf) | |
39 | { | |
40 | urcu_ref_get(&sf->ref); | |
41 | } | |
42 | ||
43 | static void stream_fd_release(struct urcu_ref *ref) | |
44 | { | |
45 | struct stream_fd *sf = caa_container_of(ref, struct stream_fd, ref); | |
46 | int ret; | |
47 | ||
48 | ret = close(sf->fd); | |
49 | if (ret) { | |
50 | PERROR("Error closing stream FD %d", sf->fd); | |
51 | } | |
52 | free(sf); | |
53 | } | |
54 | ||
55 | void stream_fd_put(struct stream_fd *sf) | |
56 | { | |
57 | urcu_ref_put(&sf->ref, stream_fd_release); | |
58 | } |