1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "alloc_arena.hxx"
22 #include "alloc_impl.hxx"
23 #include "internal/rtllifecycle.h"
24 #include "sal/macros.h"
25 #include "osl/diagnose.h"
31 extern AllocMode alloc_mode
;
33 /* ================================================================= *
37 * ================================================================= */
42 struct rtl_arena_list_st
44 rtl_memory_lock_type m_lock
;
45 rtl_arena_type m_arena_head
;
48 static rtl_arena_list_st g_arena_list
;
51 * provided for arena_type allocations, and hash_table resizing.
55 static rtl_arena_type
* gp_arena_arena
= 0;
59 * Low level virtual memory (pseudo) arena
60 * (platform dependent implementation)
64 static rtl_arena_type
* gp_machdep_arena
= 0;
68 rtl_arena_type
* gp_default_arena
= 0;
74 SAL_CALL
rtl_machdep_alloc (
75 rtl_arena_type
* pArena
,
80 SAL_CALL
rtl_machdep_free (
81 rtl_arena_type
* pArena
,
87 rtl_machdep_pagesize();
89 /* ================================================================= */
91 /** rtl_arena_segment_constructor()
94 rtl_arena_segment_constructor (void * obj
)
96 rtl_arena_segment_type
* segment
= (rtl_arena_segment_type
*)(obj
);
98 QUEUE_START_NAMED(segment
, s
);
99 QUEUE_START_NAMED(segment
, f
);
104 /** rtl_arena_segment_destructor()
107 rtl_arena_segment_destructor (void * obj
)
109 rtl_arena_segment_type
* segment
= static_cast< rtl_arena_segment_type
* >(
111 assert(QUEUE_STARTED_NAMED(segment
, s
));
112 assert(QUEUE_STARTED_NAMED(segment
, f
));
113 (void) segment
; // avoid warnings
116 /* ================================================================= */
118 /** rtl_arena_segment_populate()
120 * @precond arena->m_lock acquired.
123 rtl_arena_segment_populate (
124 rtl_arena_type
* arena
127 rtl_arena_segment_type
*span
;
128 sal_Size size
= rtl_machdep_pagesize();
130 span
= static_cast< rtl_arena_segment_type
* >(
131 rtl_machdep_alloc(gp_machdep_arena
, &size
));
134 rtl_arena_segment_type
*first
, *last
, *head
;
135 sal_Size count
= size
/ sizeof(rtl_arena_segment_type
);
137 /* insert onto reserve span list */
138 QUEUE_INSERT_TAIL_NAMED(&(arena
->m_segment_reserve_span_head
), span
, s
);
139 QUEUE_START_NAMED(span
, f
);
140 span
->m_addr
= (sal_uIntPtr
)(span
);
142 span
->m_type
= RTL_ARENA_SEGMENT_TYPE_SPAN
;
144 /* insert onto reserve list */
145 head
= &(arena
->m_segment_reserve_head
);
146 for (first
= span
+ 1, last
= span
+ count
; first
< last
; ++first
)
148 QUEUE_INSERT_TAIL_NAMED(head
, first
, s
);
149 QUEUE_START_NAMED(first
, f
);
158 /** rtl_arena_segment_get()
160 * @precond arena->m_lock acquired.
161 * @precond (*ppSegment == 0)
164 rtl_arena_segment_get (
165 rtl_arena_type
* arena
,
166 rtl_arena_segment_type
** ppSegment
169 rtl_arena_segment_type
* head
;
171 assert(*ppSegment
== 0);
173 head
= &(arena
->m_segment_reserve_head
);
174 if ((head
->m_snext
!= head
) || rtl_arena_segment_populate (arena
))
176 (*ppSegment
) = head
->m_snext
;
177 QUEUE_REMOVE_NAMED((*ppSegment
), s
);
181 /** rtl_arena_segment_put()
183 * @precond arena->m_lock acquired.
184 * @postcond (*ppSegment == 0)
187 rtl_arena_segment_put (
188 rtl_arena_type
* arena
,
189 rtl_arena_segment_type
** ppSegment
192 rtl_arena_segment_type
* head
;
194 assert(QUEUE_STARTED_NAMED((*ppSegment
), s
));
195 assert(QUEUE_STARTED_NAMED((*ppSegment
), f
));
197 (*ppSegment
)->m_addr
= 0;
198 (*ppSegment
)->m_size
= 0;
200 assert((*ppSegment
)->m_type
!= RTL_ARENA_SEGMENT_TYPE_HEAD
);
201 (*ppSegment
)->m_type
= 0;
203 /* keep as reserve */
204 head
= &(arena
->m_segment_reserve_head
);
205 QUEUE_INSERT_HEAD_NAMED(head
, (*ppSegment
), s
);
211 /** rtl_arena_freelist_insert()
213 * @precond arena->m_lock acquired.
216 rtl_arena_freelist_insert (
217 rtl_arena_type
* arena
,
218 rtl_arena_segment_type
* segment
221 rtl_arena_segment_type
* head
;
223 head
= &(arena
->m_freelist_head
[highbit(segment
->m_size
) - 1]);
224 QUEUE_INSERT_TAIL_NAMED(head
, segment
, f
);
226 arena
->m_freelist_bitmap
|= head
->m_size
;
229 /** rtl_arena_freelist_remove()
231 * @precond arena->m_lock acquired.
234 rtl_arena_freelist_remove (
235 rtl_arena_type
* arena
,
236 rtl_arena_segment_type
* segment
239 if ((segment
->m_fnext
->m_type
== RTL_ARENA_SEGMENT_TYPE_HEAD
) &&
240 (segment
->m_fprev
->m_type
== RTL_ARENA_SEGMENT_TYPE_HEAD
) )
242 rtl_arena_segment_type
* head
;
244 head
= segment
->m_fprev
;
245 assert(arena
->m_freelist_bitmap
& head
->m_size
);
246 arena
->m_freelist_bitmap
^= head
->m_size
;
248 QUEUE_REMOVE_NAMED(segment
, f
);
251 /* ================================================================= */
253 /** RTL_ARENA_HASH_INDEX()
255 #define RTL_ARENA_HASH_INDEX_IMPL(a, s, q, m) \
256 ((((a) + ((a) >> (s)) + ((a) >> ((s) << 1))) >> (q)) & (m))
258 #define RTL_ARENA_HASH_INDEX(arena, addr) \
259 RTL_ARENA_HASH_INDEX_IMPL((addr), (arena)->m_hash_shift, (arena)->m_quantum_shift, ((arena)->m_hash_size - 1))
261 /** rtl_arena_hash_rescale()
263 * @precond arena->m_lock released.
266 rtl_arena_hash_rescale (
267 rtl_arena_type
* arena
,
271 rtl_arena_segment_type
** new_table
;
274 new_bytes
= new_size
* sizeof(rtl_arena_segment_type
*);
275 new_table
= (rtl_arena_segment_type
**)rtl_arena_alloc (gp_arena_arena
, &new_bytes
);
279 rtl_arena_segment_type
** old_table
;
280 sal_Size old_size
, i
;
282 memset (new_table
, 0, new_bytes
);
284 RTL_MEMORY_LOCK_ACQUIRE(&(arena
->m_lock
));
286 old_table
= arena
->m_hash_table
;
287 old_size
= arena
->m_hash_size
;
291 // "rtl_arena_hash_rescale(" << arena->m_name << "): nseg: "
292 // << (arena->m_stats.m_alloc - arena->m_stats.m_free) << " (ave: "
293 // << ((arena->m_stats.m_alloc - arena->m_stats.m_free)
294 // >> arena->m_hash_shift)
295 // << "), frees: " << arena->m_stats.m_free << " [old_size: "
296 // << old_size << ", new_size: " << new_size << ']');
298 arena
->m_hash_table
= new_table
;
299 arena
->m_hash_size
= new_size
;
300 arena
->m_hash_shift
= highbit(arena
->m_hash_size
) - 1;
302 for (i
= 0; i
< old_size
; i
++)
304 rtl_arena_segment_type
* curr
= old_table
[i
];
307 rtl_arena_segment_type
* next
= curr
->m_fnext
;
308 rtl_arena_segment_type
** head
;
310 head
= &(arena
->m_hash_table
[RTL_ARENA_HASH_INDEX(arena
, curr
->m_addr
)]);
311 curr
->m_fnext
= (*head
);
319 RTL_MEMORY_LOCK_RELEASE(&(arena
->m_lock
));
321 if (old_table
!= arena
->m_hash_table_0
)
323 sal_Size old_bytes
= old_size
* sizeof(rtl_arena_segment_type
*);
324 rtl_arena_free (gp_arena_arena
, old_table
, old_bytes
);
329 /** rtl_arena_hash_insert()
330 * ...and update stats.
333 rtl_arena_hash_insert (
334 rtl_arena_type
* arena
,
335 rtl_arena_segment_type
* segment
338 rtl_arena_segment_type
** ppSegment
;
340 ppSegment
= &(arena
->m_hash_table
[RTL_ARENA_HASH_INDEX(arena
, segment
->m_addr
)]);
342 segment
->m_fnext
= (*ppSegment
);
343 (*ppSegment
) = segment
;
345 arena
->m_stats
.m_alloc
+= 1;
346 arena
->m_stats
.m_mem_alloc
+= segment
->m_size
;
349 /** rtl_arena_hash_remove()
350 * ...and update stats.
352 rtl_arena_segment_type
*
353 rtl_arena_hash_remove (
354 rtl_arena_type
* arena
,
359 rtl_arena_segment_type
*segment
, **segpp
;
360 sal_Size lookups
= 0;
362 segpp
= &(arena
->m_hash_table
[RTL_ARENA_HASH_INDEX(arena
, addr
)]);
363 while ((segment
= *segpp
) != 0)
365 if (segment
->m_addr
== addr
)
367 *segpp
= segment
->m_fnext
, segment
->m_fnext
= segment
->m_fprev
= segment
;
371 /* update lookup miss stats */
373 segpp
= &(segment
->m_fnext
);
376 assert(segment
!= 0); // bad free
379 assert(segment
->m_size
== size
);
380 (void) size
; // avoid warnings
382 arena
->m_stats
.m_free
+= 1;
383 arena
->m_stats
.m_mem_alloc
-= segment
->m_size
;
387 sal_Size nseg
= (sal_Size
)(arena
->m_stats
.m_alloc
- arena
->m_stats
.m_free
);
388 if (nseg
> 4 * arena
->m_hash_size
)
390 if (!(arena
->m_flags
& RTL_ARENA_FLAG_RESCALE
))
392 sal_Size ave
= nseg
>> arena
->m_hash_shift
;
393 sal_Size new_size
= arena
->m_hash_size
<< (highbit(ave
) - 1);
395 arena
->m_flags
|= RTL_ARENA_FLAG_RESCALE
;
396 RTL_MEMORY_LOCK_RELEASE(&(arena
->m_lock
));
397 rtl_arena_hash_rescale (arena
, new_size
);
398 RTL_MEMORY_LOCK_ACQUIRE(&(arena
->m_lock
));
399 arena
->m_flags
&= ~RTL_ARENA_FLAG_RESCALE
;
408 /* ================================================================= */
410 /** rtl_arena_segment_alloc()
411 * allocate (and remove) segment from freelist
413 * @precond arena->m_lock acquired
414 * @precond (*ppSegment == 0)
417 rtl_arena_segment_alloc (
418 rtl_arena_type
* arena
,
420 rtl_arena_segment_type
** ppSegment
425 assert(*ppSegment
== 0);
426 if (!RTL_MEMORY_ISP2(size
))
428 int msb
= highbit(size
);
429 if (RTL_ARENA_FREELIST_SIZE
== sal::static_int_cast
< size_t >(msb
))
431 /* highest possible freelist: fall back to first fit */
432 rtl_arena_segment_type
*head
, *segment
;
434 head
= &(arena
->m_freelist_head
[msb
- 1]);
435 for (segment
= head
->m_fnext
; segment
!= head
; segment
= segment
->m_fnext
)
437 if (segment
->m_size
>= size
)
439 /* allocate first fit segment */
440 (*ppSegment
) = segment
;
444 goto dequeue_and_leave
;
447 /* roundup to next power of 2 */
448 size
= (((sal_Size
)1) << msb
);
451 index
= lowbit(RTL_MEMORY_P2ALIGN(arena
->m_freelist_bitmap
, size
));
454 /* instant fit: allocate first free segment */
455 rtl_arena_segment_type
*head
;
457 head
= &(arena
->m_freelist_head
[index
- 1]);
458 (*ppSegment
) = head
->m_fnext
;
459 assert((*ppSegment
) != head
);
465 /* remove from freelist */
466 rtl_arena_freelist_remove (arena
, (*ppSegment
));
468 return (*ppSegment
!= 0);
471 /** rtl_arena_segment_create()
472 * import new (span) segment from source arena
474 * @precond arena->m_lock acquired
475 * @precond (*ppSegment == 0)
478 rtl_arena_segment_create (
479 rtl_arena_type
* arena
,
481 rtl_arena_segment_type
** ppSegment
484 assert((*ppSegment
) == 0);
485 if (arena
->m_source_alloc
!= 0)
487 rtl_arena_segment_get (arena
, ppSegment
);
490 rtl_arena_segment_type
* span
= 0;
491 rtl_arena_segment_get (arena
, &span
);
494 /* import new span from source arena */
495 RTL_MEMORY_LOCK_RELEASE(&(arena
->m_lock
));
498 span
->m_addr
= (sal_uIntPtr
)(arena
->m_source_alloc
)(
499 arena
->m_source_arena
, &(span
->m_size
));
501 RTL_MEMORY_LOCK_ACQUIRE(&(arena
->m_lock
));
502 if (span
->m_addr
!= 0)
504 /* insert onto segment list, update stats */
505 span
->m_type
= RTL_ARENA_SEGMENT_TYPE_SPAN
;
506 QUEUE_INSERT_HEAD_NAMED(&(arena
->m_segment_head
), span
, s
);
507 arena
->m_stats
.m_mem_total
+= span
->m_size
;
509 (*ppSegment
)->m_addr
= span
->m_addr
;
510 (*ppSegment
)->m_size
= span
->m_size
;
511 (*ppSegment
)->m_type
= RTL_ARENA_SEGMENT_TYPE_FREE
;
512 QUEUE_INSERT_HEAD_NAMED(span
, (*ppSegment
), s
);
517 rtl_arena_segment_put (arena
, &span
);
519 rtl_arena_segment_put (arena
, ppSegment
);
525 /** rtl_arena_segment_coalesce()
526 * mark as free and join with adjacent free segment(s)
528 * @precond arena->m_lock acquired
529 * @precond segment marked 'used'
532 rtl_arena_segment_coalesce (
533 rtl_arena_type
* arena
,
534 rtl_arena_segment_type
* segment
537 rtl_arena_segment_type
*next
, *prev
;
539 /* mark segment free */
540 assert(segment
->m_type
== RTL_ARENA_SEGMENT_TYPE_USED
);
541 segment
->m_type
= RTL_ARENA_SEGMENT_TYPE_FREE
;
543 /* try to merge w/ next segment */
544 next
= segment
->m_snext
;
545 if (next
->m_type
== RTL_ARENA_SEGMENT_TYPE_FREE
)
547 assert(segment
->m_addr
+ segment
->m_size
== next
->m_addr
);
548 segment
->m_size
+= next
->m_size
;
550 /* remove from freelist */
551 rtl_arena_freelist_remove (arena
, next
);
553 /* remove from segment list */
554 QUEUE_REMOVE_NAMED(next
, s
);
556 /* release segment descriptor */
557 rtl_arena_segment_put (arena
, &next
);
560 /* try to merge w/ prev segment */
561 prev
= segment
->m_sprev
;
562 if (prev
->m_type
== RTL_ARENA_SEGMENT_TYPE_FREE
)
564 assert(prev
->m_addr
+ prev
->m_size
== segment
->m_addr
);
565 segment
->m_addr
= prev
->m_addr
;
566 segment
->m_size
+= prev
->m_size
;
568 /* remove from freelist */
569 rtl_arena_freelist_remove (arena
, prev
);
571 /* remove from segment list */
572 QUEUE_REMOVE_NAMED(prev
, s
);
574 /* release segment descriptor */
575 rtl_arena_segment_put (arena
, &prev
);
579 /* ================================================================= */
581 /** rtl_arena_constructor()
584 rtl_arena_constructor (void * obj
)
586 rtl_arena_type
* arena
= (rtl_arena_type
*)(obj
);
587 rtl_arena_segment_type
* head
;
590 memset (arena
, 0, sizeof(rtl_arena_type
));
592 QUEUE_START_NAMED(arena
, arena_
);
594 (void) RTL_MEMORY_LOCK_INIT(&(arena
->m_lock
));
596 head
= &(arena
->m_segment_reserve_span_head
);
597 rtl_arena_segment_constructor (head
);
598 head
->m_type
= RTL_ARENA_SEGMENT_TYPE_HEAD
;
600 head
= &(arena
->m_segment_reserve_head
);
601 rtl_arena_segment_constructor (head
);
602 head
->m_type
= RTL_ARENA_SEGMENT_TYPE_HEAD
;
604 head
= &(arena
->m_segment_head
);
605 rtl_arena_segment_constructor (head
);
606 head
->m_type
= RTL_ARENA_SEGMENT_TYPE_HEAD
;
608 for (i
= 0; i
< RTL_ARENA_FREELIST_SIZE
; i
++)
610 head
= &(arena
->m_freelist_head
[i
]);
611 rtl_arena_segment_constructor (head
);
613 head
->m_size
= (((sal_Size
)1) << i
);
614 head
->m_type
= RTL_ARENA_SEGMENT_TYPE_HEAD
;
617 arena
->m_hash_table
= arena
->m_hash_table_0
;
618 arena
->m_hash_size
= RTL_ARENA_HASH_SIZE
;
619 arena
->m_hash_shift
= highbit(arena
->m_hash_size
) - 1;
622 /** rtl_arena_destructor()
625 rtl_arena_destructor (void * obj
)
627 rtl_arena_type
* arena
= (rtl_arena_type
*)(obj
);
628 rtl_arena_segment_type
* head
;
631 assert(QUEUE_STARTED_NAMED(arena
, arena_
));
633 RTL_MEMORY_LOCK_DESTROY(&(arena
->m_lock
));
635 head
= &(arena
->m_segment_reserve_span_head
);
636 assert(head
->m_type
== RTL_ARENA_SEGMENT_TYPE_HEAD
);
637 rtl_arena_segment_destructor (head
);
639 head
= &(arena
->m_segment_reserve_head
);
640 assert(head
->m_type
== RTL_ARENA_SEGMENT_TYPE_HEAD
);
641 rtl_arena_segment_destructor (head
);
643 head
= &(arena
->m_segment_head
);
644 assert(head
->m_type
== RTL_ARENA_SEGMENT_TYPE_HEAD
);
645 rtl_arena_segment_destructor (head
);
647 for (i
= 0; i
< RTL_ARENA_FREELIST_SIZE
; i
++)
649 head
= &(arena
->m_freelist_head
[i
]);
651 assert(head
->m_size
== (((sal_Size
)1) << i
));
652 assert(head
->m_type
== RTL_ARENA_SEGMENT_TYPE_HEAD
);
654 rtl_arena_segment_destructor (head
);
657 assert(arena
->m_hash_table
== arena
->m_hash_table_0
);
658 assert(arena
->m_hash_size
== RTL_ARENA_HASH_SIZE
);
660 arena
->m_hash_shift
==
661 sal::static_int_cast
< unsigned >(highbit(arena
->m_hash_size
) - 1));
664 /* ================================================================= */
666 /** rtl_arena_activate()
670 rtl_arena_type
* arena
,
673 sal_Size quantum_cache_max
,
674 rtl_arena_type
* source_arena
,
675 void * (SAL_CALL
* source_alloc
)(rtl_arena_type
*, sal_Size
*),
676 void (SAL_CALL
* source_free
) (rtl_arena_type
*, void *, sal_Size
)
682 (void) snprintf (arena
->m_name
, sizeof(arena
->m_name
), "%s", name
);
684 if (!RTL_MEMORY_ISP2(quantum
))
686 /* roundup to next power of 2 */
687 quantum
= (((sal_Size
)1) << highbit(quantum
));
689 quantum_cache_max
= RTL_MEMORY_P2ROUNDUP(quantum_cache_max
, quantum
);
691 arena
->m_quantum
= quantum
;
692 arena
->m_quantum_shift
= highbit(arena
->m_quantum
) - 1;
693 arena
->m_qcache_max
= quantum_cache_max
;
695 arena
->m_source_arena
= source_arena
;
696 arena
->m_source_alloc
= source_alloc
;
697 arena
->m_source_free
= source_free
;
699 if (arena
->m_qcache_max
> 0)
701 char namebuf
[RTL_ARENA_NAME_LENGTH
+ 1];
702 int i
, n
= (arena
->m_qcache_max
>> arena
->m_quantum_shift
);
704 sal_Size size
= n
* sizeof(rtl_cache_type
*);
705 arena
->m_qcache_ptr
= (rtl_cache_type
**)rtl_arena_alloc (gp_arena_arena
, &size
);
706 if (!(arena
->m_qcache_ptr
))
711 for (i
= 1; i
<= n
; i
++)
713 size
= i
* arena
->m_quantum
;
714 (void) snprintf (namebuf
, sizeof(namebuf
), "%s_%" SAL_PRIuUINTPTR
, arena
->m_name
, size
);
715 arena
->m_qcache_ptr
[i
- 1] = rtl_cache_create(namebuf
, size
, 0, NULL
, NULL
, NULL
, NULL
, arena
, RTL_CACHE_FLAG_QUANTUMCACHE
);
719 /* insert into arena list */
720 RTL_MEMORY_LOCK_ACQUIRE(&(g_arena_list
.m_lock
));
721 QUEUE_INSERT_TAIL_NAMED(&(g_arena_list
.m_arena_head
), arena
, arena_
);
722 RTL_MEMORY_LOCK_RELEASE(&(g_arena_list
.m_lock
));
727 /** rtl_arena_deactivate()
730 rtl_arena_deactivate (
731 rtl_arena_type
* arena
734 rtl_arena_segment_type
* head
, * segment
;
736 /* remove from arena list */
737 RTL_MEMORY_LOCK_ACQUIRE(&(g_arena_list
.m_lock
));
738 QUEUE_REMOVE_NAMED(arena
, arena_
);
739 RTL_MEMORY_LOCK_RELEASE(&(g_arena_list
.m_lock
));
741 /* cleanup quantum cache(s) */
742 if ((arena
->m_qcache_max
> 0) && (arena
->m_qcache_ptr
!= 0))
744 int i
, n
= (arena
->m_qcache_max
>> arena
->m_quantum_shift
);
745 for (i
= 1; i
<= n
; i
++)
747 if (arena
->m_qcache_ptr
[i
- 1] != 0)
749 rtl_cache_destroy (arena
->m_qcache_ptr
[i
- 1]);
750 arena
->m_qcache_ptr
[i
- 1] = 0;
756 n
* sizeof(rtl_cache_type
*));
758 arena
->m_qcache_ptr
= 0;
761 /* check for leaked segments */
764 // "rtl_arena_deactivate(" << arena->m_name << "): allocs: "
765 // << arena->m_stats.m_alloc << ", frees: " << arena->m_stats.m_free
766 // << "; total: " << arena->m_stats.m_mem_total << ", used: "
767 // << arena->m_stats.m_mem_alloc);
768 if (arena
->m_stats
.m_alloc
> arena
->m_stats
.m_free
)
774 // "rtl_arena_deactivate(" << arena->m_name << "): cleaning up "
775 // << (arena->m_stats.m_alloc - arena->m_stats.m_free)
776 // << " leaked segment(s) [" << arena->m_stats.m_mem_alloc
779 /* cleanup still used segment(s) */
780 for (i
= 0, n
= arena
->m_hash_size
; i
< n
; i
++)
782 while ((segment
= arena
->m_hash_table
[i
]) != 0)
784 /* pop from hash table */
785 arena
->m_hash_table
[i
] = segment
->m_fnext
, segment
->m_fnext
= segment
->m_fprev
= segment
;
787 /* coalesce w/ adjacent free segment(s) */
788 rtl_arena_segment_coalesce (arena
, segment
);
790 /* insert onto freelist */
791 rtl_arena_freelist_insert (arena
, segment
);
796 /* cleanup hash table */
797 if (arena
->m_hash_table
!= arena
->m_hash_table_0
)
802 arena
->m_hash_size
* sizeof(rtl_arena_segment_type
*));
804 arena
->m_hash_table
= arena
->m_hash_table_0
;
805 arena
->m_hash_size
= RTL_ARENA_HASH_SIZE
;
806 arena
->m_hash_shift
= highbit(arena
->m_hash_size
) - 1;
809 /* cleanup segment list */
810 head
= &(arena
->m_segment_head
);
811 for (segment
= head
->m_snext
; segment
!= head
; segment
= head
->m_snext
)
813 if (segment
->m_type
== RTL_ARENA_SEGMENT_TYPE_FREE
)
815 /* remove from freelist */
816 rtl_arena_freelist_remove (arena
, segment
);
820 /* can have only free and span segments here */
821 assert(segment
->m_type
== RTL_ARENA_SEGMENT_TYPE_SPAN
);
824 /* remove from segment list */
825 QUEUE_REMOVE_NAMED(segment
, s
);
827 /* release segment descriptor */
828 rtl_arena_segment_put (arena
, &segment
);
831 /* cleanup segment reserve list */
832 head
= &(arena
->m_segment_reserve_head
);
833 for (segment
= head
->m_snext
; segment
!= head
; segment
= head
->m_snext
)
835 /* remove from segment list */
836 QUEUE_REMOVE_NAMED(segment
, s
);
839 /* cleanup segment reserve span(s) */
840 head
= &(arena
->m_segment_reserve_span_head
);
841 for (segment
= head
->m_snext
; segment
!= head
; segment
= head
->m_snext
)
843 /* can have only span segments here */
844 assert(segment
->m_type
== RTL_ARENA_SEGMENT_TYPE_SPAN
);
846 /* remove from segment list */
847 QUEUE_REMOVE_NAMED(segment
, s
);
849 /* return span to g_machdep_arena */
850 rtl_machdep_free (gp_machdep_arena
, (void*)(segment
->m_addr
), segment
->m_size
);
855 /* ================================================================= *
857 * arena implementation.
859 * ================================================================= */
861 /** rtl_arena_create()
864 SAL_CALL
rtl_arena_create (
867 sal_Size quantum_cache_max
,
868 rtl_arena_type
* source_arena
,
869 void * (SAL_CALL
* source_alloc
)(rtl_arena_type
*, sal_Size
*),
870 void (SAL_CALL
* source_free
) (rtl_arena_type
*, void *, sal_Size
),
871 SAL_UNUSED_PARAMETER
int
872 ) SAL_THROW_EXTERN_C()
874 rtl_arena_type
* result
= 0;
875 sal_Size size
= sizeof(rtl_arena_type
);
878 result
= (rtl_arena_type
*)rtl_arena_alloc (gp_arena_arena
, &size
);
881 rtl_arena_type
* arena
= result
;
882 rtl_arena_constructor (arena
);
886 assert(gp_default_arena
!= 0);
887 source_arena
= gp_default_arena
;
890 result
= rtl_arena_activate (
902 rtl_arena_deactivate (arena
);
903 rtl_arena_destructor (arena
);
904 rtl_arena_free (gp_arena_arena
, arena
, size
);
907 else if (gp_arena_arena
== 0)
909 ensureArenaSingleton();
919 /** rtl_arena_destroy()
922 SAL_CALL
rtl_arena_destroy (
923 rtl_arena_type
* arena
924 ) SAL_THROW_EXTERN_C()
928 rtl_arena_deactivate (arena
);
929 rtl_arena_destructor (arena
);
930 rtl_arena_free (gp_arena_arena
, arena
, sizeof(rtl_arena_type
));
934 /** rtl_arena_alloc()
937 SAL_CALL
rtl_arena_alloc (
938 rtl_arena_type
* arena
,
940 ) SAL_THROW_EXTERN_C()
944 if ((arena
!= 0) && (pSize
!= 0))
948 if (alloc_mode
== AMode_SYSTEM
)
949 return rtl_allocateMemory(*pSize
);
951 size
= RTL_MEMORY_ALIGN((*pSize
), arena
->m_quantum
);
952 if (size
> arena
->m_qcache_max
)
954 /* allocate from segment list */
955 rtl_arena_segment_type
*segment
= 0;
957 RTL_MEMORY_LOCK_ACQUIRE(&(arena
->m_lock
));
958 if (rtl_arena_segment_alloc (arena
, size
, &segment
) ||
959 rtl_arena_segment_create(arena
, size
, &segment
) )
964 /* mark segment used */
965 assert(segment
->m_type
== RTL_ARENA_SEGMENT_TYPE_FREE
);
966 segment
->m_type
= RTL_ARENA_SEGMENT_TYPE_USED
;
969 assert(segment
->m_size
>= size
);
970 oversize
= segment
->m_size
- size
;
971 if ((oversize
>= arena
->m_quantum
) && (oversize
>= arena
->m_qcache_max
))
973 rtl_arena_segment_type
* remainder
= 0;
974 rtl_arena_segment_get (arena
, &remainder
);
977 segment
->m_size
= size
;
979 remainder
->m_addr
= segment
->m_addr
+ segment
->m_size
;
980 remainder
->m_size
= oversize
;
981 remainder
->m_type
= RTL_ARENA_SEGMENT_TYPE_FREE
;
982 QUEUE_INSERT_HEAD_NAMED(segment
, remainder
, s
);
984 rtl_arena_freelist_insert (arena
, remainder
);
988 rtl_arena_hash_insert (arena
, segment
);
990 (*pSize
) = segment
->m_size
;
991 addr
= (void*)(segment
->m_addr
);
993 RTL_MEMORY_LOCK_RELEASE(&(arena
->m_lock
));
997 /* allocate from quantum cache(s) */
998 int index
= (size
>> arena
->m_quantum_shift
) - 1;
999 assert(arena
->m_qcache_ptr
[index
] != 0);
1001 addr
= rtl_cache_alloc (arena
->m_qcache_ptr
[index
]);
1009 /** rtl_arena_free()
1012 SAL_CALL
rtl_arena_free (
1013 rtl_arena_type
* arena
,
1016 ) SAL_THROW_EXTERN_C()
1020 if (alloc_mode
== AMode_SYSTEM
)
1022 rtl_freeMemory(addr
);
1026 size
= RTL_MEMORY_ALIGN(size
, arena
->m_quantum
);
1027 if (size
> arena
->m_qcache_max
)
1029 /* free to segment list */
1030 rtl_arena_segment_type
* segment
;
1032 RTL_MEMORY_LOCK_ACQUIRE(&(arena
->m_lock
));
1034 segment
= rtl_arena_hash_remove (arena
, (sal_uIntPtr
)(addr
), size
);
1037 rtl_arena_segment_type
*next
, *prev
;
1039 /* coalesce w/ adjacent free segment(s) */
1040 rtl_arena_segment_coalesce (arena
, segment
);
1042 /* determine (new) next and prev segment */
1043 next
= segment
->m_snext
, prev
= segment
->m_sprev
;
1045 /* entire span free when prev is a span, and next is either a span or a list head */
1046 if (((prev
->m_type
== RTL_ARENA_SEGMENT_TYPE_SPAN
)) &&
1047 ((next
->m_type
== RTL_ARENA_SEGMENT_TYPE_SPAN
) ||
1048 (next
->m_type
== RTL_ARENA_SEGMENT_TYPE_HEAD
)) )
1051 prev
->m_addr
== segment
->m_addr
1052 && prev
->m_size
== segment
->m_size
);
1054 if (arena
->m_source_free
)
1056 addr
= (void*)(prev
->m_addr
);
1057 size
= prev
->m_size
;
1059 /* remove from segment list */
1060 QUEUE_REMOVE_NAMED(segment
, s
);
1062 /* release segment descriptor */
1063 rtl_arena_segment_put (arena
, &segment
);
1065 /* remove from segment list */
1066 QUEUE_REMOVE_NAMED(prev
, s
);
1068 /* release (span) segment descriptor */
1069 rtl_arena_segment_put (arena
, &prev
);
1071 /* update stats, return span to source arena */
1072 arena
->m_stats
.m_mem_total
-= size
;
1073 RTL_MEMORY_LOCK_RELEASE(&(arena
->m_lock
));
1075 (arena
->m_source_free
)(arena
->m_source_arena
, addr
, size
);
1080 /* insert onto freelist */
1081 rtl_arena_freelist_insert (arena
, segment
);
1084 RTL_MEMORY_LOCK_RELEASE(&(arena
->m_lock
));
1088 /* free to quantum cache(s) */
1089 int index
= (size
>> arena
->m_quantum_shift
) - 1;
1090 assert(arena
->m_qcache_ptr
[index
] != 0);
1092 rtl_cache_free (arena
->m_qcache_ptr
[index
], addr
);
1097 /* ================================================================= *
1099 * machdep internals.
1101 * ================================================================= */
1103 #if defined(SAL_UNX)
1104 #include <sys/mman.h>
1105 #elif defined(SAL_W32)
1106 #define MAP_FAILED 0
1107 #endif /* SAL_UNX || SAL_W32 */
1112 /** rtl_machdep_alloc()
1115 SAL_CALL
rtl_machdep_alloc (
1116 rtl_arena_type
* pArena
,
1121 sal_Size size
= (*pSize
);
1123 assert(pArena
== gp_machdep_arena
);
1125 #if defined(SOLARIS) && defined(SPARC)
1126 /* see @ mmap(2) man pages */
1127 size
+= (pArena
->m_quantum
+ pArena
->m_quantum
); /* "red-zone" pages */
1128 if (size
> (4 << 20))
1129 size
= RTL_MEMORY_P2ROUNDUP(size
, (4 << 20));
1130 else if (size
> (512 << 10))
1131 size
= RTL_MEMORY_P2ROUNDUP(size
, (512 << 10));
1133 size
= RTL_MEMORY_P2ROUNDUP(size
, (64 << 10));
1134 size
-= (pArena
->m_quantum
+ pArena
->m_quantum
); /* "red-zone" pages */
1136 /* default allocation granularity */
1137 if(pArena
->m_quantum
< (64 << 10))
1139 size
= RTL_MEMORY_P2ROUNDUP(size
, (64 << 10));
1143 size
= RTL_MEMORY_P2ROUNDUP(size
, pArena
->m_quantum
);
1147 #if defined(SAL_UNX)
1148 addr
= mmap (NULL
, (size_t)(size
), PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0);
1149 #elif defined(SAL_W32)
1150 addr
= VirtualAlloc (NULL
, (SIZE_T
)(size
), MEM_COMMIT
, PAGE_READWRITE
);
1151 #endif /* (SAL_UNX || SAL_W32) */
1153 if (addr
!= MAP_FAILED
)
1155 pArena
->m_stats
.m_alloc
+= 1;
1156 pArena
->m_stats
.m_mem_total
+= size
;
1157 pArena
->m_stats
.m_mem_alloc
+= size
;
1165 /** rtl_machdep_free()
1168 SAL_CALL
rtl_machdep_free (
1169 rtl_arena_type
* pArena
,
1174 assert(pArena
== gp_machdep_arena
);
1176 pArena
->m_stats
.m_free
+= 1;
1177 pArena
->m_stats
.m_mem_total
-= nSize
;
1178 pArena
->m_stats
.m_mem_alloc
-= nSize
;
1180 #if defined(SAL_UNX)
1181 (void) munmap(pAddr
, nSize
);
1182 #elif defined(SAL_W32)
1183 (void) VirtualFree ((LPVOID
)(pAddr
), (SIZE_T
)(0), MEM_RELEASE
);
1184 #endif /* (SAL_UNX || SAL_W32) */
1188 rtl_machdep_pagesize()
1190 #if defined(SAL_UNX)
1191 #if defined(FREEBSD) || defined(NETBSD) || defined(DRAGONFLY)
1192 return ((sal_Size
)getpagesize());
1194 return ((sal_Size
)sysconf(_SC_PAGESIZE
));
1195 #endif /* xBSD || POSIX */
1196 #elif defined(SAL_W32)
1198 GetSystemInfo (&info
);
1199 return ((sal_Size
)(info
.dwPageSize
));
1200 #endif /* (SAL_UNX || SAL_W32) */
1205 /* ================================================================= *
1207 * arena initialization.
1209 * ================================================================= */
1215 /* list of arenas */
1216 RTL_MEMORY_LOCK_INIT(&(g_arena_list
.m_lock
));
1217 rtl_arena_constructor (&(g_arena_list
.m_arena_head
));
1220 /* machdep (pseudo) arena */
1221 static rtl_arena_type g_machdep_arena
;
1223 assert(gp_machdep_arena
== 0);
1224 rtl_arena_constructor (&g_machdep_arena
);
1226 gp_machdep_arena
= rtl_arena_activate (
1228 "rtl_machdep_arena",
1229 rtl_machdep_pagesize(),
1230 0, /* no quantum caching */
1231 0, 0, 0 /* no source */
1233 assert(gp_machdep_arena
!= 0);
1237 static rtl_arena_type g_default_arena
;
1239 assert(gp_default_arena
== 0);
1240 rtl_arena_constructor (&g_default_arena
);
1242 gp_default_arena
= rtl_arena_activate (
1244 "rtl_default_arena",
1245 rtl_machdep_pagesize(),
1246 0, /* no quantum caching */
1247 gp_machdep_arena
, /* source */
1251 assert(gp_default_arena
!= 0);
1254 /* arena internal arena */
1255 static rtl_arena_type g_arena_arena
;
1257 assert(gp_arena_arena
== 0);
1258 rtl_arena_constructor (&g_arena_arena
);
1260 gp_arena_arena
= rtl_arena_activate (
1262 "rtl_arena_internal_arena",
1264 0, /* no quantum caching */
1265 gp_default_arena
, /* source */
1269 assert(gp_arena_arena
!= 0);
1271 // SAL_INFO("sal.rtl", "rtl_arena_init completed");
1274 /* ================================================================= */
1279 if (gp_arena_arena
!= 0)
1281 rtl_arena_type
* arena
, * head
;
1283 RTL_MEMORY_LOCK_ACQUIRE(&(g_arena_list
.m_lock
));
1284 head
= &(g_arena_list
.m_arena_head
);
1286 for (arena
= head
->m_arena_next
; arena
!= head
; arena
= arena
->m_arena_next
)
1290 // "rtl_arena_fini(" << arena->m_name << "): allocs: "
1291 // << arena->m_stats.m_alloc << ", frees: "
1292 // << arena->m_stats.m_free << "; total: "
1293 // << arena->m_stats.m_mem_total << ", used: "
1294 // << arena->m_stats.m_mem_alloc);
1296 RTL_MEMORY_LOCK_RELEASE(&(g_arena_list
.m_lock
));
1298 // SAL_INFO("sal.rtl", "rtl_arena_fini completed");
1301 /* ================================================================= */
1303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */