diff options
author | xengineering <mail2xengineering@protonmail.com> | 2020-10-25 16:52:57 +0100 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2020-10-25 16:52:57 +0100 |
commit | 56c268e88a87f708de1824d87fbf7b824d4c1c2a (patch) | |
tree | 0a4ab861b2f03c2fde1de8d7a3b61d2603b4d503 /libraries/fifo.c | |
parent | 58d3787ca563b7d6a2cb4e0a1ab7fdde67fd5557 (diff) | |
download | stm32f103c8-examples-56c268e88a87f708de1824d87fbf7b824d4c1c2a.tar stm32f103c8-examples-56c268e88a87f708de1824d87fbf7b824d4c1c2a.tar.zst stm32f103c8-examples-56c268e88a87f708de1824d87fbf7b824d4c1c2a.zip |
Remove any Heap Allocation
Diffstat (limited to 'libraries/fifo.c')
-rw-r--r-- | libraries/fifo.c | 61 |
1 files changed, 28 insertions, 33 deletions
diff --git a/libraries/fifo.c b/libraries/fifo.c index faa9fc4..8c5ad8b 100644 --- a/libraries/fifo.c +++ b/libraries/fifo.c @@ -3,64 +3,59 @@ #include "fifo.h" -void fifo_init(FIFO *fifo, uint16_t bufferlength) +void fifo_init(FIFO *fifo, uint8_t *buffer, uint16_t bufferlength) { - fifo->buffer_ptr = calloc(bufferlength, sizeof(uint8_t)); - fifo->read_index = 0; - fifo->write_index = 0; - fifo->bufferlength = bufferlength; -} - -void fifo_deinit(FIFO *fifo) -{ - free(fifo->buffer_ptr); + fifo->buffer_ptr = buffer; + fifo->bufferlength = bufferlength; + fifo->read_index = 0; + fifo->write_index = 0; } uint16_t fifo_get_buffersize(FIFO *fifo) { - return fifo->bufferlength; + return fifo->bufferlength; } uint16_t fifo_bytes_available(FIFO *fifo) { - if (fifo->write_index > fifo->read_index){ - return (fifo->write_index - fifo->read_index); - } - else if (fifo->write_index < fifo->read_index){ - return (fifo_get_buffersize(fifo) - (fifo->read_index - fifo->write_index)); - } - else { // read_index == write_index - return 0; - } + if (fifo->write_index > fifo->read_index){ + return (fifo->write_index - fifo->read_index); + } + else if (fifo->write_index < fifo->read_index){ + return (fifo_get_buffersize(fifo) - (fifo->read_index - fifo->write_index)); + } + else { // read_index == write_index + return 0; + } } //uint16_t fifo_lines_available(FIFO *fifo){} void fifo_move_read_index(FIFO *fifo) { - fifo->read_index++; - fifo->read_index %= fifo_get_buffersize(fifo); + fifo->read_index++; + fifo->read_index %= fifo_get_buffersize(fifo); } void fifo_move_write_index(FIFO *fifo) { - fifo->write_index++; - fifo->write_index %= fifo_get_buffersize(fifo); + fifo->write_index++; + fifo->write_index %= fifo_get_buffersize(fifo); } void fifo_get_byte(FIFO *fifo, uint8_t *data_sink) { - if(fifo_bytes_available(fifo)){ - *data_sink = fifo->buffer_ptr[fifo->read_index]; - fifo_move_read_index(fifo); - } + if(fifo_bytes_available(fifo)){ + *data_sink = fifo->buffer_ptr[fifo->read_index]; + fifo_move_read_index(fifo); + } } void fifo_put_byte(FIFO *fifo, uint8_t *data_source) { - fifo->buffer_ptr[fifo->write_index] = *data_source; - fifo_move_write_index(fifo); - if(fifo->read_index == fifo->write_index){ // buffer overflow case - fifo_move_read_index(fifo); - } + fifo->buffer_ptr[fifo->write_index] = *data_source; + fifo_move_write_index(fifo); + if(fifo->read_index == fifo->write_index){ // buffer overflow case + fifo_move_read_index(fifo); + } } |