From 5e376d47fce612a06664a048d5f49a5fed9e2b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 7 Mar 2013 18:15:20 +0100 Subject: [PATCH] Namespace the bitvec functions --- include/ucore/bitvec.h | 30 +++++++++++++++--------------- src/bitvec.c | 32 ++++++++++++++++---------------- test/test_bitvec.c | 42 +++++++++++++++++++++--------------------- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/include/ucore/bitvec.h b/include/ucore/bitvec.h index 7a88f30..af11d67 100644 --- a/include/ucore/bitvec.h +++ b/include/ucore/bitvec.h @@ -5,7 +5,7 @@ extern "C" { #endif -struct bitvec { +struct UCBitVec { //storage for the bits unsigned long *vec; //length of the above vec. (Not the number of bits !) @@ -15,14 +15,14 @@ struct bitvec { /** * Evaluates the length required for a bitvec to store nbit bits. */ -#define BITVEC_VEC_LEN(nbits) ((nbits/(sizeof(unsigned long)*CHAR_BIT)) + (nbits % (sizeof(unsigned long)*CHAR_BIT) == 0 ? 0 : 1)) +#define UC_BV_LEN(nbits) ((nbits/(sizeof(unsigned long)*CHAR_BIT)) + (nbits % (sizeof(unsigned long)*CHAR_BIT) == 0 ? 0 : 1)) /** * Use as : * unsigned long v[10]; - * struct bitvec v = BITVEC_STATIC_INIT(v); + * struct UCBitVec v = BITVEC_STATIC_INIT(v); */ -#define BITVEC_STATIC_INIT(vec_data)\ +#define UC_BV_STATIC_INIT(vec_data)\ {\ vec_data,\ sizeof vec_data/sizeof vec_data[0]} @@ -33,29 +33,29 @@ sizeof vec_data/sizeof vec_data[0]} * All bits are initially zero. * */ -struct bitvec *bitvec_new(size_t nbits); +struct UCBitVec *uc_bv_new(size_t nbits); /** free a bitvec previously allocated by bitvec_new */ -void bitvec_free(struct bitvec *v); +void uc_bv_free(struct UCBitVec *v); //Note that accessing a bit beyond the nbits originally initialized //for the given bitvec is undedefined /** Sets bit no. b */ -void set_bit(struct bitvec *v,int b); +void uc_bv_set_bit(struct UCBitVec *v,int b); /** Clears bit no. b*/ -void clear_bit(struct bitvec *v,int b); +void uc_bv_clr_bit(struct UCBitVec *v,int b); /** Gets the current value (0 or 1) of bit no. b*/ -int get_bit(const struct bitvec *v,int b); +int uc_bv_get_bit(const struct UCBitVec *v,int b); /** Sets all bits to zero */ -void clear_all(struct bitvec *v); +void uc_bv_clr_all(struct UCBitVec *v); /** Sets all bits to one */ -void set_all(struct bitvec *v); +void uc_bv_set_all(struct UCBitVec *v); /** Initialize bits from a array of ints, each array element maps to one bit. * The bits are initialized from the int array so zero maps to zero and non-zero maps to one. @@ -63,16 +63,16 @@ void set_all(struct bitvec *v); * int a[] = {0,1,1,1,0}; * set_bits_from_array(v,a,5); * */ -void set_bits_from_array(struct bitvec *v,char *array,size_t array_len); +void uc_bv_set_bits_from_array(struct UCBitVec *v,char *array,size_t array_len); // The _s ("secure") versions does boundary checking and assert() if they // try to access a bit out of bounds. -void set_bit_s(struct bitvec *v,int b); +void uc_bv_set_bit_s(struct UCBitVec *v,int b); -void clear_bit_s(struct bitvec *v,int b); +void uc_bv_clear_bit_s(struct UCBitVec *v,int b); -int get_bit_s(const struct bitvec *v,int b); +int uc_bv_get_bit_s(const struct UCBitVec *v,int b); #ifdef __cplusplus } diff --git a/src/bitvec.c b/src/bitvec.c index 5933597..fff5d31 100644 --- a/src/bitvec.c +++ b/src/bitvec.c @@ -22,25 +22,25 @@ static inline int bit_offset(int b) return b % (sizeof(unsigned long) * CHAR_BIT); } -struct bitvec *bitvec_new(size_t nbits) +struct UCBitVec *uc_bv_new(size_t nbits) { - struct bitvec *v = malloc(sizeof *v); + struct UCBitVec *v = malloc(sizeof *v); if (v == NULL) return NULL; - v->vec_len = BITVEC_VEC_LEN(nbits); + v->vec_len = UC_BV_LEN(nbits); v->vec = malloc(v->vec_len * sizeof(unsigned long)); if (v->vec == NULL) { free(v); return NULL; } - clear_all(v); + uc_bv_clr_all(v); return v; } -void bitvec_free(struct bitvec *v) +void uc_bv_free(struct UCBitVec *v) { if (v) { free(v->vec); @@ -49,22 +49,22 @@ void bitvec_free(struct bitvec *v) } -void clear_bit(struct bitvec *v,int b) +void uc_bv_clr_bit(struct UCBitVec *v,int b) { v->vec[bit_index(b)] &= ~(1UL << (bit_offset(b))); } -int get_bit(const struct bitvec *v,int b) +int uc_bv_get_bit(const struct UCBitVec *v,int b) { return !!(v->vec[bit_index(b)] & (1UL << bit_offset(b))); } -void set_bit(struct bitvec *v,int b) +void uc_bv_set_bit(struct UCBitVec *v,int b) { v->vec[bit_index(b)] |= 1UL << (bit_offset(b)); } -void set_bit_s(struct bitvec *v,int b) +void uc_bv_set_bit_s(struct UCBitVec *v,int b) { size_t idx = (size_t)bit_index(b); assert(b >= 0); @@ -74,7 +74,7 @@ void set_bit_s(struct bitvec *v,int b) v->vec[idx] |= 1UL << (bit_offset(b)); } -void clear_bit_s(struct bitvec *v,int b) +void uc_bv_clear_bit_s(struct UCBitVec *v,int b) { size_t idx = (size_t)bit_index(b); @@ -85,7 +85,7 @@ void clear_bit_s(struct bitvec *v,int b) v->vec[idx] &= ~(1UL << (bit_offset(b))); } -int get_bit_s(const struct bitvec *v,int b) +int uc_bv_get_bit_s(const struct UCBitVec *v,int b) { size_t idx = (size_t)bit_index(b); @@ -98,7 +98,7 @@ int get_bit_s(const struct bitvec *v,int b) return 0; } -void set_bits_from_array(struct bitvec *v,char *array,size_t array_len) +void uc_bv_set_bits_from_array(struct UCBitVec *v,char *array,size_t array_len) { size_t i; @@ -108,18 +108,18 @@ void set_bits_from_array(struct bitvec *v,char *array,size_t array_len) break; if (array[i]) - set_bit(v,i); + uc_bv_set_bit(v,i); else - clear_bit(v,i); + uc_bv_clr_bit(v,i); } } -void clear_all(struct bitvec *v) +void uc_bv_clr_all(struct UCBitVec *v) { memset(v->vec,0, v->vec_len * sizeof(unsigned long)); } -void set_all(struct bitvec *v) +void uc_bv_set_all(struct UCBitVec *v) { memset(v->vec,0xff,v->vec_len * sizeof(unsigned long)); } diff --git a/test/test_bitvec.c b/test/test_bitvec.c index 564de9a..629c241 100644 --- a/test/test_bitvec.c +++ b/test/test_bitvec.c @@ -4,67 +4,67 @@ START_TEST (test_bitvec_1) unsigned long s[3] = {0}; - struct bitvec v = BITVEC_STATIC_INIT(s); + struct UCBitVec v = UC_BV_STATIC_INIT(s); size_t i; for (i = 0; i < sizeof s * 8; i++) - fail_if(get_bit(&v, i), "bit %zu is not 0", i); + fail_if(uc_bv_get_bit(&v, i), "bit %zu is not 0", i); END_TEST START_TEST (test_bitvec_2) unsigned long s[3] = {0}; - struct bitvec v = BITVEC_STATIC_INIT(s); + struct UCBitVec v = UC_BV_STATIC_INIT(s); size_t i; for (i = 0; i < sizeof s * 8; i++) - set_bit(&v, i); + uc_bv_set_bit(&v, i); for (i = 0; i < sizeof s * 8; i++) - fail_if(get_bit(&v, i) != 1 , "bit %zu is not 1", i); + fail_if(uc_bv_get_bit(&v, i) != 1 , "bit %zu is not 1", i); END_TEST START_TEST (test_bitvec_clearbit) unsigned long s[3] = {-1, -1, -1}; - struct bitvec v = BITVEC_STATIC_INIT(s); + struct UCBitVec v = UC_BV_STATIC_INIT(s); size_t i; for (i = 0; i < sizeof(unsigned long) * 8; i++) - fail_if(get_bit(&v, i) != 1 , "bit %zu is not 1", 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++) - clear_bit(&v, i); + uc_bv_clr_bit(&v, i); for (i = 0; i < sizeof(unsigned long) * 8; i++) - fail_if(get_bit(&v, i) != 0 , "bit %zu is not 0", i); + fail_if(uc_bv_get_bit(&v, i) != 0 , "bit %zu is not 0", i); END_TEST START_TEST (test_bitvec_set_bits_from_array) - struct bitvec *v = bitvec_new(5); + struct UCBitVec *v = uc_bv_new(5); char a[] = {1, 0,1 ,0, 1}; - set_bits_from_array(v,a , 5); - fail_if(get_bit(v, 0) != 1 , "bit 0 is wrong"); - fail_if(get_bit(v, 1) != 0 , "bit 1 is wrong"); - fail_if(get_bit(v, 2) != 1 , "bit 2 is wrong"); - fail_if(get_bit(v, 3) != 0 , "bit 3 is wrong"); - fail_if(get_bit(v, 4) != 1 , "bit 4 is wrong"); + uc_bv_set_bits_from_array(v,a , 5); + fail_if(uc_bv_get_bit(v, 0) != 1 , "bit 0 is wrong"); + fail_if(uc_bv_get_bit(v, 1) != 0 , "bit 1 is wrong"); + fail_if(uc_bv_get_bit(v, 2) != 1 , "bit 2 is wrong"); + fail_if(uc_bv_get_bit(v, 3) != 0 , "bit 3 is wrong"); + fail_if(uc_bv_get_bit(v, 4) != 1 , "bit 4 is wrong"); END_TEST START_TEST (test_bitvec_setall_clearall) - struct bitvec *v = bitvec_new(511); + struct UCBitVec *v = uc_bv_new(511); size_t i; - set_all(v); + uc_bv_set_all(v); for (i = 0; i < 511; i++) - fail_if(get_bit(v, i) != 1 , "bit %zu is not 1", i); + fail_if(uc_bv_get_bit(v, i) != 1 , "bit %zu is not 1", i); - clear_all(v); + uc_bv_clr_all(v); for (i = 0; i < sizeof(unsigned long) * 8; i++) - fail_if(get_bit(v, i) != 0 , "bit %zu is not 0", i); + fail_if(uc_bv_get_bit(v, i) != 0 , "bit %zu is not 0", i); END_TEST