diff options
-rw-r--r-- | cbuf.c | 11 | ||||
-rw-r--r-- | cbuf.h | 20 |
2 files changed, 17 insertions, 14 deletions
@@ -27,16 +27,17 @@ */ #include <assert.h> +#include <stddef.h> #include "cbuf.h" #include "error.h" #include "macros.h" /* Negative close to 0 so that an overflow occurs early */ -#define CBUF_INIT_INDEX ((unsigned long)-500) +#define CBUF_INIT_INDEX ((size_t)-500) void -cbuf_init(struct cbuf *cbuf, char *buf, unsigned long capacity) +cbuf_init(struct cbuf *cbuf, char *buf, size_t capacity) { assert(ISP2(capacity)); @@ -46,8 +47,8 @@ cbuf_init(struct cbuf *cbuf, char *buf, unsigned long capacity) cbuf->end = cbuf->start; } -static unsigned long -cbuf_index(const struct cbuf *cbuf, unsigned long abs_index) +static size_t +cbuf_index(const struct cbuf *cbuf, size_t abs_index) { return abs_index & (cbuf->capacity - 1); } @@ -77,7 +78,7 @@ cbuf_pop(struct cbuf *cbuf, char *bytep) } int -cbuf_read(const struct cbuf *cbuf, unsigned long index, char *bytep) +cbuf_read(const struct cbuf *cbuf, size_t index, char *bytep) { /* Mind integer overflows */ if ((cbuf->end - index - 1) >= cbuf_size(cbuf)) { @@ -31,6 +31,8 @@ #ifndef _CBUF_H #define _CBUF_H +#include <stddef.h> + /* * Circular buffer descriptor. * @@ -39,30 +41,30 @@ */ struct cbuf { char *buf; - unsigned long capacity; - unsigned long start; - unsigned long end; + size_t capacity; + size_t start; + size_t end; }; -static inline unsigned long +static inline size_t cbuf_capacity(const struct cbuf *cbuf) { return cbuf->capacity; } -static inline unsigned long +static inline size_t cbuf_start(const struct cbuf *cbuf) { return cbuf->start; } -static inline unsigned long +static inline size_t cbuf_end(const struct cbuf *cbuf) { return cbuf->end; } -static inline unsigned long +static inline size_t cbuf_size(const struct cbuf *cbuf) { return cbuf->end - cbuf->start; @@ -80,7 +82,7 @@ cbuf_clear(struct cbuf *cbuf) * The descriptor is set to use the given buffer for storage. Capacity * must be a power-of-two. */ -void cbuf_init(struct cbuf *cbuf, char *buf, unsigned long capacity); +void cbuf_init(struct cbuf *cbuf, char *buf, size_t capacity); /* * Append a byte to a circular buffer. @@ -106,6 +108,6 @@ int cbuf_pop(struct cbuf *cbuf, char *bytep); * Otherwise the byte is stored at the bytep address and 0 is returned. * The buffer isn't changed by this operation. */ -int cbuf_read(const struct cbuf *cbuf, unsigned long index, char *bytep); +int cbuf_read(const struct cbuf *cbuf, size_t index, char *bytep); #endif /* _CBUF_H */ |