File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / lighttpd / src / bitset.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Oct 14 10:32:48 2013 UTC (10 years, 8 months ago) by misho
Branches: lighttpd, MAIN
CVS tags: v1_4_33, HEAD
1.4.33

    1: #include "buffer.h"
    2: #include "bitset.h"
    3: 
    4: #include <limits.h>
    5: #include <stdlib.h>
    6: #include <string.h>
    7: #include <stdio.h>
    8: #include <assert.h>
    9: 
   10: #define BITSET_BITS \
   11: 	( CHAR_BIT * sizeof(size_t) )
   12: 
   13: #define BITSET_MASK(pos) \
   14: 	( ((size_t)1) << ((pos) % BITSET_BITS) )
   15: 
   16: #define BITSET_WORD(set, pos) \
   17: 	( (set)->bits[(pos) / BITSET_BITS] )
   18: 
   19: #define BITSET_USED(nbits) \
   20: 	( ((nbits) + (BITSET_BITS - 1)) / BITSET_BITS )
   21: 
   22: bitset *bitset_init(size_t nbits) {
   23: 	bitset *set;
   24: 
   25: 	set = malloc(sizeof(*set));
   26: 	assert(set);
   27: 
   28: 	set->bits = calloc(BITSET_USED(nbits), sizeof(*set->bits));
   29: 	set->nbits = nbits;
   30: 
   31: 	assert(set->bits);
   32: 
   33: 	return set;
   34: }
   35: 
   36: void bitset_reset(bitset *set) {
   37: 	memset(set->bits, 0, BITSET_USED(set->nbits) * sizeof(*set->bits));
   38: }
   39: 
   40: void bitset_free(bitset *set) {
   41: 	free(set->bits);
   42: 	free(set);
   43: }
   44: 
   45: void bitset_clear_bit(bitset *set, size_t pos) {
   46: 	if (pos >= set->nbits) {
   47: 	    SEGFAULT();
   48: 	}
   49: 
   50: 	BITSET_WORD(set, pos) &= ~BITSET_MASK(pos);
   51: }
   52: 
   53: void bitset_set_bit(bitset *set, size_t pos) {
   54: 	if (pos >= set->nbits) {
   55: 	    SEGFAULT();
   56: 	}
   57: 
   58: 	BITSET_WORD(set, pos) |= BITSET_MASK(pos);
   59: }
   60: 
   61: int bitset_test_bit(bitset *set, size_t pos) {
   62: 	if (pos >= set->nbits) {
   63: 	    SEGFAULT();
   64: 	}
   65: 
   66: 	return (BITSET_WORD(set, pos) & BITSET_MASK(pos)) != 0;
   67: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>