[pulseaudio-commits] r1745 - in /branches/lennart/src/pulsecore: core-util.c core-util.h
svnmailer-noreply at 0pointer.de
svnmailer-noreply at 0pointer.de
Sun Sep 2 13:36:32 PDT 2007
Author: lennart
Date: Sun Sep 2 22:36:32 2007
New Revision: 1745
URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=3D1745&root=3Dpulseaudio&vi=
ew=3Drev
Log:
add new pa_will_need() API for paging in memory
Modified:
branches/lennart/src/pulsecore/core-util.c
branches/lennart/src/pulsecore/core-util.h
Modified: branches/lennart/src/pulsecore/core-util.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/=
core-util.c?rev=3D1745&root=3Dpulseaudio&r1=3D1744&r2=3D1745&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/pulsecore/core-util.c (original)
+++ branches/lennart/src/pulsecore/core-util.c Sun Sep 2 22:36:32 2007
@@ -54,6 +54,10 @@
#include <sys/capability.h>
#endif
=
+#ifdef HAVE_SYS_MMAN_H
+#include <sys/mman.h>
+#endif
+
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
@@ -1241,3 +1245,69 @@
pa_xfree(cwd);
return r;
}
+
+void *pa_will_need(const void *p, size_t l) {
+#ifdef RLIMIT_MEMLOCK
+ struct rlimit rlim;
+#endif
+ const void *a;
+ size_t size;
+ int r;
+ size_t bs;
+ =
+ pa_assert(p);
+ pa_assert(l > 0);
+
+ a =3D PA_PAGE_ALIGN_PTR(p);
+ size =3D (const uint8_t*) p + l - (const uint8_t*) a;
+ =
+ if ((r =3D posix_madvise((void*) a, size, POSIX_MADV_WILLNEED)) =3D=3D=
0) {
+ pa_log_debug("posix_madvise() worked fine!");
+ return (void*) p;
+ }
+ =
+ /* Most likely the memory was not mmap()ed from a file and thus
+ * madvise() didn't work, so let's misuse mlock() do page this
+ * stuff back into RAM. Yeah, let's fuck with the MM! It's so
+ * inviting, the man page of mlock() tells us: "All pages that
+ * contain a part of the specified address range are guaranteed to
+ * be resident in RAM when the call returns successfully." */
+ =
+#ifdef RLIMIT_MEMLOCK
+ pa_assert_se(getrlimit(RLIMIT_MEMLOCK, &rlim) =3D=3D 0);
+ =
+ if (rlim.rlim_cur < PA_PAGE_SIZE) {
+ pa_log_debug("posix_madvise() failed, resource limits don't allow =
mlock(), can't page in data: %s", pa_cstrerror(r));
+ return (void*) p;
+ }
+ =
+ bs =3D PA_PAGE_ALIGN(rlim.rlim_cur);
+#else
+ bs =3D PA_PAGE_SIZE*4;
+#endif
+ =
+ pa_log_debug("posix_madvise() failed, trying mlock(): %s", pa_cstrerro=
r(r));
+
+ while (size > 0 && bs > 0) {
+
+ if (bs > size)
+ bs =3D size;
+ =
+ if (mlock(a, bs) < 0) {
+ bs =3D PA_PAGE_ALIGN(bs / 2);
+ continue;
+ }
+
+ pa_assert_se(munlock(a, bs) =3D=3D 0);
+
+ a =3D (const uint8_t*) a + bs;
+ size -=3D bs;
+ }
+
+ if (bs <=3D 0)
+ pa_log_debug("mlock() failed too, giving up: %s", pa_cstrerror(err=
no));
+ else
+ pa_log_debug("mlock() worked fine!");
+
+ return (void*) p;
+}
Modified: branches/lennart/src/pulsecore/core-util.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/=
core-util.h?rev=3D1745&root=3Dpulseaudio&r1=3D1744&r2=3D1745&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/pulsecore/core-util.h (original)
+++ branches/lennart/src/pulsecore/core-util.h Sun Sep 2 22:36:32 2007
@@ -100,6 +100,8 @@
char *pa_getcwd(void);
char *pa_make_path_absolute(const char *p);
=
+void *pa_will_need(const void *p, size_t l);
+
static inline int pa_is_power_of_two(unsigned n) {
return !(n & (n - 1));
}
More information about the pulseaudio-commits
mailing list