Skip to content

Commit

Permalink
shl: move next_pow2() into shl_array
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
David Herrmann committed Oct 23, 2013
1 parent 3d8698e commit c269320
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
19 changes: 17 additions & 2 deletions src/shl_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "shl_misc.h"

struct shl_array {
size_t element_size;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
14 changes: 0 additions & 14 deletions src/shl_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,4 @@

#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 */

0 comments on commit c269320

Please sign in to comment.