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
|
/** @file bitbuffer.h
*
* Implementation of a circular buffer of bits.
*
* @author Daniel Silverstone
*/
#include <stdlib.h>
#include "bitbuffer.h"
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#endif
struct bitbuffer_s {
int size;
int firstused;
int nextfree;
int bitsused;
BitField bits[0];
};
#define BYTE_FOR_BIT(n) (n>>3)
#define SHIFT_FOR_BIT(n) (n&7)
BitBuffer
bitbuffer_new(const int size)
{
int bytesize = ((size + 7) & ~7) >> 3;
BitBuffer ret = (BitBuffer)(malloc(sizeof(struct bitbuffer_s) + bytesize));
ret->size = size;
ret->firstused = 0;
ret->nextfree = 0;
ret->bitsused = 0;
return ret;
}
void
bitbuffer_free(BitBuffer buf)
{
free(buf);
}
int
bitbuffer_free_space(const BitBuffer buf)
{
return buf->size - buf->bitsused;
}
int
bitbuffer_available_bits(const BitBuffer buf)
{
return buf->bitsused;
}
int
bitbuffer_add_bits(BitBuffer buf, const BitField bits, int count)
{
int bits_to_add = MIN(MIN(8, count), bitbuffer_free_space(buf));
int i;
for (i = 0; i < bits_to_add; ++i) {
BitField subfield = buf->bits[BYTE_FOR_BIT(buf->nextfree)];
if (bits & (1<<SHIFT_FOR_BIT(i)))
subfield |= (1<<SHIFT_FOR_BIT(buf->nextfree));
else
subfield &= ~(1<<SHIFT_FOR_BIT(buf->nextfree));
buf->bits[BYTE_FOR_BIT(buf->nextfree++)] = subfield;
if (buf->nextfree == buf->size)
buf->nextfree = 0;
}
buf->bitsused += bits_to_add;
return bits_to_add;
}
int
bitbuffer_extract_bits(BitBuffer buf, BitField *bits, int count)
{
int bits_to_extract = MIN(MIN(8, count), bitbuffer_available_bits(buf));
int i;
for (i = 0; i < bits_to_extract; ++i) {
BitField subfield = buf->bits[BYTE_FOR_BIT(buf->firstused)];
if (subfield & (1<<SHIFT_FOR_BIT(buf->firstused++)))
*bits |= (1<<SHIFT_FOR_BIT(i));
else
*bits &= ~(1<<SHIFT_FOR_BIT(i));
if (buf->firstused == buf->size)
buf->firstused = 0;
}
buf->bitsused -= bits_to_extract;
return bits_to_extract;
}
#ifdef TEST
#include <stdio.h>
void
xassert(const char* msg, int val, int line, BitBuffer buf, BitField bits)
{
if (val == 0) {
fprintf(stdout, "FAILURE on line %d of condition %s\n", line, msg);
fprintf(stdout, "Buffer at %p: size %d firstused %d nextfree %d\n",
buf, buf->size, buf->firstused, buf->nextfree);
fprintf(stdout, "Bitfield is %02x\n", bits);
exit(1);
}
}
#ifdef assert
#undef assert
#endif
#define assert(X) xassert(#X, X, __LINE__, buf, bits)
int main(int argc, char** argv)
{
BitBuffer buf;
BitField bits = 0xA5;
buf = bitbuffer_new(32);
assert(buf != NULL);
assert(bitbuffer_free_space(buf) == 32);
assert(bitbuffer_available_bits(buf) == 0);
assert(bitbuffer_add_bits(buf, bits, 8) == 8);
assert(bitbuffer_add_bits(buf, bits, 8) == 8);
assert(bitbuffer_add_bits(buf, bits, 8) == 8);
assert(bitbuffer_add_bits(buf, bits, 8) == 8);
assert(bitbuffer_add_bits(buf, bits, 8) == 0);
assert(bitbuffer_free_space(buf) == 0);
assert(bitbuffer_available_bits(buf) == 32);
assert(bitbuffer_extract_bits(buf, &bits, 8) == 8);
assert(bits == 0xA5);
assert(bitbuffer_extract_bits(buf, &bits, 8) == 8);
assert(bits == 0xA5);
assert(bitbuffer_extract_bits(buf, &bits, 8) == 8);
assert(bits == 0xA5);
assert(bitbuffer_extract_bits(buf, &bits, 8) == 8);
assert(bits == 0xA5);
assert(bitbuffer_extract_bits(buf, &bits, 8) == 0);
bits = 0x96;
assert(bitbuffer_add_bits(buf, bits, 8) == 8);
bits = 0xF;
assert(bitbuffer_add_bits(buf, bits, 4) == 4);
bits = 0x96;
assert(bitbuffer_add_bits(buf, bits, 8) == 8);
bits = 0xF;
assert(bitbuffer_add_bits(buf, bits, 4) == 4);
assert(bitbuffer_extract_bits(buf, &bits, 8) == 8);
assert(bits == 0x96);
assert(bitbuffer_extract_bits(buf, &bits, 8) == 8);
assert(bits == 0x6F);
assert(bitbuffer_extract_bits(buf, &bits, 4) == 4);
assert((bits & 0xf) == 0x9);
assert(bitbuffer_extract_bits(buf, &bits, 8) == 4);
assert((bits & 0xf) == 0xF);
return 0;
}
#endif
|