2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #ifdef HAVE_VALGRIND_MEMCHECK_H
35 #include <valgrind/memcheck.h>
38 #include <pulse/xmalloc.h>
39 #include <pulse/def.h>
41 #include <pulsecore/shm.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/hashmap.h>
44 #include <pulsecore/semaphore.h>
45 #include <pulsecore/mutex.h>
46 #include <pulsecore/macro.h>
47 #include <pulsecore/refcnt.h>
48 #include <pulsecore/llist.h>
49 #include <pulsecore/flist.h>
50 #include <pulsecore/core-util.h>
51 #include <pulsecore/memtrap.h>
55 /* We can allocate 64*1024*1024 bytes at maximum. That's 64MB. Please
56 * note that the footprint is usually much smaller, since the data is
57 * stored in SHM and our OS does not commit the memory before we use
58 * it for the first time. */
59 #define PA_MEMPOOL_SLOTS_MAX 1024
60 #define PA_MEMPOOL_SLOT_SIZE (64*1024)
62 #define PA_MEMEXPORT_SLOTS_MAX 128
64 #define PA_MEMIMPORT_SLOTS_MAX 160
65 #define PA_MEMIMPORT_SEGMENTS_MAX 16
68 PA_REFCNT_DECLARE
; /* the reference counter */
71 pa_memblock_type_t type
;
73 pa_bool_t read_only
:1;
74 pa_bool_t is_silence
:1;
79 pa_atomic_t n_acquired
;
80 pa_atomic_t please_signal
;
84 /* If type == PA_MEMBLOCK_USER this points to a function for freeing this memory block */
90 pa_memimport_segment
*segment
;
95 struct pa_memimport_segment
{
102 /* A collection of multiple segments */
103 struct pa_memimport
{
107 pa_hashmap
*segments
;
110 /* Called whenever an imported memory block is no longer
112 pa_memimport_release_cb_t release_cb
;
115 PA_LLIST_FIELDS(pa_memimport
);
118 struct memexport_slot
{
119 PA_LLIST_FIELDS(struct memexport_slot
);
123 struct pa_memexport
{
127 struct memexport_slot slots
[PA_MEMEXPORT_SLOTS_MAX
];
129 PA_LLIST_HEAD(struct memexport_slot
, free_slots
);
130 PA_LLIST_HEAD(struct memexport_slot
, used_slots
);
133 /* Called whenever a client from which we imported a memory block
134 which we in turn exported to another client dies and we need to
135 revoke the memory block accordingly */
136 pa_memexport_revoke_cb_t revoke_cb
;
139 PA_LLIST_FIELDS(pa_memexport
);
143 pa_semaphore
*semaphore
;
152 PA_LLIST_HEAD(pa_memimport
, imports
);
153 PA_LLIST_HEAD(pa_memexport
, exports
);
155 /* A list of free slots that may be reused */
156 pa_flist
*free_slots
;
158 pa_mempool_stat stat
;
161 static void segment_detach(pa_memimport_segment
*seg
);
163 PA_STATIC_FLIST_DECLARE(unused_memblocks
, 0, pa_xfree
);
165 /* No lock necessary */
166 static void stat_add(pa_memblock
*b
) {
170 pa_atomic_inc(&b
->pool
->stat
.n_allocated
);
171 pa_atomic_add(&b
->pool
->stat
.allocated_size
, (int) b
->length
);
173 pa_atomic_inc(&b
->pool
->stat
.n_accumulated
);
174 pa_atomic_add(&b
->pool
->stat
.accumulated_size
, (int) b
->length
);
176 if (b
->type
== PA_MEMBLOCK_IMPORTED
) {
177 pa_atomic_inc(&b
->pool
->stat
.n_imported
);
178 pa_atomic_add(&b
->pool
->stat
.imported_size
, (int) b
->length
);
181 pa_atomic_inc(&b
->pool
->stat
.n_allocated_by_type
[b
->type
]);
182 pa_atomic_inc(&b
->pool
->stat
.n_accumulated_by_type
[b
->type
]);
185 /* No lock necessary */
186 static void stat_remove(pa_memblock
*b
) {
190 pa_assert(pa_atomic_load(&b
->pool
->stat
.n_allocated
) > 0);
191 pa_assert(pa_atomic_load(&b
->pool
->stat
.allocated_size
) >= (int) b
->length
);
193 pa_atomic_dec(&b
->pool
->stat
.n_allocated
);
194 pa_atomic_sub(&b
->pool
->stat
.allocated_size
, (int) b
->length
);
196 if (b
->type
== PA_MEMBLOCK_IMPORTED
) {
197 pa_assert(pa_atomic_load(&b
->pool
->stat
.n_imported
) > 0);
198 pa_assert(pa_atomic_load(&b
->pool
->stat
.imported_size
) >= (int) b
->length
);
200 pa_atomic_dec(&b
->pool
->stat
.n_imported
);
201 pa_atomic_sub(&b
->pool
->stat
.imported_size
, (int) b
->length
);
204 pa_atomic_dec(&b
->pool
->stat
.n_allocated_by_type
[b
->type
]);
207 static pa_memblock
*memblock_new_appended(pa_mempool
*p
, size_t length
);
209 /* No lock necessary */
210 pa_memblock
*pa_memblock_new(pa_mempool
*p
, size_t length
) {
216 if (!(b
= pa_memblock_new_pool(p
, length
)))
217 b
= memblock_new_appended(p
, length
);
222 /* No lock necessary */
223 static pa_memblock
*memblock_new_appended(pa_mempool
*p
, size_t length
) {
229 /* If -1 is passed as length we choose the size for the caller. */
231 if (length
== (size_t) -1)
232 length
= p
->block_size
- PA_ALIGN(sizeof(pa_memblock
));
234 b
= pa_xmalloc(PA_ALIGN(sizeof(pa_memblock
)) + length
);
237 b
->type
= PA_MEMBLOCK_APPENDED
;
238 b
->read_only
= b
->is_silence
= FALSE
;
239 pa_atomic_ptr_store(&b
->data
, (uint8_t*) b
+ PA_ALIGN(sizeof(pa_memblock
)));
241 pa_atomic_store(&b
->n_acquired
, 0);
242 pa_atomic_store(&b
->please_signal
, 0);
248 /* No lock necessary */
249 static struct mempool_slot
* mempool_allocate_slot(pa_mempool
*p
) {
250 struct mempool_slot
*slot
;
253 if (!(slot
= pa_flist_pop(p
->free_slots
))) {
256 /* The free list was empty, we have to allocate a new entry */
258 if ((unsigned) (idx
= pa_atomic_inc(&p
->n_init
)) >= p
->n_blocks
)
259 pa_atomic_dec(&p
->n_init
);
261 slot
= (struct mempool_slot
*) ((uint8_t*) p
->memory
.ptr
+ (p
->block_size
* (size_t) idx
));
264 if (pa_log_ratelimit(PA_LOG_DEBUG
))
265 pa_log_debug("Pool full");
266 pa_atomic_inc(&p
->stat
.n_pool_full
);
271 /* #ifdef HAVE_VALGRIND_MEMCHECK_H */
272 /* if (PA_UNLIKELY(pa_in_valgrind())) { */
273 /* VALGRIND_MALLOCLIKE_BLOCK(slot, p->block_size, 0, 0); */
280 /* No lock necessary, totally redundant anyway */
281 static inline void* mempool_slot_data(struct mempool_slot
*slot
) {
285 /* No lock necessary */
286 static unsigned mempool_slot_idx(pa_mempool
*p
, void *ptr
) {
289 pa_assert((uint8_t*) ptr
>= (uint8_t*) p
->memory
.ptr
);
290 pa_assert((uint8_t*) ptr
< (uint8_t*) p
->memory
.ptr
+ p
->memory
.size
);
292 return (unsigned) ((size_t) ((uint8_t*) ptr
- (uint8_t*) p
->memory
.ptr
) / p
->block_size
);
295 /* No lock necessary */
296 static struct mempool_slot
* mempool_slot_by_ptr(pa_mempool
*p
, void *ptr
) {
299 if ((idx
= mempool_slot_idx(p
, ptr
)) == (unsigned) -1)
302 return (struct mempool_slot
*) ((uint8_t*) p
->memory
.ptr
+ (idx
* p
->block_size
));
305 /* No lock necessary */
306 pa_memblock
*pa_memblock_new_pool(pa_mempool
*p
, size_t length
) {
307 pa_memblock
*b
= NULL
;
308 struct mempool_slot
*slot
;
309 static int mempool_disable
= 0;
314 if (mempool_disable
== 0)
315 mempool_disable
= getenv("PULSE_MEMPOOL_DISABLE") ? 1 : -1;
317 if (mempool_disable
> 0)
320 /* If -1 is passed as length we choose the size for the caller: we
321 * take the largest size that fits in one of our slots. */
323 if (length
== (size_t) -1)
324 length
= pa_mempool_block_size_max(p
);
326 if (p
->block_size
>= PA_ALIGN(sizeof(pa_memblock
)) + length
) {
328 if (!(slot
= mempool_allocate_slot(p
)))
331 b
= mempool_slot_data(slot
);
332 b
->type
= PA_MEMBLOCK_POOL
;
333 pa_atomic_ptr_store(&b
->data
, (uint8_t*) b
+ PA_ALIGN(sizeof(pa_memblock
)));
335 } else if (p
->block_size
>= length
) {
337 if (!(slot
= mempool_allocate_slot(p
)))
340 if (!(b
= pa_flist_pop(PA_STATIC_FLIST_GET(unused_memblocks
))))
341 b
= pa_xnew(pa_memblock
, 1);
343 b
->type
= PA_MEMBLOCK_POOL_EXTERNAL
;
344 pa_atomic_ptr_store(&b
->data
, mempool_slot_data(slot
));
347 pa_log_debug("Memory block too large for pool: %lu > %lu", (unsigned long) length
, (unsigned long) p
->block_size
);
348 pa_atomic_inc(&p
->stat
.n_too_large_for_pool
);
354 b
->read_only
= b
->is_silence
= FALSE
;
356 pa_atomic_store(&b
->n_acquired
, 0);
357 pa_atomic_store(&b
->please_signal
, 0);
363 /* No lock necessary */
364 pa_memblock
*pa_memblock_new_fixed(pa_mempool
*p
, void *d
, size_t length
, pa_bool_t read_only
) {
369 pa_assert(length
!= (size_t) -1);
372 if (!(b
= pa_flist_pop(PA_STATIC_FLIST_GET(unused_memblocks
))))
373 b
= pa_xnew(pa_memblock
, 1);
377 b
->type
= PA_MEMBLOCK_FIXED
;
378 b
->read_only
= read_only
;
379 b
->is_silence
= FALSE
;
380 pa_atomic_ptr_store(&b
->data
, d
);
382 pa_atomic_store(&b
->n_acquired
, 0);
383 pa_atomic_store(&b
->please_signal
, 0);
389 /* No lock necessary */
390 pa_memblock
*pa_memblock_new_user(pa_mempool
*p
, void *d
, size_t length
, pa_free_cb_t free_cb
, pa_bool_t read_only
) {
396 pa_assert(length
!= (size_t) -1);
399 if (!(b
= pa_flist_pop(PA_STATIC_FLIST_GET(unused_memblocks
))))
400 b
= pa_xnew(pa_memblock
, 1);
404 b
->type
= PA_MEMBLOCK_USER
;
405 b
->read_only
= read_only
;
406 b
->is_silence
= FALSE
;
407 pa_atomic_ptr_store(&b
->data
, d
);
409 pa_atomic_store(&b
->n_acquired
, 0);
410 pa_atomic_store(&b
->please_signal
, 0);
412 b
->per_type
.user
.free_cb
= free_cb
;
418 /* No lock necessary */
419 pa_bool_t
pa_memblock_is_read_only(pa_memblock
*b
) {
421 pa_assert(PA_REFCNT_VALUE(b
) > 0);
423 return b
->read_only
&& PA_REFCNT_VALUE(b
) == 1;
426 /* No lock necessary */
427 pa_bool_t
pa_memblock_is_silence(pa_memblock
*b
) {
429 pa_assert(PA_REFCNT_VALUE(b
) > 0);
431 return b
->is_silence
;
434 /* No lock necessary */
435 void pa_memblock_set_is_silence(pa_memblock
*b
, pa_bool_t v
) {
437 pa_assert(PA_REFCNT_VALUE(b
) > 0);
442 /* No lock necessary */
443 pa_bool_t
pa_memblock_ref_is_one(pa_memblock
*b
) {
447 pa_assert_se((r
= PA_REFCNT_VALUE(b
)) > 0);
452 /* No lock necessary */
453 void* pa_memblock_acquire(pa_memblock
*b
) {
455 pa_assert(PA_REFCNT_VALUE(b
) > 0);
457 pa_atomic_inc(&b
->n_acquired
);
459 return pa_atomic_ptr_load(&b
->data
);
462 /* No lock necessary, in corner cases locks by its own */
463 void pa_memblock_release(pa_memblock
*b
) {
466 pa_assert(PA_REFCNT_VALUE(b
) > 0);
468 r
= pa_atomic_dec(&b
->n_acquired
);
471 /* Signal a waiting thread that this memblock is no longer used */
472 if (r
== 1 && pa_atomic_load(&b
->please_signal
))
473 pa_semaphore_post(b
->pool
->semaphore
);
476 size_t pa_memblock_get_length(pa_memblock
*b
) {
478 pa_assert(PA_REFCNT_VALUE(b
) > 0);
483 pa_mempool
* pa_memblock_get_pool(pa_memblock
*b
) {
485 pa_assert(PA_REFCNT_VALUE(b
) > 0);
490 /* No lock necessary */
491 pa_memblock
* pa_memblock_ref(pa_memblock
*b
) {
493 pa_assert(PA_REFCNT_VALUE(b
) > 0);
499 static void memblock_free(pa_memblock
*b
) {
502 pa_assert(pa_atomic_load(&b
->n_acquired
) == 0);
507 case PA_MEMBLOCK_USER
:
508 pa_assert(b
->per_type
.user
.free_cb
);
509 b
->per_type
.user
.free_cb(pa_atomic_ptr_load(&b
->data
));
513 case PA_MEMBLOCK_FIXED
:
514 if (pa_flist_push(PA_STATIC_FLIST_GET(unused_memblocks
), b
) < 0)
519 case PA_MEMBLOCK_APPENDED
:
521 /* We could attached it unused_memblocks, but that would
522 * probably waste some considerable memory */
526 case PA_MEMBLOCK_IMPORTED
: {
527 pa_memimport_segment
*segment
;
528 pa_memimport
*import
;
530 /* FIXME! This should be implemented lock-free */
532 pa_assert_se(segment
= b
->per_type
.imported
.segment
);
533 pa_assert_se(import
= segment
->import
);
535 pa_mutex_lock(import
->mutex
);
537 pa_assert_se(pa_hashmap_remove(import
->blocks
, PA_UINT32_TO_PTR(b
->per_type
.imported
.id
)));
539 pa_assert(segment
->n_blocks
>= 1);
540 if (-- segment
->n_blocks
<= 0)
541 segment_detach(segment
);
543 pa_mutex_unlock(import
->mutex
);
545 import
->release_cb(import
, b
->per_type
.imported
.id
, import
->userdata
);
547 if (pa_flist_push(PA_STATIC_FLIST_GET(unused_memblocks
), b
) < 0)
553 case PA_MEMBLOCK_POOL_EXTERNAL
:
554 case PA_MEMBLOCK_POOL
: {
555 struct mempool_slot
*slot
;
558 pa_assert_se(slot
= mempool_slot_by_ptr(b
->pool
, pa_atomic_ptr_load(&b
->data
)));
560 call_free
= b
->type
== PA_MEMBLOCK_POOL_EXTERNAL
;
562 /* #ifdef HAVE_VALGRIND_MEMCHECK_H */
563 /* if (PA_UNLIKELY(pa_in_valgrind())) { */
564 /* VALGRIND_FREELIKE_BLOCK(slot, b->pool->block_size); */
568 /* The free list dimensions should easily allow all slots
569 * to fit in, hence try harder if pushing this slot into
570 * the free list fails */
571 while (pa_flist_push(b
->pool
->free_slots
, slot
) < 0)
575 if (pa_flist_push(PA_STATIC_FLIST_GET(unused_memblocks
), b
) < 0)
581 case PA_MEMBLOCK_TYPE_MAX
:
583 pa_assert_not_reached();
587 /* No lock necessary */
588 void pa_memblock_unref(pa_memblock
*b
) {
590 pa_assert(PA_REFCNT_VALUE(b
) > 0);
592 if (PA_REFCNT_DEC(b
) > 0)
599 static void memblock_wait(pa_memblock
*b
) {
602 if (pa_atomic_load(&b
->n_acquired
) > 0) {
603 /* We need to wait until all threads gave up access to the
604 * memory block before we can go on. Unfortunately this means
605 * that we have to lock and wait here. Sniff! */
607 pa_atomic_inc(&b
->please_signal
);
609 while (pa_atomic_load(&b
->n_acquired
) > 0)
610 pa_semaphore_wait(b
->pool
->semaphore
);
612 pa_atomic_dec(&b
->please_signal
);
616 /* No lock necessary. This function is not multiple caller safe! */
617 static void memblock_make_local(pa_memblock
*b
) {
620 pa_atomic_dec(&b
->pool
->stat
.n_allocated_by_type
[b
->type
]);
622 if (b
->length
<= b
->pool
->block_size
) {
623 struct mempool_slot
*slot
;
625 if ((slot
= mempool_allocate_slot(b
->pool
))) {
627 /* We can move it into a local pool, perfect! */
629 new_data
= mempool_slot_data(slot
);
630 memcpy(new_data
, pa_atomic_ptr_load(&b
->data
), b
->length
);
631 pa_atomic_ptr_store(&b
->data
, new_data
);
633 b
->type
= PA_MEMBLOCK_POOL_EXTERNAL
;
634 b
->read_only
= FALSE
;
640 /* Humm, not enough space in the pool, so lets allocate the memory with malloc() */
641 b
->per_type
.user
.free_cb
= pa_xfree
;
642 pa_atomic_ptr_store(&b
->data
, pa_xmemdup(pa_atomic_ptr_load(&b
->data
), b
->length
));
644 b
->type
= PA_MEMBLOCK_USER
;
645 b
->read_only
= FALSE
;
648 pa_atomic_inc(&b
->pool
->stat
.n_allocated_by_type
[b
->type
]);
649 pa_atomic_inc(&b
->pool
->stat
.n_accumulated_by_type
[b
->type
]);
653 /* No lock necessary. This function is not multiple caller safe*/
654 void pa_memblock_unref_fixed(pa_memblock
*b
) {
656 pa_assert(PA_REFCNT_VALUE(b
) > 0);
657 pa_assert(b
->type
== PA_MEMBLOCK_FIXED
);
659 if (PA_REFCNT_VALUE(b
) > 1)
660 memblock_make_local(b
);
662 pa_memblock_unref(b
);
665 /* No lock necessary. */
666 pa_memblock
*pa_memblock_will_need(pa_memblock
*b
) {
670 pa_assert(PA_REFCNT_VALUE(b
) > 0);
672 p
= pa_memblock_acquire(b
);
673 pa_will_need(p
, b
->length
);
674 pa_memblock_release(b
);
679 /* Self-locked. This function is not multiple-caller safe */
680 static void memblock_replace_import(pa_memblock
*b
) {
681 pa_memimport_segment
*segment
;
682 pa_memimport
*import
;
685 pa_assert(b
->type
== PA_MEMBLOCK_IMPORTED
);
687 pa_assert(pa_atomic_load(&b
->pool
->stat
.n_imported
) > 0);
688 pa_assert(pa_atomic_load(&b
->pool
->stat
.imported_size
) >= (int) b
->length
);
689 pa_atomic_dec(&b
->pool
->stat
.n_imported
);
690 pa_atomic_sub(&b
->pool
->stat
.imported_size
, (int) b
->length
);
692 pa_assert_se(segment
= b
->per_type
.imported
.segment
);
693 pa_assert_se(import
= segment
->import
);
695 pa_mutex_lock(import
->mutex
);
697 pa_assert_se(pa_hashmap_remove(import
->blocks
, PA_UINT32_TO_PTR(b
->per_type
.imported
.id
)));
699 memblock_make_local(b
);
701 pa_assert(segment
->n_blocks
>= 1);
702 if (-- segment
->n_blocks
<= 0)
703 segment_detach(segment
);
705 pa_mutex_unlock(import
->mutex
);
708 pa_mempool
* pa_mempool_new(pa_bool_t shared
, size_t size
) {
710 char t1
[PA_BYTES_SNPRINT_MAX
], t2
[PA_BYTES_SNPRINT_MAX
];
712 p
= pa_xnew(pa_mempool
, 1);
714 p
->mutex
= pa_mutex_new(TRUE
, TRUE
);
715 p
->semaphore
= pa_semaphore_new(0);
717 p
->block_size
= PA_PAGE_ALIGN(PA_MEMPOOL_SLOT_SIZE
);
718 if (p
->block_size
< PA_PAGE_SIZE
)
719 p
->block_size
= PA_PAGE_SIZE
;
722 p
->n_blocks
= PA_MEMPOOL_SLOTS_MAX
;
724 p
->n_blocks
= (unsigned) (size
/ p
->block_size
);
730 if (pa_shm_create_rw(&p
->memory
, p
->n_blocks
* p
->block_size
, shared
, 0700) < 0) {
735 pa_log_debug("Using %s memory pool with %u slots of size %s each, total size is %s, maximum usable slot size is %lu",
736 p
->memory
.shared
? "shared" : "private",
738 pa_bytes_snprint(t1
, sizeof(t1
), (unsigned) p
->block_size
),
739 pa_bytes_snprint(t2
, sizeof(t2
), (unsigned) (p
->n_blocks
* p
->block_size
)),
740 (unsigned long) pa_mempool_block_size_max(p
));
742 memset(&p
->stat
, 0, sizeof(p
->stat
));
743 pa_atomic_store(&p
->n_init
, 0);
745 PA_LLIST_HEAD_INIT(pa_memimport
, p
->imports
);
746 PA_LLIST_HEAD_INIT(pa_memexport
, p
->exports
);
748 p
->free_slots
= pa_flist_new(p
->n_blocks
);
753 void pa_mempool_free(pa_mempool
*p
) {
756 pa_mutex_lock(p
->mutex
);
759 pa_memimport_free(p
->imports
);
762 pa_memexport_free(p
->exports
);
764 pa_mutex_unlock(p
->mutex
);
766 pa_flist_free(p
->free_slots
, NULL
);
768 if (pa_atomic_load(&p
->stat
.n_allocated
) > 0) {
770 /* Ouch, somebody is retaining a memory block reference! */
776 /* Let's try to find at least one of those leaked memory blocks */
778 list
= pa_flist_new(p
->n_blocks
);
780 for (i
= 0; i
< (unsigned) pa_atomic_load(&p
->n_init
); i
++) {
781 struct mempool_slot
*slot
;
784 slot
= (struct mempool_slot
*) ((uint8_t*) p
->memory
.ptr
+ (p
->block_size
* (size_t) i
));
785 b
= mempool_slot_data(slot
);
787 while ((k
= pa_flist_pop(p
->free_slots
))) {
788 while (pa_flist_push(list
, k
) < 0)
796 pa_log("REF: Leaked memory block %p", b
);
798 while ((k
= pa_flist_pop(list
)))
799 while (pa_flist_push(p
->free_slots
, k
) < 0)
803 pa_flist_free(list
, NULL
);
807 pa_log_error("Memory pool destroyed but not all memory blocks freed! %u remain.", pa_atomic_load(&p
->stat
.n_allocated
));
812 pa_shm_free(&p
->memory
);
814 pa_mutex_free(p
->mutex
);
815 pa_semaphore_free(p
->semaphore
);
820 /* No lock necessary */
821 const pa_mempool_stat
* pa_mempool_get_stat(pa_mempool
*p
) {
827 /* No lock necessary */
828 size_t pa_mempool_block_size_max(pa_mempool
*p
) {
831 return p
->block_size
- PA_ALIGN(sizeof(pa_memblock
));
834 /* No lock necessary */
835 void pa_mempool_vacuum(pa_mempool
*p
) {
836 struct mempool_slot
*slot
;
841 list
= pa_flist_new(p
->n_blocks
);
843 while ((slot
= pa_flist_pop(p
->free_slots
)))
844 while (pa_flist_push(list
, slot
) < 0)
847 while ((slot
= pa_flist_pop(list
))) {
848 pa_shm_punch(&p
->memory
, (size_t) ((uint8_t*) slot
- (uint8_t*) p
->memory
.ptr
), p
->block_size
);
850 while (pa_flist_push(p
->free_slots
, slot
))
854 pa_flist_free(list
, NULL
);
857 /* No lock necessary */
858 int pa_mempool_get_shm_id(pa_mempool
*p
, uint32_t *id
) {
861 if (!p
->memory
.shared
)
869 /* No lock necessary */
870 pa_bool_t
pa_mempool_is_shared(pa_mempool
*p
) {
873 return !!p
->memory
.shared
;
876 /* For recieving blocks from other nodes */
877 pa_memimport
* pa_memimport_new(pa_mempool
*p
, pa_memimport_release_cb_t cb
, void *userdata
) {
883 i
= pa_xnew(pa_memimport
, 1);
884 i
->mutex
= pa_mutex_new(TRUE
, TRUE
);
886 i
->segments
= pa_hashmap_new(NULL
, NULL
);
887 i
->blocks
= pa_hashmap_new(NULL
, NULL
);
889 i
->userdata
= userdata
;
891 pa_mutex_lock(p
->mutex
);
892 PA_LLIST_PREPEND(pa_memimport
, p
->imports
, i
);
893 pa_mutex_unlock(p
->mutex
);
898 static void memexport_revoke_blocks(pa_memexport
*e
, pa_memimport
*i
);
900 /* Should be called locked */
901 static pa_memimport_segment
* segment_attach(pa_memimport
*i
, uint32_t shm_id
) {
902 pa_memimport_segment
* seg
;
904 if (pa_hashmap_size(i
->segments
) >= PA_MEMIMPORT_SEGMENTS_MAX
)
907 seg
= pa_xnew0(pa_memimport_segment
, 1);
909 if (pa_shm_attach_ro(&seg
->memory
, shm_id
) < 0) {
915 seg
->trap
= pa_memtrap_add(seg
->memory
.ptr
, seg
->memory
.size
);
917 pa_hashmap_put(i
->segments
, PA_UINT32_TO_PTR(seg
->memory
.id
), seg
);
921 /* Should be called locked */
922 static void segment_detach(pa_memimport_segment
*seg
) {
925 pa_hashmap_remove(seg
->import
->segments
, PA_UINT32_TO_PTR(seg
->memory
.id
));
926 pa_shm_free(&seg
->memory
);
929 pa_memtrap_remove(seg
->trap
);
934 /* Self-locked. Not multiple-caller safe */
935 void pa_memimport_free(pa_memimport
*i
) {
941 pa_mutex_lock(i
->mutex
);
943 while ((b
= pa_hashmap_first(i
->blocks
)))
944 memblock_replace_import(b
);
946 pa_assert(pa_hashmap_size(i
->segments
) == 0);
948 pa_mutex_unlock(i
->mutex
);
950 pa_mutex_lock(i
->pool
->mutex
);
952 /* If we've exported this block further we need to revoke that export */
953 for (e
= i
->pool
->exports
; e
; e
= e
->next
)
954 memexport_revoke_blocks(e
, i
);
956 PA_LLIST_REMOVE(pa_memimport
, i
->pool
->imports
, i
);
958 pa_mutex_unlock(i
->pool
->mutex
);
960 pa_hashmap_free(i
->blocks
, NULL
, NULL
);
961 pa_hashmap_free(i
->segments
, NULL
, NULL
);
963 pa_mutex_free(i
->mutex
);
969 pa_memblock
* pa_memimport_get(pa_memimport
*i
, uint32_t block_id
, uint32_t shm_id
, size_t offset
, size_t size
) {
970 pa_memblock
*b
= NULL
;
971 pa_memimport_segment
*seg
;
975 pa_mutex_lock(i
->mutex
);
977 if ((b
= pa_hashmap_get(i
->blocks
, PA_UINT32_TO_PTR(block_id
)))) {
982 if (pa_hashmap_size(i
->blocks
) >= PA_MEMIMPORT_SLOTS_MAX
)
985 if (!(seg
= pa_hashmap_get(i
->segments
, PA_UINT32_TO_PTR(shm_id
))))
986 if (!(seg
= segment_attach(i
, shm_id
)))
989 if (offset
+size
> seg
->memory
.size
)
992 if (!(b
= pa_flist_pop(PA_STATIC_FLIST_GET(unused_memblocks
))))
993 b
= pa_xnew(pa_memblock
, 1);
997 b
->type
= PA_MEMBLOCK_IMPORTED
;
999 b
->is_silence
= FALSE
;
1000 pa_atomic_ptr_store(&b
->data
, (uint8_t*) seg
->memory
.ptr
+ offset
);
1002 pa_atomic_store(&b
->n_acquired
, 0);
1003 pa_atomic_store(&b
->please_signal
, 0);
1004 b
->per_type
.imported
.id
= block_id
;
1005 b
->per_type
.imported
.segment
= seg
;
1007 pa_hashmap_put(i
->blocks
, PA_UINT32_TO_PTR(block_id
), b
);
1014 pa_mutex_unlock(i
->mutex
);
1019 int pa_memimport_process_revoke(pa_memimport
*i
, uint32_t id
) {
1024 pa_mutex_lock(i
->mutex
);
1026 if (!(b
= pa_hashmap_get(i
->blocks
, PA_UINT32_TO_PTR(id
)))) {
1031 memblock_replace_import(b
);
1034 pa_mutex_unlock(i
->mutex
);
1039 /* For sending blocks to other nodes */
1040 pa_memexport
* pa_memexport_new(pa_mempool
*p
, pa_memexport_revoke_cb_t cb
, void *userdata
) {
1046 if (!p
->memory
.shared
)
1049 e
= pa_xnew(pa_memexport
, 1);
1050 e
->mutex
= pa_mutex_new(TRUE
, TRUE
);
1052 PA_LLIST_HEAD_INIT(struct memexport_slot
, e
->free_slots
);
1053 PA_LLIST_HEAD_INIT(struct memexport_slot
, e
->used_slots
);
1056 e
->userdata
= userdata
;
1058 pa_mutex_lock(p
->mutex
);
1059 PA_LLIST_PREPEND(pa_memexport
, p
->exports
, e
);
1060 pa_mutex_unlock(p
->mutex
);
1064 void pa_memexport_free(pa_memexport
*e
) {
1067 pa_mutex_lock(e
->mutex
);
1068 while (e
->used_slots
)
1069 pa_memexport_process_release(e
, (uint32_t) (e
->used_slots
- e
->slots
));
1070 pa_mutex_unlock(e
->mutex
);
1072 pa_mutex_lock(e
->pool
->mutex
);
1073 PA_LLIST_REMOVE(pa_memexport
, e
->pool
->exports
, e
);
1074 pa_mutex_unlock(e
->pool
->mutex
);
1076 pa_mutex_free(e
->mutex
);
1081 int pa_memexport_process_release(pa_memexport
*e
, uint32_t id
) {
1086 pa_mutex_lock(e
->mutex
);
1088 if (id
>= e
->n_init
)
1091 if (!e
->slots
[id
].block
)
1094 b
= e
->slots
[id
].block
;
1095 e
->slots
[id
].block
= NULL
;
1097 PA_LLIST_REMOVE(struct memexport_slot
, e
->used_slots
, &e
->slots
[id
]);
1098 PA_LLIST_PREPEND(struct memexport_slot
, e
->free_slots
, &e
->slots
[id
]);
1100 pa_mutex_unlock(e
->mutex
);
1102 /* pa_log("Processing release for %u", id); */
1104 pa_assert(pa_atomic_load(&e
->pool
->stat
.n_exported
) > 0);
1105 pa_assert(pa_atomic_load(&e
->pool
->stat
.exported_size
) >= (int) b
->length
);
1107 pa_atomic_dec(&e
->pool
->stat
.n_exported
);
1108 pa_atomic_sub(&e
->pool
->stat
.exported_size
, (int) b
->length
);
1110 pa_memblock_unref(b
);
1115 pa_mutex_unlock(e
->mutex
);
1121 static void memexport_revoke_blocks(pa_memexport
*e
, pa_memimport
*i
) {
1122 struct memexport_slot
*slot
, *next
;
1126 pa_mutex_lock(e
->mutex
);
1128 for (slot
= e
->used_slots
; slot
; slot
= next
) {
1132 if (slot
->block
->type
!= PA_MEMBLOCK_IMPORTED
||
1133 slot
->block
->per_type
.imported
.segment
->import
!= i
)
1136 idx
= (uint32_t) (slot
- e
->slots
);
1137 e
->revoke_cb(e
, idx
, e
->userdata
);
1138 pa_memexport_process_release(e
, idx
);
1141 pa_mutex_unlock(e
->mutex
);
1144 /* No lock necessary */
1145 static pa_memblock
*memblock_shared_copy(pa_mempool
*p
, pa_memblock
*b
) {
1151 if (b
->type
== PA_MEMBLOCK_IMPORTED
||
1152 b
->type
== PA_MEMBLOCK_POOL
||
1153 b
->type
== PA_MEMBLOCK_POOL_EXTERNAL
) {
1154 pa_assert(b
->pool
== p
);
1155 return pa_memblock_ref(b
);
1158 if (!(n
= pa_memblock_new_pool(p
, b
->length
)))
1161 memcpy(pa_atomic_ptr_load(&n
->data
), pa_atomic_ptr_load(&b
->data
), b
->length
);
1166 int pa_memexport_put(pa_memexport
*e
, pa_memblock
*b
, uint32_t *block_id
, uint32_t *shm_id
, size_t *offset
, size_t * size
) {
1168 struct memexport_slot
*slot
;
1173 pa_assert(block_id
);
1177 pa_assert(b
->pool
== e
->pool
);
1179 if (!(b
= memblock_shared_copy(e
->pool
, b
)))
1182 pa_mutex_lock(e
->mutex
);
1184 if (e
->free_slots
) {
1185 slot
= e
->free_slots
;
1186 PA_LLIST_REMOVE(struct memexport_slot
, e
->free_slots
, slot
);
1187 } else if (e
->n_init
< PA_MEMEXPORT_SLOTS_MAX
)
1188 slot
= &e
->slots
[e
->n_init
++];
1190 pa_mutex_unlock(e
->mutex
);
1191 pa_memblock_unref(b
);
1195 PA_LLIST_PREPEND(struct memexport_slot
, e
->used_slots
, slot
);
1197 *block_id
= (uint32_t) (slot
- e
->slots
);
1199 pa_mutex_unlock(e
->mutex
);
1200 /* pa_log("Got block id %u", *block_id); */
1202 data
= pa_memblock_acquire(b
);
1204 if (b
->type
== PA_MEMBLOCK_IMPORTED
) {
1205 pa_assert(b
->per_type
.imported
.segment
);
1206 memory
= &b
->per_type
.imported
.segment
->memory
;
1208 pa_assert(b
->type
== PA_MEMBLOCK_POOL
|| b
->type
== PA_MEMBLOCK_POOL_EXTERNAL
);
1210 memory
= &b
->pool
->memory
;
1213 pa_assert(data
>= memory
->ptr
);
1214 pa_assert((uint8_t*) data
+ b
->length
<= (uint8_t*) memory
->ptr
+ memory
->size
);
1216 *shm_id
= memory
->id
;
1217 *offset
= (size_t) ((uint8_t*) data
- (uint8_t*) memory
->ptr
);
1220 pa_memblock_release(b
);
1222 pa_atomic_inc(&e
->pool
->stat
.n_exported
);
1223 pa_atomic_add(&e
->pool
->stat
.exported_size
, (int) b
->length
);