Commit | Line | Data |
---|---|---|
7591bab1 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 | ||
7591bab1 MD |
18 | #define _LGPL_SOURCE |
19 | #include <common/common.h> | |
20 | ||
21 | #include "stream-fd.h" | |
22 | ||
23 | struct stream_fd *stream_fd_create(int fd) | |
24 | { | |
25 | struct stream_fd *sf; | |
26 | ||
27 | sf = zmalloc(sizeof(*sf)); | |
28 | if (!sf) { | |
29 | goto end; | |
30 | } | |
31 | urcu_ref_init(&sf->ref); | |
32 | sf->fd = fd; | |
33 | end: | |
34 | return sf; | |
35 | } | |
36 | ||
37 | void stream_fd_get(struct stream_fd *sf) | |
38 | { | |
39 | urcu_ref_get(&sf->ref); | |
40 | } | |
41 | ||
42 | static void stream_fd_release(struct urcu_ref *ref) | |
43 | { | |
44 | struct stream_fd *sf = caa_container_of(ref, struct stream_fd, ref); | |
45 | int ret; | |
46 | ||
47 | ret = close(sf->fd); | |
48 | if (ret) { | |
49 | PERROR("Error closing stream FD %d", sf->fd); | |
50 | } | |
51 | free(sf); | |
52 | } | |
53 | ||
54 | void stream_fd_put(struct stream_fd *sf) | |
55 | { | |
5a72475c FD |
56 | if (!sf) { |
57 | return; | |
58 | } | |
7591bab1 MD |
59 | urcu_ref_put(&sf->ref, stream_fd_release); |
60 | } |