165bb6593SMichael Jones //===-- Implementation of strdup ------------------------------------------===// 265bb6593SMichael Jones // 365bb6593SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 465bb6593SMichael Jones // See https://llvm.org/LICENSE.txt for license information. 565bb6593SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 665bb6593SMichael Jones // 765bb6593SMichael Jones //===----------------------------------------------------------------------===// 865bb6593SMichael Jones 965bb6593SMichael Jones #include "src/string/strdup.h" 107b59fcb7SSiva Chandra Reddy #include "src/string/memory_utils/memcpy_implementations.h" 1165bb6593SMichael Jones #include "src/string/string_utils.h" 1265bb6593SMichael Jones 1365bb6593SMichael Jones #include "src/__support/common.h" 1465bb6593SMichael Jones 1565bb6593SMichael Jones #include <stdlib.h> 1665bb6593SMichael Jones 1765bb6593SMichael Jones namespace __llvm_libc { 1865bb6593SMichael Jones 1965bb6593SMichael Jones LLVM_LIBC_FUNCTION(char *, strdup, (const char *src)) { 2065bb6593SMichael Jones if (src == nullptr) { 2165bb6593SMichael Jones return nullptr; 2265bb6593SMichael Jones } 2365bb6593SMichael Jones size_t len = internal::string_length(src) + 1; 24*155f5a6dSMichael Jones char *dest = reinterpret_cast<char *>(::malloc(len)); 2565bb6593SMichael Jones if (dest == nullptr) { 2665bb6593SMichael Jones return nullptr; 2765bb6593SMichael Jones } 287b59fcb7SSiva Chandra Reddy inline_memcpy(dest, src, len); 297b59fcb7SSiva Chandra Reddy return dest; 3065bb6593SMichael Jones } 3165bb6593SMichael Jones 3265bb6593SMichael Jones } // namespace __llvm_libc 33