Skip to content

Commit

Permalink
Refactor: Change buffer sizes from uint16_t to size_t for larger capa…
Browse files Browse the repository at this point in the history
…city
  • Loading branch information
neithanmo committed Sep 16, 2024
1 parent 50df5b5 commit 04a28c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions include/buffering.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ extern "C" {

typedef struct {
uint8_t *data;
uint16_t size;
uint16_t pos;
size_t size;
size_t pos;
uint8_t in_use: 1;
} buffer_state_t;

Expand All @@ -37,9 +37,9 @@ typedef struct {
/// \param flash_buffer
/// \param flash_buffer_size
void buffering_init(uint8_t *ram_buffer,
uint16_t ram_buffer_size,
size_t ram_buffer_size,
uint8_t *flash_buffer,
uint16_t flash_buffer_size);
size_t flash_buffer_size);

/// Reset buffer
void buffering_reset();
Expand All @@ -48,7 +48,7 @@ void buffering_reset();
/// \param data
/// \param length
/// \return the number of appended bytes
int buffering_append(uint8_t *data, int length);
int buffering_append(uint8_t *data, size_t length);

/// buffering_get_ram_buffer
/// \return
Expand Down
10 changes: 5 additions & 5 deletions src/buffering.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ buffer_state_t ram; // Ram
buffer_state_t flash; // Flash

void buffering_init(uint8_t *ram_buffer,
uint16_t ram_buffer_size,
size_t ram_buffer_size,
uint8_t *flash_buffer,
uint16_t flash_buffer_size) {
size_t flash_buffer_size) {
ram.data = ram_buffer;
ram.size = ram_buffer_size;
ram.pos = 0;
Expand All @@ -46,11 +46,11 @@ void buffering_reset() {
flash.in_use = 0;
}

int buffering_append(uint8_t *data, int length) {
int buffering_append(uint8_t *data, size_t length) {
if (ram.in_use) {
if (ram.size - ram.pos >= length) {
// RAM in use, append to ram if there is enough space
MEMCPY(ram.data + ram.pos, data, (size_t) length);
MEMCPY(ram.data + ram.pos, data, length);
ram.pos += length;
} else {
// If RAM is not big enough copy memory to flash
Expand All @@ -66,7 +66,7 @@ int buffering_append(uint8_t *data, int length) {
} else {
// Flash in use, append to flash
if (flash.size - flash.pos >= length) {
MEMCPY_NV(flash.data + flash.pos, data, (size_t) length);
MEMCPY_NV(flash.data + flash.pos, data, length);
flash.pos += length;
} else {
return 0;
Expand Down

0 comments on commit 04a28c3

Please sign in to comment.