Last active
August 29, 2015 14:23
Revisions
-
dpc revised this gist
Jun 19, 2015 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ #include <stddef.h> #include <stdint.h> #include <stdbool.h> -
Dawid Ciężarkiewicz revised this gist
Jun 18, 2015 . 3 changed files with 113 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ cbuf: cbuf.h cbuf.c gcc -O3 -std=gnu99 cbuf.c -o cbuf This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ #include <assert.h> #include <stdlib.h> #include <stdio.h> #include "cbuf.h" uint8_t buf[16]; int main() { struct cbuf cbuf; size_t cur_len = 0; uint8_t put_val = 0; uint8_t get_val = 0; cbuf_init(&cbuf, buf, sizeof buf); while (true) { if (cur_len == 0) { assert(cbuf_is_empty(&cbuf)); } if (cur_len == sizeof buf) { assert(cbuf_is_full(&cbuf)); } if (rand() & 1) { if (!cbuf_is_empty(&cbuf)) { uint8_t val = cbuf_get(&cbuf); assert(val == get_val++); cur_len--; printf("%u len: %u\n", (unsigned)val, (unsigned) cur_len); } } else { if (!cbuf_is_full(&cbuf)) { cbuf_put(&cbuf, put_val++); cur_len++; } } } return 0; } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ #include <stddef.h> #include <stdint.h> #include <stdbool.h> #include <string.h> /* TODO: Fix on 64 architecture */ #define CBUF_DATA_BIT (1 << 31) /** * Char circular buffer */ struct cbuf { uint8_t *data; size_t size; size_t start; size_t end; }; static inline bool cbuf_is_empty(struct cbuf* cbuf) { return cbuf->start == cbuf->end; } static inline bool cbuf_is_full(struct cbuf* cbuf) { return (cbuf->start ^ cbuf->end) == CBUF_DATA_BIT; } static inline uint8_t cbuf_get(struct cbuf* cbuf) { uint8_t ret = cbuf->data[cbuf->start & ~CBUF_DATA_BIT]; cbuf->start++; if ((cbuf->start & ~CBUF_DATA_BIT) >= cbuf->size) { cbuf->start -= cbuf->size; cbuf->start ^= CBUF_DATA_BIT; } return ret; } static inline void cbuf_put(struct cbuf* cbuf, uint8_t byte) { cbuf->data[cbuf->end & ~CBUF_DATA_BIT] = byte; cbuf->end++; if ((cbuf->end & ~CBUF_DATA_BIT) >= cbuf->size) { cbuf->end -= cbuf->size; cbuf->end ^= CBUF_DATA_BIT; } } static inline void cbuf_init(struct cbuf* cbuf, uint8_t* data, size_t size) { memset(cbuf, 0, sizeof(*cbuf)); cbuf->data = data; cbuf->size = size; } -
dpc created this gist
Jun 18, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ TBD