Updated logging to include the class/method so that it is more obvious where these...
[ACE_TAO.git] / TAO / tao / String_Alloc.cpp
blob5e03a6710eda3d8b0bb06ef92dfcf8da38f3f0a0
1 // -*- C++ -*-
2 #include "tao/String_Alloc.h"
3 #include "ace/OS_NS_string.h"
4 #include "ace/OS_NS_wchar.h"
5 #include "ace/OS_Memory.h"
6 #include <cstring>
8 #ifndef TAO_NO_SHARED_NULL_CORBA_STRING
9 static char null_char[]= "";
10 static CORBA::WChar null_wchar[]=
11 # if defined(ACE_HAS_WCHAR) || defined(ACE_HAS_XPG4_MULTIBYTE_CHAR)
12 L"";
13 # else
14 { 0 };
15 # endif
16 #endif /* TAO_NO_SHARED_NULL_CORBA_STRING */
18 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
20 char *
21 CORBA::string_dup (const char *str)
23 if (!str)
25 errno = EINVAL;
26 return nullptr;
29 #ifndef TAO_NO_SHARED_NULL_CORBA_STRING
30 if (!*str)
31 return null_char;
32 #endif /* TAO_NO_SHARED_NULL_CORBA_STRING */
34 size_t const len = std::strlen (str);
36 // This allocates an extra byte for the '\0';
37 char * copy = CORBA::string_alloc (static_cast<CORBA::ULong> (len));
38 if (copy)
40 // The memcpy() assumes that the destination is a valid buffer.
41 ACE_OS::memcpy (copy, str, len + 1);
44 return copy;
47 char *
48 CORBA::string_alloc (CORBA::ULong len)
50 // Allocate 1 + strlen to accomodate the null terminating character.
51 char *s = nullptr;
52 ACE_NEW_RETURN (s,
53 char[size_t (len + 1)],
54 nullptr);
55 s[0]= '\0';
57 return s;
60 void
61 CORBA::string_free (char *str)
63 #ifndef TAO_NO_SHARED_NULL_CORBA_STRING
64 if (null_char != str)
65 #endif /* TAO_NO_SHARED_NULL_CORBA_STRING */
66 delete [] str;
69 // ****************************************************************
71 CORBA::WChar*
72 CORBA::wstring_dup (const WChar *const str)
74 if (!str)
76 errno = EINVAL;
77 return nullptr;
80 #ifndef TAO_NO_SHARED_NULL_CORBA_STRING
81 if (!*str)
82 return null_wchar;
83 #endif /* TAO_NO_SHARED_NULL_CORBA_STRING */
85 CORBA::WChar* retval =
86 CORBA::wstring_alloc (static_cast <CORBA::ULong> (ACE_OS::strlen (str)));
87 if (retval == nullptr)
89 // The wscpy() below assumes that the destination is a valid buffer.
90 return nullptr;
93 return ACE_OS::wscpy (retval, str);
96 CORBA::WChar*
97 CORBA::wstring_alloc (CORBA::ULong len)
99 CORBA::WChar *s = nullptr;
100 ACE_NEW_RETURN (s,
101 CORBA::WChar [(size_t) (len + 1)],
102 nullptr);
103 return s;
106 void
107 CORBA::wstring_free (CORBA::WChar *const str)
109 #ifndef TAO_NO_SHARED_NULL_CORBA_STRING
110 if (null_wchar != str)
111 #endif /* TAO_NO_SHARED_NULL_CORBA_STRING */
112 delete [] str;
115 TAO_END_VERSIONED_NAMESPACE_DECL