forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] Implement vasprintf and asprintf (llvm#98824)
[libc] Implement vasprintf and asprintf --------- Co-authored-by: Izaak Schroeder <[email protected]>
- Loading branch information
1 parent
2a5f7e5
commit a5e67fb
Showing
20 changed files
with
515 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//===-- Implementation of asprintf -----------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/stdio/asprintf.h" | ||
#include "src/__support/arg_list.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/stdio/printf_core/vasprintf_internal.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
LLVM_LIBC_FUNCTION(int, asprintf, | ||
(char **__restrict buffer, const char *format, ...)) { | ||
va_list vlist; | ||
va_start(vlist, format); | ||
internal::ArgList args(vlist); // This holder class allows for easier copying | ||
// and pointer semantics, as well as handling | ||
// destruction automatically. | ||
va_end(vlist); | ||
int ret = printf_core::vasprintf_internal(buffer, format, args); | ||
return ret; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===-- Implementation header of asprintf ----------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_STDIO_ASPRINTF_H | ||
#define LLVM_LIBC_SRC_STDIO_ASPRINTF_H | ||
|
||
#include "src/__support/macros/config.h" | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
int asprintf(char **__restrict s, const char *format, ...); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_STDIO_ASPRINTF_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//===-- Internal Implementation of asprintf ---------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/__support/arg_list.h" | ||
#include "src/stdio/printf.h" | ||
#include "src/stdio/printf_core/core_structs.h" | ||
#include "src/stdio/printf_core/printf_main.h" | ||
#include "src/stdio/printf_core/writer.h" | ||
#include <stdlib.h> // malloc, realloc, free | ||
|
||
namespace LIBC_NAMESPACE { | ||
namespace printf_core { | ||
|
||
LIBC_INLINE int resize_overflow_hook(cpp::string_view new_str, void *target) { | ||
printf_core::WriteBuffer *wb = | ||
reinterpret_cast<printf_core::WriteBuffer *>(target); | ||
size_t new_size = new_str.size() + wb->buff_cur; | ||
const bool isBuffOnStack = (wb->buff == wb->init_buff); | ||
char *new_buff = static_cast<char *>( | ||
isBuffOnStack ? malloc(new_size + 1) | ||
: realloc(wb->buff, new_size + 1)); // +1 for null | ||
if (new_buff == nullptr) { | ||
if (wb->buff != wb->init_buff) | ||
free(wb->buff); | ||
return printf_core::ALLOCATION_ERROR; | ||
} | ||
if (isBuffOnStack) | ||
inline_memcpy(new_buff, wb->buff, wb->buff_cur); | ||
wb->buff = new_buff; | ||
inline_memcpy(wb->buff + wb->buff_cur, new_str.data(), new_str.size()); | ||
wb->buff_cur = new_size; | ||
wb->buff_len = new_size; | ||
return printf_core::WRITE_OK; | ||
} | ||
|
||
constexpr size_t DEFAULT_BUFFER_SIZE = 200; | ||
|
||
LIBC_INLINE int vasprintf_internal(char **ret, const char *format, | ||
internal::ArgList args) { | ||
char init_buff_on_stack[DEFAULT_BUFFER_SIZE]; | ||
printf_core::WriteBuffer wb(init_buff_on_stack, DEFAULT_BUFFER_SIZE, | ||
resize_overflow_hook); | ||
printf_core::Writer writer(&wb); | ||
|
||
auto ret_val = printf_core::printf_main(&writer, format, args); | ||
if (ret_val < 0) { | ||
*ret = nullptr; | ||
return -1; | ||
} | ||
if (wb.buff == init_buff_on_stack) { | ||
*ret = static_cast<char *>(malloc(ret_val + 1)); | ||
if (ret == nullptr) | ||
return printf_core::ALLOCATION_ERROR; | ||
inline_memcpy(*ret, wb.buff, ret_val); | ||
} else { | ||
*ret = wb.buff; | ||
} | ||
(*ret)[ret_val] = '\0'; | ||
return ret_val; | ||
} | ||
} // namespace printf_core | ||
} // namespace LIBC_NAMESPACE |
Oops, something went wrong.