pulsecore: Transform pa_mempool_new() into a factory method

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@gmail.com>
This commit is contained in:
Ahmed S. Darwish 2016-03-13 00:57:06 +02:00 committed by David Henningsson
parent 211a520543
commit b88acd0266
18 changed files with 97 additions and 37 deletions

View file

@ -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;
}

51
src/pulsecore/mem.h Normal file
View file

@ -0,0 +1,51 @@
#ifndef foopulsememhfoo
#define foopulsememhfoo
/***
This file is part of PulseAudio.
Copyright 2016 Ahmed S. Darwish <darwish.07@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

View file

@ -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)),
@ -922,11 +922,18 @@ void pa_mempool_vacuum(pa_mempool *p) {
pa_flist_free(list, NULL);
}
/* 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);

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -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);