1d85699ebSJoseph Huber //===-- Allocating string utils ---------------------------------*- C++ -*-===// 2d85699ebSJoseph Huber // 3d85699ebSJoseph Huber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4d85699ebSJoseph Huber // See https://llvm.org/LICENSE.txt for license information. 5d85699ebSJoseph Huber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6d85699ebSJoseph Huber // 7d85699ebSJoseph Huber //===----------------------------------------------------------------------===// 8d85699ebSJoseph Huber 9270547f3SGuillaume Chatelet #ifndef LLVM_LIBC_SRC_STRING_ALLOCATING_STRING_UTILS_H 10270547f3SGuillaume Chatelet #define LLVM_LIBC_SRC_STRING_ALLOCATING_STRING_UTILS_H 11d85699ebSJoseph Huber 12f3400305SSiva Chandra Reddy #include "src/__support/CPP/new.h" 13f3400305SSiva Chandra Reddy #include "src/__support/CPP/optional.h" 14ab07fd9fSSiva Chandra Reddy #include "src/__support/macros/config.h" 151f578347SGuillaume Chatelet #include "src/string/memory_utils/inline_memcpy.h" 16d85699ebSJoseph Huber #include "src/string/string_utils.h" 17f3400305SSiva Chandra Reddy 18d85699ebSJoseph Huber #include <stddef.h> // For size_t 19d85699ebSJoseph Huber 20*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 21d85699ebSJoseph Huber namespace internal { 22d85699ebSJoseph Huber 23ab07fd9fSSiva Chandra Reddy LIBC_INLINE cpp::optional<char *> strdup(const char *src) { 24d85699ebSJoseph Huber if (src == nullptr) 25f3400305SSiva Chandra Reddy return cpp::nullopt; 26d85699ebSJoseph Huber size_t len = string_length(src) + 1; 27f3400305SSiva Chandra Reddy AllocChecker ac; 28f3400305SSiva Chandra Reddy char *newstr = new (ac) char[len]; 29f3400305SSiva Chandra Reddy if (!ac) 30f3400305SSiva Chandra Reddy return cpp::nullopt; 31d85699ebSJoseph Huber inline_memcpy(newstr, src, len); 32d85699ebSJoseph Huber return newstr; 33d85699ebSJoseph Huber } 34d85699ebSJoseph Huber 35d85699ebSJoseph Huber } // namespace internal 36*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 37d85699ebSJoseph Huber 38270547f3SGuillaume Chatelet #endif // LLVM_LIBC_SRC_STRING_ALLOCATING_STRING_UTILS_H 39