ReactOS 0.4.16-dev-927-g467dec4
strdup.cpp
Go to the documentation of this file.
1//
2// strdup.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines _strdup() and _strdup_dbg(), which dynamically allocate a buffer and
7// duplicate a string into it.
8//
9// These functions allocate storage via malloc() or _malloc_dbg(). The caller
10// is responsible for free()ing the returned array. If the input string is null
11// or if sufficient memory could not be allocated, these functions return null.
12//
13#include <corecrt_internal.h>
14#include <malloc.h>
15#include <string.h>
16
17
18
19#ifdef _DEBUG
20
21extern "C" char* __cdecl _strdup(char const* const string)
22{
23 return _strdup_dbg(string, _NORMAL_BLOCK, nullptr, 0);
24}
25
26extern "C" char* __cdecl _strdup_dbg(
27 char const* const string,
28 int const block_use,
29 char const* const file_name,
30 int const line_number
31 )
32
33#else // ^^^ _DEBUG ^^^ // vvv !_DEBUG vvv //
34
35extern "C" char* __cdecl _strdup(
36 char const* string
37 )
38
39#endif // !_DEBUG
40{
41 if (string == nullptr)
42 return nullptr;
43
44 size_t const size = strlen(string) + 1;
45
46#ifdef _DEBUG
47 char* const memory = static_cast<char*>(_malloc_dbg(
48 size,
52#else
53 char* const memory = static_cast<char*>(malloc(size));
54#endif
55
56 if (memory == nullptr)
57 return nullptr;
58
59 _ERRCHECK(strcpy_s(memory, size, string));
60 return memory;
61}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define __cdecl
Definition: accygwin.h:79
#define _ERRCHECK(e)
#define _malloc_dbg(s, t, f, l)
Definition: crtdbg.h:204
#define _NORMAL_BLOCK
Definition: crtdbg.h:67
#define _strdup_dbg(s, t, f, l)
Definition: crtdbg.h:223
int const char const *const int const line_number
Definition: debug_heap.cpp:499
#define _strdup
Definition: debug_ros.c:7
#define malloc
Definition: debug_ros.c:4
_Out_opt_ size_t *const Character const *const int const block_use
Definition: getenv.cpp:258
GLsizeiptr size
Definition: glext.h:5919
#define strcpy_s(d, l, s)
Definition: utility.h:200
static char memory[1024 *256]
Definition: process.c:116
static LPCWSTR file_name
Definition: protocol.c:147