diff --git a/include/ucore/bitvec.h b/include/ucore/bitvec.h index 5eec670..06085b8 100644 --- a/include/ucore/bitvec.h +++ b/include/ucore/bitvec.h @@ -1,13 +1,18 @@ #ifndef UC_BITEVEC_H_ #define UC_BITEVEC_H_ +#include + #ifdef __cplusplus extern "C" { #endif +/** Backing type of the bit vector*/ +typedef unsigned long uc_bv_integer; + struct UCBitVec { //storage for the bits - unsigned long *vec; + uc_bv_integer *vec; //length in bytes of the above vec. size_t vec_len; }; @@ -15,11 +20,11 @@ struct UCBitVec { /** * Evaluates the length required for a bitvec to store nbit bits. */ -#define UC_BV_LEN(nbits) ((nbits/(sizeof(unsigned long)*CHAR_BIT)) + (nbits % (sizeof(unsigned long)*CHAR_BIT) == 0 ? 0 : 1)) +#define UC_BV_LEN(nbits) ((nbits/(sizeof(uc_bv_integer)*CHAR_BIT)) + (nbits % (sizeof(uc_bv_integer)*CHAR_BIT) == 0 ? 0 : 1)) /** * Use as : - * unsigned long v[10]; + * uc_bv_integer v[10]; * struct UCBitVec v = BITVEC_STATIC_INIT(v); */ #define UC_BV_STATIC_INIT(vec_data)\ diff --git a/src/bitvec.c b/src/bitvec.c index abab68b..edb6cc3 100644 --- a/src/bitvec.c +++ b/src/bitvec.c @@ -1,7 +1,6 @@ #include #include #include -#include #include "ucore/bitvec.h" #ifdef __GNUC__ @@ -14,12 +13,12 @@ static inline int bit_index(int b) { - return b / (sizeof(unsigned long) * CHAR_BIT); + return b / (sizeof(uc_bv_integer) * CHAR_BIT); } static inline int bit_offset(int b) { - return b % (sizeof(unsigned long) * CHAR_BIT); + return b % (sizeof(uc_bv_integer) * CHAR_BIT); } struct UCBitVec *uc_bv_new(size_t nbits) @@ -29,7 +28,7 @@ struct UCBitVec *uc_bv_new(size_t nbits) if (v == NULL) return NULL; v->vec_len = UC_BV_LEN(nbits); - v->vec = malloc(v->vec_len * sizeof(unsigned long)); + v->vec = malloc(v->vec_len * sizeof(uc_bv_integer)); if (v->vec == NULL) { free(v); return NULL; @@ -81,11 +80,11 @@ void uc_bv_set_bits_from_array(struct UCBitVec *v,char *array,size_t array_len) void uc_bv_clr_all(struct UCBitVec *v) { - memset(v->vec,0, v->vec_len * sizeof(unsigned long)); + memset(v->vec,0, v->vec_len * sizeof(uc_bv_integer)); } void uc_bv_set_all(struct UCBitVec *v) { - memset(v->vec,0xff,v->vec_len * sizeof(unsigned long)); + memset(v->vec,0xff,v->vec_len * sizeof(uc_bv_integer)); } diff --git a/test/test_bitvec.c b/test/test_bitvec.c index 3805ebb..d1a0a6b 100644 --- a/test/test_bitvec.c +++ b/test/test_bitvec.c @@ -3,7 +3,7 @@ START_TEST (test_bitvec_1) - unsigned long s[3] = {0}; + uc_bv_integer s[3] = {0}; struct UCBitVec v = UC_BV_STATIC_INIT(s); size_t i; @@ -12,7 +12,7 @@ START_TEST (test_bitvec_1) END_TEST START_TEST (test_bitvec_2) - unsigned long s[3] = {0}; + uc_bv_integer s[3] = {0}; struct UCBitVec v = UC_BV_STATIC_INIT(s); size_t i; @@ -24,17 +24,17 @@ START_TEST (test_bitvec_2) END_TEST START_TEST (test_bitvec_clearbit) - unsigned long s[3] = {-1, -1, -1}; + uc_bv_integer s[3] = {-1, -1, -1}; struct UCBitVec v = UC_BV_STATIC_INIT(s); size_t i; - for (i = 0; i < sizeof(unsigned long) * 8; i++) + for (i = 0; i < sizeof(uc_bv_integer) * 8; i++) fail_if(uc_bv_get_bit(&v, i) != 1 , "bit %zu is not 1", i); - for (i = 0; i < sizeof(unsigned long) * 8; i++) + for (i = 0; i < sizeof(uc_bv_integer) * 8; i++) uc_bv_clr_bit(&v, i); - for (i = 0; i < sizeof(unsigned long) * 8; i++) + for (i = 0; i < sizeof(uc_bv_integer) * 8; i++) fail_if(uc_bv_get_bit(&v, i) != 0 , "bit %zu is not 0", i); END_TEST @@ -63,7 +63,7 @@ START_TEST (test_bitvec_setall_clearall) uc_bv_clr_all(v); - for (i = 0; i < sizeof(unsigned long) * 8; i++) + for (i = 0; i < sizeof(uc_bv_integer) * 8; i++) fail_if(uc_bv_get_bit(v, i) != 0 , "bit %zu is not 0", i); uc_bv_free(v);