[libc-commits] [PATCH] D138607: [libc] Move strdup implementation to a new header

Joseph Huber via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Nov 23 13:44:46 PST 2022


jhuber6 created this revision.
jhuber6 added reviewers: sivachandra, lntue, michaelrj.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
jhuber6 requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D138607

Files:
  libc/src/string/strdup.cpp
  libc/src/string/string_utils.h
  libc/src/string/string_utils_malloc.h
  libc/src/unistd/linux/getcwd.cpp


Index: libc/src/unistd/linux/getcwd.cpp
===================================================================
--- libc/src/unistd/linux/getcwd.cpp
+++ 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/string_utils_malloc.h" // For strdup.
 
 #include <errno.h>
 #include <linux/limits.h> // This is safe to include without any name pollution.
Index: libc/src/string/string_utils_malloc.h
===================================================================
--- /dev/null
+++ libc/src/string/string_utils_malloc.h
@@ -0,0 +1,37 @@
+//===-- String utils malloc -------------------------------------*- 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_STRING_UTILS_MALLOC_H
+#define LIBC_SRC_STRING_STRING_UTILS_MALLOC_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
Index: libc/src/string/string_utils.h
===================================================================
--- libc/src/string/string_utils.h
+++ libc/src/string/string_utils.h
@@ -14,7 +14,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 +98,6 @@
   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
 
Index: libc/src/string/strdup.cpp
===================================================================
--- libc/src/string/strdup.cpp
+++ libc/src/string/strdup.cpp
@@ -8,7 +8,7 @@
 
 #include "src/string/strdup.h"
 #include "src/string/memory_utils/memcpy_implementations.h"
-#include "src/string/string_utils.h"
+#include "src/string/string_utils_malloc.h"
 
 #include "src/__support/common.h"
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138607.477594.patch
Type: text/x-patch
Size: 3120 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20221123/811c1df4/attachment.bin>


More information about the libc-commits mailing list