From 1fec6e5f77e9d5ef73fd88dee8f5587b7c76f5ed Mon Sep 17 00:00:00 2001 From: Andy CA6JAU Date: Mon, 16 Jul 2018 16:04:49 -0400 Subject: [PATCH] Optimizing memory for bit ring buffer --- BitRB.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/BitRB.cpp b/BitRB.cpp index 0aa8ae5..0260d10 100644 --- a/BitRB.cpp +++ b/BitRB.cpp @@ -1,7 +1,7 @@ /* TX fifo control - Copyright (C) KI6ZUM 2015 Copyright (C) 2015,2016 by Jonathan Naylor G4KLX -Copyright (C) 2016,2017 by Andy Uribe CA6JAU +Copyright (C) 2016,2017,2018 by Andy Uribe CA6JAU This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -21,6 +21,11 @@ Boston, MA 02110-1301, USA. #include "BitRB.h" +const uint8_t BIT_MASK_TABLE[] = {0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U}; + +#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7]) +#define READ_BIT1(p,i) ((p[(i)>>3] & BIT_MASK_TABLE[(i)&7]) >> (7 - ((i)&7))) + CBitRB::CBitRB(uint16_t length) : m_length(length), m_bits(NULL), @@ -30,7 +35,7 @@ m_tail(0U), m_full(false), m_overflow(false) { - m_bits = new uint8_t[length]; + m_bits = new uint8_t[length / 8U]; m_control = new uint8_t[length]; } @@ -68,7 +73,7 @@ bool CBitRB::put(uint8_t bit, uint8_t control) return false; } - m_bits[m_head] = bit; + WRITE_BIT1(m_bits, m_head, bit); m_control[m_head] = control; m_head++; @@ -86,7 +91,7 @@ bool CBitRB::get(uint8_t& bit, uint8_t& control) if (m_head == m_tail && !m_full) return false; - bit = m_bits[m_tail]; + bit = READ_BIT1(m_bits, m_tail); control = m_control[m_tail]; m_full = false;