Skip to content

Commit

Permalink
Support the full set of Address variants.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Feb 19, 2024
1 parent 6358796 commit d01bf33
Show file tree
Hide file tree
Showing 4 changed files with 617 additions and 294 deletions.
65 changes: 65 additions & 0 deletions app/src/mem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Dynamic allocator that uses a fixed-length buffer that is hopefully big enough
*
* The two functions alloc & dealloc use the buffer as a simple stack.
* Especially useful when an unpredictable amount of data will be received and have to be stored
* during the transaction but discarded right after.
*/

//#ifdef HAVE_DYN_MEM_ALLOC

#include <stdint.h>
#include "mem.h"

#define SIZE_MEM_BUFFER 8192

static uint8_t mem_buffer[SIZE_MEM_BUFFER];
static size_t mem_idx;

/**
* Initializes the memory buffer index
*/
void mem_init(void) {
mem_idx = 0;
}

/**
* Resets the memory buffer index
*/
void mem_reset(void) {
mem_init();
}

/**
* Allocates (push) a chunk of the memory buffer of a given size.
*
* Checks to see if there are enough space left in the memory buffer, returns
* the current location in the memory buffer and moves the index accordingly.
*
* @param[in] size Requested allocation size in bytes
* @return Allocated memory pointer; \ref NULL if not enough space left.
*/
void *mem_alloc(size_t size) {
if ((mem_idx + size) > SIZE_MEM_BUFFER) // Buffer exceeded
{
return NULL;
}
mem_idx += size;
return &mem_buffer[mem_idx - size];
}

/**
* De-allocates (pop) a chunk of memory buffer by a given size.
*
* @param[in] size Requested deallocation size in bytes
*/
void mem_dealloc(size_t size) {
if (size > mem_idx) // More than is already allocated
{
mem_idx = 0;
} else {
mem_idx -= size;
}
}

//#endif // HAVE_DYN_MEM_ALLOC
15 changes: 15 additions & 0 deletions app/src/mem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef MEM_H_
#define MEM_H_

//#ifdef HAVE_DYN_MEM_ALLOC

#include <stdlib.h>

void mem_init(void);
void mem_reset(void);
void *mem_alloc(size_t size);
void mem_dealloc(size_t size);

//#endif // HAVE_DYN_MEM_ALLOC

#endif // MEM_H_
Loading

0 comments on commit d01bf33

Please sign in to comment.