diff options
Diffstat (limited to 'sal/rtl/alloc_global.cxx')
-rw-r--r-- | sal/rtl/alloc_global.cxx | 265 |
1 files changed, 3 insertions, 262 deletions
diff --git a/sal/rtl/alloc_global.cxx b/sal/rtl/alloc_global.cxx index 1a3a26f00069..9ceba4f4120f 100644 --- a/sal/rtl/alloc_global.cxx +++ b/sal/rtl/alloc_global.cxx @@ -30,239 +30,12 @@ #include <rtllifecycle.h> #include <oslmemory.h> -AllocMode alloc_mode = AllocMode::UNSET; - -#if !defined(FORCE_SYSALLOC) -static void determine_alloc_mode() -{ - assert(alloc_mode == AllocMode::UNSET); - alloc_mode = AllocMode::SYSTEM; -} - -static const sal_Size g_alloc_sizes[] = -{ - /* powers of 2**(1/4) */ - 4 * 4, 6 * 4, - 4 * 8, 5 * 8, 6 * 8, 7 * 8, - 4 * 16, 5 * 16, 6 * 16, 7 * 16, - 4 * 32, 5 * 32, 6 * 32, 7 * 32, - 4 * 64, 5 * 64, 6 * 64, 7 * 64, - 4 * 128, 5 * 128, 6 * 128, 7 * 128, - 4 * 256, 5 * 256, 6 * 256, 7 * 256, - 4 * 512, 5 * 512, 6 * 512, 7 * 512, - 4 * 1024, 5 * 1024, 6 * 1024, 7 * 1024, - 4 * 2048, 5 * 2048, 6 * 2048, 7 * 2048, - 4 * 4096 -}; - -#define RTL_MEMORY_CACHED_LIMIT 4 * 4096 -#define RTL_MEMORY_CACHED_SIZES (SAL_N_ELEMENTS(g_alloc_sizes)) - -static rtl_cache_type * g_alloc_caches[RTL_MEMORY_CACHED_SIZES] = -{ - nullptr, -}; - -#define RTL_MEMALIGN 8 -#define RTL_MEMALIGN_SHIFT 3 - -static rtl_cache_type * g_alloc_table[RTL_MEMORY_CACHED_LIMIT >> RTL_MEMALIGN_SHIFT] = -{ - nullptr, -}; - -static rtl_arena_type * gp_alloc_arena = nullptr; - -void * rtl_allocateMemory_CUSTOM(sal_Size n) SAL_THROW_EXTERN_C() -{ - void * p = nullptr; - if (n > 0) - { - char * addr; - sal_Size size = RTL_MEMORY_ALIGN(n + RTL_MEMALIGN, RTL_MEMALIGN); - - assert(RTL_MEMALIGN >= sizeof(sal_Size)); - if (n >= SAL_MAX_SIZE - (RTL_MEMALIGN + RTL_MEMALIGN - 1)) - { - /* requested size too large for roundup alignment */ - return nullptr; - } - -try_alloc: - if (size <= RTL_MEMORY_CACHED_LIMIT) - addr = static_cast<char*>(rtl_cache_alloc(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT])); - else - addr = static_cast<char*>(rtl_arena_alloc (gp_alloc_arena, &size)); - - if (addr) - { - reinterpret_cast<sal_Size*>(addr)[0] = size; - p = addr + RTL_MEMALIGN; - } - else if (!gp_alloc_arena) - { - ensureMemorySingleton(); - if (gp_alloc_arena) - { - /* try again */ - goto try_alloc; - } - } - } - return p; -} - -void rtl_freeMemory_CUSTOM (void * p) SAL_THROW_EXTERN_C() -{ - if (p) - { - char * addr = static_cast<char*>(p) - RTL_MEMALIGN; - sal_Size size = reinterpret_cast<sal_Size*>(addr)[0]; - - if (size <= RTL_MEMORY_CACHED_LIMIT) - rtl_cache_free(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT], addr); - else - rtl_arena_free (gp_alloc_arena, addr, size); - } -} - -void * rtl_reallocateMemory_CUSTOM (void * p, sal_Size n) SAL_THROW_EXTERN_C() -{ - if (n > 0) - { - if (p) - { - void * p_old = p; - sal_Size n_old = reinterpret_cast<sal_Size*>( static_cast<char*>(p) - RTL_MEMALIGN )[0] - RTL_MEMALIGN; - - p = rtl_allocateMemory (n); - if (p) - { - memcpy (p, p_old, std::min(n, n_old)); - rtl_freeMemory (p_old); - } - } - else - { - p = rtl_allocateMemory (n); - } - } - else if (p) - { - rtl_freeMemory (p); - p = nullptr; - } - return p; -} - -#endif - -void rtl_memory_init() -{ -#if !defined(FORCE_SYSALLOC) - { - /* global memory arena */ - assert(!gp_alloc_arena); - - gp_alloc_arena = rtl_arena_create ( - "rtl_alloc_arena", - 2048, /* quantum */ - 0, /* w/o quantum caching */ - nullptr, /* default source */ - rtl_arena_alloc, - rtl_arena_free, - 0 /* flags */ - ); - assert(gp_alloc_arena); - } - { - sal_Size size; - int i, n = RTL_MEMORY_CACHED_SIZES; - - for (i = 0; i < n; i++) - { - char name[RTL_CACHE_NAME_LENGTH + 1]; - (void) snprintf (name, sizeof(name), "rtl_alloc_%" SAL_PRIuUINTPTR, g_alloc_sizes[i]); - g_alloc_caches[i] = rtl_cache_create (name, g_alloc_sizes[i], 0, nullptr, nullptr, nullptr, nullptr, nullptr, 0); - } - - size = RTL_MEMALIGN; - for (i = 0; i < n; i++) - { - while (size <= g_alloc_sizes[i]) - { - g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT] = g_alloc_caches[i]; - size += RTL_MEMALIGN; - } - } - } -#endif -} - -void rtl_memory_fini() -{ -#if !defined(FORCE_SYSALLOC) - int i, n; - - /* clear g_alloc_table */ - // cppcheck-suppress sizeofwithsilentarraypointer - memset (g_alloc_table, 0, sizeof(g_alloc_table)); - - /* cleanup g_alloc_caches */ - for (i = 0, n = RTL_MEMORY_CACHED_SIZES; i < n; i++) - { - if (g_alloc_caches[i]) - { - rtl_cache_destroy (g_alloc_caches[i]); - g_alloc_caches[i] = nullptr; - } - } - - /* cleanup gp_alloc_arena */ - if (gp_alloc_arena) - { - rtl_arena_destroy (gp_alloc_arena); - gp_alloc_arena = nullptr; - } -#endif -} - -void * rtl_allocateMemory_SYSTEM(sal_Size n) -{ - return malloc (n); -} - -void rtl_freeMemory_SYSTEM(void * p) -{ - free (p); -} - -void * rtl_reallocateMemory_SYSTEM(void * p, sal_Size n) -{ - return realloc (p, n); -} - void* SAL_CALL rtl_allocateMemory(sal_Size n) SAL_THROW_EXTERN_C() { SAL_WARN_IF( n >= SAL_MAX_INT32, "sal.rtl", "suspicious massive alloc " << n); -#if !defined(FORCE_SYSALLOC) - while (true) - { - if (alloc_mode == AllocMode::CUSTOM) - { - return rtl_allocateMemory_CUSTOM(n); - } - if (alloc_mode == AllocMode::SYSTEM) - { - return rtl_allocateMemory_SYSTEM(n); - } - determine_alloc_mode(); - } -#else - return rtl_allocateMemory_SYSTEM(n); -#endif + return malloc (n); } void* SAL_CALL rtl_reallocateMemory(void * p, sal_Size n) SAL_THROW_EXTERN_C() @@ -270,44 +43,12 @@ void* SAL_CALL rtl_reallocateMemory(void * p, sal_Size n) SAL_THROW_EXTERN_C() SAL_WARN_IF( n >= SAL_MAX_INT32, "sal.rtl", "suspicious massive alloc " << n); -#if !defined(FORCE_SYSALLOC) - while (true) - { - if (alloc_mode == AllocMode::CUSTOM) - { - return rtl_reallocateMemory_CUSTOM(p,n); - } - if (alloc_mode == AllocMode::SYSTEM) - { - return rtl_reallocateMemory_SYSTEM(p,n); - } - determine_alloc_mode(); - } -#else - return rtl_reallocateMemory_SYSTEM(p,n); -#endif + return realloc (p, n); } void SAL_CALL rtl_freeMemory(void * p) SAL_THROW_EXTERN_C() { -#if !defined(FORCE_SYSALLOC) - while (true) - { - if (alloc_mode == AllocMode::CUSTOM) - { - rtl_freeMemory_CUSTOM(p); - return; - } - if (alloc_mode == AllocMode::SYSTEM) - { - rtl_freeMemory_SYSTEM(p); - return; - } - determine_alloc_mode(); - } -#else - rtl_freeMemory_SYSTEM(p); -#endif + free (p); } void * SAL_CALL rtl_allocateZeroMemory(sal_Size n) SAL_THROW_EXTERN_C() |