[pulseaudio-discuss] [PATCH v3 04/11] pulsecore: Transform pa_mempool_new() into a factory method
Ahmed S. Darwish
darwish.07 at gmail.com
Sat Mar 12 22:57:06 UTC 2016
Soon we're going to have three types of memory pools: POSIX shm_open()
pools, memfd memfd_create() ones, and privately malloc()-ed pools.
Thus introduce annotations for the memory types supported and change
pa_mempool_new() into a factory method based on required memory.
Signed-off-by: Ahmed S. Darwish <darwish.07 at gmail.com>
---
src/Makefile.am | 1 +
src/pulse/context.c | 10 +++++---
src/pulsecore/core.c | 4 ++--
src/pulsecore/mem.h | 51 +++++++++++++++++++++++++++++++++++++++++
src/pulsecore/memblock.c | 24 +++++++++----------
src/pulsecore/memblock.h | 3 ++-
src/pulsecore/protocol-native.c | 3 ++-
src/pulsecore/shm.c | 11 +++++----
src/pulsecore/shm.h | 5 ++--
src/tests/cpu-mix-test.c | 2 +-
src/tests/lfe-filter-test.c | 2 +-
src/tests/mcalign-test.c | 2 +-
src/tests/memblock-test.c | 6 ++---
src/tests/memblockq-test.c | 2 +-
src/tests/mix-test.c | 2 +-
src/tests/remix-test.c | 2 +-
src/tests/resampler-test.c | 2 +-
src/tests/srbchannel-test.c | 2 +-
18 files changed, 97 insertions(+), 37 deletions(-)
create mode 100644 src/pulsecore/mem.h
diff --git a/src/Makefile.am b/src/Makefile.am
index aa96999..5b6084f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -700,6 +700,7 @@ libpulsecommon_ at PA_MAJORMINOR@_la_SOURCES = \
pulsecore/refcnt.h \
pulsecore/srbchannel.c pulsecore/srbchannel.h \
pulsecore/sample-util.c pulsecore/sample-util.h \
+ pulsecore/mem.h \
pulsecore/shm.c pulsecore/shm.h \
pulsecore/bitset.c pulsecore/bitset.h \
pulsecore/socket-client.c pulsecore/socket-client.h \
diff --git a/src/pulse/context.c b/src/pulse/context.c
index 927d020..e4716fb 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -125,6 +125,7 @@ static void reset_callbacks(pa_context *c) {
pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *name, pa_proplist *p) {
pa_context *c;
+ pa_mem_type_t type;
pa_assert(mainloop);
@@ -170,10 +171,13 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *
c->srb_template.readfd = -1;
c->srb_template.writefd = -1;
- if (!(c->mempool = pa_mempool_new(!c->conf->disable_shm, c->conf->shm_size))) {
+ type = !c->conf->disable_shm ? PA_MEM_TYPE_SHARED_POSIX : PA_MEM_TYPE_PRIVATE;
+ if (!(c->mempool = pa_mempool_new(type, c->conf->shm_size))) {
- if (!c->conf->disable_shm)
- c->mempool = pa_mempool_new(false, c->conf->shm_size);
+ if (!c->conf->disable_shm) {
+ pa_log_warn("Failed to allocate shared memory pool. Falling back to a normal private one.");
+ c->mempool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, c->conf->shm_size);
+ }
if (!c->mempool) {
context_free(c);
diff --git a/src/pulsecore/core.c b/src/pulsecore/core.c
index fe67109..199a26b 100644
--- a/src/pulsecore/core.c
+++ b/src/pulsecore/core.c
@@ -69,14 +69,14 @@ pa_core* pa_core_new(pa_mainloop_api *m, bool shared, size_t shm_size) {
pa_assert(m);
if (shared) {
- if (!(pool = pa_mempool_new(shared, shm_size))) {
+ if (!(pool = pa_mempool_new(PA_MEM_TYPE_SHARED_POSIX, shm_size))) {
pa_log_warn("Failed to allocate shared memory pool. Falling back to a normal memory pool.");
shared = false;
}
}
if (!shared) {
- if (!(pool = pa_mempool_new(shared, shm_size))) {
+ if (!(pool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, shm_size))) {
pa_log("pa_mempool_new() failed.");
return NULL;
}
diff --git a/src/pulsecore/mem.h b/src/pulsecore/mem.h
new file mode 100644
index 0000000..11a8086
--- /dev/null
+++ b/src/pulsecore/mem.h
@@ -0,0 +1,51 @@
+#ifndef foopulsememhfoo
+#define foopulsememhfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2016 Ahmed S. Darwish <darwish.07 at gmail.com>
+
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdbool.h>
+
+#include <pulsecore/macro.h>
+
+typedef enum pa_mem_type {
+ PA_MEM_TYPE_SHARED_POSIX, /* Data is shared and created using POSIX shm_open() */
+ PA_MEM_TYPE_SHARED_MEMFD, /* Data is shared and created using Linux memfd_create() */
+ PA_MEM_TYPE_PRIVATE, /* Data is private and created using classic memory allocation
+ (posix_memalign(), malloc() or anonymous mmap()) */
+} pa_mem_type_t;
+
+static inline const char *pa_mem_type_to_string(pa_mem_type_t type) {
+ switch (type) {
+ case PA_MEM_TYPE_SHARED_POSIX:
+ return "shared posix-shm";
+ case PA_MEM_TYPE_SHARED_MEMFD:
+ return "shared memfd";
+ case PA_MEM_TYPE_PRIVATE:
+ return "private";
+ }
+
+ pa_assert_not_reached();
+}
+
+static inline bool pa_mem_type_is_shared(pa_mem_type_t t) {
+ return (t == PA_MEM_TYPE_SHARED_POSIX) || (t == PA_MEM_TYPE_SHARED_MEMFD);
+}
+
+#endif
diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c
index ceea813..49665ee 100644
--- a/src/pulsecore/memblock.c
+++ b/src/pulsecore/memblock.c
@@ -773,7 +773,7 @@ static void memblock_replace_import(pa_memblock *b) {
pa_mutex_unlock(import->mutex);
}
-pa_mempool* pa_mempool_new(bool shared, size_t size) {
+pa_mempool *pa_mempool_new(pa_mem_type_t type, size_t size) {
pa_mempool *p;
char t1[PA_BYTES_SNPRINT_MAX], t2[PA_BYTES_SNPRINT_MAX];
@@ -793,13 +793,13 @@ pa_mempool* pa_mempool_new(bool shared, size_t size) {
p->n_blocks = 2;
}
- if (pa_shm_create_rw(&p->memory, p->n_blocks * p->block_size, shared, 0700) < 0) {
+ if (pa_shm_create_rw(&p->memory, type, p->n_blocks * p->block_size, 0700) < 0) {
pa_xfree(p);
return NULL;
}
pa_log_debug("Using %s memory pool with %u slots of size %s each, total size is %s, maximum usable slot size is %lu",
- p->memory.shared ? "shared" : "private",
+ pa_mem_type_to_string(type),
p->n_blocks,
pa_bytes_snprint(t1, sizeof(t1), (unsigned) p->block_size),
pa_bytes_snprint(t2, sizeof(t2), (unsigned) (p->n_blocks * p->block_size)),
@@ -923,10 +923,17 @@ void pa_mempool_vacuum(pa_mempool *p) {
}
/* No lock necessary */
+bool pa_mempool_is_shared(pa_mempool *p) {
+ pa_assert(p);
+
+ return pa_mem_type_is_shared(p->memory.type);
+}
+
+/* No lock necessary */
int pa_mempool_get_shm_id(pa_mempool *p, uint32_t *id) {
pa_assert(p);
- if (!p->memory.shared)
+ if (!pa_mempool_is_shared(p))
return -1;
*id = p->memory.id;
@@ -934,13 +941,6 @@ int pa_mempool_get_shm_id(pa_mempool *p, uint32_t *id) {
return 0;
}
-/* No lock necessary */
-bool pa_mempool_is_shared(pa_mempool *p) {
- pa_assert(p);
-
- return p->memory.shared;
-}
-
pa_mempool* pa_mempool_ref(pa_mempool *p) {
pa_assert(p);
pa_assert(PA_REFCNT_VALUE(p) > 0);
@@ -1139,7 +1139,7 @@ pa_memexport* pa_memexport_new(pa_mempool *p, pa_memexport_revoke_cb_t cb, void
pa_assert(p);
pa_assert(cb);
- if (!p->memory.shared)
+ if (!pa_mempool_is_shared(p))
return NULL;
e = pa_xnew(pa_memexport, 1);
diff --git a/src/pulsecore/memblock.h b/src/pulsecore/memblock.h
index 960ef25..718235f 100644
--- a/src/pulsecore/memblock.h
+++ b/src/pulsecore/memblock.h
@@ -30,6 +30,7 @@ typedef struct pa_memblock pa_memblock;
#include <pulse/xmalloc.h>
#include <pulsecore/atomic.h>
#include <pulsecore/memchunk.h>
+#include <pulsecore/mem.h>
/* A pa_memblock is a reference counted memory block. PulseAudio
* passes references to pa_memblocks around instead of copying
@@ -123,7 +124,7 @@ pa_mempool * pa_memblock_get_pool(pa_memblock *b);
pa_memblock *pa_memblock_will_need(pa_memblock *b);
/* The memory block manager */
-pa_mempool* pa_mempool_new(bool shared, size_t size);
+pa_mempool *pa_mempool_new(pa_mem_type_t type, size_t size);
void pa_mempool_unref(pa_mempool *p);
pa_mempool* pa_mempool_ref(pa_mempool *p);
const pa_mempool_stat* pa_mempool_get_stat(pa_mempool *p);
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index 82ea267..afb4850 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -55,6 +55,7 @@
#include <pulsecore/core-util.h>
#include <pulsecore/ipacl.h>
#include <pulsecore/thread-mq.h>
+#include <pulsecore/mem.h>
#include "protocol-native.h"
@@ -2621,7 +2622,7 @@ static void setup_srbchannel(pa_native_connection *c) {
return;
}
- if (!(c->rw_mempool = pa_mempool_new(true, c->protocol->core->shm_size))) {
+ if (!(c->rw_mempool = pa_mempool_new(PA_MEM_TYPE_SHARED_POSIX, c->protocol->core->shm_size))) {
pa_log_warn("Disabling srbchannel, reason: Failed to allocate shared "
"writable memory pool.");
return;
diff --git a/src/pulsecore/shm.c b/src/pulsecore/shm.c
index d613168..758dece 100644
--- a/src/pulsecore/shm.c
+++ b/src/pulsecore/shm.c
@@ -51,6 +51,7 @@
#include <pulsecore/core-util.h>
#include <pulsecore/macro.h>
#include <pulsecore/atomic.h>
+#include <pulsecore/mem.h>
#include "shm.h"
@@ -100,7 +101,7 @@ static char *segment_name(char *fn, size_t l, unsigned id) {
}
#endif
-int pa_shm_create_rw(pa_shm *m, size_t size, bool shared, mode_t mode) {
+int pa_shm_create_rw(pa_shm *m, pa_mem_type_t type, size_t size, mode_t mode) {
#ifdef HAVE_SHM_OPEN
char fn[32];
int fd = -1;
@@ -119,7 +120,7 @@ int pa_shm_create_rw(pa_shm *m, size_t size, bool shared, mode_t mode) {
/* Round up to make it page aligned */
size = PA_PAGE_ALIGN(size);
- if (!shared) {
+ if (!pa_mem_type_is_shared(type)) {
m->id = 0;
m->size = size;
@@ -184,7 +185,7 @@ int pa_shm_create_rw(pa_shm *m, size_t size, bool shared, mode_t mode) {
#endif
}
- m->shared = shared;
+ m->type = type;
return 0;
@@ -209,7 +210,7 @@ void pa_shm_free(pa_shm *m) {
pa_assert(m->ptr != MAP_FAILED);
#endif
- if (!m->shared) {
+ if (!pa_mem_type_is_shared(m->type)) {
#ifdef MAP_ANONYMOUS
if (munmap(m->ptr, m->size) < 0)
pa_log("munmap() failed: %s", pa_cstrerror(errno));
@@ -325,7 +326,7 @@ static int shm_attach(pa_shm *m, unsigned id, bool writable, bool for_cleanup) {
}
m->do_unlink = false;
- m->shared = true;
+ m->type = PA_MEM_TYPE_SHARED_POSIX;
pa_assert_se(pa_close(fd) == 0);
diff --git a/src/pulsecore/shm.h b/src/pulsecore/shm.h
index d438961..f0fda91 100644
--- a/src/pulsecore/shm.h
+++ b/src/pulsecore/shm.h
@@ -23,16 +23,17 @@
#include <sys/types.h>
#include <pulsecore/macro.h>
+#include <pulsecore/mem.h>
typedef struct pa_shm {
+ pa_mem_type_t type;
unsigned id;
void *ptr;
size_t size;
bool do_unlink:1;
- bool shared:1;
} pa_shm;
-int pa_shm_create_rw(pa_shm *m, size_t size, bool shared, mode_t mode);
+int pa_shm_create_rw(pa_shm *m, pa_mem_type_t type, size_t size, mode_t mode);
int pa_shm_attach(pa_shm *m, unsigned id, bool writable);
void pa_shm_punch(pa_shm *m, size_t offset, size_t size);
diff --git a/src/tests/cpu-mix-test.c b/src/tests/cpu-mix-test.c
index 2513d14..e487a20 100644
--- a/src/tests/cpu-mix-test.c
+++ b/src/tests/cpu-mix-test.c
@@ -76,7 +76,7 @@ static void run_mix_test(
samples_ref = out_ref + (8 - align);
nsamples = channels * (SAMPLES - (8 - align));
- fail_unless((pool = pa_mempool_new(false, 0)) != NULL, NULL);
+ fail_unless((pool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0)) != NULL, NULL);
pa_random(samples0, nsamples * sizeof(int16_t));
c0.memblock = pa_memblock_new_fixed(pool, samples0, nsamples * sizeof(int16_t), false);
diff --git a/src/tests/lfe-filter-test.c b/src/tests/lfe-filter-test.c
index e64288d..8d61cc6 100644
--- a/src/tests/lfe-filter-test.c
+++ b/src/tests/lfe-filter-test.c
@@ -136,7 +136,7 @@ START_TEST (lfe_filter_test) {
a.format = PA_SAMPLE_S16NE;
lft.ss = &a;
- pa_assert_se(lft.pool = pa_mempool_new(false, 0));
+ pa_assert_se(lft.pool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0));
/* We prepare pseudo-random input audio samples for lfe-filter rewind testing*/
ori_sample_ptr = pa_xmalloc(pa_frame_size(lft.ss) * TOTAL_SAMPLES);
diff --git a/src/tests/mcalign-test.c b/src/tests/mcalign-test.c
index ad9a760..f121972 100644
--- a/src/tests/mcalign-test.c
+++ b/src/tests/mcalign-test.c
@@ -36,7 +36,7 @@ int main(int argc, char *argv[]) {
pa_mcalign *a;
pa_memchunk c;
- p = pa_mempool_new(false, 0);
+ p = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0);
a = pa_mcalign_new(11);
diff --git a/src/tests/memblock-test.c b/src/tests/memblock-test.c
index 78a43cd..089648f 100644
--- a/src/tests/memblock-test.c
+++ b/src/tests/memblock-test.c
@@ -80,11 +80,11 @@ START_TEST (memblock_test) {
const char txt[] = "This is a test!";
- pool_a = pa_mempool_new(true, 0);
+ pool_a = pa_mempool_new(PA_MEM_TYPE_SHARED_POSIX, 0);
fail_unless(pool_a != NULL);
- pool_b = pa_mempool_new(true, 0);
+ pool_b = pa_mempool_new(PA_MEM_TYPE_SHARED_POSIX, 0);
fail_unless(pool_b != NULL);
- pool_c = pa_mempool_new(true, 0);
+ pool_c = pa_mempool_new(PA_MEM_TYPE_SHARED_POSIX, 0);
fail_unless(pool_c != NULL);
pa_mempool_get_shm_id(pool_a, &id_a);
diff --git a/src/tests/memblockq-test.c b/src/tests/memblockq-test.c
index ed33b2c..37cdd77 100644
--- a/src/tests/memblockq-test.c
+++ b/src/tests/memblockq-test.c
@@ -108,7 +108,7 @@ START_TEST (memblockq_test) {
pa_log_set_level(PA_LOG_DEBUG);
- p = pa_mempool_new(false, 0);
+ p = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0);
silence.memblock = pa_memblock_new_fixed(p, (char*) "__", 2, 1);
fail_unless(silence.memblock != NULL);
diff --git a/src/tests/mix-test.c b/src/tests/mix-test.c
index abf5fc7..ce6686a 100644
--- a/src/tests/mix-test.c
+++ b/src/tests/mix-test.c
@@ -286,7 +286,7 @@ START_TEST (mix_test) {
if (!getenv("MAKE_CHECK"))
pa_log_set_level(PA_LOG_DEBUG);
- fail_unless((pool = pa_mempool_new(false, 0)) != NULL, NULL);
+ fail_unless((pool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0)) != NULL, NULL);
a.channels = 1;
a.rate = 44100;
diff --git a/src/tests/remix-test.c b/src/tests/remix-test.c
index 578a30c..6ee5f6d 100644
--- a/src/tests/remix-test.c
+++ b/src/tests/remix-test.c
@@ -51,7 +51,7 @@ int main(int argc, char *argv[]) {
pa_log_set_level(PA_LOG_DEBUG);
- pa_assert_se(pool = pa_mempool_new(false, 0));
+ pa_assert_se(pool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0));
for (i = 0; maps[i].channels > 0; i++)
for (j = 0; maps[j].channels > 0; j++) {
diff --git a/src/tests/resampler-test.c b/src/tests/resampler-test.c
index 2833d7e..28f03d6 100644
--- a/src/tests/resampler-test.c
+++ b/src/tests/resampler-test.c
@@ -404,7 +404,7 @@ int main(int argc, char *argv[]) {
}
ret = 0;
- pa_assert_se(pool = pa_mempool_new(false, 0));
+ pa_assert_se(pool = pa_mempool_new(PA_MEM_TYPE_PRIVATE, 0));
if (!all_formats) {
diff --git a/src/tests/srbchannel-test.c b/src/tests/srbchannel-test.c
index 3dbcc2b..253abcf 100644
--- a/src/tests/srbchannel-test.c
+++ b/src/tests/srbchannel-test.c
@@ -85,7 +85,7 @@ START_TEST (srbchannel_test) {
int pipefd[4];
pa_mainloop *ml = pa_mainloop_new();
- pa_mempool *mp = pa_mempool_new(true, 0);
+ pa_mempool *mp = pa_mempool_new(PA_MEM_TYPE_SHARED_POSIX, 0);
pa_iochannel *io1, *io2;
pa_pstream *p1, *p2;
pa_srbchannel *sr1, *sr2;
--
2.7.2
More information about the pulseaudio-discuss
mailing list