xref: /llvm-project/libc/src/string/strdup.cpp (revision 155f5a6dac62a902a30f60e2717c4ba8fb828139)
1 //===-- Implementation of strdup ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/string/strdup.h"
10 #include "src/string/memory_utils/memcpy_implementations.h"
11 #include "src/string/string_utils.h"
12 
13 #include "src/__support/common.h"
14 
15 #include <stdlib.h>
16 
17 namespace __llvm_libc {
18 
19 LLVM_LIBC_FUNCTION(char *, strdup, (const char *src)) {
20   if (src == nullptr) {
21     return nullptr;
22   }
23   size_t len = internal::string_length(src) + 1;
24   char *dest = reinterpret_cast<char *>(::malloc(len));
25   if (dest == nullptr) {
26     return nullptr;
27   }
28   inline_memcpy(dest, src, len);
29   return dest;
30 }
31 
32 } // namespace __llvm_libc
33