diff options
author | Michael Meeks <michael.meeks@collabora.com> | 2016-01-29 12:11:50 +0100 |
---|---|---|
committer | Ashod Nakashian <ashod.nakashian@collabora.co.uk> | 2016-01-29 20:55:21 -0500 |
commit | 26d69c1c0321a657d159b5c850044df1aa317d43 (patch) | |
tree | 15466309fa28327ec85b80e988aeaa45d2c46ce4 | |
parent | 1c565696231ae5296a47bbb9df78d80d2da031f4 (diff) |
Use a custom allocator for strings.
Change-Id: I627d56b08a72187b15d7992c42179a05bef71c2b
-rw-r--r-- | sal/rtl/alloc_fini.cxx | 3 | ||||
-rw-r--r-- | sal/rtl/alloc_global.cxx | 45 | ||||
-rw-r--r-- | sal/rtl/alloc_global.hxx | 27 | ||||
-rw-r--r-- | sal/rtl/string.cxx | 6 | ||||
-rw-r--r-- | sal/rtl/strtmpl.cxx | 6 | ||||
-rw-r--r-- | sal/rtl/ustring.cxx | 8 |
6 files changed, 86 insertions, 9 deletions
diff --git a/sal/rtl/alloc_fini.cxx b/sal/rtl/alloc_fini.cxx index 32c6fd5a5996..b7f8b8f69917 100644 --- a/sal/rtl/alloc_fini.cxx +++ b/sal/rtl/alloc_fini.cxx @@ -20,6 +20,7 @@ #include <rtl/instance.hxx> #include "internal/rtllifecycle.h" +#include "alloc_global.hxx" namespace { @@ -72,9 +73,11 @@ namespace rtlArenaSingleton() { rtl_arena_init(); + rtl_string_alloc_init(); } ~rtlArenaSingleton() { + rtl_string_alloc_fini(); rtl_arena_fini(); } }; diff --git a/sal/rtl/alloc_global.cxx b/sal/rtl/alloc_global.cxx index 60dbc9c85b78..32df71f59fa4 100644 --- a/sal/rtl/alloc_global.cxx +++ b/sal/rtl/alloc_global.cxx @@ -28,6 +28,7 @@ #include "internal/rtllifecycle.h" #include <internal/oslmemory.h> +#include "alloc_global.hxx" AllocMode alloc_mode = AMode_UNSET; @@ -391,4 +392,48 @@ void SAL_CALL rtl_freeAlignedMemory (void* Ptr) SAL_THROW_EXTERN_C() /* ================================================================= */ +namespace { + +static rtl_memory_lock_type pLock; +static sal_uInt64 nBytesAllocated = 0; + +void *SAL_CALL string_allocate(sal_Size n) +{ + RTL_MEMORY_LOCK_ACQUIRE(&pLock); + nBytesAllocated += n; + RTL_MEMORY_LOCK_RELEASE(&pLock); + sal_Size *p = static_cast<sal_Size *>(rtl_allocateMemory(n + sizeof(sal_Size))); + *p = n; + return p + 1; +} + +void SAL_CALL string_free(void * p) +{ + sal_Size *prefix = static_cast<sal_Size *>(p); + prefix--; + sal_Size n = *prefix; + RTL_MEMORY_LOCK_ACQUIRE(&pLock); + nBytesAllocated -= n; + static int nPrint = 0; + if (nPrint++ % 100 == 0) + fprintf(stderr,"Total strings allocated %ld\n", (long)nBytesAllocated); + RTL_MEMORY_LOCK_RELEASE(&pLock); + + rtl_freeMemory(prefix); +} + +} + +void SAL_CALL rtl_string_alloc_init() +{ + RTL_MEMORY_LOCK_INIT(&pLock); +} + +void SAL_CALL rtl_string_alloc_fini() +{ +} + +rtl_AllocateMemory *rtl_StringAllocateMemory = string_allocate; +rtl_FreeMemory *rtl_StringFreeMemory = string_free; + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/rtl/alloc_global.hxx b/sal/rtl/alloc_global.hxx new file mode 100644 index 000000000000..3c3fbb82b3a9 --- /dev/null +++ b/sal/rtl/alloc_global.hxx @@ -0,0 +1,27 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +#ifndef INCLUDED_SAL_RTL_ALLOC_GLOBAL_HXX +#define INCLUDED_SAL_RTL_ALLOC_GLOBAL_HXX + +#include "sal/types.h" + +typedef void *SAL_CALL (rtl_AllocateMemory) (sal_Size n); +typedef void SAL_CALL (rtl_FreeMemory) (void * p); + +// String allocation function pointers + +extern rtl_AllocateMemory *rtl_StringAllocateMemory; +extern rtl_FreeMemory *rtl_StringFreeMemory; + +extern void SAL_CALL rtl_string_alloc_init(); +extern void SAL_CALL rtl_string_alloc_fini(); + +#endif // INCLUDED_SAL_RTL_ALLOC_GLOBAL_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx index 3647908c3ada..22bcfa1389d8 100644 --- a/sal/rtl/string.cxx +++ b/sal/rtl/string.cxx @@ -274,7 +274,7 @@ bool SAL_CALL rtl_impl_convertUStringToString(rtl_String ** pTarget, &nInfo, &nSrcChars ); if (bCheckErrors && (nInfo & RTL_UNICODETOTEXT_INFO_ERROR) != 0) { - rtl_freeMemory(pTemp); + rtl_StringFreeMemory(pTemp); rtl_destroyUnicodeToTextConverter(hConverter); return false; } @@ -283,7 +283,7 @@ bool SAL_CALL rtl_impl_convertUStringToString(rtl_String ** pTarget, break; /* Buffer not big enough, try again with enough space */ - rtl_freeMemory( pTemp ); + rtl_StringFreeMemory( pTemp ); /* Try with the max. count of characters with additional overhead for replacing functionality */ @@ -298,7 +298,7 @@ bool SAL_CALL rtl_impl_convertUStringToString(rtl_String ** pTarget, rtl_String* pTemp2 = rtl_string_ImplAlloc( nDestBytes ); OSL_ASSERT(pTemp2 != NULL); rtl_str_ImplCopy( pTemp2->buffer, pTemp->buffer, nDestBytes ); - rtl_freeMemory( pTemp ); + rtl_StringFreeMemory( pTemp ); pTemp = pTemp2; } else diff --git a/sal/rtl/strtmpl.cxx b/sal/rtl/strtmpl.cxx index 1dba94baf6e3..a362fbf4c0d2 100644 --- a/sal/rtl/strtmpl.cxx +++ b/sal/rtl/strtmpl.cxx @@ -31,6 +31,8 @@ #include <sal/log.hxx> #include <rtl/character.hxx> +#include "alloc_global.hxx" + /* inline void rtl_str_ImplCopy( IMPL_RTL_STRCODE* pDest, const IMPL_RTL_STRCODE* pSrc, @@ -1148,7 +1150,7 @@ static IMPL_RTL_STRINGDATA* IMPL_RTL_STRINGNAME( ImplAlloc )( sal_Int32 nLen ) = (sal::static_int_cast< sal_uInt32 >(nLen) <= ((SAL_MAX_UINT32 - sizeof (IMPL_RTL_STRINGDATA)) / sizeof (IMPL_RTL_STRCODE))) - ? static_cast<IMPL_RTL_STRINGDATA *>(rtl_allocateMemory( + ? static_cast<IMPL_RTL_STRINGDATA *>(rtl_StringAllocateMemory( sizeof (IMPL_RTL_STRINGDATA) + nLen * sizeof (IMPL_RTL_STRCODE))) : NULL; if (pData != NULL) { @@ -1229,7 +1231,7 @@ void SAL_CALL IMPL_RTL_STRINGNAME( release )( IMPL_RTL_STRINGDATA* pThis ) if ( !osl_atomic_decrement( &(pThis->refCount) ) ) { RTL_LOG_STRING_DELETE( pThis ); - rtl_freeMemory( pThis ); + rtl_StringFreeMemory( pThis ); } } diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx index 940113d9e752..618b3feff243 100644 --- a/sal/rtl/ustring.cxx +++ b/sal/rtl/ustring.cxx @@ -768,7 +768,7 @@ retry: code here. Could be the case for apple encodings */ while ( nInfo & RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL ) { - rtl_freeMemory( pTemp ); + rtl_StringFreeMemory( pTemp ); nNewLen += 8; pTemp = rtl_uString_ImplAlloc( nNewLen ); if (pTemp == NULL) { @@ -797,7 +797,7 @@ retry: if (pTemp2 != NULL) { rtl_str_ImplCopy(pTemp2->buffer, pTemp->buffer, nDestChars); - rtl_freeMemory(pTemp); + rtl_StringFreeMemory(pTemp); pTemp = pTemp2; } else @@ -881,7 +881,7 @@ static void rtl_ustring_intern_internal( rtl_uString ** newStr, if( can_return && *newStr != str ) { /* we dupped, then found a match */ - rtl_freeMemory( str ); + rtl_StringFreeMemory( str ); } } @@ -1032,7 +1032,7 @@ internRelease (rtl_uString *pThis) osl_releaseMutex( pPoolMutex ); } if (pFree) - rtl_freeMemory (pFree); + rtl_StringFreeMemory( pFree ); } sal_uInt32 SAL_CALL rtl_uString_iterateCodePoints( |