static void mmap_fill_memblocks(struct userdata *u, unsigned n) { pa_assert(u); pa_assert(u->out_mmap_memblocks); /* pa_log("Mmmap writing %u blocks", n); */ while (n > 0) { pa_memchunk chunk; if (u->out_mmap_memblocks[u->out_mmap_current]) pa_memblock_unref_fixed(u->out_mmap_memblocks[u->out_mmap_current]); chunk.memblock = u->out_mmap_memblocks[u->out_mmap_current] = pa_memblock_new_fixed( u->core->mempool, (uint8_t*) u->out_mmap + u->out_fragment_size * u->out_mmap_current, u->out_fragment_size, 1); chunk.length = pa_memblock_get_length(chunk.memblock); chunk.index = 0; pa_sink_render_into_full(u->sink, &chunk); u->out_mmap_current++; while (u->out_mmap_current >= u->out_nfrags) u->out_mmap_current -= u->out_nfrags; n--; } }
static void mmap_post_memblocks(struct userdata *u, unsigned n) { pa_assert(u); pa_assert(u->in_mmap_memblocks); /* pa_log("Mmmap reading %u blocks", n); */ while (n > 0) { pa_memchunk chunk; if (!u->in_mmap_memblocks[u->in_mmap_current]) { chunk.memblock = u->in_mmap_memblocks[u->in_mmap_current] = pa_memblock_new_fixed( u->core->mempool, (uint8_t*) u->in_mmap + u->in_fragment_size*u->in_mmap_current, u->in_fragment_size, 1); chunk.length = pa_memblock_get_length(chunk.memblock); chunk.index = 0; pa_source_post(u->source, &chunk); } u->in_mmap_current++; while (u->in_mmap_current >= u->in_nfrags) u->in_mmap_current -= u->in_nfrags; n--; } }
int main(int argc, char *argv[]) { pa_mempool *pool_a, *pool_b, *pool_c; unsigned id_a, id_b, id_c; pa_memexport *export_a, *export_b; pa_memimport *import_b, *import_c; pa_memblock *mb_a, *mb_b, *mb_c; int r, i; pa_memblock* blocks[5]; uint32_t id, shm_id; size_t offset, size; char *x; const char txt[] = "This is a test!"; pool_a = pa_mempool_new(1); pool_b = pa_mempool_new(1); pool_c = pa_mempool_new(1); pa_mempool_get_shm_id(pool_a, &id_a); pa_mempool_get_shm_id(pool_b, &id_b); pa_mempool_get_shm_id(pool_c, &id_c); pa_assert(pool_a && pool_b && pool_c); blocks[0] = pa_memblock_new_fixed(pool_a, (void*) txt, sizeof(txt), 1); blocks[1] = pa_memblock_new(pool_a, sizeof(txt)); x = pa_memblock_acquire(blocks[1]); snprintf(x, pa_memblock_get_length(blocks[1]), "%s", txt); pa_memblock_release(blocks[1]); blocks[2] = pa_memblock_new_pool(pool_a, sizeof(txt)); x = pa_memblock_acquire(blocks[2]); snprintf(x, pa_memblock_get_length(blocks[2]), "%s", txt); pa_memblock_release(blocks[2]); blocks[3] = pa_memblock_new_malloced(pool_a, pa_xstrdup(txt), sizeof(txt)); blocks[4] = NULL; for (i = 0; blocks[i]; i++) { printf("Memory block %u\n", i); mb_a = blocks[i]; pa_assert(mb_a); export_a = pa_memexport_new(pool_a, revoke_cb, (void*) "A"); export_b = pa_memexport_new(pool_b, revoke_cb, (void*) "B"); pa_assert(export_a && export_b); import_b = pa_memimport_new(pool_b, release_cb, (void*) "B"); import_c = pa_memimport_new(pool_c, release_cb, (void*) "C"); pa_assert(import_b && import_c); r = pa_memexport_put(export_a, mb_a, &id, &shm_id, &offset, &size); pa_assert(r >= 0); pa_assert(shm_id == id_a); printf("A: Memory block exported as %u\n", id); mb_b = pa_memimport_get(import_b, id, shm_id, offset, size); pa_assert(mb_b); r = pa_memexport_put(export_b, mb_b, &id, &shm_id, &offset, &size); pa_assert(r >= 0); pa_assert(shm_id == id_a || shm_id == id_b); pa_memblock_unref(mb_b); printf("B: Memory block exported as %u\n", id); mb_c = pa_memimport_get(import_c, id, shm_id, offset, size); pa_assert(mb_c); x = pa_memblock_acquire(mb_c); printf("1 data=%s\n", x); pa_memblock_release(mb_c); print_stats(pool_a, "A"); print_stats(pool_b, "B"); print_stats(pool_c, "C"); pa_memexport_free(export_b); x = pa_memblock_acquire(mb_c); printf("2 data=%s\n", x); pa_memblock_release(mb_c); pa_memblock_unref(mb_c); pa_memimport_free(import_b); pa_memblock_unref(mb_a); pa_memimport_free(import_c); pa_memexport_free(export_a); } printf("vaccuuming...\n"); pa_mempool_vacuum(pool_a); pa_mempool_vacuum(pool_b); pa_mempool_vacuum(pool_c); printf("vaccuuming done...\n"); pa_mempool_free(pool_a); pa_mempool_free(pool_b); pa_mempool_free(pool_c); return 0; }
int main(int argc, char *argv[]) { int ret; pa_mempool *p; pa_memblockq *bq; pa_memchunk chunk1, chunk2, chunk3, chunk4; pa_memchunk silence; pa_sample_spec ss = { .format = PA_SAMPLE_S16LE, .rate = 48000, .channels = 1 }; pa_log_set_level(PA_LOG_DEBUG); p = pa_mempool_new(FALSE, 0); pa_assert_se(silence.memblock = pa_memblock_new_fixed(p, (char*) "__", 2, 1)); silence.index = 0; silence.length = pa_memblock_get_length(silence.memblock); pa_assert_se(bq = pa_memblockq_new("test memblockq", 0, 200, 10, &ss, 4, 4, 40, &silence)); pa_assert_se(chunk1.memblock = pa_memblock_new_fixed(p, (char*) "11", 2, 1)); chunk1.index = 0; chunk1.length = 2; pa_assert_se(chunk2.memblock = pa_memblock_new_fixed(p, (char*) "XX22", 4, 1)); chunk2.index = 2; chunk2.length = 2; pa_assert_se(chunk3.memblock = pa_memblock_new_fixed(p, (char*) "3333", 4, 1)); chunk3.index = 0; chunk3.length = 4; pa_assert_se(chunk4.memblock = pa_memblock_new_fixed(p, (char*) "44444444", 8, 1)); chunk4.index = 0; chunk4.length = 8; ret = pa_memblockq_push(bq, &chunk1); assert(ret == 0); ret = pa_memblockq_push(bq, &chunk2); assert(ret == 0); ret = pa_memblockq_push(bq, &chunk3); assert(ret == 0); ret = pa_memblockq_push(bq, &chunk4); assert(ret == 0); pa_memblockq_seek(bq, -6, 0, TRUE); ret = pa_memblockq_push(bq, &chunk3); assert(ret == 0); pa_memblockq_seek(bq, -2, 0, TRUE); ret = pa_memblockq_push(bq, &chunk1); assert(ret == 0); pa_memblockq_seek(bq, -10, 0, TRUE); ret = pa_memblockq_push(bq, &chunk4); assert(ret == 0); pa_memblockq_seek(bq, 10, 0, TRUE); ret = pa_memblockq_push(bq, &chunk1); assert(ret == 0); pa_memblockq_seek(bq, -6, 0, TRUE); ret = pa_memblockq_push(bq, &chunk2); assert(ret == 0); /* Test splitting */ pa_memblockq_seek(bq, -12, 0, TRUE); ret = pa_memblockq_push(bq, &chunk1); assert(ret == 0); pa_memblockq_seek(bq, 20, 0, TRUE); /* Test merging */ ret = pa_memblockq_push(bq, &chunk3); assert(ret == 0); pa_memblockq_seek(bq, -2, 0, TRUE); chunk3.index += 2; chunk3.length -= 2; ret = pa_memblockq_push(bq, &chunk3); assert(ret == 0); pa_memblockq_seek(bq, 30, PA_SEEK_RELATIVE, TRUE); dump(bq); pa_memblockq_rewind(bq, 52); dump(bq); pa_memblockq_free(bq); pa_memblock_unref(silence.memblock); pa_memblock_unref(chunk1.memblock); pa_memblock_unref(chunk2.memblock); pa_memblock_unref(chunk3.memblock); pa_memblock_unref(chunk4.memblock); pa_mempool_free(p); return 0; }