Debian Codegraph @

src/dex-compat-private.h

raw symbols SCIP
1 /* dex-compat-private.h
2 *
3 * Copyright 2022 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 #pragma once
22
23 #include <errno.h>
24
25 #include <glib-object.h>
26
27 #if !GLIB_CHECK_VERSION(2, 72, 0)
28 # ifndef _ISOC11_SOURCE
29 # define _ISOC11_SOURCE
30 # endif
31 # include <stdlib.h>
32 # include <string.h>
33 #endif
34
35 #ifdef G_OS_WIN32
36 # include <windows.h>
37 # include <io.h>
38 #endif
39
40 G_BEGIN_DECLS
41
42 #if !GLIB_CHECK_VERSION(2, 68, 0)
43 static inline gpointer
44 g_memdup2 (gconstpointer mem,
45 gsize byte_size)
46 {
47 g_assert (byte_size <= G_MAXUINT);
48 return g_memdup (mem, byte_size);
49 }
50 #endif
51
52 static inline void
53 _g_source_set_static_name (GSource *source,
54 const char *name)
55 {
56 #if GLIB_CHECK_VERSION(2, 70, 0)
57 g_source_set_static_name (source, name);
58 #else
59 g_source_set_name (source, name);
60 #endif
61 }
62
63 #if !GLIB_CHECK_VERSION(2, 70, 0)
64 # define G_DEFINE_FINAL_TYPE_WITH_CODE(TN, t_n, T_P, _C_) _G_DEFINE_TYPE_EXTENDED_BEGIN (TN, t_n, T_P, 0) {_C_;} _G_DEFINE_TYPE_EXTENDED_END()
65 # define G_TYPE_FLAG_FINAL 0
66 # define G_TYPE_IS_FINAL(type) 1
67 #endif
68
69 #if !GLIB_CHECK_VERSION(2, 73, 0)
70 # define G_DEFINE_ENUM_VALUE(EnumValue, EnumNick) \
71 { EnumValue, #EnumValue, EnumNick }
72 # define G_DEFINE_ENUM_TYPE(TypeName, type_name, ...) \
73 GType \
74 G_PASTE(type_name, _get_type) (void) \
75 { \
76 static gsize g_define_type__static = 0; \
77 if (g_once_init_enter (&g_define_type__static)) { \
78 static const GEnumValue enum_values[] = { \
79 __VA_ARGS__ , \
80 { 0, NULL, NULL }, \
81 }; \
82 GType g_define_type = g_enum_register_static (g_intern_static_string (G_STRINGIFY (TypeName)), enum_values); \
83 g_once_init_leave (&g_define_type__static, g_define_type); \
84 } \
85 return g_define_type__static; \
86 }
87 #endif
88
89 #if !GLIB_CHECK_VERSION(2, 72, 0)
90 static inline gpointer
91 g_aligned_alloc (gsize n_blocks,
92 gsize n_block_bytes,
93 gsize alignment)
94 {
95 gpointer mem = aligned_alloc (alignment, n_blocks * n_block_bytes);
96
97 if (mem == NULL)
98 g_error ("Failed to allocate %"G_GSIZE_FORMAT" bytes",
99 n_blocks * n_block_bytes);
100
101 return mem;
102 }
103 static inline gpointer
104 g_aligned_alloc0 (gsize n_blocks,
105 gsize n_block_bytes,
106 gsize alignment)
107 {
108 gpointer mem = g_aligned_alloc (n_blocks, n_block_bytes, alignment);
109 memset (mem, 0, n_blocks * n_block_bytes);
110 return mem;
111 }
112 static inline void
113 g_aligned_free (gpointer mem)
114 {
115 free (mem);
116 }
117 #endif
118
119 #ifdef G_OS_WIN32
120 static inline gsize
121 pread (int fd,
122 gpointer buf,
123 gsize count,
124 goffset offset)
125 {
126 OVERLAPPED ov = {0};
127 DWORD n_read = 0;
128 HANDLE hFile;
129
130 hFile = (HANDLE)_get_osfhandle (fd);
131
132 ov.OffsetHigh = (offset & 0xFFFFFFFF00000000LL) >> 32;
133 ov.Offset = (offset & 0xFFFFFFFFLL);
134
135 SetLastError (0);
136
137 if (!ReadFile (hFile, buf, count, &n_read, &ov))
138 {
139 int errsv = GetLastError ();
140
141 if (errsv != ERROR_HANDLE_EOF)
142 {
143 errno = errsv;
144 return -1;
145 }
146 }
147
148 return n_read;
149 }
150
151 static inline gsize
152 pwrite (int fd,
153 gconstpointer buf,
154 gsize count,
155 goffset offset)
156 {
157 OVERLAPPED ov = {0};
158 DWORD n_written = 0;
159 HANDLE hFile;
160
161 hFile = (HANDLE)_get_osfhandle (fd);
162
163 ov.OffsetHigh = (offset & 0xFFFFFFFF00000000LL) >> 32;
164 ov.Offset = (offset & 0xFFFFFFFFLL);
165
166 SetLastError (0);
167
168 if (!WriteFile (hFile, buf, count, &n_written, &ov))
169 {
170 errno = GetLastError ();
171 return -1;
172 }
173
174 return n_written;
175 }
176 #endif
177
178 G_END_DECLS