[libc-commits] [libc] d85699e - [libc] Move strdup implementation to a new header

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Tue Nov 29 12:52:04 PST 2022


Author: Joseph Huber
Date: 2022-11-29T14:51:52-06:00
New Revision: d85699eb7d90571b8c39b86774e4ac98058d56f0

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

LOG: [libc] Move strdup implementation to a new header

The `strdup` family of functions rely on `malloc` to be implemented.
Its presence in the `string_utils.h` header meant that compiling many of
the string functions relied on `malloc` being implementated as well.
This patch simply moves the implementation into a new file to avoid
including `stdlib.h` from the other string functions. This was a barrier
for compiling string functions for the GPU where there is no malloc
currently.

Reviewed By: sivachandra

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

Added: 
    libc/src/string/allocating_string_utils.h

Modified: 
    libc/src/string/CMakeLists.txt
    libc/src/string/strdup.cpp
    libc/src/string/string_utils.h
    libc/src/unistd/linux/getcwd.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 9e8287fe8b938..2c960af216fc6 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -11,6 +11,15 @@ add_header_library(
     .memory_utils.bzero_implementation
 )
 
+add_header_library(
+  allocating_string_utils
+  HDRS
+    allocating_string_utils.h
+  DEPENDS
+    libc.include.stdlib
+    .memory_utils.memcpy_implementation
+)
+
 add_entrypoint_object(
   memccpy
   SRCS

diff  --git a/libc/src/string/allocating_string_utils.h b/libc/src/string/allocating_string_utils.h
new file mode 100644
index 0000000000000..32204007d622c
--- /dev/null
+++ b/libc/src/string/allocating_string_utils.h
@@ -0,0 +1,37 @@
+//===-- Allocating string utils ---------------------------------*- 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 LIBC_SRC_STRING_ALLOCATING_STRING_UTILS_H
+#define LIBC_SRC_STRING_ALLOCATING_STRING_UTILS_H
+
+#include "src/__support/CPP/bitset.h"
+#include "src/__support/common.h"
+#include "src/string/memory_utils/bzero_implementations.h"
+#include "src/string/memory_utils/memcpy_implementations.h"
+#include "src/string/string_utils.h"
+#include <stddef.h> // For size_t
+#include <stdlib.h> // For malloc
+
+namespace __llvm_libc {
+namespace internal {
+
+inline char *strdup(const char *src) {
+  if (src == nullptr)
+    return nullptr;
+  size_t len = string_length(src) + 1;
+  char *newstr = reinterpret_cast<char *>(::malloc(len));
+  if (newstr == nullptr)
+    return nullptr;
+  inline_memcpy(newstr, src, len);
+  return newstr;
+}
+
+} // namespace internal
+} // namespace __llvm_libc
+
+#endif

diff  --git a/libc/src/string/strdup.cpp b/libc/src/string/strdup.cpp
index d7adc85fc223b..9a52b298501b1 100644
--- a/libc/src/string/strdup.cpp
+++ b/libc/src/string/strdup.cpp
@@ -7,8 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/string/strdup.h"
+#include "src/string/allocating_string_utils.h"
 #include "src/string/memory_utils/memcpy_implementations.h"
-#include "src/string/string_utils.h"
 
 #include "src/__support/common.h"
 

diff  --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h
index da4ad8e4a270e..f8de6964983ce 100644
--- a/libc/src/string/string_utils.h
+++ b/libc/src/string/string_utils.h
@@ -5,6 +5,11 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+//
+// Standalone string utility functions. Utilities requiring memory allocations
+// should be placed in allocating_string_utils.h intead.
+//
+//===----------------------------------------------------------------------===//
 
 #ifndef LIBC_SRC_STRING_STRING_UTILS_H
 #define LIBC_SRC_STRING_STRING_UTILS_H
@@ -14,7 +19,6 @@
 #include "src/string/memory_utils/bzero_implementations.h"
 #include "src/string/memory_utils/memcpy_implementations.h"
 #include <stddef.h> // For size_t
-#include <stdlib.h> // For malloc and free
 
 namespace __llvm_libc {
 namespace internal {
@@ -99,17 +103,6 @@ static inline size_t strlcpy(char *__restrict dst, const char *__restrict src,
   return len;
 }
 
-inline char *strdup(const char *src) {
-  if (src == nullptr)
-    return nullptr;
-  size_t len = string_length(src) + 1;
-  char *newstr = reinterpret_cast<char *>(::malloc(len));
-  if (newstr == nullptr)
-    return nullptr;
-  inline_memcpy(newstr, src, len);
-  return newstr;
-}
-
 } // namespace internal
 } // namespace __llvm_libc
 

diff  --git a/libc/src/unistd/linux/getcwd.cpp b/libc/src/unistd/linux/getcwd.cpp
index 7475f461d8b9f..84a16b3d4efd1 100644
--- a/libc/src/unistd/linux/getcwd.cpp
+++ b/libc/src/unistd/linux/getcwd.cpp
@@ -10,7 +10,7 @@
 
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
-#include "src/string/string_utils.h" // For strdup.
+#include "src/string/allocating_string_utils.h" // For strdup.
 
 #include <errno.h>
 #include <linux/limits.h> // This is safe to include without any name pollution.


        


More information about the libc-commits mailing list