]> git.sesse.net Git - fjl/commitdiff
Rename data_source to bit_source, and add a little comment.
authorSteinar H. Gunderson <sesse@debian.org>
Fri, 2 Jan 2009 16:19:18 +0000 (17:19 +0100)
committerSteinar H. Gunderson <sesse@debian.org>
Fri, 2 Jan 2009 16:19:18 +0000 (17:19 +0100)
input.c
input.h
input_test.c

diff --git a/input.c b/input.c
index b7f703992cc3baafea9086cde2f6076e25c7ec9e..603eb6da31bf42e924a0ab83b713d38b7bc36602 100644 (file)
--- a/input.c
+++ b/input.c
@@ -4,7 +4,7 @@
 
 #include "input.h"
 
-void init_data_source(struct data_source* source, input_func_t* input_func, void* userdata)
+void init_bit_source(struct bit_source* source, input_func_t* input_func, void* userdata)
 {
        memset(source, 0, sizeof(*source));
        source->bytes = (uint8_t*)malloc(bytereservoir_size);
@@ -12,7 +12,7 @@ void init_data_source(struct data_source* source, input_func_t* input_func, void
        source->userdata = userdata;
 }
 
-void possibly_refill_slow_path(struct data_source* source, unsigned num_bits)
+void possibly_refill_slow_path(struct bit_source* source, unsigned num_bits)
 {
        // First, make sure there's stuff in the byte reservoir if we can.
        assert(source->bytes_available <= bytereservoir_size);
diff --git a/input.h b/input.h
index 28c5f0b8530e820101063cef9c0ab83ea6aa0748..34765c25b113b35efff8c77ccf4d3f3e09ae30f0 100644 (file)
--- a/input.h
+++ b/input.h
@@ -21,11 +21,13 @@ static const unsigned bitreservoir_size = 8 * sizeof(bitreservoir_t);
 static const unsigned bitreservoir_fill_size = 8 * sizeof(bitreservoir_fill_t);
 static const unsigned bytereservoir_size = 4096;
 
-// A function to read bytes from some input source.
+// A function to read bytes from some input source. The bytes should be
+// already unstuffed (and thus without markers).
 // A return value of -1 indicates error, a return value of 0 indicates EOF.
 typedef ssize_t (input_func_t)(void*, uint8_t*, size_t);
 
-struct data_source {
+// A data source for efficient reading of bit-level data.
+struct bit_source {
        // Short-term bit reservoir; holds up to 64 bits. When it's empty,
        // it needs to get refilled from the medium-term bit reservoir.
        bitreservoir_t bits;
@@ -41,17 +43,17 @@ struct data_source {
        input_func_t* input_func;
        void* userdata;
 };
-       
-void init_data_source(struct data_source* source, input_func_t* input_func, void* userdata);
+
+void init_bit_source(struct bit_source* source, input_func_t* input_func, void* userdata);
 
 // Internal function. Do not use.
-void possibly_refill_slow_path(struct data_source* source, unsigned num_bits);
+void possibly_refill_slow_path(struct bit_source* source, unsigned num_bits);
 
 // Make sure there's at least NUM_BITS available in the short-term bit reservoir.
 // You usually want to call this before read_bits(). The reason it's separate
 // is that if you want two reads and you know the size of both, it's faster to
 // refill A+B, read A, read B than refill A, read A, refill B, read B.
-static inline void possibly_refill(struct data_source* source, unsigned num_bits)
+static inline void possibly_refill(struct bit_source* source, unsigned num_bits)
 {
        assert(num_bits <= bitreservoir_fill_size + 1);
 
@@ -76,7 +78,7 @@ static inline void possibly_refill(struct data_source* source, unsigned num_bits
        possibly_refill_slow_path(source, num_bits);
 }
 
-static inline unsigned read_bits(struct data_source* source, unsigned num_bits)
+static inline unsigned read_bits(struct bit_source* source, unsigned num_bits)
 {
        assert(source->bits_available >= num_bits);
        unsigned ret = (source->bits >> (bitreservoir_size - num_bits));
index 3f003d2bbc040fa3a435400350a37aadc55b602c..108b3b63a85ce2c47db7f7019d572e1dc22ed3a4 100644 (file)
@@ -39,8 +39,8 @@ void test_basic_reading()
        ud.bytes = bytes;
        ud.bytes_left = sizeof(bytes);
 
-       struct data_source source;
-       init_data_source(&source, custom_read, &ud);
+       struct bit_source source;
+       init_bit_source(&source, custom_read, &ud);
 
        for (int i = 0; i < sizeof(bytes) * 8 / 6; ++i) {
                possibly_refill(&source, 6);
@@ -59,8 +59,8 @@ void test_slow_source()
        ud.bytes = bytes;
        ud.bytes_left = sizeof(bytes);
 
-       struct data_source source;
-       init_data_source(&source, custom_read_slow, &ud);
+       struct bit_source source;
+       init_bit_source(&source, custom_read_slow, &ud);
 
        for (int i = 0; i < sizeof(bytes) * 8 / 6; ++i) {
                possibly_refill(&source, 6);
@@ -79,8 +79,8 @@ void test_variable_size()
        ud.bytes = bytes;
        ud.bytes_left = sizeof(bytes);
 
-       struct data_source source;
-       init_data_source(&source, custom_read, &ud);
+       struct bit_source source;
+       init_bit_source(&source, custom_read, &ud);
 
        {
                possibly_refill(&source, 4);