]> git.sesse.net Git - vlc/blobdiff - src/misc/cpu.c
programs is a string, which is a string, which is not a list
[vlc] / src / misc / cpu.c
index 90228681f7d2c93dd95221ba37b31d0c1c7b0598..83b95603b9982c4ce4b995f1e4d6a0c2e2c05e33 100644 (file)
 #else
 #include <errno.h>
 #endif
+#include <assert.h>
 
 #include "libvlc.h"
 
-#if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
+#if defined(__APPLE__)
 #include <sys/sysctl.h>
 #endif
 
@@ -335,7 +336,7 @@ unsigned vlc_GetCPUCount(void)
         system_mask >>= 1;
     }
     return count;
-#elif HAVE_SCHED_GETAFFINITY
+#elif defined(HAVE_SCHED_GETAFFINITY)
     cpu_set_t cpu;
     CPU_ZERO(&cpu);
     if (sched_getaffinity(0, sizeof(cpu), &cpu) < 0)
@@ -405,3 +406,32 @@ void *vlc_memset (void *tgt, int c, size_t n)
 {
     return pf_vlc_memset (tgt, c, n);
 }
+
+/**
+ * Returned an aligned pointer on newly allocated memory.
+ * \param alignment must be a power of 2 and a multiple of sizeof(void*)
+ * \param size is the size of the usable memory returned.
+ *
+ * It must not be freed directly, *base must.
+ */
+void *vlc_memalign(void **base, size_t alignment, size_t size)
+{
+    assert(alignment >= sizeof(void*));
+    for (size_t t = alignment; t > 1; t >>= 1)
+        assert((t&1) == 0);
+#if defined(HAVE_POSIX_MEMALIGN)
+    if (posix_memalign(base, alignment, size)) {
+        *base = NULL;
+        return NULL;
+    }
+    return *base;
+#elif defined(HAVE_MEMALIGN)
+    return *base = memalign(alignment, size);
+#else
+    unsigned char *p = *base = malloc(size + alignment - 1);
+    if (!p)
+        return NULL;
+    return (void*)((uintptr_t)(p + alignment - 1) & ~(alignment - 1));
+#endif
+}
+