I'm using the Atmel AVR ATmega328p chip, and I'm trying to create multiple ring buffers, with varying lengths, using the FifoBuffer class, in file fifobuffer.h, which I created...
class FifoBuffer {
private:
uint8_t buf_head; // Head position, where next character is to be added
uint8_t buf_tail; // Tail position, when next character is to be removed
uint8_t buf_size; // Size of buffer in number of characters
uint8_t *buffer; // Declare buffer pointer
public:
FifoBuffer (uint8_t); // Constructor declaration
uint8_t get () { return buffer[2]; }
void put (uint8_t character) { }
void empty () { }
uint8_t count () { return 10; }
uint8_t head () { return buf_head; }
uint8_t tail () { return buf_tail; }
uint8_t size () { return buf_size; }
};
// Constructor
//
FifoBuffer::FifoBuffer (uint8_t buffer_length) {
buf_head = 0;
buf_tail = 0;
buf_size = buffer_length;
buffer = new uint8_t [buffer_length];
buffer[1] = 20; // Test
buffer[2] = 16; // Test
buffer[3] = 32; // Test
}
In my main.cpp file I have...
...
void *operator new(size_t s) { return malloc(s); }
void *operator new[](size_t s) { return malloc(s); }
void operator delete(void *m) { free(m); }
void operator delete[](void *m) { free(m); }
#include "fifobuffer.h"
...
FifoBuffer tty_rx_buf(64);
FifoBuffer tty_tx_buf(64);
uint8_t ttt = tty_rx_buf.get();
show_8_bits (ttt, 'n');
ttt = tty_rx_buf.size();
show_8_bits (ttt, 'n');
...
Now everything complies, and the .get()
returns 16, and .size()
returns 64, which I would expect.
But I observe that the size of the program (Program Memory Usage: 1194 bytes, Data Memory Usage: 11 bytes) does not change, whether I select a size of 64 or 10 for the ring buffer constructor calls. When I make only 1 ring buffer constructor call, memory use does change, to 1178 bytes, and 11 bytes, respectively.
I'm worried that the buffer = new uint8_t [buffer_length]
line is not really allocating buffer_length bytes.
Is my concern warranted? Is there a better way to do this? Yes I am new at this.