File: simple_bitarray.h

package info (click to toggle)
python-easysnmp 0.2.6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 692 kB
  • sloc: ansic: 3,735; python: 1,410; makefile: 161
file content (259 lines) | stat: -rw-r--r-- 6,098 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#ifndef SIMPLE_BITARRAY_H
#define SIMPLE_BITARRAY_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if (__STDC_VERSION__ < 199901L)
#define inline
#endif

/*
 * No weird architectures; we expect the following:
 *   - a byte to be 8 bits (i.e. CHAR_BIT == 8)
 *   - integral types do not have padding or reserved bits
 *     e.g. if sizeof(unsigned int) == 4 then UINT_MAX == 4294967295
 */
typedef unsigned int bitarray;

/*
 * This is not guaranteed to be portable since an integral type size
 * dosn't necessarily define the range. See warning above about
 * "no weird architectures."
 */
#define BITARRAY_LIMB_BITS (sizeof(bitarray) * CHAR_BIT)

/* macro to find the number of limbs to store num-bits */
#define BITARRAY_NUM_BITS_TO_LIMBS(num_bits) \
    (((num_bits) + BITARRAY_LIMB_BITS - 1) / BITARRAY_LIMB_BITS)

/* macro to find the number of limbs to store num-bits */
#define BITARRAY_NUM_BITS_TO_BUF_SIZE(num_bits) \
    ((BITARRAY_NUM_BITS_TO_LIMBS(num_bits) + 1) * sizeof(bitarray))

/* fetch the limb containing bit position n */
#define BITARRAY_LIMB(bitarray, nbit) \
    ((bitarray)[1 + ((nbit) / BITARRAY_LIMB_BITS)])

/* build mask to select bit in limb */
#define BITARRAY_LIMBBIT(nbit) \
    ((1UL << ((nbit) % BITARRAY_LIMB_BITS)))

/*
 * To declare a bitarray with automatic storage:
 * {
 *     BITARRAY_DECLARE(x, 1024); // 'x' will now be set to bitarray
 * }
 */
#define BITARRAY_DECLARE(name, nbits) \
    bitarray (name)[1 + BITARRAY_NUM_BITS_TO_LIMBS((nbits))] = { nbits }

static inline size_t bitarray_num_limbs(bitarray *ba)
{
    return BITARRAY_NUM_BITS_TO_LIMBS(ba[0]);
}

static inline size_t bitarray_num_bits(bitarray *ba)
{
    return ba[0];
}

static inline void bitarray_set_bit(bitarray *ba, size_t n)
{
    BITARRAY_LIMB(ba, n) |= BITARRAY_LIMBBIT(n);
}

static inline void bitarray_clear_bit(bitarray *ba, size_t n)
{
    BITARRAY_LIMB(ba, n) &= ~BITARRAY_LIMBBIT(n);
}

static inline void bitarray_change_bit(bitarray *ba, size_t n)
{
    BITARRAY_LIMB(ba, n) ^= BITARRAY_LIMBBIT(n);
}

static inline int bitarray_test_bit(const bitarray *ba, size_t n)
{
    return !!(BITARRAY_LIMB(ba, n) & BITARRAY_LIMBBIT(n));
}

static inline void bitarray_zero(bitarray *ba)
{
    size_t nbytes = sizeof(bitarray) * bitarray_num_limbs(ba);
    memset(&ba[1], 0, nbytes);
}

/* clear bits at position 0 to (nbits-1) */
static inline void bitarray_clear_bits(bitarray *ba, size_t nbits)
{
    if (ba[0] >= nbits)
    {
        /* clear the entire bitarray */
        bitarray_zero(ba);
    }
    else
    {
        size_t nbytes;

        /*
         * cases:
         *   - (1) nbits align on byte boundary
         *   - (2) nbits does not align on byte boundary,
         *         manually clear remaining bits
         */
        if (nbits % CHAR_BIT == 0)
        {
            nbytes = nbits * CHAR_BIT;
        }
        else
        {
            size_t remaining_bits = nbits % CHAR_BIT;
            size_t i;

            /* clear bits in the partial byte first */
            for (i = nbits; i > (nbits - remaining_bits); i--)
            {
                bitarray_clear_bit(ba, i - 1);
            }
        }

        memset(&ba[1], 0, nbytes);
    }
}


/*
 * Allocation functions
 */
static inline bitarray *bitarray_alloc(size_t nbits)
{
        bitarray *ba = NULL;

        size_t nlimbs = 1 + BITARRAY_NUM_BITS_TO_LIMBS(nbits);

        ba = malloc(sizeof(bitarray) * nlimbs);
        if (ba)
        {
            ba[0] = nbits;
        }

        return ba;
}

static inline bitarray *bitarray_calloc(size_t nbits)
{
        bitarray *ba = NULL;

        size_t nlimbs = 1 + BITARRAY_NUM_BITS_TO_LIMBS(nbits);

        ba = calloc(sizeof(bitarray), nlimbs);
        if (ba)
        {
            ba[0] = nbits;
        }

        return ba;
}

static inline void bitarray_free(bitarray *ba)
{
    if (ba)
    {
        free(ba);
    }
}

/*
 * Take in a raw buffer and corresponding size and return a pointer to
 * a newly initialised bitarray struct.
 *
 * buf_size is required to be minimum 2*sizeof(bitarray) in size.
 *
 * e.g.
 *     unsigned char p[1024]; // bitarray will be slightly <8192 bits.
 *     bitarray *ba = bitarray_buf_init(p, sizeof(p));
 */
static inline bitarray *bitarray_buf_init(void *buf, size_t buf_size)
{
    bitarray *ba = buf;
    size_t nlimbs;
    size_t nbits;

    if (!ba)
    {
        return NULL;
    }

    /* the buf needs to have the size of at least 1 limb */
    if (buf_size < sizeof(bitarray))
    {
        return NULL;
    }

    /*
     * using the free available space (after allocating the initial limb)
     * to determine how many limbs are available.
     */
    nlimbs = (buf_size - sizeof(bitarray)) / sizeof(bitarray);

    if (nlimbs < 1)
    {
        nbits = 0;
    }
    else
    {
        nbits = nlimbs * sizeof(bitarray) * CHAR_BIT;
    }

    /* reserve a limb for storing number of bits */
    ba[0] = nbits;
    bitarray_zero(ba);

    return (ba);
}

static inline void bitarray_print_base16(bitarray *ba)
{
    bitarray i;
    size_t num_limbs = bitarray_num_limbs(ba);

    printf("DEBUG numbits=%lu\n", (unsigned long) ba[0]);
    printf("DEBUG sizeof(limb)=%lu\n", sizeof(ba[0]));
    printf("DEBUG num_limbs=%lu\n", num_limbs);
    for (i = 0; i <= num_limbs; i++)
    {
        unsigned char c;
        unsigned int j;
        size_t limb_size = sizeof(ba);

        for (j = 0; j < limb_size; j++)
        {
            /* mask the byte we want to print in hex */
            unsigned long mask = (0xFFUL) << (j * CHAR_BIT);
            c = (unsigned char) ((ba[i] & mask) >> (j * CHAR_BIT));
            printf("%02x", c);
        }

        printf(" ");
    }

    printf("\n");
}


/* ignore unused function warnings */
static void wno_unused_function_simple_bitarray_h(void)
{
    (void) bitarray_num_bits(NULL);
    (void) bitarray_change_bit(NULL, 0);
    (void) bitarray_clear_bit(NULL, 0);
    (void) bitarray_clear_bits(NULL, 0);
    (void) bitarray_alloc(0);
    (void) bitarray_calloc(0);
    (void) bitarray_free(NULL);
    return;
}

#endif