mirror of
https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
synced 2025-11-02 09:01:46 -05:00
core: Replace PA_PAGE_SIZE with pa_page_size()
PA_PAGE_SIZE using sysconf() may return a negative number CID 1137925, CID 1137926, CID 1138485 instead of calling sysconf() directly, add function pa_page_size() which uses the guestimate 4096 in case sysconf(_SC_PAGE_SIZE) fails using PA_ONCE to only evaluate sysconf() once
This commit is contained in:
parent
c99efbffd6
commit
45d9030638
10 changed files with 55 additions and 38 deletions
|
|
@ -918,7 +918,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
pa_log_debug("Found %u CPUs.", pa_ncpus());
|
pa_log_debug("Found %u CPUs.", pa_ncpus());
|
||||||
|
|
||||||
pa_log_info("Page size is %lu bytes", (unsigned long) PA_PAGE_SIZE);
|
pa_log_info("Page size is %zu bytes", pa_page_size());
|
||||||
|
|
||||||
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
||||||
pa_log_debug("Compiled with Valgrind support: yes");
|
pa_log_debug("Compiled with Valgrind support: yes");
|
||||||
|
|
|
||||||
|
|
@ -138,6 +138,7 @@
|
||||||
#include <pulsecore/strlist.h>
|
#include <pulsecore/strlist.h>
|
||||||
#include <pulsecore/cpu-x86.h>
|
#include <pulsecore/cpu-x86.h>
|
||||||
#include <pulsecore/pipe.h>
|
#include <pulsecore/pipe.h>
|
||||||
|
#include <pulsecore/once.h>
|
||||||
|
|
||||||
#include "core-util.h"
|
#include "core-util.h"
|
||||||
|
|
||||||
|
|
@ -2553,6 +2554,7 @@ void *pa_will_need(const void *p, size_t l) {
|
||||||
size_t size;
|
size_t size;
|
||||||
int r = ENOTSUP;
|
int r = ENOTSUP;
|
||||||
size_t bs;
|
size_t bs;
|
||||||
|
const size_t page_size = pa_page_size();
|
||||||
|
|
||||||
pa_assert(p);
|
pa_assert(p);
|
||||||
pa_assert(l > 0);
|
pa_assert(l > 0);
|
||||||
|
|
@ -2577,7 +2579,7 @@ void *pa_will_need(const void *p, size_t l) {
|
||||||
#ifdef RLIMIT_MEMLOCK
|
#ifdef RLIMIT_MEMLOCK
|
||||||
pa_assert_se(getrlimit(RLIMIT_MEMLOCK, &rlim) == 0);
|
pa_assert_se(getrlimit(RLIMIT_MEMLOCK, &rlim) == 0);
|
||||||
|
|
||||||
if (rlim.rlim_cur < PA_PAGE_SIZE) {
|
if (rlim.rlim_cur < page_size) {
|
||||||
pa_log_debug("posix_madvise() failed (or doesn't exist), resource limits don't allow mlock(), can't page in data: %s", pa_cstrerror(r));
|
pa_log_debug("posix_madvise() failed (or doesn't exist), resource limits don't allow mlock(), can't page in data: %s", pa_cstrerror(r));
|
||||||
errno = EPERM;
|
errno = EPERM;
|
||||||
return (void*) p;
|
return (void*) p;
|
||||||
|
|
@ -2585,7 +2587,7 @@ void *pa_will_need(const void *p, size_t l) {
|
||||||
|
|
||||||
bs = PA_PAGE_ALIGN((size_t) rlim.rlim_cur);
|
bs = PA_PAGE_ALIGN((size_t) rlim.rlim_cur);
|
||||||
#else
|
#else
|
||||||
bs = PA_PAGE_SIZE*4;
|
bs = page_size*4;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
pa_log_debug("posix_madvise() failed (or doesn't exist), trying mlock(): %s", pa_cstrerror(r));
|
pa_log_debug("posix_madvise() failed (or doesn't exist), trying mlock(): %s", pa_cstrerror(r));
|
||||||
|
|
@ -3669,3 +3671,23 @@ bool pa_running_in_vm(void) {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t pa_page_size(void) {
|
||||||
|
#if defined(PAGE_SIZE)
|
||||||
|
return PAGE_SIZE;
|
||||||
|
#elif defined(PAGESIZE)
|
||||||
|
return PAGESIZE;
|
||||||
|
#elif defined(HAVE_SYSCONF)
|
||||||
|
static size_t page_size = 4096; /* Let's hope it's like x86. */
|
||||||
|
|
||||||
|
PA_ONCE_BEGIN {
|
||||||
|
long ret = sysconf(_SC_PAGE_SIZE);
|
||||||
|
if (ret > 0)
|
||||||
|
page_size = ret;
|
||||||
|
} PA_ONCE_END;
|
||||||
|
|
||||||
|
return page_size;
|
||||||
|
#else
|
||||||
|
return 4096;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -302,4 +302,17 @@ bool pa_running_in_vm(void);
|
||||||
char *pa_win32_get_toplevel(HANDLE handle);
|
char *pa_win32_get_toplevel(HANDLE handle);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
size_t pa_page_size(void);
|
||||||
|
|
||||||
|
/* Rounds down */
|
||||||
|
static inline void* PA_PAGE_ALIGN_PTR(const void *p) {
|
||||||
|
return (void*) (((size_t) p) & ~(pa_page_size() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Rounds up */
|
||||||
|
static inline size_t PA_PAGE_ALIGN(size_t l) {
|
||||||
|
size_t page_size = pa_page_size();
|
||||||
|
return (l + page_size - 1) & ~(page_size - 1);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -44,17 +44,6 @@
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(PAGE_SIZE)
|
|
||||||
#define PA_PAGE_SIZE ((size_t) PAGE_SIZE)
|
|
||||||
#elif defined(PAGESIZE)
|
|
||||||
#define PA_PAGE_SIZE ((size_t) PAGESIZE)
|
|
||||||
#elif defined(HAVE_SYSCONF)
|
|
||||||
#define PA_PAGE_SIZE ((size_t) (sysconf(_SC_PAGE_SIZE)))
|
|
||||||
#else
|
|
||||||
/* Let's hope it's like x86. */
|
|
||||||
#define PA_PAGE_SIZE ((size_t) 4096)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Rounds down */
|
/* Rounds down */
|
||||||
static inline void* PA_ALIGN_PTR(const void *p) {
|
static inline void* PA_ALIGN_PTR(const void *p) {
|
||||||
return (void*) (((size_t) p) & ~(sizeof(void*) - 1));
|
return (void*) (((size_t) p) & ~(sizeof(void*) - 1));
|
||||||
|
|
@ -65,16 +54,6 @@ static inline size_t PA_ALIGN(size_t l) {
|
||||||
return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1));
|
return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Rounds down */
|
|
||||||
static inline void* PA_PAGE_ALIGN_PTR(const void *p) {
|
|
||||||
return (void*) (((size_t) p) & ~(PA_PAGE_SIZE - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Rounds up */
|
|
||||||
static inline size_t PA_PAGE_ALIGN(size_t l) {
|
|
||||||
return (l + PA_PAGE_SIZE - 1) & ~(PA_PAGE_SIZE - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
#define PA_UNUSED __attribute__ ((unused))
|
#define PA_UNUSED __attribute__ ((unused))
|
||||||
#else
|
#else
|
||||||
|
|
|
||||||
|
|
@ -828,13 +828,14 @@ static void memblock_replace_import(pa_memblock *b) {
|
||||||
pa_mempool *pa_mempool_new(pa_mem_type_t type, size_t size, bool per_client) {
|
pa_mempool *pa_mempool_new(pa_mem_type_t type, size_t size, bool per_client) {
|
||||||
pa_mempool *p;
|
pa_mempool *p;
|
||||||
char t1[PA_BYTES_SNPRINT_MAX], t2[PA_BYTES_SNPRINT_MAX];
|
char t1[PA_BYTES_SNPRINT_MAX], t2[PA_BYTES_SNPRINT_MAX];
|
||||||
|
const size_t page_size = pa_page_size();
|
||||||
|
|
||||||
p = pa_xnew0(pa_mempool, 1);
|
p = pa_xnew0(pa_mempool, 1);
|
||||||
PA_REFCNT_INIT(p);
|
PA_REFCNT_INIT(p);
|
||||||
|
|
||||||
p->block_size = PA_PAGE_ALIGN(PA_MEMPOOL_SLOT_SIZE);
|
p->block_size = PA_PAGE_ALIGN(PA_MEMPOOL_SLOT_SIZE);
|
||||||
if (p->block_size < PA_PAGE_SIZE)
|
if (p->block_size < page_size)
|
||||||
p->block_size = PA_PAGE_SIZE;
|
p->block_size = page_size;
|
||||||
|
|
||||||
if (size <= 0)
|
if (size <= 0)
|
||||||
p->n_blocks = PA_MEMPOOL_SLOTS_MAX;
|
p->n_blocks = PA_MEMPOOL_SLOTS_MAX;
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@
|
||||||
|
|
||||||
#include "sample-util.h"
|
#include "sample-util.h"
|
||||||
|
|
||||||
#define PA_SILENCE_MAX (PA_PAGE_SIZE*16)
|
#define PA_SILENCE_MAX (pa_page_size()*16)
|
||||||
|
|
||||||
pa_memblock *pa_silence_memblock(pa_memblock* b, const pa_sample_spec *spec) {
|
pa_memblock *pa_silence_memblock(pa_memblock* b, const pa_sample_spec *spec) {
|
||||||
void *data;
|
void *data;
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,7 @@ static int privatemem_create(pa_shm *m, size_t size) {
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
if ((r = posix_memalign(&m->ptr, PA_PAGE_SIZE, size)) < 0) {
|
if ((r = posix_memalign(&m->ptr, pa_page_size(), size)) < 0) {
|
||||||
pa_log("posix_memalign() failed: %s", pa_cstrerror(r));
|
pa_log("posix_memalign() failed: %s", pa_cstrerror(r));
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
@ -300,6 +300,7 @@ finish:
|
||||||
void pa_shm_punch(pa_shm *m, size_t offset, size_t size) {
|
void pa_shm_punch(pa_shm *m, size_t offset, size_t size) {
|
||||||
void *ptr;
|
void *ptr;
|
||||||
size_t o;
|
size_t o;
|
||||||
|
const size_t page_size = pa_page_size();
|
||||||
|
|
||||||
pa_assert(m);
|
pa_assert(m);
|
||||||
pa_assert(m->ptr);
|
pa_assert(m->ptr);
|
||||||
|
|
@ -318,13 +319,13 @@ void pa_shm_punch(pa_shm *m, size_t offset, size_t size) {
|
||||||
o = (size_t) ((uint8_t*) ptr - (uint8_t*) PA_PAGE_ALIGN_PTR(ptr));
|
o = (size_t) ((uint8_t*) ptr - (uint8_t*) PA_PAGE_ALIGN_PTR(ptr));
|
||||||
|
|
||||||
if (o > 0) {
|
if (o > 0) {
|
||||||
size_t delta = PA_PAGE_SIZE - o;
|
size_t delta = page_size - o;
|
||||||
ptr = (uint8_t*) ptr + delta;
|
ptr = (uint8_t*) ptr + delta;
|
||||||
size -= delta;
|
size -= delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Align the size down to multiples of page size */
|
/* Align the size down to multiples of page size */
|
||||||
size = (size / PA_PAGE_SIZE) * PA_PAGE_SIZE;
|
size = (size / page_size) * page_size;
|
||||||
|
|
||||||
#ifdef MADV_REMOVE
|
#ifdef MADV_REMOVE
|
||||||
if (madvise(ptr, size, MADV_REMOVE) >= 0)
|
if (madvise(ptr, size, MADV_REMOVE) >= 0)
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@
|
||||||
/* #define SINK_INPUT_DEBUG */
|
/* #define SINK_INPUT_DEBUG */
|
||||||
|
|
||||||
#define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
|
#define MEMBLOCKQ_MAXLENGTH (32*1024*1024)
|
||||||
#define CONVERT_BUFFER_LENGTH (PA_PAGE_SIZE)
|
#define CONVERT_BUFFER_LENGTH (pa_page_size())
|
||||||
|
|
||||||
PA_DEFINE_PUBLIC_CLASS(pa_sink_input, pa_msgobject);
|
PA_DEFINE_PUBLIC_CLASS(pa_sink_input, pa_msgobject);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@
|
||||||
#include "sink.h"
|
#include "sink.h"
|
||||||
|
|
||||||
#define MAX_MIX_CHANNELS 32
|
#define MAX_MIX_CHANNELS 32
|
||||||
#define MIX_BUFFER_LENGTH (PA_PAGE_SIZE)
|
#define MIX_BUFFER_LENGTH (pa_page_size())
|
||||||
#define ABSOLUTE_MIN_LATENCY (500)
|
#define ABSOLUTE_MIN_LATENCY (500)
|
||||||
#define ABSOLUTE_MAX_LATENCY (10*PA_USEC_PER_SEC)
|
#define ABSOLUTE_MAX_LATENCY (10*PA_USEC_PER_SEC)
|
||||||
#define DEFAULT_FIXED_LATENCY (250*PA_USEC_PER_MSEC)
|
#define DEFAULT_FIXED_LATENCY (250*PA_USEC_PER_MSEC)
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ START_TEST (sigbus_test) {
|
||||||
void *p;
|
void *p;
|
||||||
int fd;
|
int fd;
|
||||||
pa_memtrap *m;
|
pa_memtrap *m;
|
||||||
|
const size_t page_size = pa_page_size();
|
||||||
|
|
||||||
pa_log_set_level(PA_LOG_DEBUG);
|
pa_log_set_level(PA_LOG_DEBUG);
|
||||||
pa_memtrap_install();
|
pa_memtrap_install();
|
||||||
|
|
@ -40,14 +41,14 @@ START_TEST (sigbus_test) {
|
||||||
/* Create the memory map */
|
/* Create the memory map */
|
||||||
fail_unless((fd = open("sigbus-test-map", O_RDWR|O_TRUNC|O_CREAT, 0660)) >= 0);
|
fail_unless((fd = open("sigbus-test-map", O_RDWR|O_TRUNC|O_CREAT, 0660)) >= 0);
|
||||||
fail_unless(unlink("sigbus-test-map") == 0);
|
fail_unless(unlink("sigbus-test-map") == 0);
|
||||||
fail_unless(ftruncate(fd, PA_PAGE_SIZE) >= 0);
|
fail_unless(ftruncate(fd, page_size) >= 0);
|
||||||
fail_unless((p = mmap(NULL, PA_PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) != MAP_FAILED);
|
fail_unless((p = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) != MAP_FAILED);
|
||||||
|
|
||||||
/* Register memory map */
|
/* Register memory map */
|
||||||
m = pa_memtrap_add(p, PA_PAGE_SIZE);
|
m = pa_memtrap_add(p, page_size);
|
||||||
|
|
||||||
/* Use memory map */
|
/* Use memory map */
|
||||||
pa_snprintf(p, PA_PAGE_SIZE, "This is a test that should work fine.");
|
pa_snprintf(p, page_size, "This is a test that should work fine.");
|
||||||
|
|
||||||
/* Verify memory map */
|
/* Verify memory map */
|
||||||
pa_log("Let's see if this worked: %s", (char*) p);
|
pa_log("Let's see if this worked: %s", (char*) p);
|
||||||
|
|
@ -58,7 +59,7 @@ START_TEST (sigbus_test) {
|
||||||
fail_unless(ftruncate(fd, 0) >= 0);
|
fail_unless(ftruncate(fd, 0) >= 0);
|
||||||
|
|
||||||
/* Use memory map */
|
/* Use memory map */
|
||||||
pa_snprintf(p, PA_PAGE_SIZE, "This is a test that should fail but get caught.");
|
pa_snprintf(p, page_size, "This is a test that should fail but get caught.");
|
||||||
|
|
||||||
/* Verify memory map */
|
/* Verify memory map */
|
||||||
pa_log("Let's see if this worked: %s", (char*) p);
|
pa_log("Let's see if this worked: %s", (char*) p);
|
||||||
|
|
@ -66,7 +67,7 @@ START_TEST (sigbus_test) {
|
||||||
fail_unless(pa_memtrap_is_good(m) == false);
|
fail_unless(pa_memtrap_is_good(m) == false);
|
||||||
|
|
||||||
pa_memtrap_remove(m);
|
pa_memtrap_remove(m);
|
||||||
munmap(p, PA_PAGE_SIZE);
|
munmap(p, page_size);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue