1647f063dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3647f063dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4647f063dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5647f063dSAndrew Rist * distributed with this work for additional information 6647f063dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7647f063dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8647f063dSAndrew Rist * "License"); you may not use this file except in compliance 9647f063dSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11647f063dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13647f063dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14647f063dSAndrew Rist * software distributed under the License is distributed on an 15647f063dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16647f063dSAndrew Rist * KIND, either express or implied. See the License for the 17647f063dSAndrew Rist * specific language governing permissions and limitations 18647f063dSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20647f063dSAndrew Rist *************************************************************/ 21647f063dSAndrew Rist 22647f063dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "rtl/alloc.h" 25cdf0e10cSrcweir #include "alloc_impl.h" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #ifndef INCLUDED_STRING_H 28cdf0e10cSrcweir #include <string.h> 29cdf0e10cSrcweir #define INCLUDED_STRING_H 30cdf0e10cSrcweir #endif 31cdf0e10cSrcweir 32cdf0e10cSrcweir #if !defined(FORCE_SYSALLOC) 33cdf0e10cSrcweir 34cdf0e10cSrcweir /* ================================================================= * 35cdf0e10cSrcweir * 36cdf0e10cSrcweir * custom allocator includes. 37cdf0e10cSrcweir * 38cdf0e10cSrcweir * ================================================================= */ 39cdf0e10cSrcweir 40cdf0e10cSrcweir #ifndef INCLUDED_STDIO_H 41cdf0e10cSrcweir #include <stdio.h> 42cdf0e10cSrcweir #define INCLUDED_STDIO_H 43cdf0e10cSrcweir #endif 44cdf0e10cSrcweir #include "internal/once.h" 45cdf0e10cSrcweir #include "sal/macros.h" 46cdf0e10cSrcweir #include "osl/diagnose.h" 47cdf0e10cSrcweir 48cdf0e10cSrcweir /* ================================================================= * 49cdf0e10cSrcweir * 50cdf0e10cSrcweir * custom allocator internals. 51cdf0e10cSrcweir * 52cdf0e10cSrcweir * ================================================================= */ 53cdf0e10cSrcweir 54cdf0e10cSrcweir static const sal_Size g_alloc_sizes[] = 55cdf0e10cSrcweir { 56cdf0e10cSrcweir /* powers of 2**(1/4) */ 57cdf0e10cSrcweir 4 * 4, 6 * 4, 58cdf0e10cSrcweir 4 * 8, 5 * 8, 6 * 8, 7 * 8, 59cdf0e10cSrcweir 4 * 16, 5 * 16, 6 * 16, 7 * 16, 60cdf0e10cSrcweir 4 * 32, 5 * 32, 6 * 32, 7 * 32, 61cdf0e10cSrcweir 4 * 64, 5 * 64, 6 * 64, 7 * 64, 62cdf0e10cSrcweir 4 * 128, 5 * 128, 6 * 128, 7 * 128, 63cdf0e10cSrcweir 4 * 256, 5 * 256, 6 * 256, 7 * 256, 64cdf0e10cSrcweir 4 * 512, 5 * 512, 6 * 512, 7 * 512, 65cdf0e10cSrcweir 4 * 1024, 5 * 1024, 6 * 1024, 7 * 1024, 66cdf0e10cSrcweir 4 * 2048, 5 * 2048, 6 * 2048, 7 * 2048, 67cdf0e10cSrcweir 4 * 4096 68cdf0e10cSrcweir }; 69cdf0e10cSrcweir 70cdf0e10cSrcweir #define RTL_MEMORY_CACHED_LIMIT 4 * 4096 71cdf0e10cSrcweir #define RTL_MEMORY_CACHED_SIZES (sizeof(g_alloc_sizes) / sizeof(g_alloc_sizes[0])) 72cdf0e10cSrcweir 73cdf0e10cSrcweir static rtl_cache_type * g_alloc_caches[RTL_MEMORY_CACHED_SIZES] = 74cdf0e10cSrcweir { 75cdf0e10cSrcweir 0, 76cdf0e10cSrcweir }; 77cdf0e10cSrcweir 78cdf0e10cSrcweir #define RTL_MEMALIGN 8 79cdf0e10cSrcweir #define RTL_MEMALIGN_SHIFT 3 80cdf0e10cSrcweir 81cdf0e10cSrcweir static rtl_cache_type * g_alloc_table[RTL_MEMORY_CACHED_LIMIT >> RTL_MEMALIGN_SHIFT] = 82cdf0e10cSrcweir { 83cdf0e10cSrcweir 0, 84cdf0e10cSrcweir }; 85cdf0e10cSrcweir 86*509a48ffSpfg static rtl_arena_type * gp_alloc_arena = NULL; 87cdf0e10cSrcweir 88cdf0e10cSrcweir /* ================================================================= * 89cdf0e10cSrcweir * 90cdf0e10cSrcweir * custom allocator initialization / finalization. 91cdf0e10cSrcweir * 92cdf0e10cSrcweir * ================================================================= */ 93cdf0e10cSrcweir 94cdf0e10cSrcweir static void 95cdf0e10cSrcweir rtl_memory_once_init (void) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir { 98cdf0e10cSrcweir /* global memory arena */ 99*509a48ffSpfg OSL_ASSERT(gp_alloc_arena == NULL); 100cdf0e10cSrcweir 101cdf0e10cSrcweir gp_alloc_arena = rtl_arena_create ( 102cdf0e10cSrcweir "rtl_alloc_arena", 103cdf0e10cSrcweir 2048, /* quantum */ 104cdf0e10cSrcweir 0, /* w/o quantum caching */ 105cdf0e10cSrcweir 0, /* default source */ 106cdf0e10cSrcweir rtl_arena_alloc, 107cdf0e10cSrcweir rtl_arena_free, 108cdf0e10cSrcweir 0 /* flags */ 109cdf0e10cSrcweir ); 110*509a48ffSpfg OSL_ASSERT(gp_alloc_arena != NULL); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir { 113cdf0e10cSrcweir sal_Size size; 114cdf0e10cSrcweir int i, n = RTL_MEMORY_CACHED_SIZES; 115cdf0e10cSrcweir 116cdf0e10cSrcweir for (i = 0; i < n; i++) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir char name[RTL_CACHE_NAME_LENGTH + 1]; 119cdf0e10cSrcweir (void) snprintf (name, sizeof(name), "rtl_alloc_%lu", g_alloc_sizes[i]); 120cdf0e10cSrcweir g_alloc_caches[i] = rtl_cache_create (name, g_alloc_sizes[i], 0, NULL, NULL, NULL, NULL, NULL, 0); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir size = RTL_MEMALIGN; 124cdf0e10cSrcweir for (i = 0; i < n; i++) 125cdf0e10cSrcweir { 126cdf0e10cSrcweir while (size <= g_alloc_sizes[i]) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT] = g_alloc_caches[i]; 129cdf0e10cSrcweir size += RTL_MEMALIGN; 130cdf0e10cSrcweir } 131cdf0e10cSrcweir } 132cdf0e10cSrcweir } 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir static int 136cdf0e10cSrcweir rtl_memory_init (void) 137cdf0e10cSrcweir { 138cdf0e10cSrcweir static sal_once_type g_once = SAL_ONCE_INIT; 139cdf0e10cSrcweir SAL_ONCE(&g_once, rtl_memory_once_init); 140*509a48ffSpfg return (gp_alloc_arena != NULL); 141cdf0e10cSrcweir } 142cdf0e10cSrcweir 143cdf0e10cSrcweir /* ================================================================= */ 144cdf0e10cSrcweir 145cdf0e10cSrcweir /* 146cdf0e10cSrcweir Issue http://udk.openoffice.org/issues/show_bug.cgi?id=92388 147cdf0e10cSrcweir 148cdf0e10cSrcweir Mac OS X does not seem to support "__cxa__atexit", thus leading 149cdf0e10cSrcweir to the situation that "__attribute__((destructor))__" functions 150cdf0e10cSrcweir (in particular "rtl_{memory|cache|arena}_fini") become called 151cdf0e10cSrcweir _before_ global C++ object d'tors. 152cdf0e10cSrcweir 153cdf0e10cSrcweir Delegated the call to "rtl_memory_fini()" into a dummy C++ object, 154cdf0e10cSrcweir see alloc_fini.cxx . 155cdf0e10cSrcweir */ 156cdf0e10cSrcweir #if defined(__GNUC__) && !defined(MACOSX) 157cdf0e10cSrcweir static void rtl_memory_fini (void) __attribute__((destructor)); 158cdf0e10cSrcweir #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) 159cdf0e10cSrcweir #pragma fini(rtl_memory_fini) 160cdf0e10cSrcweir static void rtl_memory_fini (void); 161cdf0e10cSrcweir #endif /* __GNUC__ || __SUNPRO_C */ 162cdf0e10cSrcweir 163cdf0e10cSrcweir void 164cdf0e10cSrcweir rtl_memory_fini (void) 165cdf0e10cSrcweir { 166cdf0e10cSrcweir int i, n; 167cdf0e10cSrcweir 168cdf0e10cSrcweir /* clear g_alloc_table */ 169cdf0e10cSrcweir memset (g_alloc_table, 0, sizeof(g_alloc_table)); 170cdf0e10cSrcweir 171cdf0e10cSrcweir /* cleanup g_alloc_caches */ 172cdf0e10cSrcweir for (i = 0, n = RTL_MEMORY_CACHED_SIZES; i < n; i++) 173cdf0e10cSrcweir { 174*509a48ffSpfg if (g_alloc_caches[i] != NULL) 175cdf0e10cSrcweir { 176cdf0e10cSrcweir rtl_cache_destroy (g_alloc_caches[i]); 177*509a48ffSpfg g_alloc_caches[i] = NULL; 178cdf0e10cSrcweir } 179cdf0e10cSrcweir } 180cdf0e10cSrcweir 181cdf0e10cSrcweir /* cleanup gp_alloc_arena */ 182*509a48ffSpfg if (gp_alloc_arena != NULL) 183cdf0e10cSrcweir { 184cdf0e10cSrcweir rtl_arena_destroy (gp_alloc_arena); 185*509a48ffSpfg gp_alloc_arena = NULL; 186cdf0e10cSrcweir } 187cdf0e10cSrcweir } 188cdf0e10cSrcweir 189cdf0e10cSrcweir /* ================================================================= * 190cdf0e10cSrcweir * 191cdf0e10cSrcweir * custom allocator implemenation. 192cdf0e10cSrcweir * 193cdf0e10cSrcweir * ================================================================= */ 194cdf0e10cSrcweir 195cdf0e10cSrcweir void * 196cdf0e10cSrcweir SAL_CALL rtl_allocateMemory (sal_Size n) SAL_THROW_EXTERN_C() 197cdf0e10cSrcweir { 198cdf0e10cSrcweir void * p = 0; 199cdf0e10cSrcweir if (n > 0) 200cdf0e10cSrcweir { 201cdf0e10cSrcweir char * addr; 202cdf0e10cSrcweir sal_Size size = RTL_MEMORY_ALIGN(n + RTL_MEMALIGN, RTL_MEMALIGN); 203cdf0e10cSrcweir 204cdf0e10cSrcweir OSL_ASSERT(RTL_MEMALIGN >= sizeof(sal_Size)); 205cdf0e10cSrcweir if (n >= SAL_MAX_SIZE - (RTL_MEMALIGN + RTL_MEMALIGN - 1)) 206cdf0e10cSrcweir { 207cdf0e10cSrcweir /* requested size too large for roundup alignment */ 208cdf0e10cSrcweir return 0; 209cdf0e10cSrcweir } 210cdf0e10cSrcweir 211cdf0e10cSrcweir try_alloc: 212cdf0e10cSrcweir if (size <= RTL_MEMORY_CACHED_LIMIT) 213cdf0e10cSrcweir addr = (char*)rtl_cache_alloc(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT]); 214cdf0e10cSrcweir else 215cdf0e10cSrcweir addr = (char*)rtl_arena_alloc (gp_alloc_arena, &size); 216cdf0e10cSrcweir 217cdf0e10cSrcweir if (addr != 0) 218cdf0e10cSrcweir { 219cdf0e10cSrcweir ((sal_Size*)(addr))[0] = size; 220cdf0e10cSrcweir p = addr + RTL_MEMALIGN; 221cdf0e10cSrcweir } 222cdf0e10cSrcweir else if (gp_alloc_arena == 0) 223cdf0e10cSrcweir { 224cdf0e10cSrcweir if (rtl_memory_init()) 225cdf0e10cSrcweir { 226cdf0e10cSrcweir /* try again */ 227cdf0e10cSrcweir goto try_alloc; 228cdf0e10cSrcweir } 229cdf0e10cSrcweir } 230cdf0e10cSrcweir } 231cdf0e10cSrcweir return (p); 232cdf0e10cSrcweir } 233cdf0e10cSrcweir 234cdf0e10cSrcweir /* ================================================================= */ 235cdf0e10cSrcweir 236cdf0e10cSrcweir void SAL_CALL rtl_freeMemory (void * p) SAL_THROW_EXTERN_C() 237cdf0e10cSrcweir { 238cdf0e10cSrcweir if (p != 0) 239cdf0e10cSrcweir { 240cdf0e10cSrcweir char * addr = (char*)(p) - RTL_MEMALIGN; 241cdf0e10cSrcweir sal_Size size = ((sal_Size*)(addr))[0]; 242cdf0e10cSrcweir 243cdf0e10cSrcweir if (size <= RTL_MEMORY_CACHED_LIMIT) 244cdf0e10cSrcweir rtl_cache_free(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT], addr); 245cdf0e10cSrcweir else 246cdf0e10cSrcweir rtl_arena_free (gp_alloc_arena, addr, size); 247cdf0e10cSrcweir } 248cdf0e10cSrcweir } 249cdf0e10cSrcweir 250cdf0e10cSrcweir /* ================================================================= */ 251cdf0e10cSrcweir 252cdf0e10cSrcweir void * SAL_CALL rtl_reallocateMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C() 253cdf0e10cSrcweir { 254cdf0e10cSrcweir if (n > 0) 255cdf0e10cSrcweir { 256cdf0e10cSrcweir if (p != 0) 257cdf0e10cSrcweir { 258cdf0e10cSrcweir void * p_old = p; 259cdf0e10cSrcweir sal_Size n_old = ((sal_Size*)( (char*)(p) - RTL_MEMALIGN ))[0] - RTL_MEMALIGN; 260cdf0e10cSrcweir 261cdf0e10cSrcweir p = rtl_allocateMemory (n); 262cdf0e10cSrcweir if (p != 0) 263cdf0e10cSrcweir { 264cdf0e10cSrcweir memcpy (p, p_old, SAL_MIN(n, n_old)); 265cdf0e10cSrcweir rtl_freeMemory (p_old); 266cdf0e10cSrcweir } 267cdf0e10cSrcweir } 268cdf0e10cSrcweir else 269cdf0e10cSrcweir { 270cdf0e10cSrcweir p = rtl_allocateMemory (n); 271cdf0e10cSrcweir } 272cdf0e10cSrcweir } 273cdf0e10cSrcweir else if (p != 0) 274cdf0e10cSrcweir { 275cdf0e10cSrcweir rtl_freeMemory (p), p = 0; 276cdf0e10cSrcweir } 277cdf0e10cSrcweir return (p); 278cdf0e10cSrcweir } 279cdf0e10cSrcweir 280cdf0e10cSrcweir #else /* FORCE_SYSALLOC */ 281cdf0e10cSrcweir 282cdf0e10cSrcweir /* ================================================================= * 283cdf0e10cSrcweir * 284cdf0e10cSrcweir * system allocator includes. 285cdf0e10cSrcweir * 286cdf0e10cSrcweir * ================================================================= */ 287cdf0e10cSrcweir 288cdf0e10cSrcweir #ifndef INCLUDED_STDLIB_H 289cdf0e10cSrcweir #include <stdlib.h> 290cdf0e10cSrcweir #define INCLUDED_STDLIB_H 291cdf0e10cSrcweir #endif 292cdf0e10cSrcweir 293cdf0e10cSrcweir /* ================================================================= * 294cdf0e10cSrcweir * 295cdf0e10cSrcweir * system allocator implemenation. 296cdf0e10cSrcweir * 297cdf0e10cSrcweir * ================================================================= */ 298cdf0e10cSrcweir 299cdf0e10cSrcweir void * SAL_CALL rtl_allocateMemory (sal_Size n) 300cdf0e10cSrcweir { 301cdf0e10cSrcweir return malloc (n); 302cdf0e10cSrcweir } 303cdf0e10cSrcweir 304cdf0e10cSrcweir /* ================================================================= */ 305cdf0e10cSrcweir 306cdf0e10cSrcweir void SAL_CALL rtl_freeMemory (void * p) 307cdf0e10cSrcweir { 308cdf0e10cSrcweir free (p); 309cdf0e10cSrcweir } 310cdf0e10cSrcweir 311cdf0e10cSrcweir /* ================================================================= */ 312cdf0e10cSrcweir 313cdf0e10cSrcweir void * SAL_CALL rtl_reallocateMemory (void * p, sal_Size n) 314cdf0e10cSrcweir { 315cdf0e10cSrcweir return realloc (p, n); 316cdf0e10cSrcweir } 317cdf0e10cSrcweir 318cdf0e10cSrcweir /* ================================================================= */ 319cdf0e10cSrcweir 320cdf0e10cSrcweir void 321cdf0e10cSrcweir rtl_memory_fini (void) 322cdf0e10cSrcweir { 323cdf0e10cSrcweir /* nothing to do */ 324cdf0e10cSrcweir } 325cdf0e10cSrcweir 326cdf0e10cSrcweir #endif /* FORCE_SYSALLOC */ 327cdf0e10cSrcweir 328cdf0e10cSrcweir /* ================================================================= * 329cdf0e10cSrcweir * 330cdf0e10cSrcweir * rtl_(allocate|free)ZeroMemory() implemenation. 331cdf0e10cSrcweir * 332cdf0e10cSrcweir * ================================================================= */ 333cdf0e10cSrcweir 334cdf0e10cSrcweir void * SAL_CALL rtl_allocateZeroMemory (sal_Size n) SAL_THROW_EXTERN_C() 335cdf0e10cSrcweir { 336cdf0e10cSrcweir void * p = rtl_allocateMemory (n); 337cdf0e10cSrcweir if (p != 0) 338cdf0e10cSrcweir memset (p, 0, n); 339cdf0e10cSrcweir return (p); 340cdf0e10cSrcweir } 341cdf0e10cSrcweir 342cdf0e10cSrcweir /* ================================================================= */ 343cdf0e10cSrcweir 344cdf0e10cSrcweir void SAL_CALL rtl_freeZeroMemory (void * p, sal_Size n) SAL_THROW_EXTERN_C() 345cdf0e10cSrcweir { 346cdf0e10cSrcweir if (p != 0) 347cdf0e10cSrcweir { 348cdf0e10cSrcweir memset (p, 0, n); 349cdf0e10cSrcweir rtl_freeMemory (p); 350cdf0e10cSrcweir } 351cdf0e10cSrcweir } 352cdf0e10cSrcweir 353cdf0e10cSrcweir /* ================================================================= */ 354