Date: prev next · Thread: first prev next last
2012 Archives by date, by thread · List index


On Tuesday 28 of February 2012, Michael Meeks wrote:
      I would really prefer to use a new:

        rtl_uString_newFromAsciiL( &pNew, literal, N - 1 );

 Attached.

      method - which should shrink the call-site, and allow for a rather
better implementation vs.


 Speaking of the size at the call-site, I good part is the code trying to 
throw std::bad_alloc in case the allocation fails. That actually looks rather 
useless to me, for several reasons:

- not all OUString methods check for this anyway
- rtl_uString* functions do OSL_ASSERT() after allocations
- with today's systems (overcommitting, etc.) it is rather pointless to guard 
against allocation failures

 Does somebody see a good reason not to just remove it?

-- 
 Lubos Lunak
 l.lunak@suse.cz
From e1e3406cb73559c7760f0e28965a8bb0c4c19b4a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= <l.lunak@suse.cz>
Date: Tue, 28 Feb 2012 13:09:46 +0100
Subject: [PATCH 1/2] rtl_uString_newFromAscii_WithLength() and use it in OUString

---
 sal/inc/rtl/ustring.h      |   10 ++++++++++
 sal/inc/rtl/ustring.hxx    |    4 ++--
 sal/rtl/source/ustring.cxx |   10 +++++++++-
 sal/util/sal.map           |    1 +
 4 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/sal/inc/rtl/ustring.h b/sal/inc/rtl/ustring.h
index 03f145b..80703f8 100644
--- a/sal/inc/rtl/ustring.h
+++ b/sal/inc/rtl/ustring.h
@@ -1243,6 +1243,16 @@ SAL_DLLPUBLIC void SAL_CALL rtl_uString_newFromStr_WithLength(
 SAL_DLLPUBLIC void SAL_CALL rtl_uString_newFromAscii(
         rtl_uString ** newStr, const sal_Char * value ) SAL_THROW_EXTERN_C();
 
+/** Allocate a new string that contains a copy of a character array.
+
+    This is equivalent to rtl_uString_newFromAscii(), except that
+    length of the character array is explicitly passed to the function.
+
+    @since 3.6
+ */
+SAL_DLLPUBLIC void SAL_CALL rtl_uString_newFromAscii_WithLength(
+        rtl_uString ** newStr, const sal_Char * value, sal_Int32 len ) SAL_THROW_EXTERN_C();
+
 /** Allocate a new string from an array of Unicode code points.
 
     @param newString
diff --git a/sal/inc/rtl/ustring.hxx b/sal/inc/rtl/ustring.hxx
index b74cd37..151e0a2 100644
--- a/sal/inc/rtl/ustring.hxx
+++ b/sal/inc/rtl/ustring.hxx
@@ -184,7 +184,7 @@ public:
     OUString( const char (&literal)[ N ] )
     {
         pData = 0;
-        rtl_string2UString( &pData, literal, N - 1, RTL_TEXTENCODING_ASCII_US, 0 );
+        rtl_uString_newFromAscii_WithLength( &pData, literal, N - 1 );
         if (pData == 0) {
 #if defined EXCEPTIONS_OFF
             SAL_WARN("sal", "std::bad_alloc but EXCEPTIONS_OFF");
@@ -338,7 +338,7 @@ public:
     template< int N >
     OUString& operator=( const char (&literal)[ N ] )
     {
-        rtl_string2UString( &pData, literal, N - 1, RTL_TEXTENCODING_ASCII_US, 0 );
+        rtl_uString_newFromAscii_WithLength( &pData, literal, N - 1 );
         if (pData == 0) {
 #if defined EXCEPTIONS_OFF
             SAL_WARN("sal", "std::bad_alloc but EXCEPTIONS_OFF");
diff --git a/sal/rtl/source/ustring.cxx b/sal/rtl/source/ustring.cxx
index 7c99758..763d9c5 100644
--- a/sal/rtl/source/ustring.cxx
+++ b/sal/rtl/source/ustring.cxx
@@ -471,6 +471,14 @@ void SAL_CALL rtl_uString_newFromAscii( rtl_uString** ppThis,
     else
         nLen = 0;
 
+    rtl_uString_newFromAscii_WithLength( ppThis, pCharStr, nLen );
+}
+
+void SAL_CALL rtl_uString_newFromAscii_WithLength( rtl_uString** ppThis,
+                                        const sal_Char* pCharStr,
+                                        sal_Int32 nLen )
+    SAL_THROW_EXTERN_C()
+{
     if ( !nLen )
     {
         IMPL_RTL_STRINGNAME( new )( ppThis );
@@ -489,7 +497,7 @@ void SAL_CALL rtl_uString_newFromAscii( rtl_uString** ppThis,
         {
             /* Check ASCII range */
             SAL_WARN_IF( ((unsigned char)*pCharStr) > 127, "rtl.string",
-                        "rtl_uString_newFromAscii - Found char > 127" );
+                        "rtl_uString_newFromAscii_WithLength - Found char > 127" );
 
             *pBuffer = *pCharStr;
             pBuffer++;
diff --git a/sal/util/sal.map b/sal/util/sal.map
index d6c3ab1..5ee60b9 100644
--- a/sal/util/sal.map
+++ b/sal/util/sal.map
@@ -305,6 +305,7 @@ UDK_3_0_0 {
         rtl_uString_newFromStr;
         rtl_uString_newFromStr_WithLength;
         rtl_uString_newFromAscii;
+        rtl_uString_newFromAscii_WithLength;
         rtl_uString_newFromString;
         rtl_uString_newReplace;
         rtl_uString_newReplaceStrAt;
-- 
1.7.3.4

From 81117980acf002a35ebee69c5964e5595cecb4c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= <l.lunak@suse.cz>
Date: Tue, 28 Feb 2012 14:39:41 +0100
Subject: [PATCH 2/2] remove std::bad_alloc() throwing in some OUString functions

Not all OUString functions check this, rtl_uString* functions
usually assert on allocations, and checking for allocation failures
with memory overcommitting is pointless anyway. This way it just
creates useless code that is expanded everywhere where these inline
functions are called.
---
 sal/inc/rtl/ustring.hxx |   62 +++++-----------------------------------------
 1 files changed, 7 insertions(+), 55 deletions(-)

diff --git a/sal/inc/rtl/ustring.hxx b/sal/inc/rtl/ustring.hxx
index 151e0a2..0ffbc7a 100644
--- a/sal/inc/rtl/ustring.hxx
+++ b/sal/inc/rtl/ustring.hxx
@@ -39,12 +39,6 @@
 #include <rtl/memory.h>
 #include "sal/log.hxx"
 
-#if defined EXCEPTIONS_OFF
-#include <stdlib.h>
-#else
-#include <new>
-#endif
-
 namespace rtl
 {
 /* ======================================================================= */
@@ -181,17 +175,10 @@ public:
       @since 3.6
     */
     template< int N >
-    OUString( const char (&literal)[ N ] )
+    OUString( const char (&literal)[ N ] ) SAL_THROW(())
     {
         pData = 0;
         rtl_uString_newFromAscii_WithLength( &pData, literal, N - 1 );
-        if (pData == 0) {
-#if defined EXCEPTIONS_OFF
-            SAL_WARN("sal", "std::bad_alloc but EXCEPTIONS_OFF");
-#else
-            throw std::bad_alloc();
-#endif
-        }
     }
 
     /**
@@ -246,17 +233,10 @@ public:
     */
     OUString( const sal_Char * value, sal_Int32 length,
               rtl_TextEncoding encoding,
-              sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS )
+              sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS ) SAL_THROW(())
     {
         pData = 0;
         rtl_string2UString( &pData, value, length, encoding, convertFlags );
-        if (pData == 0) {
-#if defined EXCEPTIONS_OFF
-            SAL_WARN("sal", "std::bad_alloc but EXCEPTIONS_OFF");
-#else
-            throw std::bad_alloc();
-#endif
-        }
     }
 
     /** Create a new string from an array of Unicode code points.
@@ -276,17 +256,10 @@ public:
         @since UDK 3.2.7
     */
     inline explicit OUString(
-        sal_uInt32 const * codePoints, sal_Int32 codePointCount):
-        pData(NULL)
+        sal_uInt32 const * codePoints, sal_Int32 codePointCount)  SAL_THROW(())
+        : pData(NULL)
     {
         rtl_uString_newFromCodePoints(&pData, codePoints, codePointCount);
-        if (pData == NULL) {
-#if defined EXCEPTIONS_OFF
-            abort();
-#else
-            throw std::bad_alloc();
-#endif
-        }
     }
 
     /**
@@ -336,16 +309,9 @@ public:
       @since 3.6
     */
     template< int N >
-    OUString& operator=( const char (&literal)[ N ] )
+    OUString& operator=( const char (&literal)[ N ] ) SAL_THROW(())
     {
         rtl_uString_newFromAscii_WithLength( &pData, literal, N - 1 );
-        if (pData == 0) {
-#if defined EXCEPTIONS_OFF
-            SAL_WARN("sal", "std::bad_alloc but EXCEPTIONS_OFF");
-#else
-            throw std::bad_alloc();
-#endif
-        }
         return *this;
     }
 
@@ -1642,17 +1608,10 @@ public:
 
        @since UDK 3.2.7
     */
-    OUString intern() const
+    OUString intern() const SAL_THROW(())
     {
         rtl_uString * pNew = 0;
         rtl_uString_intern( &pNew, pData );
-        if (pNew == 0) {
-#if defined EXCEPTIONS_OFF
-            SAL_WARN("sal", "std::bad_alloc but EXCEPTIONS_OFF");
-#else
-            throw std::bad_alloc();
-#endif
-        }
         return OUString( pNew, (DO_NOT_ACQUIRE *)0 );
     }
 
@@ -1684,18 +1643,11 @@ public:
     static OUString intern( const sal_Char * value, sal_Int32 length,
                             rtl_TextEncoding encoding,
                             sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS,
-                            sal_uInt32 *pInfo = NULL )
+                            sal_uInt32 *pInfo = NULL ) SAL_THROW(())
     {
         rtl_uString * pNew = 0;
         rtl_uString_internConvert( &pNew, value, length, encoding,
                                    convertFlags, pInfo );
-        if (pNew == 0) {
-#if defined EXCEPTIONS_OFF
-            SAL_WARN("sal", "std::bad_alloc but EXCEPTIONS_OFF");
-#else
-            throw std::bad_alloc();
-#endif
-        }
         return OUString( pNew, (DO_NOT_ACQUIRE *)0 );
     }
 
-- 
1.7.3.4


Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.