[pulseaudio-discuss] [PATCH 08/11] pulsecore: Standardize memory-types annotation using pa_mem_type_t

Ahmed S. Darwish darwish.07 at gmail.com
Sun Sep 20 14:35:06 PDT 2015


By introducing memfd as another type of shared memory, the mempools
will not be the only object in need of an annotation for different
memory types.

The code for memexporting and memimporting a memblock (from and to
another PulseAudio endpoint) will also need an equivalent marker.
Thus standardize everything on pa_mem_type_t.

Signed-off-by: Ahmed S. Darwish <darwish.07 at gmail.com>
---
 src/pulse/context.c             |  6 +++---
 src/pulsecore/core.c            |  4 ++--
 src/pulsecore/mem.c             | 14 ++++++++++++++
 src/pulsecore/mem.h             |  9 ++++++++-
 src/pulsecore/memblock.c        | 40 +++++++++++++---------------------------
 src/pulsecore/memblock.h        |  9 ++-------
 src/pulsecore/protocol-native.c |  2 +-
 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 +-
 16 files changed, 54 insertions(+), 52 deletions(-)

diff --git a/src/pulse/context.c b/src/pulse/context.c
index d3ea1d4..ede01fa 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -125,7 +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_mempool_type_t type;
+    pa_mem_type_t type;
 
     pa_assert(mainloop);
 
@@ -171,12 +171,12 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *
     c->srb_template.readfd = -1;
     c->srb_template.writefd = -1;
 
-    type = !c->conf->disable_shm ? PA_MEMPOOL_SHARED_POSIX : PA_MEMPOOL_PRIVATE;
+    type = !c->conf->disable_shm ? PA_MEMORY_SHARED_POSIX : PA_MEMORY_PRIVATE;
     if (!(c->mempool = pa_mempool_new(type, 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_MEMPOOL_PRIVATE, c->conf->shm_size);
+            c->mempool = pa_mempool_new(PA_MEMORY_PRIVATE, c->conf->shm_size);
         }
 
         if (!c->mempool) {
diff --git a/src/pulsecore/core.c b/src/pulsecore/core.c
index bf2448e..6b32279 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(PA_MEMPOOL_SHARED_POSIX, shm_size))) {
+        if (!(pool = pa_mempool_new(PA_MEMORY_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(PA_MEMPOOL_PRIVATE, shm_size))) {
+        if (!(pool = pa_mempool_new(PA_MEMORY_PRIVATE, shm_size))) {
             pa_log("pa_mempool_new() failed.");
             return NULL;
         }
diff --git a/src/pulsecore/mem.c b/src/pulsecore/mem.c
index a0ca1d8..4f8a59c 100644
--- a/src/pulsecore/mem.c
+++ b/src/pulsecore/mem.c
@@ -30,6 +30,20 @@
 #include <pulsecore/macro.h>
 #include <pulsecore/core-error.h>
 
+const char *pa_mem_type_tostr(pa_mem_type_t type) {
+    switch (type) {
+    case PA_MEMORY_SHARED_POSIX:
+        return "shared posix-shm";
+    case PA_MEMORY_SHARED_MEMFD:
+        return "shared memfd";
+    case PA_MEMORY_PRIVATE:
+        return "private";
+    case PA_MEMORY_UNDEFINED:
+    default:
+        pa_assert_not_reached();
+    }
+}
+
 int shared_fd_get_file_size(int fd, size_t *size) {
     struct stat st;
 
diff --git a/src/pulsecore/mem.h b/src/pulsecore/mem.h
index e5a9ec3..e7a8182 100644
--- a/src/pulsecore/mem.h
+++ b/src/pulsecore/mem.h
@@ -28,7 +28,7 @@
 
 /* Parent anonymous structure representing a plain memory area and its
  * size. Different structures inherit from this, representing different
- * memory area types (e.g. Posix SHM, Linux memfds, or private).
+ * memory area types (e.g. Posix SHM, Linux memfds, or private mallocs).
  *
  * To inherit from this object, just include __PA_MEM_STRUCT__ as the
  * very first element of your structure. Check "pa_mempool", "pa_shm",
@@ -44,6 +44,12 @@ typedef struct pa_mem {
     __PA_PARENT_MEM_STRUCT__;
 } pa_mem;
 
+typedef enum pa_mem_type {
+    PA_MEMORY_UNDEFINED = 0,        /* NULL marker */
+    PA_MEMORY_SHARED_POSIX,         /* Data is shared and created using POSIX shm_open() */
+    PA_MEMORY_SHARED_MEMFD,         /* Data is shared and created using Linux memfd_create() */
+    PA_MEMORY_PRIVATE,              /* Data is private and created using classic memory allocation (malloc, etc.) */
+} pa_mem_type_t;
 
 /*
  * Some methods and definitions commonly used by our children
@@ -51,6 +57,7 @@ typedef struct pa_mem {
 
 #define MAX_SHARED_MEM_SIZE (PA_ALIGN(1024*1024*1024))
 
+const char *pa_mem_type_tostr(pa_mem_type_t type);
 int shared_fd_get_file_size(int fd, size_t *size);
 void pa_mem_punch_region(pa_mem *shm_area, size_t punch_offset, size_t punch_size);
 
diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c
index e0e4dfb..6047ead 100644
--- a/src/pulsecore/memblock.c
+++ b/src/pulsecore/memblock.c
@@ -148,7 +148,7 @@ struct pa_mempool {
     pa_semaphore *semaphore;
     pa_mutex *mutex;
 
-    pa_mempool_type_t type;
+    pa_mem_type_t mem_type;
     union {
         pa_mem mem;
         union {
@@ -756,27 +756,14 @@ static void memblock_replace_import(pa_memblock *b) {
     pa_mutex_unlock(import->mutex);
 }
 
-static const char *pa_mempool_type_tostr(pa_mempool_type_t type) {
-    switch (type) {
-    case PA_MEMPOOL_SHARED_POSIX:
-        return "shared posix-shm";
-    case PA_MEMPOOL_SHARED_MEMFD:
-        return "shared memfd";
-    case PA_MEMPOOL_PRIVATE:
-        return "private";
-    default:
-        pa_assert_not_reached();
-    }
-}
-
-pa_mempool *pa_mempool_new(pa_mempool_type_t type, size_t size) {
+pa_mempool *pa_mempool_new(pa_mem_type_t type, size_t size) {
     pa_mempool *p;
     size_t pa_mem_size;
     char t1[PA_BYTES_SNPRINT_MAX], t2[PA_BYTES_SNPRINT_MAX];
 
     p = pa_xnew0(pa_mempool, 1);
 
-    p->type = type;
+    p->mem_type = type;
 
     p->block_size = PA_PAGE_ALIGN(PA_MEMPOOL_SLOT_SIZE);
     if (p->block_size < PA_PAGE_SIZE)
@@ -793,16 +780,15 @@ pa_mempool *pa_mempool_new(pa_mempool_type_t type, size_t size) {
     pa_mem_size = p->n_blocks * p->block_size;
 
     switch (type) {
-
-    case PA_MEMPOOL_SHARED_POSIX:
+    case PA_MEMORY_SHARED_POSIX:
         if (pa_shm_create(&p->per_type.shm, pa_mem_size, 0700) < 0)
             goto fail;
         break;
-    case PA_MEMPOOL_SHARED_MEMFD:
+    case PA_MEMORY_SHARED_MEMFD:
         if (pa_memfd_create(&p->per_type.memfd, pa_mem_size) < 0)
             goto fail;
         break;
-    case PA_MEMPOOL_PRIVATE:
+    case PA_MEMORY_PRIVATE:
         if (pa_privatemem_create(&p->per_type.privatemem, pa_mem_size) < 0)
             goto fail;
         break;
@@ -812,7 +798,7 @@ pa_mempool *pa_mempool_new(pa_mempool_type_t type, size_t size) {
     }
 
     pa_log_debug("Using %s memory pool with %u slots of size %s each, total size is %s, maximum usable slot size is %lu",
-                 pa_mempool_type_tostr(type),
+                 pa_mem_type_tostr(type),
                  p->n_blocks,
                  pa_bytes_snprint(t1, sizeof(t1), (unsigned) p->block_size),
                  pa_bytes_snprint(t2, sizeof(t2), (unsigned) (pa_mem_size)),
@@ -894,14 +880,14 @@ void pa_mempool_free(pa_mempool *p) {
 /*         PA_DEBUG_TRAP; */
     }
 
-    switch (p->type) {
-    case PA_MEMPOOL_SHARED_POSIX:
+    switch (p->mem_type) {
+    case PA_MEMORY_SHARED_POSIX:
         pa_shm_free(&p->per_type.shm);
         break;
-    case PA_MEMPOOL_SHARED_MEMFD:
+    case PA_MEMORY_SHARED_MEMFD:
         pa_memfd_free(&p->per_type.memfd);
         break;
-    case PA_MEMPOOL_PRIVATE:
+    case PA_MEMORY_PRIVATE:
         pa_privatemem_free(&p->per_type.privatemem);
         break;
     default:
@@ -954,7 +940,7 @@ void pa_mempool_vacuum(pa_mempool *p) {
 int pa_mempool_get_shm_id(pa_mempool *p, uint32_t *id) {
     pa_assert(p);
 
-    pa_assert(p->type == PA_MEMPOOL_SHARED_POSIX);
+    pa_assert(p->mem_type == PA_MEMORY_SHARED_POSIX);
 
     *id = p->per_type.shm.id;
 
@@ -965,7 +951,7 @@ int pa_mempool_get_shm_id(pa_mempool *p, uint32_t *id) {
 bool pa_mempool_is_shared(pa_mempool *p) {
     pa_assert(p);
 
-    return p->type == PA_MEMPOOL_SHARED_POSIX || p->type == PA_MEMPOOL_SHARED_MEMFD;
+    return p->mem_type == PA_MEMORY_SHARED_POSIX || p->mem_type == PA_MEMORY_SHARED_MEMFD;
 }
 
 /* For receiving blocks from other nodes */
diff --git a/src/pulsecore/memblock.h b/src/pulsecore/memblock.h
index a3b8a07..184ba55 100644
--- a/src/pulsecore/memblock.h
+++ b/src/pulsecore/memblock.h
@@ -27,6 +27,7 @@ typedef struct pa_memblock pa_memblock;
 #include <inttypes.h>
 
 #include <pulse/def.h>
+#include <pulsecore/mem.h>
 #include <pulse/xmalloc.h>
 #include <pulsecore/atomic.h>
 #include <pulsecore/memchunk.h>
@@ -47,12 +48,6 @@ typedef enum pa_memblock_type {
     PA_MEMBLOCK_TYPE_MAX
 } pa_memblock_type_t;
 
-typedef enum pa_mempool_type {
-    PA_MEMPOOL_SHARED_POSIX,      /* Mempool data is shared and created using POSIX shm_open() */
-    PA_MEMPOOL_SHARED_MEMFD,      /* Mempool data is shared and created using Linux kernel memfd_create() */
-    PA_MEMPOOL_PRIVATE,           /* Mempool data is private and created using classic memory allocation (malloc, etc.) */
-} pa_mempool_type_t;
-
 typedef struct pa_mempool pa_mempool;
 typedef struct pa_mempool_stat pa_mempool_stat;
 typedef struct pa_memimport_segment pa_memimport_segment;
@@ -127,7 +122,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(pa_mempool_type_t type, size_t size);
+pa_mempool *pa_mempool_new(pa_mem_type_t type, size_t size);
 void pa_mempool_free(pa_mempool *p);
 const pa_mempool_stat* pa_mempool_get_stat(pa_mempool *p);
 void pa_mempool_vacuum(pa_mempool *p);
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index ae0c974..87d3c01 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -2607,7 +2607,7 @@ static void setup_srbchannel(pa_native_connection *c) {
         return;
     }
 
-    if (!(rw_mempool = pa_mempool_new(PA_MEMPOOL_SHARED_POSIX, c->protocol->core->shm_size))) {
+    if (!(rw_mempool = pa_mempool_new(PA_MEMORY_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/tests/cpu-mix-test.c b/src/tests/cpu-mix-test.c
index ff5dda8..165b92d 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(PA_MEMPOOL_PRIVATE, 0)) != NULL, NULL);
+    fail_unless((pool = pa_mempool_new(PA_MEMORY_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 7e68243..4d12cf6 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(PA_MEMPOOL_PRIVATE, 0));
+    pa_assert_se(lft.pool = pa_mempool_new(PA_MEMORY_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 6a371f5..527e2ac 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(PA_MEMPOOL_PRIVATE, 0);
+    p = pa_mempool_new(PA_MEMORY_PRIVATE, 0);
 
     a = pa_mcalign_new(11);
 
diff --git a/src/tests/memblock-test.c b/src/tests/memblock-test.c
index fb52705..e4c9d0a 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(PA_MEMPOOL_SHARED_POSIX, 0);
+    pool_a = pa_mempool_new(PA_MEMORY_SHARED_POSIX, 0);
     fail_unless(pool_a != NULL);
-    pool_b = pa_mempool_new(PA_MEMPOOL_SHARED_POSIX, 0);
+    pool_b = pa_mempool_new(PA_MEMORY_SHARED_POSIX, 0);
     fail_unless(pool_b != NULL);
-    pool_c = pa_mempool_new(PA_MEMPOOL_SHARED_POSIX, 0);
+    pool_c = pa_mempool_new(PA_MEMORY_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 96fac9f..9842d6d 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(PA_MEMPOOL_PRIVATE, 0);
+    p = pa_mempool_new(PA_MEMORY_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 2074695..1513c94 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(PA_MEMPOOL_PRIVATE, 0)) != NULL, NULL);
+    fail_unless((pool = pa_mempool_new(PA_MEMORY_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 70fcd18..401eb82 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(PA_MEMPOOL_PRIVATE, 0));
+    pa_assert_se(pool = pa_mempool_new(PA_MEMORY_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 b7d7729..efcc584 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(PA_MEMPOOL_PRIVATE, 0));
+    pa_assert_se(pool = pa_mempool_new(PA_MEMORY_PRIVATE, 0));
 
     if (!all_formats) {
 
diff --git a/src/tests/srbchannel-test.c b/src/tests/srbchannel-test.c
index cf8f281..d4c89f0 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(PA_MEMPOOL_SHARED_POSIX, 0);
+    pa_mempool *mp = pa_mempool_new(PA_MEMORY_SHARED_POSIX, 0);
     pa_iochannel *io1, *io2;
     pa_pstream *p1, *p2;
     pa_srbchannel *sr1, *sr2;

-- 
Darwish
http://darwish.chasingpointers.com


More information about the pulseaudio-discuss mailing list