shl: move next_pow2() into shl_array
authorDavid Herrmann <dh.herrmann@gmail.com>
Wed, 23 Oct 2013 14:24:49 +0000 (16:24 +0200)
committerDavid Herrmann <dh.herrmann@gmail.com>
Wed, 23 Oct 2013 14:24:49 +0000 (16:24 +0200)
We use next_pow2() only in the shl_array implementation, so move it. We
want shl APIs to be self-contained so this removes the array->misc
dependency.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
src/shl_array.h
src/shl_misc.h

index a02a97b..f776807 100644 (file)
@@ -33,7 +33,6 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
-#include "shl_misc.h"
 
 struct shl_array {
        size_t element_size;
@@ -83,6 +82,22 @@ static inline void shl_array_free(struct shl_array *arr)
        free(arr);
 }
 
+/* Compute next higher power-of-2 of @v. Returns 4 in case v is 0. */
+static inline size_t shl_array_pow2(size_t v)
+{
+       size_t i;
+
+       if (!v)
+               return 4;
+
+       --v;
+
+       for (i = 1; i < 8 * sizeof(size_t); i *= 2)
+               v |= v >> i;
+
+       return ++v;
+}
+
 /* resize to length=size and zero out new array entries */
 static inline int shl_array_zresize(struct shl_array *arr, size_t size)
 {
@@ -93,7 +108,7 @@ static inline int shl_array_zresize(struct shl_array *arr, size_t size)
                return -EINVAL;
 
        if (size > arr->size) {
-               newsize = shl_next_pow2(size);
+               newsize = shl_array_pow2(size);
                tmp = realloc(arr->data, arr->element_size * newsize);
                if (!tmp)
                        return -ENOMEM;
index adaeecc..ac810d5 100644 (file)
 
 #define SHL_EXPORT __attribute__((visibility("default")))
 
-static inline unsigned long shl_next_pow2(unsigned long num)
-{
-       unsigned int i;
-
-       if (!num)
-               return 0;
-
-       --num;
-       for (i = 1; i < sizeof(unsigned long) * CHAR_BIT; i <<= 1)
-               num = num | num >> i;
-
-       return num + 1;
-}
-
 #endif /* SHL_MISC_H */