Debian Codegraph @

src/libsysprof/sysprof-fd.c

raw
1 /* sysprof-fd.c
2 *
3 * Copyright 2024 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #include "config.h"
22
23 #include <unistd.h>
24
25 #include "sysprof-fd-private.h"
26
27 int
28 sysprof_fd_peek (const SysprofFD *fd)
29 {
30 if (fd == NULL)
31 return -1;
32
33 return *(int *)fd;
34 }
35
36 int
37 sysprof_fd_steal (SysprofFD *fd)
38 {
39 if (fd == NULL)
40 return -1;
41
42 return g_steal_fd ((int *)fd);
43 }
44
45 void
46 sysprof_fd_free (SysprofFD *fd)
47 {
48 int real = sysprof_fd_steal (fd);
49 if (real != -1)
50 close (real);
51 g_free (fd);
52 }
53
54 SysprofFD *
55 sysprof_fd_dup (const SysprofFD *fd)
56 {
57 int real = sysprof_fd_peek (fd);
58
59 if (real == -1)
60 return NULL;
61
62 real = dup (real);
63
64 return g_memdup2 (&real, sizeof real);
65 }
66
67 G_DEFINE_BOXED_TYPE (SysprofFD, sysprof_fd, sysprof_fd_dup, sysprof_fd_free)