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>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
-#include "shl_misc.h"
struct shl_array {
size_t element_size;
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)
{
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;
#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 */