[libc-commits] [libc] 65bb659 - [libc] add strdup implementation

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed Oct 27 10:21:10 PDT 2021


Author: Michael Jones
Date: 2021-10-27T10:21:04-07:00
New Revision: 65bb6593e57412e50eb9cd536e8c54a3442cb986

URL: https://github.com/llvm/llvm-project/commit/65bb6593e57412e50eb9cd536e8c54a3442cb986
DIFF: https://github.com/llvm/llvm-project/commit/65bb6593e57412e50eb9cd536e8c54a3442cb986.diff

LOG: [libc] add strdup implementation

Add an implementation for strdup.

Reviewed By: lntue, sivachandra

Differential Revision: https://reviews.llvm.org/D111584

Added: 
    libc/src/string/strdup.cpp
    libc/src/string/strdup.h
    libc/test/src/string/strdup_test.cpp

Modified: 
    libc/config/linux/x86_64/entrypoints.txt
    libc/spec/stdc.td
    libc/src/string/CMakeLists.txt
    libc/test/src/string/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 951e76fb83158..b70aabd88a057 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -36,6 +36,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strcmp
     libc.src.string.strcpy
     libc.src.string.strcspn
+    libc.src.string.strdup
     libc.src.string.strlen
     libc.src.string.strncat
     libc.src.string.strncmp
@@ -237,7 +238,8 @@ if(LLVM_LIBC_INCLUDE_SCUDO)
     libc.src.stdlib.realloc
     libc.src.stdlib.free
 
-
+    # string.h entrypoints that depend on malloc
+    libc.src.string.strdup
   )
 endif()
 

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index f55f269ad3f96..c997d4364fb15 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -279,6 +279,11 @@ def StdC : StandardSpec<"stdc"> {
               RetValSpec<SizeTType>,
               [ArgSpec<ConstCharPtr>, ArgSpec<ConstCharPtr>]
           >,
+          FunctionSpec<
+              "strdup",
+              RetValSpec<CharPtr>,
+              [ArgSpec<ConstCharPtr>]
+          >,
           FunctionSpec<
               "strpbrk",
               RetValSpec<CharPtr>,

diff  --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 05edd49900217..86c8f0f02cfe8 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -104,6 +104,18 @@ add_entrypoint_object(
     .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.cpp b/libc/src/string/strdup.cpp
new file mode 100644
index 0000000000000..cb28868188e0c
--- /dev/null
+++ b/libc/src/string/strdup.cpp
@@ -0,0 +1,32 @@
+//===-- 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 <stdlib.h>
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(char *, strdup, (const char *src)) {
+  if (src == nullptr) {
+    return nullptr;
+  }
+  size_t len = internal::string_length(src) + 1;
+  char *dest = reinterpret_cast<char *>(::malloc(len)); // NOLINT
+  if (dest == nullptr) {
+    return nullptr;
+  }
+  char *result = reinterpret_cast<char *>(__llvm_libc::memcpy(dest, src, len));
+  return result;
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/string/strdup.h b/libc/src/string/strdup.h
new file mode 100644
index 0000000000000..8f83558db919f
--- /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 <string.h>
+
+namespace __llvm_libc {
+
+char *strdup(const char *src);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STRING_STRDUP_H

diff  --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index 2c8d6627a68d7..8154aaa225a2e 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -92,6 +92,17 @@ add_libc_unittest(
     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
index 0000000000000..7820c1e7c5124
--- /dev/null
+++ b/libc/test/src/string/strdup_test.cpp
@@ -0,0 +1,39 @@
+//===-- 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 <stdlib.h>
+
+TEST(LlvmLibcStrDupTest, EmptyString) {
+  const char *empty = "";
+
+  char *result = __llvm_libc::strdup(empty);
+  ASSERT_NE(result, static_cast<char *>(nullptr));
+  ASSERT_NE(empty, const_cast<const char *>(result));
+  ASSERT_STREQ(empty, result);
+  ::free(result);
+}
+
+TEST(LlvmLibcStrDupTest, AnyString) {
+  const char *abc = "abc";
+
+  char *result = __llvm_libc::strdup(abc);
+
+  ASSERT_NE(result, static_cast<char *>(nullptr));
+  ASSERT_NE(abc, const_cast<const char *>(result));
+  ASSERT_STREQ(abc, result);
+  ::free(result);
+}
+
+TEST(LlvmLibcStrDupTest, NullPtr) {
+
+  char *result = __llvm_libc::strdup(nullptr);
+
+  ASSERT_EQ(result, static_cast<char *>(nullptr));
+}


        


More information about the libc-commits mailing list