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/memcpy.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)); // NOLINT
25   if (dest == nullptr) {
26     return nullptr;
27   }
28   char *result = reinterpret_cast<char *>(__llvm_libc::memcpy(dest, src, len));
29   return result;
30 }
31 
32 } // namespace __llvm_libc
33