diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -34,6 +34,7 @@ libc.src.string.strcmp libc.src.string.strcpy libc.src.string.strcspn + libc.src.string.strdup libc.src.string.strlen libc.src.string.strncmp libc.src.string.strncpy @@ -182,6 +183,11 @@ libc.src.stdlib._Exit libc.src.stdlib.abort +# libc.src.stdlib.malloc +# libc.src.stdlib.calloc +# libc.src.stdlib.realloc +# libc.src.stdlib.free + # signal.h entrypoints libc.src.signal.raise libc.src.signal.sigaction diff --git a/libc/include/stdlib.h.def b/libc/include/stdlib.h.def --- a/libc/include/stdlib.h.def +++ b/libc/include/stdlib.h.def @@ -13,4 +13,12 @@ %%public_api() + +__BEGIN_C_DECLS + void* malloc(size_t); + void* calloc(size_t, size_t); + void* realloc(void*, size_t); + void free(void*); +__END_C_DECLS + #endif // LLVM_LIBC_STDLIB_H diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -279,6 +279,11 @@ RetValSpec, [ArgSpec, ArgSpec] >, + FunctionSpec< + "strdup", + RetValSpec, + [ArgSpec] + >, FunctionSpec< "strpbrk", RetValSpec, @@ -510,6 +515,11 @@ FunctionSpec<"strtoul", RetValSpec, [ArgSpec, ArgSpec, ArgSpec]>, FunctionSpec<"strtoull", RetValSpec, [ArgSpec, ArgSpec, ArgSpec]>, + FunctionSpec<"malloc", RetValSpec, [ArgSpec]>, + FunctionSpec<"calloc", RetValSpec, [ArgSpec, ArgSpec]>, + FunctionSpec<"realloc", RetValSpec, [ArgSpec, ArgSpec]>, + FunctionSpec<"free", RetValSpec, [ArgSpec]>, + FunctionSpec<"_Exit", RetValSpec, [ArgSpec]>, ] >; diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt --- a/libc/src/string/CMakeLists.txt +++ b/libc/src/string/CMakeLists.txt @@ -85,6 +85,18 @@ .string_utils ) +add_entrypoint_object( + strdup + SRCS + strdup.cpp + HDRS + strdup.h + DEPENDS + .memcpy + .string_utils + libc.include.stdlib +) + add_entrypoint_object( strlen SRCS diff --git a/libc/src/string/strdup.h b/libc/src/string/strdup.h new file mode 100644 --- /dev/null +++ b/libc/src/string/strdup.h @@ -0,0 +1,20 @@ +//===-- Implementation header for strdup ------------------------*- 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_STRING_STRDUP_H +#define LLVM_LIBC_SRC_STRING_STRDUP_H + +#include + +namespace __llvm_libc { + +char *strdup(const char *src); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_STRING_STRDUP_H diff --git a/libc/src/string/strdup.cpp b/libc/src/string/strdup.cpp new file mode 100644 --- /dev/null +++ b/libc/src/string/strdup.cpp @@ -0,0 +1,30 @@ +//===-- Implementation of strdup ------------------------------------------===// +// +// 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/string/strdup.h" +#include "src/string/memcpy.h" +#include "src/string/string_utils.h" + +#include "src/__support/common.h" + +#include + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(char *, strdup, (const char *src)) { + size_t len = internal::string_length(src) + 1; + char *newStr = reinterpret_cast(::malloc(len)); // NOLINT + if (newStr == nullptr) { + return nullptr; + } + char *result = + reinterpret_cast(__llvm_libc::memcpy(newStr, src, len)); + return result; +} + +} // namespace __llvm_libc diff --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt --- a/libc/test/src/string/CMakeLists.txt +++ b/libc/test/src/string/CMakeLists.txt @@ -72,6 +72,17 @@ libc.src.string.strcspn ) +add_libc_unittest( + strdup_test + SUITE + libc_string_unittests + SRCS + strdup_test.cpp + DEPENDS + libc.include.stdlib + libc.src.string.strdup +) + add_libc_unittest( strlen_test SUITE diff --git a/libc/test/src/string/strdup_test.cpp b/libc/test/src/string/strdup_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/string/strdup_test.cpp @@ -0,0 +1,30 @@ +//===-- Unittests for strdup ----------------------------------------------===// +// +// 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/string/strdup.h" +#include "utils/UnitTest/Test.h" +#include + +TEST(LlvmLibcStrDupTest, EmptyString) { + const char *empty = ""; + + char *result = __llvm_libc::strdup(empty); + ASSERT_NE(result, static_cast(nullptr)); + ASSERT_STREQ(empty, result); + ::free(result); +} + +TEST(LlvmLibcStrDupTest, AnyString) { + const char *abc = "abc"; + + char *result = __llvm_libc::strdup(abc); + + ASSERT_NE(result, static_cast(nullptr)); + ASSERT_STREQ(abc, result); + ::free(result); +}