Skip to content

Commit

Permalink
I should have this done this lifetime, i hope
Browse files Browse the repository at this point in the history
  • Loading branch information
jlitewski committed May 2, 2024
1 parent 8a9148e commit 671f3df
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
67 changes: 65 additions & 2 deletions armsrc/palloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
// This is designed to be a replacement to the BigBuf implementation the
// Iceman firmware uses
//============================== Special Thanks ================================
// thi-ng/tinyalloc - for the general idea of the blocks and the heap
// rhempel/umm_malloc - for the best-fit algo
// thi-ng/tinyalloc - for the initial chunk of code palloc used
//==============================================================================
#include "palloc.h"

Expand Down Expand Up @@ -53,6 +52,10 @@ typedef struct {

static pHeap *heap = NULL;

/**
* @brief Initialize the palloc heap and blocks.
* This must be used before any other palloc function!
*/
void palloc_init(void) {
// Set up the heap
heap = (pHeap*)(_stack_start - __bss_end__);
Expand All @@ -72,6 +75,66 @@ void palloc_init(void) {
block->next = NULL;
}

/**
* @brief Copy `len` data from `src` to `ptr`
*
* @param ptr The pointer to have the data copied to
* @param src The source of the data to copy
* @param len The amount of data (in bytes) to copy from the source to the pointer
*/
void palloc_copy(void *ptr, const void *src, uint16_t len) {
size_t *dst = (size_t*)ptr;
const size_t *data = (size_t*)src;

size_t endLen = len & 0x03;

while((len -= 4) > endLen) {
*dst++ = *data++;
}

uint8_t *dstByte = (uint8_t*)dst;
uint8_t *dataByte = (uint8_t*)data;

while(endLen--) {
*dstByte++ = *dataByte++;
}
}

static void insert(pBlock *blk) {
pBlock *ptr = heap->free;
pBlock *prev = NULL;

}

/**
* @brief Free the memory a pointer holds
*
* @param ptr The pointer to free
* @return true If the memory at pointer was freed
* @return false otherwise
*/
bool palloc_free(void *ptr) {
pBlock *blk = heap->used;
pBlock *prev = NULL;

while(blk != NULL) {
if(ptr == blk->address) {
if(prev) {
prev->next = blk->next;
} else {
heap->used = blk->next;
}

return true;
}

prev = blk;
blk = blk->next;
}

return false;
}

/**
* @brief Returns the amount of blocks in a specific Block container
*
Expand Down
1 change: 1 addition & 0 deletions armsrc/palloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

void palloc_init(void);
void *palloc(uint16_t numElement, uint16_t size);
void palloc_copy(void *ptr, const void *src, uint16_t len);
bool palloc_free(void *ptr);

uint8_t palloc_get_free(void);
Expand Down

0 comments on commit 671f3df

Please sign in to comment.