misc: tst-poll: Proper synchronize with child before sending the signal
[glibc.git] / include / printf_buffer.h
blobeff8e3413a63e45344a4db8aa1db93d451c36d91
1 /* Multibyte and wide buffers for implementing printf-related functions.
2 Copyright (C) 2022-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but 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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* The naming of the multibyte and wide variants is intentionally
20 consistent, so that it is possible to use the Xprintf macro in
21 stdio-common/printf_buffer-char.h and
22 stdio-common/printf_buffer-wchar_t.h to select between them in
23 type-generic code. */
25 #ifndef PRINTF_BUFFER_H
26 #define PRINTF_BUFFER_H
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <sys/types.h>
31 #include <wchar.h>
33 /* <printf_buffer_as_file.h> introduces a way to use struct
34 __printf_buffer objects from FILE * streams. To avoid storing a
35 function pointer (or vtable pointer) in struct __printf_buffer
36 (which would defeat libio vtable hardening), a switch statement
37 over the different flush implementations is used to implement
38 __printf_buffer_flush.
40 __printf_buffer_mode_failed is special: it is the sticky failure
41 indicator. Unlike struct alloc_buffer, this is not folded into
42 write_ptr, so that snprintf and other string-writing functions can
43 discover the end of the string even in the error case, to be able
44 to add the null terminator. */
45 enum __printf_buffer_mode
47 __printf_buffer_mode_failed,
48 __printf_buffer_mode_sprintf,
49 __printf_buffer_mode_snprintf,
50 __printf_buffer_mode_sprintf_chk,
51 __printf_buffer_mode_to_file,
52 __printf_buffer_mode_asprintf,
53 __printf_buffer_mode_dprintf,
54 __printf_buffer_mode_strfmon,
55 __printf_buffer_mode_fp, /* For __printf_fp_l_buffer. */
56 __printf_buffer_mode_fp_to_wide, /* For __wprintf_fp_l_buffer. */
57 __printf_buffer_mode_fphex_to_wide, /* For __wprintf_fphex_l_buffer. */
58 __printf_buffer_mode_obstack, /* For __printf_buffer_flush_obstack. */
61 /* Buffer for fast character writing with overflow handling.
62 Typically embedded in another struct with further data that is used
63 by the flush function. */
64 struct __printf_buffer
66 /* These pointer members follow FILE streams. write_ptr and
67 write_end must be initialized to cover the target buffer. See
68 __printf_buffer_init.
70 Data can be written directly to *write_ptr while write_ptr !=
71 write_end, and write_ptr can be advanced accordingly. Note that
72 is not possible to use the apparently-unused part of the buffer
73 as scratch space because sprintf (and snprintf, but that is a bit
74 iffy) must only write the minimum number of characters produced
75 by the format string and its arguments.
77 write_base must be initialized to be equal to write_ptr. The
78 framework uses this pointer to compute the total number of
79 written bytes, together with the written field. See
80 __printf_buffer_done.
82 write_base and write_end are only read by the generic functions
83 after initialization, only the flush implementation called from
84 __printf_buffer_flush might change these pointers. See the
85 comment on Xprintf (buffer_do_flush) in Xprintf_buffer_flush.c
86 for details regarding the flush operation. */
87 char *write_base;
88 char *write_ptr;
89 char *write_end;
91 /* Number of characters written so far (excluding the current
92 buffer). Potentially updated on flush. The actual number of
93 written bytes also includes the unflushed-but-written buffer
94 part, write_ptr - write_base. A 64-bit value is used to avoid
95 the need for overflow checks. */
96 uint64_t written;
98 /* Identifies the flush callback. */
99 enum __printf_buffer_mode mode;
102 /* Marks the buffer as failed, so that __printf_buffer_has_failed
103 returns true and future flush operations are no-ops. */
104 static inline void
105 __printf_buffer_mark_failed (struct __printf_buffer *buf)
107 buf->mode = __printf_buffer_mode_failed;
110 /* Returns true if the sticky error indicator of the buffer has been
111 set to failed. */
112 static inline bool __attribute_warn_unused_result__
113 __printf_buffer_has_failed (struct __printf_buffer *buf)
115 return buf->mode == __printf_buffer_mode_failed;
118 /* Initialization of a buffer, using the memory region from [BASE,
119 END) as the initial buffer contents. */
120 static inline void
121 __printf_buffer_init_end (struct __printf_buffer *buf, char *base, char *end,
122 enum __printf_buffer_mode mode)
124 buf->write_base = base;
125 buf->write_ptr = base;
126 buf->write_end = end;
127 buf->written = 0;
128 buf->mode = mode;
131 /* Initialization of a buffer, using the memory region from [BASE, BASE +LEN)
132 as the initial buffer contents. LEN can be zero. */
133 static inline void
134 __printf_buffer_init (struct __printf_buffer *buf, char *base, size_t len,
135 enum __printf_buffer_mode mode)
137 __printf_buffer_init_end (buf, base, base + len, mode);
140 /* Called by printf_buffer_putc for a full buffer. */
141 void __printf_buffer_putc_1 (struct __printf_buffer *buf, char ch)
142 attribute_hidden;
144 /* Writes CH to BUF. */
145 static inline void
146 __printf_buffer_putc (struct __printf_buffer *buf, char ch)
148 if (buf->write_ptr != buf->write_end)
149 *buf->write_ptr++ = ch;
150 else
151 __printf_buffer_putc_1 (buf, ch);
154 /* Writes COUNT repeats of CH to BUF. */
155 void __printf_buffer_pad_1 (struct __printf_buffer *buf,
156 char ch, size_t count) attribute_hidden;
158 /* __printf_buffer_pad with fast path for no padding. COUNT is
159 ssize_t to accommodate signed uses in printf and elsewhere. */
160 static inline void
161 __printf_buffer_pad (struct __printf_buffer *buf, char ch, ssize_t count)
163 if (count > 0)
164 __printf_buffer_pad_1 (buf, ch, count);
167 /* Write COUNT bytes starting at S to BUF. S must not overlap with
168 the internal buffer. */
169 void __printf_buffer_write (struct __printf_buffer *buf, const char *s,
170 size_t count) attribute_hidden;
172 /* Write S to BUF. S must not overlap with the internal buffer. */
173 void __printf_buffer_puts_1 (struct __printf_buffer *buf, const char *s)
174 attribute_hidden;
176 static inline void
177 __printf_buffer_puts (struct __printf_buffer *buf, const char *s)
179 if (__builtin_constant_p (__builtin_strlen (s)))
180 __printf_buffer_write (buf, s, __builtin_strlen (s));
181 else
182 __printf_buffer_puts_1 (buf, s);
185 /* Returns the number of bytes written through the buffer, or -1 if
186 there was an error (that is, __printf_buffer_has_failed (BUF) is true).
188 The number of written bytes includes pending bytes in the buffer
189 (between BUF->write_base and BUF->write_ptr).
191 If the number is larger than INT_MAX, returns -1 and sets errno to
192 EOVERFLOW. This function does not flush the buffer. If the caller
193 needs the side effect of flushing, it has to do this
194 separately. */
195 int __printf_buffer_done (struct __printf_buffer *buf) attribute_hidden;
197 /* Internally used to call the flush function. This can be called
198 explicitly for certain modes to flush the buffer prematuraly. In
199 such cases, it is often the case that the buffer mode is statically
200 known, and the flush implementation can be called directly. */
201 bool __printf_buffer_flush (struct __printf_buffer *buf) attribute_hidden;
203 /* Wide version of struct __printf_buffer follows. */
205 enum __wprintf_buffer_mode
207 __wprintf_buffer_mode_failed,
208 __wprintf_buffer_mode_swprintf,
209 __wprintf_buffer_mode_to_file,
212 struct __wprintf_buffer
214 wchar_t *write_base;
215 wchar_t *write_ptr;
216 wchar_t *write_end;
217 uint64_t written;
218 enum __wprintf_buffer_mode mode;
221 static inline void
222 __wprintf_buffer_mark_failed (struct __wprintf_buffer *buf)
224 buf->mode = __wprintf_buffer_mode_failed;
227 static inline bool __attribute_warn_unused_result__
228 __wprintf_buffer_has_failed (struct __wprintf_buffer *buf)
230 return buf->mode == __wprintf_buffer_mode_failed;
233 static inline void
234 __wprintf_buffer_init (struct __wprintf_buffer *buf,
235 wchar_t *base, size_t len,
236 enum __wprintf_buffer_mode mode)
238 buf->write_base = base;
239 buf->write_ptr = base;
240 buf->write_end = base + len;
241 buf->written = 0;
242 buf->mode = mode;
245 void __wprintf_buffer_putc_1 (struct __wprintf_buffer *buf, wchar_t ch)
246 attribute_hidden;
248 static inline void
249 __wprintf_buffer_putc (struct __wprintf_buffer *buf, wchar_t ch)
251 if (buf->write_ptr != buf->write_end)
252 *buf->write_ptr++ = ch;
253 else
254 __wprintf_buffer_putc_1 (buf, ch);
257 void __wprintf_buffer_pad_1 (struct __wprintf_buffer *buf,
258 wchar_t ch, size_t count) attribute_hidden;
260 static inline void
261 __wprintf_buffer_pad (struct __wprintf_buffer *buf, char ch, ssize_t count)
263 if (count > 0)
264 __wprintf_buffer_pad_1 (buf, ch, count);
267 void __wprintf_buffer_write (struct __wprintf_buffer *buf, const wchar_t *s,
268 size_t count) attribute_hidden;
270 void __wprintf_buffer_puts (struct __wprintf_buffer *buf, const wchar_t *s)
271 attribute_hidden;
273 int __wprintf_buffer_done (struct __wprintf_buffer *buf) attribute_hidden;
275 bool __wprintf_buffer_flush (struct __wprintf_buffer *buf) attribute_hidden;
277 /* Type-generic convenience macros. They are useful if
278 printf_buffer-char.h or printf_buffer-wchar_t.h is included as
279 well. */
281 #define Xprintf_buffer Xprintf (buffer)
282 #define Xprintf_buffer_done Xprintf (buffer_done)
283 #define Xprintf_buffer_flush Xprintf (buffer_flush)
284 #define Xprintf_buffer_has_failed Xprintf (buffer_has_failed)
285 #define Xprintf_buffer_mark_failed Xprintf (buffer_mark_failed)
286 #define Xprintf_buffer_pad Xprintf (buffer_pad)
287 #define Xprintf_buffer_putc Xprintf (buffer_putc)
288 #define Xprintf_buffer_puts Xprintf (buffer_puts)
289 #define Xprintf_buffer_write Xprintf (buffer_write)
291 /* Commonly used buffers. */
293 struct __printf_buffer_snprintf
295 struct __printf_buffer base;
296 #define PRINTF_BUFFER_SIZE_DISCARD 128
297 char discard[PRINTF_BUFFER_SIZE_DISCARD]; /* Used in counting mode. */
300 /* Sets up [BUFFER, BUFFER + LENGTH) as the write target. If LENGTH
301 is positive, also writes a NUL byte to *BUFFER. */
302 void __printf_buffer_snprintf_init (struct __printf_buffer_snprintf *,
303 char *buffer, size_t length)
304 attribute_hidden;
306 /* Add the null terminator after everything has been written. The
307 return value is the one expected by printf (see __printf_buffer_done). */
308 int __printf_buffer_snprintf_done (struct __printf_buffer_snprintf *)
309 attribute_hidden;
311 /* Flush function implementations follow. They are called from
312 __printf_buffer_flush. Generic code should not call these flush
313 functions directly. Some modes have inline implementations. */
315 void __printf_buffer_flush_snprintf (struct __printf_buffer_snprintf *)
316 attribute_hidden;
317 struct __printf_buffer_to_file;
318 void __printf_buffer_flush_to_file (struct __printf_buffer_to_file *)
319 attribute_hidden;
320 struct __printf_buffer_asprintf;
321 void __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *)
322 attribute_hidden;
323 struct __printf_buffer_dprintf;
324 void __printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *)
325 attribute_hidden;
326 struct __printf_buffer_fp;
327 void __printf_buffer_flush_fp (struct __printf_buffer_fp *)
328 attribute_hidden;
329 struct __printf_buffer_fp_to_wide;
330 void __printf_buffer_flush_fp_to_wide (struct __printf_buffer_fp_to_wide *)
331 attribute_hidden;
332 struct __printf_buffer_fphex_to_wide;
333 void __printf_buffer_flush_fphex_to_wide (struct
334 __printf_buffer_fphex_to_wide *)
335 attribute_hidden;
336 struct __printf_buffer_obstack;
337 void __printf_buffer_flush_obstack (struct __printf_buffer_obstack *)
338 attribute_hidden;
340 struct __wprintf_buffer_to_file;
341 void __wprintf_buffer_flush_to_file (struct __wprintf_buffer_to_file *)
342 attribute_hidden;
344 /* Buffer sizes. These can be tuned as necessary. There is a tension
345 here between stack consumption, cache usage, and additional system
346 calls or heap allocations (if the buffer is too small).
348 Also see PRINTF_BUFFER_SIZE_DISCARD above for snprintf. */
350 /* Fallback buffer if the underlying FILE * stream does not provide
351 buffer space. */
352 #define PRINTF_BUFFER_SIZE_TO_FILE_STAGE 128
354 /* Temporary buffer used during floating point digit translation. */
355 #define PRINTF_BUFFER_SIZE_DIGITS 64
357 /* Size of the initial on-stack buffer for asprintf. It should be
358 large enough to copy almost all asprintf usages with just a single
359 (final, correctly sized) heap allocation. */
360 #define PRINTF_BUFFER_SIZE_ASPRINTF 200
362 /* This should cover most of the packet-oriented file descriptors,
363 where boundaries between writes could be visible to readers. But
364 it is still small enough not to cause too many stack overflow issues. */
365 #define PRINTF_BUFFER_SIZE_DPRINTF 2048
367 #endif /* PRINTF_BUFFER_H */