1*647f063dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*647f063dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*647f063dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*647f063dSAndrew Rist * distributed with this work for additional information 6*647f063dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*647f063dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*647f063dSAndrew Rist * "License"); you may not use this file except in compliance 9*647f063dSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*647f063dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*647f063dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*647f063dSAndrew Rist * software distributed under the License is distributed on an 15*647f063dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*647f063dSAndrew Rist * KIND, either express or implied. See the License for the 17*647f063dSAndrew Rist * specific language governing permissions and limitations 18*647f063dSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*647f063dSAndrew Rist *************************************************************/ 21*647f063dSAndrew Rist 22*647f063dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #define _BSD_SOURCE /* sys/mman.h: MAP_ANON */ 25cdf0e10cSrcweir #include "alloc_arena.h" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "alloc_impl.h" 28cdf0e10cSrcweir #include "internal/once.h" 29cdf0e10cSrcweir #include "sal/macros.h" 30cdf0e10cSrcweir #include "osl/diagnose.h" 31cdf0e10cSrcweir 32cdf0e10cSrcweir #include <string.h> 33cdf0e10cSrcweir #include <stdio.h> 34cdf0e10cSrcweir 35cdf0e10cSrcweir #ifdef OS2 36cdf0e10cSrcweir #undef OSL_TRACE 37cdf0e10cSrcweir #define OSL_TRACE 1 ? ((void)0) : _OSL_GLOBAL osl_trace 38cdf0e10cSrcweir #define INCL_DOS 39cdf0e10cSrcweir #include <os2.h> 40cdf0e10cSrcweir #endif 41cdf0e10cSrcweir 42cdf0e10cSrcweir /* ================================================================= * 43cdf0e10cSrcweir * 44cdf0e10cSrcweir * arena internals. 45cdf0e10cSrcweir * 46cdf0e10cSrcweir * ================================================================= */ 47cdf0e10cSrcweir 48cdf0e10cSrcweir /** g_arena_list 49cdf0e10cSrcweir * @internal 50cdf0e10cSrcweir */ 51cdf0e10cSrcweir struct rtl_arena_list_st 52cdf0e10cSrcweir { 53cdf0e10cSrcweir rtl_memory_lock_type m_lock; 54cdf0e10cSrcweir rtl_arena_type m_arena_head; 55cdf0e10cSrcweir }; 56cdf0e10cSrcweir 57cdf0e10cSrcweir static struct rtl_arena_list_st g_arena_list; 58cdf0e10cSrcweir 59cdf0e10cSrcweir 60cdf0e10cSrcweir /** gp_arena_arena 61cdf0e10cSrcweir * provided for arena_type allocations, and hash_table resizing. 62cdf0e10cSrcweir * 63cdf0e10cSrcweir * @internal 64cdf0e10cSrcweir */ 65cdf0e10cSrcweir static rtl_arena_type * gp_arena_arena = 0; 66cdf0e10cSrcweir 67cdf0e10cSrcweir 68cdf0e10cSrcweir /** gp_machdep_arena 69cdf0e10cSrcweir * 70cdf0e10cSrcweir * Low level virtual memory (pseudo) arena 71cdf0e10cSrcweir * (platform dependent implementation) 72cdf0e10cSrcweir * 73cdf0e10cSrcweir * @internal 74cdf0e10cSrcweir */ 75cdf0e10cSrcweir static rtl_arena_type * gp_machdep_arena = 0; 76cdf0e10cSrcweir 77cdf0e10cSrcweir 78cdf0e10cSrcweir static void * 79cdf0e10cSrcweir SAL_CALL rtl_machdep_alloc ( 80cdf0e10cSrcweir rtl_arena_type * pArena, 81cdf0e10cSrcweir sal_Size * pSize 82cdf0e10cSrcweir ); 83cdf0e10cSrcweir 84cdf0e10cSrcweir static void 85cdf0e10cSrcweir SAL_CALL rtl_machdep_free ( 86cdf0e10cSrcweir rtl_arena_type * pArena, 87cdf0e10cSrcweir void * pAddr, 88cdf0e10cSrcweir sal_Size nSize 89cdf0e10cSrcweir ); 90cdf0e10cSrcweir 91cdf0e10cSrcweir static sal_Size 92cdf0e10cSrcweir rtl_machdep_pagesize (void); 93cdf0e10cSrcweir 94cdf0e10cSrcweir 95cdf0e10cSrcweir /** gp_default_arena 96cdf0e10cSrcweir */ 97cdf0e10cSrcweir rtl_arena_type * gp_default_arena = 0; 98cdf0e10cSrcweir 99cdf0e10cSrcweir 100cdf0e10cSrcweir /** rtl_arena_init() 101cdf0e10cSrcweir * @internal 102cdf0e10cSrcweir */ 103cdf0e10cSrcweir static int 104cdf0e10cSrcweir rtl_arena_init (void); 105cdf0e10cSrcweir 106cdf0e10cSrcweir 107cdf0e10cSrcweir /* ================================================================= */ 108cdf0e10cSrcweir 109cdf0e10cSrcweir /** rtl_arena_segment_constructor() 110cdf0e10cSrcweir */ 111cdf0e10cSrcweir static int 112cdf0e10cSrcweir rtl_arena_segment_constructor (void * obj) 113cdf0e10cSrcweir { 114cdf0e10cSrcweir rtl_arena_segment_type * segment = (rtl_arena_segment_type*)(obj); 115cdf0e10cSrcweir 116cdf0e10cSrcweir QUEUE_START_NAMED(segment, s); 117cdf0e10cSrcweir QUEUE_START_NAMED(segment, f); 118cdf0e10cSrcweir 119cdf0e10cSrcweir return (1); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir 123cdf0e10cSrcweir /** rtl_arena_segment_destructor() 124cdf0e10cSrcweir */ 125cdf0e10cSrcweir static void 126cdf0e10cSrcweir rtl_arena_segment_destructor (void * obj) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir #if OSL_DEBUG_LEVEL == 0 129cdf0e10cSrcweir (void) obj; /* unused */ 130cdf0e10cSrcweir #else /* OSL_DEBUG_LEVEL */ 131cdf0e10cSrcweir rtl_arena_segment_type * segment = (rtl_arena_segment_type*)(obj); 132cdf0e10cSrcweir 133cdf0e10cSrcweir OSL_ASSERT(QUEUE_STARTED_NAMED(segment, s)); 134cdf0e10cSrcweir OSL_ASSERT(QUEUE_STARTED_NAMED(segment, f)); 135cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */ 136cdf0e10cSrcweir } 137cdf0e10cSrcweir 138cdf0e10cSrcweir /* ================================================================= */ 139cdf0e10cSrcweir 140cdf0e10cSrcweir /** rtl_arena_segment_populate() 141cdf0e10cSrcweir * 142cdf0e10cSrcweir * @precond arena->m_lock acquired. 143cdf0e10cSrcweir */ 144cdf0e10cSrcweir static int 145cdf0e10cSrcweir rtl_arena_segment_populate ( 146cdf0e10cSrcweir rtl_arena_type * arena 147cdf0e10cSrcweir ) 148cdf0e10cSrcweir { 149cdf0e10cSrcweir rtl_arena_segment_type *span; 150cdf0e10cSrcweir sal_Size size = rtl_machdep_pagesize(); 151cdf0e10cSrcweir 152cdf0e10cSrcweir span = rtl_machdep_alloc(gp_machdep_arena, &size); 153cdf0e10cSrcweir if (span != 0) 154cdf0e10cSrcweir { 155cdf0e10cSrcweir rtl_arena_segment_type *first, *last, *head; 156cdf0e10cSrcweir sal_Size count = size / sizeof(rtl_arena_segment_type); 157cdf0e10cSrcweir 158cdf0e10cSrcweir /* insert onto reserve span list */ 159cdf0e10cSrcweir QUEUE_INSERT_TAIL_NAMED(&(arena->m_segment_reserve_span_head), span, s); 160cdf0e10cSrcweir QUEUE_START_NAMED(span, f); 161cdf0e10cSrcweir span->m_addr = (sal_uIntPtr)(span); 162cdf0e10cSrcweir span->m_size = size; 163cdf0e10cSrcweir span->m_type = RTL_ARENA_SEGMENT_TYPE_SPAN; 164cdf0e10cSrcweir 165cdf0e10cSrcweir /* insert onto reserve list */ 166cdf0e10cSrcweir head = &(arena->m_segment_reserve_head); 167cdf0e10cSrcweir for (first = span + 1, last = span + count; first < last; ++first) 168cdf0e10cSrcweir { 169cdf0e10cSrcweir QUEUE_INSERT_TAIL_NAMED(head, first, s); 170cdf0e10cSrcweir QUEUE_START_NAMED(first, f); 171cdf0e10cSrcweir first->m_addr = 0; 172cdf0e10cSrcweir first->m_size = 0; 173cdf0e10cSrcweir first->m_type = 0; 174cdf0e10cSrcweir } 175cdf0e10cSrcweir } 176cdf0e10cSrcweir return (span != 0); 177cdf0e10cSrcweir } 178cdf0e10cSrcweir 179cdf0e10cSrcweir 180cdf0e10cSrcweir /** rtl_arena_segment_get() 181cdf0e10cSrcweir * 182cdf0e10cSrcweir * @precond arena->m_lock acquired. 183cdf0e10cSrcweir * @precond (*ppSegment == 0) 184cdf0e10cSrcweir */ 185cdf0e10cSrcweir static RTL_MEMORY_INLINE void 186cdf0e10cSrcweir rtl_arena_segment_get ( 187cdf0e10cSrcweir rtl_arena_type * arena, 188cdf0e10cSrcweir rtl_arena_segment_type ** ppSegment 189cdf0e10cSrcweir ) 190cdf0e10cSrcweir { 191cdf0e10cSrcweir rtl_arena_segment_type * head; 192cdf0e10cSrcweir 193cdf0e10cSrcweir OSL_ASSERT(*ppSegment == 0); 194cdf0e10cSrcweir 195cdf0e10cSrcweir head = &(arena->m_segment_reserve_head); 196cdf0e10cSrcweir if ((head->m_snext != head) || rtl_arena_segment_populate (arena)) 197cdf0e10cSrcweir { 198cdf0e10cSrcweir (*ppSegment) = head->m_snext; 199cdf0e10cSrcweir QUEUE_REMOVE_NAMED((*ppSegment), s); 200cdf0e10cSrcweir } 201cdf0e10cSrcweir } 202cdf0e10cSrcweir 203cdf0e10cSrcweir #if defined(__SUNPRO_C) || defined(__SUNPRO_CC) 204cdf0e10cSrcweir #pragma inline(rtl_arena_segment_get) 205cdf0e10cSrcweir #endif 206cdf0e10cSrcweir 207cdf0e10cSrcweir 208cdf0e10cSrcweir /** rtl_arena_segment_put() 209cdf0e10cSrcweir * 210cdf0e10cSrcweir * @precond arena->m_lock acquired. 211cdf0e10cSrcweir * @postcond (*ppSegment == 0) 212cdf0e10cSrcweir */ 213cdf0e10cSrcweir static RTL_MEMORY_INLINE void 214cdf0e10cSrcweir rtl_arena_segment_put ( 215cdf0e10cSrcweir rtl_arena_type * arena, 216cdf0e10cSrcweir rtl_arena_segment_type ** ppSegment 217cdf0e10cSrcweir ) 218cdf0e10cSrcweir { 219cdf0e10cSrcweir rtl_arena_segment_type * head; 220cdf0e10cSrcweir 221cdf0e10cSrcweir OSL_ASSERT(QUEUE_STARTED_NAMED((*ppSegment), s)); 222cdf0e10cSrcweir OSL_ASSERT(QUEUE_STARTED_NAMED((*ppSegment), f)); 223cdf0e10cSrcweir 224cdf0e10cSrcweir (*ppSegment)->m_addr = 0; 225cdf0e10cSrcweir (*ppSegment)->m_size = 0; 226cdf0e10cSrcweir 227cdf0e10cSrcweir OSL_ASSERT((*ppSegment)->m_type != RTL_ARENA_SEGMENT_TYPE_HEAD); 228cdf0e10cSrcweir (*ppSegment)->m_type = 0; 229cdf0e10cSrcweir 230cdf0e10cSrcweir /* keep as reserve */ 231cdf0e10cSrcweir head = &(arena->m_segment_reserve_head); 232cdf0e10cSrcweir QUEUE_INSERT_HEAD_NAMED(head, (*ppSegment), s); 233cdf0e10cSrcweir 234cdf0e10cSrcweir /* clear */ 235cdf0e10cSrcweir (*ppSegment) = 0; 236cdf0e10cSrcweir } 237cdf0e10cSrcweir 238cdf0e10cSrcweir #if defined(__SUNPRO_C) || defined(__SUNPRO_CC) 239cdf0e10cSrcweir #pragma inline(rtl_arena_segment_put) 240cdf0e10cSrcweir #endif 241cdf0e10cSrcweir 242cdf0e10cSrcweir /* ================================================================= */ 243cdf0e10cSrcweir 244cdf0e10cSrcweir /** rtl_arena_freelist_insert() 245cdf0e10cSrcweir * 246cdf0e10cSrcweir * @precond arena->m_lock acquired. 247cdf0e10cSrcweir */ 248cdf0e10cSrcweir static RTL_MEMORY_INLINE void 249cdf0e10cSrcweir rtl_arena_freelist_insert ( 250cdf0e10cSrcweir rtl_arena_type * arena, 251cdf0e10cSrcweir rtl_arena_segment_type * segment 252cdf0e10cSrcweir ) 253cdf0e10cSrcweir { 254cdf0e10cSrcweir rtl_arena_segment_type * head; 255cdf0e10cSrcweir 256cdf0e10cSrcweir head = &(arena->m_freelist_head[highbit(segment->m_size) - 1]); 257cdf0e10cSrcweir QUEUE_INSERT_TAIL_NAMED(head, segment, f); 258cdf0e10cSrcweir 259cdf0e10cSrcweir arena->m_freelist_bitmap |= head->m_size; 260cdf0e10cSrcweir } 261cdf0e10cSrcweir 262cdf0e10cSrcweir #if defined(__SUNPRO_C) || defined(__SUNPRO_CC) 263cdf0e10cSrcweir #pragma inline(rtl_arena_freelist_insert) 264cdf0e10cSrcweir #endif /* __SUNPRO_C */ 265cdf0e10cSrcweir 266cdf0e10cSrcweir 267cdf0e10cSrcweir /** rtl_arena_freelist_remove() 268cdf0e10cSrcweir * 269cdf0e10cSrcweir * @precond arena->m_lock acquired. 270cdf0e10cSrcweir */ 271cdf0e10cSrcweir static RTL_MEMORY_INLINE void 272cdf0e10cSrcweir rtl_arena_freelist_remove ( 273cdf0e10cSrcweir rtl_arena_type * arena, 274cdf0e10cSrcweir rtl_arena_segment_type * segment 275cdf0e10cSrcweir ) 276cdf0e10cSrcweir { 277cdf0e10cSrcweir if ((segment->m_fnext->m_type == RTL_ARENA_SEGMENT_TYPE_HEAD) && 278cdf0e10cSrcweir (segment->m_fprev->m_type == RTL_ARENA_SEGMENT_TYPE_HEAD) ) 279cdf0e10cSrcweir { 280cdf0e10cSrcweir rtl_arena_segment_type * head; 281cdf0e10cSrcweir 282cdf0e10cSrcweir head = segment->m_fprev; 283cdf0e10cSrcweir OSL_ASSERT(arena->m_freelist_bitmap & head->m_size); 284cdf0e10cSrcweir arena->m_freelist_bitmap ^= head->m_size; 285cdf0e10cSrcweir } 286cdf0e10cSrcweir QUEUE_REMOVE_NAMED(segment, f); 287cdf0e10cSrcweir } 288cdf0e10cSrcweir 289cdf0e10cSrcweir #if defined(__SUNPRO_C) || defined(__SUNPRO_CC) 290cdf0e10cSrcweir #pragma inline(rtl_arena_freelist_remove) 291cdf0e10cSrcweir #endif /* __SUNPRO_C */ 292cdf0e10cSrcweir 293cdf0e10cSrcweir 294cdf0e10cSrcweir /* ================================================================= */ 295cdf0e10cSrcweir 296cdf0e10cSrcweir /** RTL_ARENA_HASH_INDEX() 297cdf0e10cSrcweir */ 298cdf0e10cSrcweir #define RTL_ARENA_HASH_INDEX_IMPL(a, s, q, m) \ 299cdf0e10cSrcweir ((((a) + ((a) >> (s)) + ((a) >> ((s) << 1))) >> (q)) & (m)) 300cdf0e10cSrcweir 301cdf0e10cSrcweir #define RTL_ARENA_HASH_INDEX(arena, addr) \ 302cdf0e10cSrcweir RTL_ARENA_HASH_INDEX_IMPL((addr), (arena)->m_hash_shift, (arena)->m_quantum_shift, ((arena)->m_hash_size - 1)) 303cdf0e10cSrcweir 304cdf0e10cSrcweir /** rtl_arena_hash_rescale() 305cdf0e10cSrcweir * 306cdf0e10cSrcweir * @precond arena->m_lock released. 307cdf0e10cSrcweir */ 308cdf0e10cSrcweir static void 309cdf0e10cSrcweir rtl_arena_hash_rescale ( 310cdf0e10cSrcweir rtl_arena_type * arena, 311cdf0e10cSrcweir sal_Size new_size 312cdf0e10cSrcweir ) 313cdf0e10cSrcweir { 314cdf0e10cSrcweir rtl_arena_segment_type ** new_table; 315cdf0e10cSrcweir sal_Size new_bytes; 316cdf0e10cSrcweir 317cdf0e10cSrcweir new_bytes = new_size * sizeof(rtl_arena_segment_type*); 318cdf0e10cSrcweir new_table = (rtl_arena_segment_type **)rtl_arena_alloc (gp_arena_arena, &new_bytes); 319cdf0e10cSrcweir 320cdf0e10cSrcweir if (new_table != 0) 321cdf0e10cSrcweir { 322cdf0e10cSrcweir rtl_arena_segment_type ** old_table; 323cdf0e10cSrcweir sal_Size old_size, i; 324cdf0e10cSrcweir 325cdf0e10cSrcweir memset (new_table, 0, new_bytes); 326cdf0e10cSrcweir 327cdf0e10cSrcweir RTL_MEMORY_LOCK_ACQUIRE(&(arena->m_lock)); 328cdf0e10cSrcweir 329cdf0e10cSrcweir old_table = arena->m_hash_table; 330cdf0e10cSrcweir old_size = arena->m_hash_size; 331cdf0e10cSrcweir 332cdf0e10cSrcweir OSL_TRACE( 333cdf0e10cSrcweir "rtl_arena_hash_rescale(\"%s\"): " 334cdf0e10cSrcweir "nseg: %"PRIu64" (ave: %"PRIu64"), frees: %"PRIu64" " 335cdf0e10cSrcweir "[old_size: %lu, new_size: %lu]", 336cdf0e10cSrcweir arena->m_name, 337cdf0e10cSrcweir arena->m_stats.m_alloc - arena->m_stats.m_free, 338cdf0e10cSrcweir (arena->m_stats.m_alloc - arena->m_stats.m_free) >> arena->m_hash_shift, 339cdf0e10cSrcweir arena->m_stats.m_free, 340cdf0e10cSrcweir old_size, new_size 341cdf0e10cSrcweir ); 342cdf0e10cSrcweir 343cdf0e10cSrcweir #if 0 /* DBG */ 344cdf0e10cSrcweir for (i = 0; i < arena->m_hash_size; i++) 345cdf0e10cSrcweir { 346cdf0e10cSrcweir sal_Size k = 0; rtl_arena_segment_type ** segpp = &(arena->m_hash_table[i]); 347cdf0e10cSrcweir while (*segpp) 348cdf0e10cSrcweir { 349cdf0e10cSrcweir k += 1; 350cdf0e10cSrcweir segpp = &((*segpp)->m_fnext); 351cdf0e10cSrcweir } 352cdf0e10cSrcweir fprintf(stdout, "%d, ", k); 353cdf0e10cSrcweir } 354cdf0e10cSrcweir fprintf(stdout, "\n"); 355cdf0e10cSrcweir #endif /* DBG */ 356cdf0e10cSrcweir 357cdf0e10cSrcweir arena->m_hash_table = new_table; 358cdf0e10cSrcweir arena->m_hash_size = new_size; 359cdf0e10cSrcweir arena->m_hash_shift = highbit(arena->m_hash_size) - 1; 360cdf0e10cSrcweir 361cdf0e10cSrcweir for (i = 0; i < old_size; i++) 362cdf0e10cSrcweir { 363cdf0e10cSrcweir rtl_arena_segment_type * curr = old_table[i]; 364cdf0e10cSrcweir while (curr != 0) 365cdf0e10cSrcweir { 366cdf0e10cSrcweir rtl_arena_segment_type * next = curr->m_fnext; 367cdf0e10cSrcweir rtl_arena_segment_type ** head; 368cdf0e10cSrcweir 369cdf0e10cSrcweir head = &(arena->m_hash_table[RTL_ARENA_HASH_INDEX(arena, curr->m_addr)]); 370cdf0e10cSrcweir curr->m_fnext = (*head); 371cdf0e10cSrcweir (*head) = curr; 372cdf0e10cSrcweir 373cdf0e10cSrcweir curr = next; 374cdf0e10cSrcweir } 375cdf0e10cSrcweir old_table[i] = 0; 376cdf0e10cSrcweir } 377cdf0e10cSrcweir 378cdf0e10cSrcweir RTL_MEMORY_LOCK_RELEASE(&(arena->m_lock)); 379cdf0e10cSrcweir 380cdf0e10cSrcweir if (old_table != arena->m_hash_table_0) 381cdf0e10cSrcweir { 382cdf0e10cSrcweir sal_Size old_bytes = old_size * sizeof(rtl_arena_segment_type*); 383cdf0e10cSrcweir rtl_arena_free (gp_arena_arena, old_table, old_bytes); 384cdf0e10cSrcweir } 385cdf0e10cSrcweir } 386cdf0e10cSrcweir } 387cdf0e10cSrcweir 388cdf0e10cSrcweir 389cdf0e10cSrcweir /** rtl_arena_hash_insert() 390cdf0e10cSrcweir * ...and update stats. 391cdf0e10cSrcweir */ 392cdf0e10cSrcweir static RTL_MEMORY_INLINE void 393cdf0e10cSrcweir rtl_arena_hash_insert ( 394cdf0e10cSrcweir rtl_arena_type * arena, 395cdf0e10cSrcweir rtl_arena_segment_type * segment 396cdf0e10cSrcweir ) 397cdf0e10cSrcweir { 398cdf0e10cSrcweir rtl_arena_segment_type ** ppSegment; 399cdf0e10cSrcweir 400cdf0e10cSrcweir ppSegment = &(arena->m_hash_table[RTL_ARENA_HASH_INDEX(arena, segment->m_addr)]); 401cdf0e10cSrcweir 402cdf0e10cSrcweir segment->m_fnext = (*ppSegment); 403cdf0e10cSrcweir (*ppSegment) = segment; 404cdf0e10cSrcweir 405cdf0e10cSrcweir arena->m_stats.m_alloc += 1; 406cdf0e10cSrcweir arena->m_stats.m_mem_alloc += segment->m_size; 407cdf0e10cSrcweir } 408cdf0e10cSrcweir 409cdf0e10cSrcweir #if defined(__SUNPRO_C) || defined(__SUNPRO_CC) 410cdf0e10cSrcweir #pragma inline(rtl_arena_hash_insert) 411cdf0e10cSrcweir #endif /* __SUNPRO_C */ 412cdf0e10cSrcweir 413cdf0e10cSrcweir 414cdf0e10cSrcweir /** rtl_arena_hash_remove() 415cdf0e10cSrcweir * ...and update stats. 416cdf0e10cSrcweir */ 417cdf0e10cSrcweir static rtl_arena_segment_type * 418cdf0e10cSrcweir rtl_arena_hash_remove ( 419cdf0e10cSrcweir rtl_arena_type * arena, 420cdf0e10cSrcweir sal_uIntPtr addr, 421cdf0e10cSrcweir sal_Size size 422cdf0e10cSrcweir ) 423cdf0e10cSrcweir { 424cdf0e10cSrcweir rtl_arena_segment_type *segment, **segpp; 425cdf0e10cSrcweir sal_Size lookups = 0; 426cdf0e10cSrcweir 427cdf0e10cSrcweir #if OSL_DEBUG_LEVEL == 0 428cdf0e10cSrcweir (void) size; /* unused */ 429cdf0e10cSrcweir #endif /* OSL_DEBUG_LEVEL */ 430cdf0e10cSrcweir 431cdf0e10cSrcweir segpp = &(arena->m_hash_table[RTL_ARENA_HASH_INDEX(arena, addr)]); 432cdf0e10cSrcweir while ((segment = *segpp) != 0) 433cdf0e10cSrcweir { 434cdf0e10cSrcweir if (segment->m_addr == addr) 435cdf0e10cSrcweir { 436cdf0e10cSrcweir *segpp = segment->m_fnext, segment->m_fnext = segment->m_fprev = segment; 437cdf0e10cSrcweir break; 438cdf0e10cSrcweir } 439cdf0e10cSrcweir 440cdf0e10cSrcweir /* update lookup miss stats */ 441cdf0e10cSrcweir lookups += 1; 442cdf0e10cSrcweir segpp = &(segment->m_fnext); 443cdf0e10cSrcweir } 444cdf0e10cSrcweir 445cdf0e10cSrcweir OSL_POSTCOND(segment != 0, "rtl_arena_hash_remove(): bad free."); 446cdf0e10cSrcweir if (segment != 0) 447cdf0e10cSrcweir { 448cdf0e10cSrcweir OSL_POSTCOND(segment->m_size == size, "rtl_arena_hash_remove(): wrong size."); 449cdf0e10cSrcweir 450cdf0e10cSrcweir arena->m_stats.m_free += 1; 451cdf0e10cSrcweir arena->m_stats.m_mem_alloc -= segment->m_size; 452cdf0e10cSrcweir 453cdf0e10cSrcweir if (lookups > 1) 454cdf0e10cSrcweir { 455cdf0e10cSrcweir sal_Size nseg = (sal_Size)(arena->m_stats.m_alloc - arena->m_stats.m_free); 456cdf0e10cSrcweir if (nseg > 4 * arena->m_hash_size) 457cdf0e10cSrcweir { 458cdf0e10cSrcweir if (!(arena->m_flags & RTL_ARENA_FLAG_RESCALE)) 459cdf0e10cSrcweir { 460cdf0e10cSrcweir sal_Size ave = nseg >> arena->m_hash_shift; 461cdf0e10cSrcweir sal_Size new_size = arena->m_hash_size << (highbit(ave) - 1); 462cdf0e10cSrcweir 463cdf0e10cSrcweir arena->m_flags |= RTL_ARENA_FLAG_RESCALE; 464cdf0e10cSrcweir RTL_MEMORY_LOCK_RELEASE(&(arena->m_lock)); 465cdf0e10cSrcweir rtl_arena_hash_rescale (arena, new_size); 466cdf0e10cSrcweir RTL_MEMORY_LOCK_ACQUIRE(&(arena->m_lock)); 467cdf0e10cSrcweir arena->m_flags &= ~RTL_ARENA_FLAG_RESCALE; 468cdf0e10cSrcweir } 469cdf0e10cSrcweir } 470cdf0e10cSrcweir } 471cdf0e10cSrcweir } 472cdf0e10cSrcweir 473cdf0e10cSrcweir return (segment); 474cdf0e10cSrcweir } 475cdf0e10cSrcweir 476cdf0e10cSrcweir /* ================================================================= */ 477cdf0e10cSrcweir 478cdf0e10cSrcweir /** rtl_arena_segment_alloc() 479cdf0e10cSrcweir * allocate (and remove) segment from freelist 480cdf0e10cSrcweir * 481cdf0e10cSrcweir * @precond arena->m_lock acquired 482cdf0e10cSrcweir * @precond (*ppSegment == 0) 483cdf0e10cSrcweir */ 484cdf0e10cSrcweir static int 485cdf0e10cSrcweir rtl_arena_segment_alloc ( 486cdf0e10cSrcweir rtl_arena_type * arena, 487cdf0e10cSrcweir sal_Size size, 488cdf0e10cSrcweir rtl_arena_segment_type ** ppSegment 489cdf0e10cSrcweir ) 490cdf0e10cSrcweir { 491cdf0e10cSrcweir int index = 0; 492cdf0e10cSrcweir 493cdf0e10cSrcweir OSL_ASSERT(*ppSegment == 0); 494cdf0e10cSrcweir if (!RTL_MEMORY_ISP2(size)) 495cdf0e10cSrcweir { 496cdf0e10cSrcweir int msb = highbit(size); 497cdf0e10cSrcweir if (RTL_ARENA_FREELIST_SIZE == SAL_INT_CAST(size_t, msb)) 498cdf0e10cSrcweir { 499cdf0e10cSrcweir /* highest possible freelist: fall back to first fit */ 500cdf0e10cSrcweir rtl_arena_segment_type *head, *segment; 501cdf0e10cSrcweir 502cdf0e10cSrcweir head = &(arena->m_freelist_head[msb - 1]); 503cdf0e10cSrcweir for (segment = head->m_fnext; segment != head; segment = segment->m_fnext) 504cdf0e10cSrcweir { 505cdf0e10cSrcweir if (segment->m_size >= size) 506cdf0e10cSrcweir { 507cdf0e10cSrcweir /* allocate first fit segment */ 508cdf0e10cSrcweir (*ppSegment) = segment; 509cdf0e10cSrcweir break; 510cdf0e10cSrcweir } 511cdf0e10cSrcweir } 512cdf0e10cSrcweir goto dequeue_and_leave; 513cdf0e10cSrcweir } 514cdf0e10cSrcweir 515cdf0e10cSrcweir /* roundup to next power of 2 */ 516cdf0e10cSrcweir size = (1UL << msb); 517cdf0e10cSrcweir } 518cdf0e10cSrcweir 519cdf0e10cSrcweir index = lowbit(RTL_MEMORY_P2ALIGN(arena->m_freelist_bitmap, size)); 520cdf0e10cSrcweir if (index > 0) 521cdf0e10cSrcweir { 522cdf0e10cSrcweir /* instant fit: allocate first free segment */ 523cdf0e10cSrcweir rtl_arena_segment_type *head; 524cdf0e10cSrcweir 525cdf0e10cSrcweir head = &(arena->m_freelist_head[index - 1]); 526cdf0e10cSrcweir (*ppSegment) = head->m_fnext; 527cdf0e10cSrcweir OSL_ASSERT((*ppSegment) != head); 528cdf0e10cSrcweir } 529cdf0e10cSrcweir 530cdf0e10cSrcweir dequeue_and_leave: 531cdf0e10cSrcweir if (*ppSegment != 0) 532cdf0e10cSrcweir { 533cdf0e10cSrcweir /* remove from freelist */ 534cdf0e10cSrcweir rtl_arena_freelist_remove (arena, (*ppSegment)); 535cdf0e10cSrcweir } 536cdf0e10cSrcweir return (*ppSegment != 0); 537cdf0e10cSrcweir } 538cdf0e10cSrcweir 539cdf0e10cSrcweir 540cdf0e10cSrcweir /** rtl_arena_segment_create() 541cdf0e10cSrcweir * import new (span) segment from source arena 542cdf0e10cSrcweir * 543cdf0e10cSrcweir * @precond arena->m_lock acquired 544cdf0e10cSrcweir * @precond (*ppSegment == 0) 545cdf0e10cSrcweir */ 546cdf0e10cSrcweir static int 547cdf0e10cSrcweir rtl_arena_segment_create ( 548cdf0e10cSrcweir rtl_arena_type * arena, 549cdf0e10cSrcweir sal_Size size, 550cdf0e10cSrcweir rtl_arena_segment_type ** ppSegment 551cdf0e10cSrcweir ) 552cdf0e10cSrcweir { 553cdf0e10cSrcweir OSL_ASSERT((*ppSegment) == 0); 554cdf0e10cSrcweir if (arena->m_source_alloc != 0) 555cdf0e10cSrcweir { 556cdf0e10cSrcweir rtl_arena_segment_get (arena, ppSegment); 557cdf0e10cSrcweir if (*ppSegment != 0) 558cdf0e10cSrcweir { 559cdf0e10cSrcweir rtl_arena_segment_type * span = 0; 560cdf0e10cSrcweir rtl_arena_segment_get (arena, &span); 561cdf0e10cSrcweir if (span != 0) 562cdf0e10cSrcweir { 563cdf0e10cSrcweir /* import new span from source arena */ 564cdf0e10cSrcweir RTL_MEMORY_LOCK_RELEASE(&(arena->m_lock)); 565cdf0e10cSrcweir 566cdf0e10cSrcweir span->m_size = size; 567cdf0e10cSrcweir span->m_addr = (sal_uIntPtr)(arena->m_source_alloc)( 568cdf0e10cSrcweir arena->m_source_arena, &(span->m_size)); 569cdf0e10cSrcweir 570cdf0e10cSrcweir RTL_MEMORY_LOCK_ACQUIRE(&(arena->m_lock)); 571cdf0e10cSrcweir if (span->m_addr != 0) 572cdf0e10cSrcweir { 573cdf0e10cSrcweir /* insert onto segment list, update stats */ 574cdf0e10cSrcweir span->m_type = RTL_ARENA_SEGMENT_TYPE_SPAN; 575cdf0e10cSrcweir QUEUE_INSERT_HEAD_NAMED(&(arena->m_segment_head), span, s); 576cdf0e10cSrcweir arena->m_stats.m_mem_total += span->m_size; 577cdf0e10cSrcweir 578cdf0e10cSrcweir (*ppSegment)->m_addr = span->m_addr; 579cdf0e10cSrcweir (*ppSegment)->m_size = span->m_size; 580cdf0e10cSrcweir (*ppSegment)->m_type = RTL_ARENA_SEGMENT_TYPE_FREE; 581cdf0e10cSrcweir QUEUE_INSERT_HEAD_NAMED(span, (*ppSegment), s); 582cdf0e10cSrcweir 583cdf0e10cSrcweir /* report success */ 584cdf0e10cSrcweir return (1); 585cdf0e10cSrcweir } 586cdf0e10cSrcweir rtl_arena_segment_put (arena, &span); 587cdf0e10cSrcweir } 588cdf0e10cSrcweir rtl_arena_segment_put (arena, ppSegment); 589cdf0e10cSrcweir } 590cdf0e10cSrcweir } 591cdf0e10cSrcweir return (0); 592cdf0e10cSrcweir } 593cdf0e10cSrcweir 594cdf0e10cSrcweir 595cdf0e10cSrcweir /** rtl_arena_segment_coalesce() 596cdf0e10cSrcweir * mark as free and join with adjacent free segment(s) 597cdf0e10cSrcweir * 598cdf0e10cSrcweir * @precond arena->m_lock acquired 599cdf0e10cSrcweir * @precond segment marked 'used' 600cdf0e10cSrcweir */ 601cdf0e10cSrcweir static void 602cdf0e10cSrcweir rtl_arena_segment_coalesce ( 603cdf0e10cSrcweir rtl_arena_type * arena, 604cdf0e10cSrcweir rtl_arena_segment_type * segment 605cdf0e10cSrcweir ) 606cdf0e10cSrcweir { 607cdf0e10cSrcweir rtl_arena_segment_type *next, *prev; 608cdf0e10cSrcweir 609cdf0e10cSrcweir /* mark segment free */ 610cdf0e10cSrcweir OSL_ASSERT(segment->m_type == RTL_ARENA_SEGMENT_TYPE_USED); 611cdf0e10cSrcweir segment->m_type = RTL_ARENA_SEGMENT_TYPE_FREE; 612cdf0e10cSrcweir 613cdf0e10cSrcweir /* try to merge w/ next segment */ 614cdf0e10cSrcweir next = segment->m_snext; 615cdf0e10cSrcweir if (next->m_type == RTL_ARENA_SEGMENT_TYPE_FREE) 616cdf0e10cSrcweir { 617cdf0e10cSrcweir OSL_ASSERT(segment->m_addr + segment->m_size == next->m_addr); 618cdf0e10cSrcweir segment->m_size += next->m_size; 619cdf0e10cSrcweir 620cdf0e10cSrcweir /* remove from freelist */ 621cdf0e10cSrcweir rtl_arena_freelist_remove (arena, next); 622cdf0e10cSrcweir 623cdf0e10cSrcweir /* remove from segment list */ 624cdf0e10cSrcweir QUEUE_REMOVE_NAMED(next, s); 625cdf0e10cSrcweir 626cdf0e10cSrcweir /* release segment descriptor */ 627cdf0e10cSrcweir rtl_arena_segment_put (arena, &next); 628cdf0e10cSrcweir } 629cdf0e10cSrcweir 630cdf0e10cSrcweir /* try to merge w/ prev segment */ 631cdf0e10cSrcweir prev = segment->m_sprev; 632cdf0e10cSrcweir if (prev->m_type == RTL_ARENA_SEGMENT_TYPE_FREE) 633cdf0e10cSrcweir { 634cdf0e10cSrcweir OSL_ASSERT(prev->m_addr + prev->m_size == segment->m_addr); 635cdf0e10cSrcweir segment->m_addr = prev->m_addr; 636cdf0e10cSrcweir segment->m_size += prev->m_size; 637cdf0e10cSrcweir 638cdf0e10cSrcweir /* remove from freelist */ 639cdf0e10cSrcweir rtl_arena_freelist_remove (arena, prev); 640cdf0e10cSrcweir 641cdf0e10cSrcweir /* remove from segment list */ 642cdf0e10cSrcweir QUEUE_REMOVE_NAMED(prev, s); 643cdf0e10cSrcweir 644cdf0e10cSrcweir /* release segment descriptor */ 645cdf0e10cSrcweir rtl_arena_segment_put (arena, &prev); 646cdf0e10cSrcweir } 647cdf0e10cSrcweir } 648cdf0e10cSrcweir 649cdf0e10cSrcweir /* ================================================================= */ 650cdf0e10cSrcweir 651cdf0e10cSrcweir /** rtl_arena_constructor() 652cdf0e10cSrcweir */ 653cdf0e10cSrcweir static void 654cdf0e10cSrcweir rtl_arena_constructor (void * obj) 655cdf0e10cSrcweir { 656cdf0e10cSrcweir rtl_arena_type * arena = (rtl_arena_type*)(obj); 657cdf0e10cSrcweir rtl_arena_segment_type * head; 658cdf0e10cSrcweir size_t i; 659cdf0e10cSrcweir 660cdf0e10cSrcweir memset (arena, 0, sizeof(rtl_arena_type)); 661cdf0e10cSrcweir 662cdf0e10cSrcweir QUEUE_START_NAMED(arena, arena_); 663cdf0e10cSrcweir 664cdf0e10cSrcweir (void) RTL_MEMORY_LOCK_INIT(&(arena->m_lock)); 665cdf0e10cSrcweir 666cdf0e10cSrcweir head = &(arena->m_segment_reserve_span_head); 667cdf0e10cSrcweir rtl_arena_segment_constructor (head); 668cdf0e10cSrcweir head->m_type = RTL_ARENA_SEGMENT_TYPE_HEAD; 669cdf0e10cSrcweir 670cdf0e10cSrcweir head = &(arena->m_segment_reserve_head); 671cdf0e10cSrcweir rtl_arena_segment_constructor (head); 672cdf0e10cSrcweir head->m_type = RTL_ARENA_SEGMENT_TYPE_HEAD; 673cdf0e10cSrcweir 674cdf0e10cSrcweir head = &(arena->m_segment_head); 675cdf0e10cSrcweir rtl_arena_segment_constructor (head); 676cdf0e10cSrcweir head->m_type = RTL_ARENA_SEGMENT_TYPE_HEAD; 677cdf0e10cSrcweir 678cdf0e10cSrcweir for (i = 0; i < RTL_ARENA_FREELIST_SIZE; i++) 679cdf0e10cSrcweir { 680cdf0e10cSrcweir head = &(arena->m_freelist_head[i]); 681cdf0e10cSrcweir rtl_arena_segment_constructor (head); 682cdf0e10cSrcweir 683cdf0e10cSrcweir head->m_size = (1UL << i); 684cdf0e10cSrcweir head->m_type = RTL_ARENA_SEGMENT_TYPE_HEAD; 685cdf0e10cSrcweir } 686cdf0e10cSrcweir 687cdf0e10cSrcweir arena->m_hash_table = arena->m_hash_table_0; 688cdf0e10cSrcweir arena->m_hash_size = RTL_ARENA_HASH_SIZE; 689cdf0e10cSrcweir arena->m_hash_shift = highbit(arena->m_hash_size) - 1; 690cdf0e10cSrcweir } 691cdf0e10cSrcweir 692cdf0e10cSrcweir 693cdf0e10cSrcweir /** rtl_arena_destructor() 694cdf0e10cSrcweir */ 695cdf0e10cSrcweir static void 696cdf0e10cSrcweir rtl_arena_destructor (void * obj) 697cdf0e10cSrcweir { 698cdf0e10cSrcweir rtl_arena_type * arena = (rtl_arena_type*)(obj); 699cdf0e10cSrcweir rtl_arena_segment_type * head; 700cdf0e10cSrcweir size_t i; 701cdf0e10cSrcweir 702cdf0e10cSrcweir OSL_ASSERT(QUEUE_STARTED_NAMED(arena, arena_)); 703cdf0e10cSrcweir 704cdf0e10cSrcweir RTL_MEMORY_LOCK_DESTROY(&(arena->m_lock)); 705cdf0e10cSrcweir 706cdf0e10cSrcweir head = &(arena->m_segment_reserve_span_head); 707cdf0e10cSrcweir OSL_ASSERT(head->m_type == RTL_ARENA_SEGMENT_TYPE_HEAD); 708cdf0e10cSrcweir rtl_arena_segment_destructor (head); 709cdf0e10cSrcweir 710cdf0e10cSrcweir head = &(arena->m_segment_reserve_head); 711cdf0e10cSrcweir OSL_ASSERT(head->m_type == RTL_ARENA_SEGMENT_TYPE_HEAD); 712cdf0e10cSrcweir rtl_arena_segment_destructor (head); 713cdf0e10cSrcweir 714cdf0e10cSrcweir head = &(arena->m_segment_head); 715cdf0e10cSrcweir OSL_ASSERT(head->m_type == RTL_ARENA_SEGMENT_TYPE_HEAD); 716cdf0e10cSrcweir rtl_arena_segment_destructor (head); 717cdf0e10cSrcweir 718cdf0e10cSrcweir for (i = 0; i < RTL_ARENA_FREELIST_SIZE; i++) 719cdf0e10cSrcweir { 720cdf0e10cSrcweir head = &(arena->m_freelist_head[i]); 721cdf0e10cSrcweir 722cdf0e10cSrcweir OSL_ASSERT(head->m_size == (1UL << i)); 723cdf0e10cSrcweir OSL_ASSERT(head->m_type == RTL_ARENA_SEGMENT_TYPE_HEAD); 724cdf0e10cSrcweir 725cdf0e10cSrcweir rtl_arena_segment_destructor (head); 726cdf0e10cSrcweir } 727cdf0e10cSrcweir 728cdf0e10cSrcweir OSL_ASSERT(arena->m_hash_table == arena->m_hash_table_0); 729cdf0e10cSrcweir OSL_ASSERT(arena->m_hash_size == RTL_ARENA_HASH_SIZE); 730cdf0e10cSrcweir OSL_ASSERT( 731cdf0e10cSrcweir arena->m_hash_shift == 732cdf0e10cSrcweir SAL_INT_CAST(unsigned, highbit(arena->m_hash_size) - 1)); 733cdf0e10cSrcweir } 734cdf0e10cSrcweir 735cdf0e10cSrcweir /* ================================================================= */ 736cdf0e10cSrcweir 737cdf0e10cSrcweir /** rtl_arena_activate() 738cdf0e10cSrcweir */ 739cdf0e10cSrcweir static rtl_arena_type * 740cdf0e10cSrcweir rtl_arena_activate ( 741cdf0e10cSrcweir rtl_arena_type * arena, 742cdf0e10cSrcweir const char * name, 743cdf0e10cSrcweir sal_Size quantum, 744cdf0e10cSrcweir sal_Size quantum_cache_max, 745cdf0e10cSrcweir rtl_arena_type * source_arena, 746cdf0e10cSrcweir void * (SAL_CALL * source_alloc)(rtl_arena_type *, sal_Size *), 747cdf0e10cSrcweir void (SAL_CALL * source_free) (rtl_arena_type *, void *, sal_Size) 748cdf0e10cSrcweir ) 749cdf0e10cSrcweir { 750cdf0e10cSrcweir OSL_ASSERT(arena != 0); 751cdf0e10cSrcweir if (arena != 0) 752cdf0e10cSrcweir { 753cdf0e10cSrcweir (void) snprintf (arena->m_name, sizeof(arena->m_name), "%s", name); 754cdf0e10cSrcweir 755cdf0e10cSrcweir if (!RTL_MEMORY_ISP2(quantum)) 756cdf0e10cSrcweir { 757cdf0e10cSrcweir /* roundup to next power of 2 */ 758cdf0e10cSrcweir quantum = (1UL << highbit(quantum)); 759cdf0e10cSrcweir } 760cdf0e10cSrcweir quantum_cache_max = RTL_MEMORY_P2ROUNDUP(quantum_cache_max, quantum); 761cdf0e10cSrcweir 762cdf0e10cSrcweir arena->m_quantum = quantum; 763cdf0e10cSrcweir arena->m_quantum_shift = highbit(arena->m_quantum) - 1; 764cdf0e10cSrcweir arena->m_qcache_max = quantum_cache_max; 765cdf0e10cSrcweir 766cdf0e10cSrcweir arena->m_source_arena = source_arena; 767cdf0e10cSrcweir arena->m_source_alloc = source_alloc; 768cdf0e10cSrcweir arena->m_source_free = source_free; 769cdf0e10cSrcweir 770cdf0e10cSrcweir if (arena->m_qcache_max > 0) 771cdf0e10cSrcweir { 772cdf0e10cSrcweir char name[RTL_ARENA_NAME_LENGTH + 1]; 773cdf0e10cSrcweir int i, n = (arena->m_qcache_max >> arena->m_quantum_shift); 774cdf0e10cSrcweir 775cdf0e10cSrcweir sal_Size size = n * sizeof(rtl_cache_type*); 776cdf0e10cSrcweir arena->m_qcache_ptr = (rtl_cache_type**)rtl_arena_alloc (gp_arena_arena, &size); 777cdf0e10cSrcweir if (!(arena->m_qcache_ptr)) 778cdf0e10cSrcweir { 779cdf0e10cSrcweir /* out of memory */ 780cdf0e10cSrcweir return (0); 781cdf0e10cSrcweir } 782cdf0e10cSrcweir for (i = 1; i <= n; i++) 783cdf0e10cSrcweir { 784cdf0e10cSrcweir size = i * arena->m_quantum; 785cdf0e10cSrcweir (void) snprintf (name, sizeof(name), "%s_%lu", arena->m_name, size); 786cdf0e10cSrcweir arena->m_qcache_ptr[i - 1] = rtl_cache_create(name, size, 0, NULL, NULL, NULL, NULL, arena, RTL_CACHE_FLAG_QUANTUMCACHE); 787cdf0e10cSrcweir } 788cdf0e10cSrcweir } 789cdf0e10cSrcweir 790cdf0e10cSrcweir /* insert into arena list */ 791cdf0e10cSrcweir RTL_MEMORY_LOCK_ACQUIRE(&(g_arena_list.m_lock)); 792cdf0e10cSrcweir QUEUE_INSERT_TAIL_NAMED(&(g_arena_list.m_arena_head), arena, arena_); 793cdf0e10cSrcweir RTL_MEMORY_LOCK_RELEASE(&(g_arena_list.m_lock)); 794cdf0e10cSrcweir } 795cdf0e10cSrcweir return (arena); 796cdf0e10cSrcweir } 797cdf0e10cSrcweir 798cdf0e10cSrcweir /** rtl_arena_deactivate() 799cdf0e10cSrcweir */ 800cdf0e10cSrcweir static void 801cdf0e10cSrcweir rtl_arena_deactivate ( 802cdf0e10cSrcweir rtl_arena_type * arena 803cdf0e10cSrcweir ) 804cdf0e10cSrcweir { 805cdf0e10cSrcweir rtl_arena_segment_type * head, * segment; 806cdf0e10cSrcweir 807cdf0e10cSrcweir /* remove from arena list */ 808cdf0e10cSrcweir RTL_MEMORY_LOCK_ACQUIRE(&(g_arena_list.m_lock)); 809cdf0e10cSrcweir QUEUE_REMOVE_NAMED(arena, arena_); 810cdf0e10cSrcweir RTL_MEMORY_LOCK_RELEASE(&(g_arena_list.m_lock)); 811cdf0e10cSrcweir 812cdf0e10cSrcweir /* cleanup quantum cache(s) */ 813cdf0e10cSrcweir if ((arena->m_qcache_max > 0) && (arena->m_qcache_ptr != 0)) 814cdf0e10cSrcweir { 815cdf0e10cSrcweir int i, n = (arena->m_qcache_max >> arena->m_quantum_shift); 816cdf0e10cSrcweir for (i = 1; i <= n; i++) 817cdf0e10cSrcweir { 818cdf0e10cSrcweir if (arena->m_qcache_ptr[i - 1] != 0) 819cdf0e10cSrcweir { 820cdf0e10cSrcweir rtl_cache_destroy (arena->m_qcache_ptr[i - 1]); 821cdf0e10cSrcweir arena->m_qcache_ptr[i - 1] = 0; 822cdf0e10cSrcweir } 823cdf0e10cSrcweir } 824cdf0e10cSrcweir rtl_arena_free ( 825cdf0e10cSrcweir gp_arena_arena, 826cdf0e10cSrcweir arena->m_qcache_ptr, 827cdf0e10cSrcweir n * sizeof(rtl_cache_type*)); 828cdf0e10cSrcweir 829cdf0e10cSrcweir arena->m_qcache_ptr = 0; 830cdf0e10cSrcweir } 831cdf0e10cSrcweir 832cdf0e10cSrcweir /* check for leaked segments */ 833cdf0e10cSrcweir OSL_TRACE( 834cdf0e10cSrcweir "rtl_arena_deactivate(\"%s\"): " 835cdf0e10cSrcweir "allocs: %"PRIu64", frees: %"PRIu64"; total: %lu, used: %lu", 836cdf0e10cSrcweir arena->m_name, 837cdf0e10cSrcweir arena->m_stats.m_alloc, arena->m_stats.m_free, 838cdf0e10cSrcweir arena->m_stats.m_mem_total, arena->m_stats.m_mem_alloc 839cdf0e10cSrcweir ); 840cdf0e10cSrcweir if (arena->m_stats.m_alloc > arena->m_stats.m_free) 841cdf0e10cSrcweir { 842cdf0e10cSrcweir sal_Size i, n; 843cdf0e10cSrcweir 844cdf0e10cSrcweir OSL_TRACE( 845cdf0e10cSrcweir "rtl_arena_deactivate(\"%s\"): " 846cdf0e10cSrcweir "cleaning up %"PRIu64" leaked segment(s) [%lu bytes]", 847cdf0e10cSrcweir arena->m_name, 848cdf0e10cSrcweir arena->m_stats.m_alloc - arena->m_stats.m_free, 849cdf0e10cSrcweir arena->m_stats.m_mem_alloc 850cdf0e10cSrcweir ); 851cdf0e10cSrcweir 852cdf0e10cSrcweir /* cleanup still used segment(s) */ 853cdf0e10cSrcweir for (i = 0, n = arena->m_hash_size; i < n; i++) 854cdf0e10cSrcweir { 855cdf0e10cSrcweir while ((segment = arena->m_hash_table[i]) != 0) 856cdf0e10cSrcweir { 857cdf0e10cSrcweir /* pop from hash table */ 858cdf0e10cSrcweir arena->m_hash_table[i] = segment->m_fnext, segment->m_fnext = segment->m_fprev = segment; 859cdf0e10cSrcweir 860cdf0e10cSrcweir /* coalesce w/ adjacent free segment(s) */ 861cdf0e10cSrcweir rtl_arena_segment_coalesce (arena, segment); 862cdf0e10cSrcweir 863cdf0e10cSrcweir /* insert onto freelist */ 864cdf0e10cSrcweir rtl_arena_freelist_insert (arena, segment); 865cdf0e10cSrcweir } 866cdf0e10cSrcweir } 867cdf0e10cSrcweir } 868cdf0e10cSrcweir 869cdf0e10cSrcweir /* cleanup hash table */ 870cdf0e10cSrcweir if (arena->m_hash_table != arena->m_hash_table_0) 871cdf0e10cSrcweir { 872cdf0e10cSrcweir rtl_arena_free ( 873cdf0e10cSrcweir gp_arena_arena, 874cdf0e10cSrcweir arena->m_hash_table, 875cdf0e10cSrcweir arena->m_hash_size * sizeof(rtl_arena_segment_type*)); 876cdf0e10cSrcweir 877cdf0e10cSrcweir arena->m_hash_table = arena->m_hash_table_0; 878cdf0e10cSrcweir arena->m_hash_size = RTL_ARENA_HASH_SIZE; 879cdf0e10cSrcweir arena->m_hash_shift = highbit(arena->m_hash_size) - 1; 880cdf0e10cSrcweir } 881cdf0e10cSrcweir 882cdf0e10cSrcweir /* cleanup segment list */ 883cdf0e10cSrcweir head = &(arena->m_segment_head); 884cdf0e10cSrcweir for (segment = head->m_snext; segment != head; segment = head->m_snext) 885cdf0e10cSrcweir { 886cdf0e10cSrcweir if (segment->m_type == RTL_ARENA_SEGMENT_TYPE_FREE) 887cdf0e10cSrcweir { 888cdf0e10cSrcweir /* remove from freelist */ 889cdf0e10cSrcweir rtl_arena_freelist_remove (arena, segment); 890cdf0e10cSrcweir } 891cdf0e10cSrcweir else 892cdf0e10cSrcweir { 893cdf0e10cSrcweir /* can have only free and span segments here */ 894cdf0e10cSrcweir OSL_ASSERT(segment->m_type == RTL_ARENA_SEGMENT_TYPE_SPAN); 895cdf0e10cSrcweir } 896cdf0e10cSrcweir 897cdf0e10cSrcweir /* remove from segment list */ 898cdf0e10cSrcweir QUEUE_REMOVE_NAMED(segment, s); 899cdf0e10cSrcweir 900cdf0e10cSrcweir /* release segment descriptor */ 901cdf0e10cSrcweir rtl_arena_segment_put (arena, &segment); 902cdf0e10cSrcweir } 903cdf0e10cSrcweir 904cdf0e10cSrcweir /* cleanup segment reserve list */ 905cdf0e10cSrcweir head = &(arena->m_segment_reserve_head); 906cdf0e10cSrcweir for (segment = head->m_snext; segment != head; segment = head->m_snext) 907cdf0e10cSrcweir { 908cdf0e10cSrcweir /* remove from segment list */ 909cdf0e10cSrcweir QUEUE_REMOVE_NAMED(segment, s); 910cdf0e10cSrcweir } 911cdf0e10cSrcweir 912cdf0e10cSrcweir /* cleanup segment reserve span(s) */ 913cdf0e10cSrcweir head = &(arena->m_segment_reserve_span_head); 914cdf0e10cSrcweir for (segment = head->m_snext; segment != head; segment = head->m_snext) 915cdf0e10cSrcweir { 916cdf0e10cSrcweir /* can have only span segments here */ 917cdf0e10cSrcweir OSL_ASSERT(segment->m_type == RTL_ARENA_SEGMENT_TYPE_SPAN); 918cdf0e10cSrcweir 919cdf0e10cSrcweir /* remove from segment list */ 920cdf0e10cSrcweir QUEUE_REMOVE_NAMED(segment, s); 921cdf0e10cSrcweir 922cdf0e10cSrcweir /* return span to g_machdep_arena */ 923cdf0e10cSrcweir rtl_machdep_free (gp_machdep_arena, (void*)(segment->m_addr), segment->m_size); 924cdf0e10cSrcweir } 925cdf0e10cSrcweir } 926cdf0e10cSrcweir 927cdf0e10cSrcweir /* ================================================================= * 928cdf0e10cSrcweir * 929cdf0e10cSrcweir * arena implementation. 930cdf0e10cSrcweir * 931cdf0e10cSrcweir * ================================================================= */ 932cdf0e10cSrcweir 933cdf0e10cSrcweir /** rtl_arena_create() 934cdf0e10cSrcweir */ 935cdf0e10cSrcweir rtl_arena_type * 936cdf0e10cSrcweir SAL_CALL rtl_arena_create ( 937cdf0e10cSrcweir const char * name, 938cdf0e10cSrcweir sal_Size quantum, 939cdf0e10cSrcweir sal_Size quantum_cache_max, 940cdf0e10cSrcweir rtl_arena_type * source_arena, 941cdf0e10cSrcweir void * (SAL_CALL * source_alloc)(rtl_arena_type *, sal_Size *), 942cdf0e10cSrcweir void (SAL_CALL * source_free) (rtl_arena_type *, void *, sal_Size), 943cdf0e10cSrcweir int flags 944cdf0e10cSrcweir ) SAL_THROW_EXTERN_C() 945cdf0e10cSrcweir { 946cdf0e10cSrcweir rtl_arena_type * result = 0; 947cdf0e10cSrcweir sal_Size size = sizeof(rtl_arena_type); 948cdf0e10cSrcweir 949cdf0e10cSrcweir (void) flags; /* unused */ 950cdf0e10cSrcweir 951cdf0e10cSrcweir try_alloc: 952cdf0e10cSrcweir result = (rtl_arena_type*)rtl_arena_alloc (gp_arena_arena, &size); 953cdf0e10cSrcweir if (result != 0) 954cdf0e10cSrcweir { 955cdf0e10cSrcweir rtl_arena_type * arena = result; 956cdf0e10cSrcweir VALGRIND_CREATE_MEMPOOL(arena, 0, 0); 957cdf0e10cSrcweir rtl_arena_constructor (arena); 958cdf0e10cSrcweir 959cdf0e10cSrcweir if (!source_arena) 960cdf0e10cSrcweir { 961cdf0e10cSrcweir OSL_ASSERT(gp_default_arena != 0); 962cdf0e10cSrcweir source_arena = gp_default_arena; 963cdf0e10cSrcweir } 964cdf0e10cSrcweir 965cdf0e10cSrcweir result = rtl_arena_activate ( 966cdf0e10cSrcweir arena, 967cdf0e10cSrcweir name, 968cdf0e10cSrcweir quantum, 969cdf0e10cSrcweir quantum_cache_max, 970cdf0e10cSrcweir source_arena, 971cdf0e10cSrcweir source_alloc, 972cdf0e10cSrcweir source_free 973cdf0e10cSrcweir ); 974cdf0e10cSrcweir 975cdf0e10cSrcweir if (result == 0) 976cdf0e10cSrcweir { 977cdf0e10cSrcweir rtl_arena_deactivate (arena); 978cdf0e10cSrcweir rtl_arena_destructor (arena); 979cdf0e10cSrcweir VALGRIND_DESTROY_MEMPOOL(arena); 980cdf0e10cSrcweir rtl_arena_free (gp_arena_arena, arena, size); 981cdf0e10cSrcweir } 982cdf0e10cSrcweir } 983cdf0e10cSrcweir else if (gp_arena_arena == 0) 984cdf0e10cSrcweir { 985cdf0e10cSrcweir if (rtl_arena_init()) 986cdf0e10cSrcweir { 987cdf0e10cSrcweir /* try again */ 988cdf0e10cSrcweir goto try_alloc; 989cdf0e10cSrcweir } 990cdf0e10cSrcweir } 991cdf0e10cSrcweir return (result); 992cdf0e10cSrcweir } 993cdf0e10cSrcweir 994cdf0e10cSrcweir /** rtl_arena_destroy() 995cdf0e10cSrcweir */ 996cdf0e10cSrcweir void 997cdf0e10cSrcweir SAL_CALL rtl_arena_destroy ( 998cdf0e10cSrcweir rtl_arena_type * arena 999cdf0e10cSrcweir ) 1000cdf0e10cSrcweir { 1001cdf0e10cSrcweir if (arena != 0) 1002cdf0e10cSrcweir { 1003cdf0e10cSrcweir rtl_arena_deactivate (arena); 1004cdf0e10cSrcweir rtl_arena_destructor (arena); 1005cdf0e10cSrcweir VALGRIND_DESTROY_MEMPOOL(arena); 1006cdf0e10cSrcweir rtl_arena_free (gp_arena_arena, arena, sizeof(rtl_arena_type)); 1007cdf0e10cSrcweir } 1008cdf0e10cSrcweir } 1009cdf0e10cSrcweir 1010cdf0e10cSrcweir /** rtl_arena_alloc() 1011cdf0e10cSrcweir */ 1012cdf0e10cSrcweir void * 1013cdf0e10cSrcweir SAL_CALL rtl_arena_alloc ( 1014cdf0e10cSrcweir rtl_arena_type * arena, 1015cdf0e10cSrcweir sal_Size * pSize 1016cdf0e10cSrcweir ) SAL_THROW_EXTERN_C() 1017cdf0e10cSrcweir { 1018cdf0e10cSrcweir void * addr = 0; 1019cdf0e10cSrcweir 1020cdf0e10cSrcweir if ((arena != 0) && (pSize != 0)) 1021cdf0e10cSrcweir { 1022cdf0e10cSrcweir sal_Size size = RTL_MEMORY_ALIGN((*pSize), arena->m_quantum); 1023cdf0e10cSrcweir if (size > arena->m_qcache_max) 1024cdf0e10cSrcweir { 1025cdf0e10cSrcweir /* allocate from segment list */ 1026cdf0e10cSrcweir rtl_arena_segment_type *segment = 0; 1027cdf0e10cSrcweir 1028cdf0e10cSrcweir RTL_MEMORY_LOCK_ACQUIRE(&(arena->m_lock)); 1029cdf0e10cSrcweir if (rtl_arena_segment_alloc (arena, size, &segment) || 1030cdf0e10cSrcweir rtl_arena_segment_create(arena, size, &segment) ) 1031cdf0e10cSrcweir { 1032cdf0e10cSrcweir /* shrink to fit */ 1033cdf0e10cSrcweir sal_Size oversize; 1034cdf0e10cSrcweir 1035cdf0e10cSrcweir /* mark segment used */ 1036cdf0e10cSrcweir OSL_ASSERT(segment->m_type == RTL_ARENA_SEGMENT_TYPE_FREE); 1037cdf0e10cSrcweir segment->m_type = RTL_ARENA_SEGMENT_TYPE_USED; 1038cdf0e10cSrcweir 1039cdf0e10cSrcweir /* resize */ 1040cdf0e10cSrcweir OSL_ASSERT(segment->m_size >= size); 1041cdf0e10cSrcweir oversize = segment->m_size - size; 1042cdf0e10cSrcweir if (oversize >= SAL_MAX(arena->m_quantum, arena->m_qcache_max)) 1043cdf0e10cSrcweir { 1044cdf0e10cSrcweir rtl_arena_segment_type * remainder = 0; 1045cdf0e10cSrcweir rtl_arena_segment_get (arena, &remainder); 1046cdf0e10cSrcweir if (remainder != 0) 1047cdf0e10cSrcweir { 1048cdf0e10cSrcweir segment->m_size = size; 1049cdf0e10cSrcweir 1050cdf0e10cSrcweir remainder->m_addr = segment->m_addr + segment->m_size; 1051cdf0e10cSrcweir remainder->m_size = oversize; 1052cdf0e10cSrcweir remainder->m_type = RTL_ARENA_SEGMENT_TYPE_FREE; 1053cdf0e10cSrcweir QUEUE_INSERT_HEAD_NAMED(segment, remainder, s); 1054cdf0e10cSrcweir 1055cdf0e10cSrcweir rtl_arena_freelist_insert (arena, remainder); 1056cdf0e10cSrcweir } 1057cdf0e10cSrcweir } 1058cdf0e10cSrcweir 1059cdf0e10cSrcweir rtl_arena_hash_insert (arena, segment); 1060cdf0e10cSrcweir 1061cdf0e10cSrcweir /* DEBUG ONLY: mark allocated, undefined */ 1062cdf0e10cSrcweir OSL_DEBUG_ONLY(memset((void*)(segment->m_addr), 0x77777777, segment->m_size)); 1063cdf0e10cSrcweir VALGRIND_MEMPOOL_ALLOC(arena, segment->m_addr, segment->m_size); 1064cdf0e10cSrcweir 1065cdf0e10cSrcweir (*pSize) = segment->m_size; 1066cdf0e10cSrcweir addr = (void*)(segment->m_addr); 1067cdf0e10cSrcweir } 1068cdf0e10cSrcweir RTL_MEMORY_LOCK_RELEASE(&(arena->m_lock)); 1069cdf0e10cSrcweir } 1070cdf0e10cSrcweir else if (size > 0) 1071cdf0e10cSrcweir { 1072cdf0e10cSrcweir /* allocate from quantum cache(s) */ 1073cdf0e10cSrcweir int index = (size >> arena->m_quantum_shift) - 1; 1074cdf0e10cSrcweir OSL_ASSERT (arena->m_qcache_ptr[index] != 0); 1075cdf0e10cSrcweir 1076cdf0e10cSrcweir addr = rtl_cache_alloc (arena->m_qcache_ptr[index]); 1077cdf0e10cSrcweir if (addr != 0) 1078cdf0e10cSrcweir (*pSize) = size; 1079cdf0e10cSrcweir } 1080cdf0e10cSrcweir } 1081cdf0e10cSrcweir return (addr); 1082cdf0e10cSrcweir } 1083cdf0e10cSrcweir 1084cdf0e10cSrcweir /** rtl_arena_free() 1085cdf0e10cSrcweir */ 1086cdf0e10cSrcweir void 1087cdf0e10cSrcweir SAL_CALL rtl_arena_free ( 1088cdf0e10cSrcweir rtl_arena_type * arena, 1089cdf0e10cSrcweir void * addr, 1090cdf0e10cSrcweir sal_Size size 1091cdf0e10cSrcweir ) SAL_THROW_EXTERN_C() 1092cdf0e10cSrcweir { 1093cdf0e10cSrcweir if (arena != 0) 1094cdf0e10cSrcweir { 1095cdf0e10cSrcweir size = RTL_MEMORY_ALIGN(size, arena->m_quantum); 1096cdf0e10cSrcweir if (size > arena->m_qcache_max) 1097cdf0e10cSrcweir { 1098cdf0e10cSrcweir /* free to segment list */ 1099cdf0e10cSrcweir rtl_arena_segment_type * segment; 1100cdf0e10cSrcweir 1101cdf0e10cSrcweir RTL_MEMORY_LOCK_ACQUIRE(&(arena->m_lock)); 1102cdf0e10cSrcweir 1103cdf0e10cSrcweir segment = rtl_arena_hash_remove (arena, (sal_uIntPtr)(addr), size); 1104cdf0e10cSrcweir if (segment != 0) 1105cdf0e10cSrcweir { 1106cdf0e10cSrcweir rtl_arena_segment_type *next, *prev; 1107cdf0e10cSrcweir 1108cdf0e10cSrcweir /* DEBUG ONLY: mark unallocated, undefined */ 1109cdf0e10cSrcweir VALGRIND_MEMPOOL_FREE(arena, segment->m_addr); 1110cdf0e10cSrcweir /* OSL_DEBUG_ONLY() */ VALGRIND_MAKE_MEM_UNDEFINED(segment->m_addr, segment->m_size); 1111cdf0e10cSrcweir OSL_DEBUG_ONLY(memset((void*)(segment->m_addr), 0x33333333, segment->m_size)); 1112cdf0e10cSrcweir 1113cdf0e10cSrcweir /* coalesce w/ adjacent free segment(s) */ 1114cdf0e10cSrcweir rtl_arena_segment_coalesce (arena, segment); 1115cdf0e10cSrcweir 1116cdf0e10cSrcweir /* determine (new) next and prev segment */ 1117cdf0e10cSrcweir next = segment->m_snext, prev = segment->m_sprev; 1118cdf0e10cSrcweir 1119cdf0e10cSrcweir /* entire span free when prev is a span, and next is either a span or a list head */ 1120cdf0e10cSrcweir if (((prev->m_type == RTL_ARENA_SEGMENT_TYPE_SPAN)) && 1121cdf0e10cSrcweir ((next->m_type == RTL_ARENA_SEGMENT_TYPE_SPAN) || 1122cdf0e10cSrcweir (next->m_type == RTL_ARENA_SEGMENT_TYPE_HEAD)) ) 1123cdf0e10cSrcweir { 1124cdf0e10cSrcweir OSL_ASSERT((prev->m_addr == segment->m_addr) && 1125cdf0e10cSrcweir (prev->m_size == segment->m_size) ); 1126cdf0e10cSrcweir 1127cdf0e10cSrcweir if (arena->m_source_free) 1128cdf0e10cSrcweir { 1129cdf0e10cSrcweir addr = (void*)(prev->m_addr); 1130cdf0e10cSrcweir size = prev->m_size; 1131cdf0e10cSrcweir 1132cdf0e10cSrcweir /* remove from segment list */ 1133cdf0e10cSrcweir QUEUE_REMOVE_NAMED(segment, s); 1134cdf0e10cSrcweir 1135cdf0e10cSrcweir /* release segment descriptor */ 1136cdf0e10cSrcweir rtl_arena_segment_put (arena, &segment); 1137cdf0e10cSrcweir 1138cdf0e10cSrcweir /* remove from segment list */ 1139cdf0e10cSrcweir QUEUE_REMOVE_NAMED(prev, s); 1140cdf0e10cSrcweir 1141cdf0e10cSrcweir /* release (span) segment descriptor */ 1142cdf0e10cSrcweir rtl_arena_segment_put (arena, &prev); 1143cdf0e10cSrcweir 1144cdf0e10cSrcweir /* update stats, return span to source arena */ 1145cdf0e10cSrcweir arena->m_stats.m_mem_total -= size; 1146cdf0e10cSrcweir RTL_MEMORY_LOCK_RELEASE(&(arena->m_lock)); 1147cdf0e10cSrcweir 1148cdf0e10cSrcweir (arena->m_source_free)(arena->m_source_arena, addr, size); 1149cdf0e10cSrcweir return; 1150cdf0e10cSrcweir } 1151cdf0e10cSrcweir } 1152cdf0e10cSrcweir 1153cdf0e10cSrcweir /* insert onto freelist */ 1154cdf0e10cSrcweir rtl_arena_freelist_insert (arena, segment); 1155cdf0e10cSrcweir } 1156cdf0e10cSrcweir 1157cdf0e10cSrcweir RTL_MEMORY_LOCK_RELEASE(&(arena->m_lock)); 1158cdf0e10cSrcweir } 1159cdf0e10cSrcweir else if (size > 0) 1160cdf0e10cSrcweir { 1161cdf0e10cSrcweir /* free to quantum cache(s) */ 1162cdf0e10cSrcweir int index = (size >> arena->m_quantum_shift) - 1; 1163cdf0e10cSrcweir OSL_ASSERT (arena->m_qcache_ptr[index] != 0); 1164cdf0e10cSrcweir 1165cdf0e10cSrcweir rtl_cache_free (arena->m_qcache_ptr[index], addr); 1166cdf0e10cSrcweir } 1167cdf0e10cSrcweir } 1168cdf0e10cSrcweir } 1169cdf0e10cSrcweir 1170cdf0e10cSrcweir /* ================================================================= * 1171cdf0e10cSrcweir * 1172cdf0e10cSrcweir * machdep internals. 1173cdf0e10cSrcweir * 1174cdf0e10cSrcweir * ================================================================= */ 1175cdf0e10cSrcweir 1176cdf0e10cSrcweir #if defined(SAL_UNX) 1177cdf0e10cSrcweir #include <sys/mman.h> 1178cdf0e10cSrcweir #elif defined(SAL_W32) || defined(SAL_OS2) 1179cdf0e10cSrcweir #define MAP_FAILED 0 1180cdf0e10cSrcweir #endif /* SAL_UNX || SAL_W32 */ 1181cdf0e10cSrcweir 1182cdf0e10cSrcweir /** rtl_machdep_alloc() 1183cdf0e10cSrcweir */ 1184cdf0e10cSrcweir static void * 1185cdf0e10cSrcweir SAL_CALL rtl_machdep_alloc ( 1186cdf0e10cSrcweir rtl_arena_type * pArena, 1187cdf0e10cSrcweir sal_Size * pSize 1188cdf0e10cSrcweir ) 1189cdf0e10cSrcweir { 1190cdf0e10cSrcweir void * addr; 1191cdf0e10cSrcweir sal_Size size = (*pSize); 1192cdf0e10cSrcweir 1193cdf0e10cSrcweir OSL_PRECOND(pArena == gp_machdep_arena, "rtl_machdep_alloc(): invalid argument"); 1194cdf0e10cSrcweir 1195cdf0e10cSrcweir #if defined(SOLARIS) && defined(SPARC) 1196cdf0e10cSrcweir /* see @ mmap(2) man pages */ 1197cdf0e10cSrcweir size += (pArena->m_quantum + pArena->m_quantum); /* "red-zone" pages */ 1198cdf0e10cSrcweir if (size > (4 << 20)) 1199cdf0e10cSrcweir size = RTL_MEMORY_P2ROUNDUP(size, (4 << 20)); 1200cdf0e10cSrcweir else if (size > (512 << 10)) 1201cdf0e10cSrcweir size = RTL_MEMORY_P2ROUNDUP(size, (512 << 10)); 1202cdf0e10cSrcweir else 1203cdf0e10cSrcweir size = RTL_MEMORY_P2ROUNDUP(size, (64 << 10)); 1204cdf0e10cSrcweir size -= (pArena->m_quantum + pArena->m_quantum); /* "red-zone" pages */ 1205cdf0e10cSrcweir #else 1206cdf0e10cSrcweir /* default allocation granularity */ 1207cdf0e10cSrcweir size = RTL_MEMORY_P2ROUNDUP(size, SAL_MAX(pArena->m_quantum, 64 << 10)); 1208cdf0e10cSrcweir #endif 1209cdf0e10cSrcweir 1210cdf0e10cSrcweir #if defined(SAL_UNX) 1211cdf0e10cSrcweir addr = mmap (NULL, (size_t)(size), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); 1212cdf0e10cSrcweir #elif defined(SAL_W32) 1213cdf0e10cSrcweir addr = VirtualAlloc (NULL, (SIZE_T)(size), MEM_COMMIT, PAGE_READWRITE); 1214cdf0e10cSrcweir #elif defined(SAL_OS2) 1215cdf0e10cSrcweir { 1216cdf0e10cSrcweir APIRET rc; 1217cdf0e10cSrcweir addr = 0; 1218cdf0e10cSrcweir // Use DosAlloc* to get a 4KB page aligned address. 1219cdf0e10cSrcweir rc = DosAllocMem( &addr, size, PAG_COMMIT | PAG_READ | PAG_WRITE | OBJ_ANY); 1220cdf0e10cSrcweir if (rc) { 1221cdf0e10cSrcweir fprintf( stderr, "sal3::DosAllocMem failed rc=%d\n", rc); 1222cdf0e10cSrcweir addr = 0; 1223cdf0e10cSrcweir } 1224cdf0e10cSrcweir } 1225cdf0e10cSrcweir #endif /* (SAL_UNX || SAL_W32 || SAL_OS2) */ 1226cdf0e10cSrcweir 1227cdf0e10cSrcweir if (addr != MAP_FAILED) 1228cdf0e10cSrcweir { 1229cdf0e10cSrcweir pArena->m_stats.m_alloc += 1; 1230cdf0e10cSrcweir pArena->m_stats.m_mem_total += size; 1231cdf0e10cSrcweir pArena->m_stats.m_mem_alloc += size; 1232cdf0e10cSrcweir 1233cdf0e10cSrcweir (*pSize) = size; 1234cdf0e10cSrcweir return (addr); 1235cdf0e10cSrcweir } 1236cdf0e10cSrcweir return (NULL); 1237cdf0e10cSrcweir } 1238cdf0e10cSrcweir 1239cdf0e10cSrcweir /** rtl_machdep_free() 1240cdf0e10cSrcweir */ 1241cdf0e10cSrcweir static void 1242cdf0e10cSrcweir SAL_CALL rtl_machdep_free ( 1243cdf0e10cSrcweir rtl_arena_type * pArena, 1244cdf0e10cSrcweir void * pAddr, 1245cdf0e10cSrcweir sal_Size nSize 1246cdf0e10cSrcweir ) 1247cdf0e10cSrcweir { 1248cdf0e10cSrcweir OSL_PRECOND(pArena == gp_machdep_arena, "rtl_machdep_free(): invalid argument"); 1249cdf0e10cSrcweir 1250cdf0e10cSrcweir pArena->m_stats.m_free += 1; 1251cdf0e10cSrcweir pArena->m_stats.m_mem_total -= nSize; 1252cdf0e10cSrcweir pArena->m_stats.m_mem_alloc -= nSize; 1253cdf0e10cSrcweir 1254cdf0e10cSrcweir #if defined(SAL_UNX) 1255cdf0e10cSrcweir (void) munmap(pAddr, nSize); 1256cdf0e10cSrcweir #elif defined(SAL_W32) 1257cdf0e10cSrcweir (void) VirtualFree ((LPVOID)(pAddr), (SIZE_T)(0), MEM_RELEASE); 1258cdf0e10cSrcweir #elif defined(SAL_OS2) 1259cdf0e10cSrcweir (void) DosFreeMem( pAddr); 1260cdf0e10cSrcweir #endif /* (SAL_UNX || SAL_W32) */ 1261cdf0e10cSrcweir } 1262cdf0e10cSrcweir 1263cdf0e10cSrcweir /** rtl_machdep_pagesize() 1264cdf0e10cSrcweir */ 1265cdf0e10cSrcweir static sal_Size 1266cdf0e10cSrcweir rtl_machdep_pagesize (void) 1267cdf0e10cSrcweir { 1268cdf0e10cSrcweir #if defined(SAL_UNX) 1269cdf0e10cSrcweir #if defined(FREEBSD) || defined(NETBSD) 1270cdf0e10cSrcweir return ((sal_Size)getpagesize()); 1271cdf0e10cSrcweir #else /* POSIX */ 1272cdf0e10cSrcweir return ((sal_Size)sysconf(_SC_PAGESIZE)); 1273cdf0e10cSrcweir #endif /* xBSD || POSIX */ 1274cdf0e10cSrcweir #elif defined(SAL_W32) 1275cdf0e10cSrcweir SYSTEM_INFO info; 1276cdf0e10cSrcweir GetSystemInfo (&info); 1277cdf0e10cSrcweir return ((sal_Size)(info.dwPageSize)); 1278cdf0e10cSrcweir #elif defined(SAL_OS2) 1279cdf0e10cSrcweir ULONG ulPageSize; 1280cdf0e10cSrcweir DosQuerySysInfo(QSV_PAGE_SIZE, QSV_PAGE_SIZE, &ulPageSize, sizeof(ULONG)); 1281cdf0e10cSrcweir return ((sal_Size)ulPageSize); 1282cdf0e10cSrcweir #endif /* (SAL_UNX || SAL_W32) */ 1283cdf0e10cSrcweir } 1284cdf0e10cSrcweir 1285cdf0e10cSrcweir /* ================================================================= * 1286cdf0e10cSrcweir * 1287cdf0e10cSrcweir * arena initialization. 1288cdf0e10cSrcweir * 1289cdf0e10cSrcweir * ================================================================= */ 1290cdf0e10cSrcweir 1291cdf0e10cSrcweir static void 1292cdf0e10cSrcweir rtl_arena_once_init (void) 1293cdf0e10cSrcweir { 1294cdf0e10cSrcweir { 1295cdf0e10cSrcweir /* list of arenas */ 1296cdf0e10cSrcweir RTL_MEMORY_LOCK_INIT(&(g_arena_list.m_lock)); 1297cdf0e10cSrcweir rtl_arena_constructor (&(g_arena_list.m_arena_head)); 1298cdf0e10cSrcweir } 1299cdf0e10cSrcweir { 1300cdf0e10cSrcweir /* machdep (pseudo) arena */ 1301cdf0e10cSrcweir static rtl_arena_type g_machdep_arena; 1302cdf0e10cSrcweir 1303cdf0e10cSrcweir OSL_ASSERT(gp_machdep_arena == 0); 1304cdf0e10cSrcweir VALGRIND_CREATE_MEMPOOL(&g_machdep_arena, 0, 0); 1305cdf0e10cSrcweir rtl_arena_constructor (&g_machdep_arena); 1306cdf0e10cSrcweir 1307cdf0e10cSrcweir gp_machdep_arena = rtl_arena_activate ( 1308cdf0e10cSrcweir &g_machdep_arena, 1309cdf0e10cSrcweir "rtl_machdep_arena", 1310cdf0e10cSrcweir rtl_machdep_pagesize(), 1311cdf0e10cSrcweir 0, /* no quantum caching */ 1312cdf0e10cSrcweir 0, 0, 0 /* no source */ 1313cdf0e10cSrcweir ); 1314cdf0e10cSrcweir OSL_ASSERT(gp_machdep_arena != 0); 1315cdf0e10cSrcweir } 1316cdf0e10cSrcweir { 1317cdf0e10cSrcweir /* default arena */ 1318cdf0e10cSrcweir static rtl_arena_type g_default_arena; 1319cdf0e10cSrcweir 1320cdf0e10cSrcweir OSL_ASSERT(gp_default_arena == 0); 1321cdf0e10cSrcweir VALGRIND_CREATE_MEMPOOL(&g_default_arena, 0, 0); 1322cdf0e10cSrcweir rtl_arena_constructor (&g_default_arena); 1323cdf0e10cSrcweir 1324cdf0e10cSrcweir gp_default_arena = rtl_arena_activate ( 1325cdf0e10cSrcweir &g_default_arena, 1326cdf0e10cSrcweir "rtl_default_arena", 1327cdf0e10cSrcweir rtl_machdep_pagesize(), 1328cdf0e10cSrcweir 0, /* no quantum caching */ 1329cdf0e10cSrcweir gp_machdep_arena, /* source */ 1330cdf0e10cSrcweir rtl_machdep_alloc, 1331cdf0e10cSrcweir rtl_machdep_free 1332cdf0e10cSrcweir ); 1333cdf0e10cSrcweir OSL_ASSERT(gp_default_arena != 0); 1334cdf0e10cSrcweir } 1335cdf0e10cSrcweir { 1336cdf0e10cSrcweir /* arena internal arena */ 1337cdf0e10cSrcweir static rtl_arena_type g_arena_arena; 1338cdf0e10cSrcweir 1339cdf0e10cSrcweir OSL_ASSERT(gp_arena_arena == 0); 1340cdf0e10cSrcweir VALGRIND_CREATE_MEMPOOL(&g_arena_arena, 0, 0); 1341cdf0e10cSrcweir rtl_arena_constructor (&g_arena_arena); 1342cdf0e10cSrcweir 1343cdf0e10cSrcweir gp_arena_arena = rtl_arena_activate ( 1344cdf0e10cSrcweir &g_arena_arena, 1345cdf0e10cSrcweir "rtl_arena_internal_arena", 1346cdf0e10cSrcweir 64, /* quantum */ 1347cdf0e10cSrcweir 0, /* no quantum caching */ 1348cdf0e10cSrcweir gp_default_arena, /* source */ 1349cdf0e10cSrcweir rtl_arena_alloc, 1350cdf0e10cSrcweir rtl_arena_free 1351cdf0e10cSrcweir ); 1352cdf0e10cSrcweir OSL_ASSERT(gp_arena_arena != 0); 1353cdf0e10cSrcweir } 1354cdf0e10cSrcweir } 1355cdf0e10cSrcweir 1356cdf0e10cSrcweir static int 1357cdf0e10cSrcweir rtl_arena_init (void) 1358cdf0e10cSrcweir { 1359cdf0e10cSrcweir static sal_once_type g_once = SAL_ONCE_INIT; 1360cdf0e10cSrcweir SAL_ONCE(&g_once, rtl_arena_once_init); 1361cdf0e10cSrcweir return (gp_arena_arena != 0); 1362cdf0e10cSrcweir } 1363cdf0e10cSrcweir 1364cdf0e10cSrcweir /* ================================================================= */ 1365cdf0e10cSrcweir 1366cdf0e10cSrcweir /* 1367cdf0e10cSrcweir Issue http://udk.openoffice.org/issues/show_bug.cgi?id=92388 1368cdf0e10cSrcweir 1369cdf0e10cSrcweir Mac OS X does not seem to support "__cxa__atexit", thus leading 1370cdf0e10cSrcweir to the situation that "__attribute__((destructor))__" functions 1371cdf0e10cSrcweir (in particular "rtl_{memory|cache|arena}_fini") become called 1372cdf0e10cSrcweir _before_ global C++ object d'tors. 1373cdf0e10cSrcweir 1374cdf0e10cSrcweir Delegated the call to "rtl_arena_fini()" into a dummy C++ object, 1375cdf0e10cSrcweir see alloc_fini.cxx . 1376cdf0e10cSrcweir */ 1377cdf0e10cSrcweir #if defined(__GNUC__) && !defined(MACOSX) 1378cdf0e10cSrcweir static void rtl_arena_fini (void) __attribute__((destructor)); 1379cdf0e10cSrcweir #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) 1380cdf0e10cSrcweir #pragma fini(rtl_arena_fini) 1381cdf0e10cSrcweir static void rtl_arena_fini (void); 1382cdf0e10cSrcweir #endif /* __GNUC__ || __SUNPRO_C */ 1383cdf0e10cSrcweir 1384cdf0e10cSrcweir void 1385cdf0e10cSrcweir rtl_arena_fini (void) 1386cdf0e10cSrcweir { 1387cdf0e10cSrcweir if (gp_arena_arena != 0) 1388cdf0e10cSrcweir { 1389cdf0e10cSrcweir rtl_arena_type * arena, * head; 1390cdf0e10cSrcweir 1391cdf0e10cSrcweir RTL_MEMORY_LOCK_ACQUIRE(&(g_arena_list.m_lock)); 1392cdf0e10cSrcweir head = &(g_arena_list.m_arena_head); 1393cdf0e10cSrcweir 1394cdf0e10cSrcweir for (arena = head->m_arena_next; arena != head; arena = arena->m_arena_next) 1395cdf0e10cSrcweir { 1396cdf0e10cSrcweir OSL_TRACE( 1397cdf0e10cSrcweir "rtl_arena_fini(\"%s\"): " 1398cdf0e10cSrcweir "allocs: %"PRIu64", frees: %"PRIu64"; total: %lu, used: %lu", 1399cdf0e10cSrcweir arena->m_name, 1400cdf0e10cSrcweir arena->m_stats.m_alloc, arena->m_stats.m_free, 1401cdf0e10cSrcweir arena->m_stats.m_mem_total, arena->m_stats.m_mem_alloc 1402cdf0e10cSrcweir ); 1403cdf0e10cSrcweir } 1404cdf0e10cSrcweir RTL_MEMORY_LOCK_RELEASE(&(g_arena_list.m_lock)); 1405cdf0e10cSrcweir } 1406cdf0e10cSrcweir } 1407cdf0e10cSrcweir 1408cdf0e10cSrcweir /* ================================================================= */ 1409