make use of new memory page alignment macros, reindent

git-svn-id: file:///home/lennart/svn/public/pulseaudio/branches/lennart@1743 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2007-09-02 20:35:36 +00:00
parent fe1f55b877
commit b54e71a96a

View file

@ -43,6 +43,7 @@
#include <pulsecore/log.h> #include <pulsecore/log.h>
#include <pulsecore/random.h> #include <pulsecore/random.h>
#include <pulsecore/core-util.h> #include <pulsecore/core-util.h>
#include <pulsecore/macro.h>
#include <pulse/xmalloc.h> #include <pulse/xmalloc.h>
#include "shm.h" #include "shm.h"
@ -80,7 +81,7 @@ int pa_shm_create_rw(pa_shm *m, size_t size, int shared, mode_t mode) {
{ {
int r; int r;
if ((r = posix_memalign(&m->ptr, sysconf(_SC_PAGESIZE), 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));
goto fail; goto fail;
} }
@ -167,7 +168,7 @@ void pa_shm_free(pa_shm *m) {
} }
#else #else
/* We shouldn't be here without shm support */ /* We shouldn't be here without shm support */
assert(0); pa_assert_not_reached();
#endif #endif
} }
@ -176,6 +177,7 @@ void pa_shm_free(pa_shm *m) {
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, ps;
assert(m); assert(m);
assert(m->ptr); assert(m->ptr);
@ -189,22 +191,15 @@ void pa_shm_punch(pa_shm *m, size_t offset, size_t size) {
/* You're welcome to implement this as NOOP on systems that don't /* You're welcome to implement this as NOOP on systems that don't
* support it */ * support it */
/* Align this to multiples of the page size */
ptr = (uint8_t*) m->ptr + offset; ptr = (uint8_t*) m->ptr + offset;
o = (uint8_t*) ptr - (uint8_t*) PA_PAGE_ALIGN_PTR(ptr);
#ifdef __linux__
{
/* On Linux ptr must be page aligned */
long psz = sysconf(_SC_PAGESIZE);
unsigned o;
o = ((unsigned long) ptr) - ((((unsigned long) ptr)/psz) * psz);
if (o > 0) { if (o > 0) {
ptr = (uint8_t*) ptr + (psz - o); ps = PA_PAGE_SIZE;
size -= psz - o; ptr = (uint8_t*) ptr + (ps - o);
size -= ps - o;
} }
}
#endif
#ifdef MADV_REMOVE #ifdef MADV_REMOVE
if (madvise(ptr, size, MADV_REMOVE) >= 0) if (madvise(ptr, size, MADV_REMOVE) >= 0)
@ -217,7 +212,9 @@ void pa_shm_punch(pa_shm *m, size_t offset, size_t size) {
#endif #endif
#ifdef MADV_DONTNEED #ifdef MADV_DONTNEED
madvise(ptr, size, MADV_DONTNEED); pa_assert_se(madvise(ptr, size, MADV_DONTNEED) == 0);
#elif defined(POSIX_MADV_DONTNEED)
pa_assert_se(posix_madvise(ptr, size, POSIX_MADV_DONTNEED) == 0);
#endif #endif
} }