1 /* Copyright (C) 1995-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>.
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
27 #include <array_length.h>
30 #include <math_ldbl_opt.h>
35 #include <printf_buffer.h>
37 struct __printf_buffer_asprintf
39 /* base.write_base points either to a heap-allocated buffer, or to
40 the direct array below. */
41 struct __printf_buffer base
;
43 /* Initial allocation. 200 should be large enough to copy almost
44 all asprintf usages with just a single (final, correctly sized)
46 char direct
[PRINTF_BUFFER_SIZE_ASPRINTF
];
50 __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf
*buf
)
52 size_t current_pos
= buf
->base
.write_ptr
- buf
->base
.write_base
;
53 if (current_pos
>= INT_MAX
)
55 /* The result is not representable. No need to continue. */
56 __set_errno (EOVERFLOW
);
57 __printf_buffer_mark_failed (&buf
->base
);
61 size_t current_size
= buf
->base
.write_end
- buf
->base
.write_base
;
62 /* Implement an exponentiation sizing policy. Keep the size
63 congruent 8 (mod 16), to account for the footer in glibc
65 size_t new_size
= ALIGN_UP (current_size
+ current_size
/ 2, 16) | 8;
67 if (buf
->base
.write_base
== buf
->direct
)
69 new_buffer
= malloc (new_size
);
70 if (new_buffer
== NULL
)
72 __printf_buffer_mark_failed (&buf
->base
);
75 memcpy (new_buffer
, buf
->direct
, current_pos
);
79 new_buffer
= realloc (buf
->base
.write_base
, new_size
);
80 if (new_buffer
== NULL
)
82 __printf_buffer_mark_failed (&buf
->base
);
87 /* Set up the new write area. */
88 buf
->base
.write_base
= new_buffer
;
89 buf
->base
.write_ptr
= new_buffer
+ current_pos
;
90 buf
->base
.write_end
= new_buffer
+ new_size
;
95 __vasprintf_internal (char **result_ptr
, const char *format
, va_list args
,
96 unsigned int mode_flags
)
98 struct __printf_buffer_asprintf buf
;
99 __printf_buffer_init (&buf
.base
, buf
.direct
, array_length (buf
.direct
),
100 __printf_buffer_mode_asprintf
);
102 __printf_buffer (&buf
.base
, format
, args
, mode_flags
);
103 int done
= __printf_buffer_done (&buf
.base
);
106 if (buf
.base
.write_base
!= buf
.direct
)
107 free (buf
.base
.write_base
);
111 /* Transfer to the final buffer. */
113 size_t size
= buf
.base
.write_ptr
- buf
.base
.write_base
;
114 if (buf
.base
.write_base
== buf
.direct
)
116 result
= malloc (size
+ 1);
119 memcpy (result
, buf
.direct
, size
);
123 result
= realloc (buf
.base
.write_base
, size
+ 1);
126 free (buf
.base
.write_base
);
131 /* Add NUL termination. */
133 *result_ptr
= result
;
139 __vasprintf (char **result_ptr
, const char *format
, va_list args
)
141 return __vasprintf_internal (result_ptr
, format
, args
, 0);
143 ldbl_weak_alias (__vasprintf
, vasprintf
)