Skip to content

Commit 7b537e3

Browse files
authored
[c] overhual the mess in c implementation with macros (#1169)
* unfinshed overhual of c's fwrite/fread etc * done
1 parent 210a9b5 commit 7b537e3

3 files changed

Lines changed: 409 additions & 77 deletions

File tree

include/fast_io_driver/qt_impl/qiodevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ inline Iter read(basic_general_qdevice_io_observer<ch_type, T> qiob, Iter begin,
112112

113113
template <::std::integral ch_type, typename T>
114114
requires(sizeof(ch_type) == 1)
115-
inline ::std::pair<ch_type, bool> try_get(basic_general_qdevice_io_observer<ch_type, T> qiob)
115+
inline ::fast_io::try_get_result<ch_type> try_get(basic_general_qdevice_io_observer<ch_type, T> qiob)
116116
{
117117
char ch;
118118
bool ef{qiob.qdevice->getChar(&ch)};

include/fast_io_legacy_impl/c/done.h

Lines changed: 154 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,78 @@ inline ::std::size_t c_fwrite_unlocked_impl(void const *__restrict begin, ::std:
5555
throw_posix_error(rent._errno);
5656
}
5757
#else
58-
::std::size_t written_count{
59-
#if defined(__CYGWIN__)
60-
my_cygwin_fwrite_unlocked(begin, type_size, count, fp)
61-
#elif (defined(__USE_MISC) || defined(__BSD_VISIBLE)) && (!defined(__BIONIC__) || (defined(__USE_BSD)))
62-
#if !defined(fwrite_unlocked) && defined(__has_builtin)
58+
59+
#pragma push_macro("FAST_IO_FWRITE_UNLOCKED")
60+
#pragma push_macro("FAST_IO_FWRITE")
61+
#pragma push_macro("FAST_IO_PLATFORM_SUPPORTS_FWRITE_UNLOCKED")
62+
#pragma push_macro("FAST_IO_FWRITE_UNLOCKED_IMPL")
63+
64+
#undef FAST_IO_FWRITE_UNLOCKED
65+
#undef FAST_IO_FWRITE
66+
#undef FAST_IO_PLATFORM_SUPPORTS_FWRITE_UNLOCKED
67+
#undef FAST_IO_FWRITE_UNLOCKED_IMPL
68+
69+
#ifdef __has_builtin
6370
#if __has_builtin(__builtin_fwrite_unlocked)
64-
__builtin_fwrite_unlocked(begin, type_size, count, fp)
65-
#else
66-
fwrite_unlocked(begin, type_size, count, fp)
71+
#define FAST_IO_FWRITE_UNLOCKED(...) __builtin_fwrite_unlocked(__VA_ARGS__)
6772
#endif
73+
#endif
74+
#ifndef FAST_IO_FWRITE_UNLOCKED
75+
#if defined(__GLIBC__) || defined(__LLVM_LIBC_TYPES_FILE_H__)
76+
#define FAST_IO_FWRITE_UNLOCKED(...) ::fwrite_unlocked(__VA_ARGS__)
77+
#elif defined(fwrite_unlocked)
78+
#define FAST_IO_FWRITE_UNLOCKED(...) fwrite_unlocked(__VA_ARGS__)
6879
#else
69-
fwrite_unlocked(begin, type_size, count, fp)
80+
#define FAST_IO_FWRITE_UNLOCKED(...) ::fast_io::noexcept_call(::fwrite_unlocked, __VA_ARGS__)
81+
#endif
7082
#endif
71-
#elif !defined(fwrite) && defined(__has_builtin)
83+
84+
#ifdef __has_builtin
7285
#if __has_builtin(__builtin_fwrite)
73-
__builtin_fwrite(begin, type_size, count, fp)
86+
#define FAST_IO_FWRITE(...) __builtin_fwrite(__VA_ARGS__)
87+
#endif
88+
#endif
89+
#ifndef FAST_IO_FWRITE
90+
#if defined(__GLIBC__) || defined(__LLVM_LIBC_TYPES_FILE_H__)
91+
#define FAST_IO_FWRITE(...) ::fwrite(__VA_ARGS__)
92+
#elif defined(fwrite)
93+
#define FAST_IO_FWRITE(...) fwrite(__VA_ARGS__)
7494
#else
75-
fwrite(begin, type_size, count, fp)
95+
#define FAST_IO_FWRITE(...) ::fast_io::noexcept_call(::fwrite, __VA_ARGS__)
96+
#endif
97+
#endif
98+
99+
#if defined(__BIONIC__)
100+
#if defined(__BIONIC_AVAILABILITY_GUARD)
101+
#if __BIONIC_AVAILABILITY_GUARD(28)
102+
#define FAST_IO_PLATFORM_SUPPORTS_FWRITE_UNLOCKED 1
103+
#endif
76104
#endif
105+
#elif defined(__LLVM_LIBC_TYPES_FILE_H__)
106+
#define FAST_IO_PLATFORM_SUPPORTS_FWRITE_UNLOCKED 1
107+
#elif defined(__USE_MISC) || defined(__BSD_VISIBLE)
108+
#define FAST_IO_PLATFORM_SUPPORTS_FWRITE_UNLOCKED 1
109+
#endif
110+
111+
#if defined(__CYGWIN__)
112+
#define FAST_IO_FWRITE_UNLOCKED_IMPL(...) my_cygwin_fwrite_unlocked(__VA_ARGS__)
113+
#elif defined(FAST_IO_PLATFORM_SUPPORTS_FWRITE_UNLOCKED)
114+
#define FAST_IO_FWRITE_UNLOCKED_IMPL(...) FAST_IO_FWRITE_UNLOCKED(__VA_ARGS__)
77115
#else
78-
fwrite(begin, type_size, count, fp)
116+
#define FAST_IO_FWRITE_UNLOCKED_IMPL(...) FAST_IO_FWRITE(__VA_ARGS__)
79117
#endif
80-
};
118+
119+
::std::size_t const written_count{FAST_IO_FWRITE_UNLOCKED_IMPL(begin, type_size, count, fp)};
120+
121+
#pragma pop_macro("FAST_IO_FWRITE_UNLOCKED_IMPL")
122+
#pragma pop_macro("FAST_IO_PLATFORM_SUPPORTS_FWRITE_UNLOCKED")
123+
#pragma pop_macro("FAST_IO_FWRITE")
124+
#pragma pop_macro("FAST_IO_FWRITE_UNLOCKED")
81125
if (!written_count) [[unlikely]]
82126
{
83127
throw_posix_error();
84128
}
129+
85130
#endif
86131
return written_count;
87132
}
@@ -110,29 +155,77 @@ inline ::std::size_t c_fread_unlocked_impl(void *__restrict begin, ::std::size_t
110155
}
111156
}
112157
#else
113-
::std::size_t read_count{
114-
#if defined(__CYGWIN__)
115-
my_cygwin_fread_unlocked(begin, type_size, count, fp)
116-
#elif (defined(__USE_MISC) || defined(__BSD_VISIBLE)) && (!defined(__BIONIC__) || (defined(__USE_BSD)))
117-
#if !defined(fread_unlocked) && defined(__has_builtin)
158+
159+
#pragma push_macro("FAST_IO_FREAD_UNLOCKED")
160+
#pragma push_macro("FAST_IO_FREAD")
161+
#pragma push_macro("FAST_IO_PLATFORM_SUPPORTS_FREAD_UNLOCKED")
162+
#pragma push_macro("FAST_IO_FREAD_UNLOCKED_IMPL")
163+
164+
#undef FAST_IO_FREAD_UNLOCKED
165+
#undef FAST_IO_FREAD
166+
#undef FAST_IO_PLATFORM_SUPPORTS_FREAD_UNLOCKED
167+
#undef FAST_IO_FREAD_UNLOCKED_IMPL
168+
169+
// Define FAST_IO_FREAD_UNLOCKED
170+
#ifdef __has_builtin
118171
#if __has_builtin(__builtin_fread_unlocked)
119-
__builtin_fread_unlocked(begin, type_size, count, fp)
120-
#else
121-
fread_unlocked(begin, type_size, count, fp)
172+
#define FAST_IO_FREAD_UNLOCKED(...) __builtin_fread_unlocked(__VA_ARGS__)
173+
#endif
122174
#endif
175+
#ifndef FAST_IO_FREAD_UNLOCKED
176+
#if defined(__GLIBC__) || defined(__LLVM_LIBC_TYPES_FILE_H__)
177+
#define FAST_IO_FREAD_UNLOCKED(...) ::fread_unlocked(__VA_ARGS__)
178+
#elif defined(fread_unlocked)
179+
#define FAST_IO_FREAD_UNLOCKED(...) fread_unlocked(__VA_ARGS__)
123180
#else
124-
fread_unlocked(begin, type_size, count, fp)
181+
#define FAST_IO_FREAD_UNLOCKED(...) ::fast_io::noexcept_call(::fread_unlocked, __VA_ARGS__)
125182
#endif
126-
#elif !defined(fread) && defined(__has_builtin)
183+
#endif
184+
185+
// Define FAST_IO_FREAD
186+
#ifdef __has_builtin
127187
#if __has_builtin(__builtin_fread)
128-
__builtin_fread(begin, type_size, count, fp)
188+
#define FAST_IO_FREAD(...) __builtin_fread(__VA_ARGS__)
189+
#endif
190+
#endif
191+
#ifndef FAST_IO_FREAD
192+
#if defined(__GLIBC__) || defined(__LLVM_LIBC_TYPES_FILE_H__)
193+
#define FAST_IO_FREAD(...) ::fread(__VA_ARGS__)
194+
#elif defined(fread)
195+
#define FAST_IO_FREAD(...) fread(__VA_ARGS__)
129196
#else
130-
fread(begin, type_size, count, fp)
197+
#define FAST_IO_FREAD(...) ::fast_io::noexcept_call(::fread, __VA_ARGS__)
198+
#endif
199+
#endif
200+
201+
// Detect whether platform supports fread_unlocked
202+
#if defined(__BIONIC__)
203+
#if defined(__BIONIC_AVAILABILITY_GUARD)
204+
#if __BIONIC_AVAILABILITY_GUARD(28)
205+
#define FAST_IO_PLATFORM_SUPPORTS_FREAD_UNLOCKED 1
206+
#endif
131207
#endif
208+
#elif defined(__LLVM_LIBC_TYPES_FILE_H__)
209+
#define FAST_IO_PLATFORM_SUPPORTS_FREAD_UNLOCKED 1
210+
#elif defined(__USE_MISC) || defined(__BSD_VISIBLE)
211+
#define FAST_IO_PLATFORM_SUPPORTS_FREAD_UNLOCKED 1
212+
#endif
213+
214+
// Define the unified dispatch macro
215+
#if defined(__CYGWIN__)
216+
#define FAST_IO_FREAD_UNLOCKED_IMPL(...) my_cygwin_fread_unlocked(__VA_ARGS__)
217+
#elif defined(FAST_IO_PLATFORM_SUPPORTS_FREAD_UNLOCKED)
218+
#define FAST_IO_FREAD_UNLOCKED_IMPL(...) FAST_IO_FREAD_UNLOCKED(__VA_ARGS__)
132219
#else
133-
fread(begin, type_size, count, fp)
220+
#define FAST_IO_FREAD_UNLOCKED_IMPL(...) FAST_IO_FREAD(__VA_ARGS__)
134221
#endif
135-
};
222+
223+
::std::size_t const read_count{FAST_IO_FREAD_UNLOCKED_IMPL(begin, type_size, count, fp)};
224+
225+
#pragma pop_macro("FAST_IO_FREAD_UNLOCKED_IMPL")
226+
#pragma pop_macro("FAST_IO_PLATFORM_SUPPORTS_FREAD_UNLOCKED")
227+
#pragma pop_macro("FAST_IO_FREAD")
228+
#pragma pop_macro("FAST_IO_FREAD_UNLOCKED")
136229
if (read_count == 0) [[unlikely]]
137230
{
138231
if (
@@ -179,18 +272,27 @@ inline ::std::size_t c_fwrite_impl(void const *__restrict begin, ::std::size_t t
179272
throw_posix_error(rent._errno);
180273
}
181274
#else
275+
#pragma push_macro("FAST_IO_FWRITE")
276+
#undef FAST_IO_FWRITE
182277

183-
::std::size_t written_count{
184-
#if !defined(fwrite) && defined(__has_builtin)
278+
#ifdef __has_builtin
185279
#if __has_builtin(__builtin_fwrite)
186-
__builtin_fwrite(begin, type_size, count, fp)
187-
#else
188-
fwrite(begin, type_size, count, fp)
280+
#define FAST_IO_FWRITE(...) __builtin_fwrite(__VA_ARGS__)
281+
#endif
189282
#endif
283+
#ifndef FAST_IO_FWRITE
284+
#if defined(__GLIBC__) || defined(__LLVM_LIBC_TYPES_FILE_H__)
285+
#define FAST_IO_FWRITE(...) ::fwrite(__VA_ARGS__)
286+
#elif defined(fwrite)
287+
#define FAST_IO_FWRITE(...) fwrite(__VA_ARGS__)
190288
#else
191-
fwrite(begin, type_size, count, fp)
289+
#define FAST_IO_FWRITE(...) ::fast_io::noexcept_call(::fwrite, __VA_ARGS__)
192290
#endif
193-
};
291+
#endif
292+
293+
::std::size_t const written_count{FAST_IO_FWRITE(begin, type_size, count, fp)};
294+
295+
#pragma pop_macro("FAST_IO_FWRITE")
194296
if (!written_count) [[unlikely]]
195297
{
196298
throw_posix_error();
@@ -217,17 +319,27 @@ inline ::std::size_t c_fread_impl(void *__restrict begin, ::std::size_t type_siz
217319
}
218320
}
219321
#else
220-
::std::size_t read_count{
221-
#if !defined(fread) && defined(__has_builtin)
322+
#pragma push_macro("FAST_IO_FREAD")
323+
#undef FAST_IO_FREAD
324+
325+
#ifdef __has_builtin
222326
#if __has_builtin(__builtin_fread)
223-
__builtin_fread(begin, type_size, count, fp)
224-
#else
225-
fread(begin, type_size, count, fp)
327+
#define FAST_IO_FREAD(...) __builtin_fread(__VA_ARGS__)
226328
#endif
329+
#endif
330+
#ifndef FAST_IO_FREAD
331+
#if defined(__GLIBC__) || defined(__LLVM_LIBC_TYPES_FILE_H__)
332+
#define FAST_IO_FREAD(...) ::fread(__VA_ARGS__)
333+
#elif defined(fread)
334+
#define FAST_IO_FREAD(...) fread(__VA_ARGS__)
227335
#else
228-
fread(begin, type_size, count, fp)
336+
#define FAST_IO_FREAD(...) ::fast_io::noexcept_call(::fread, __VA_ARGS__)
229337
#endif
230-
};
338+
#endif
339+
340+
::std::size_t const read_count{FAST_IO_FREAD(begin, type_size, count, fp)};
341+
342+
#pragma pop_macro("FAST_IO_FREAD")
231343
if (read_count == 0) [[unlikely]]
232344
{
233345
if (

0 commit comments

Comments
 (0)