Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions src/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "list.h"

void flist_push_front(s_flist_node **list, s_flist_node *node) {
if ((list != NULL) && (node != NULL)) {
node->next = *list;
*list = node;
}
}

void flist_pop_front(s_flist_node **list, f_list_node_del func) {
s_flist_node *tmp;

if (list != NULL) {
Comment thread
apaillier-ledger marked this conversation as resolved.
tmp = *list;
if (tmp != NULL) {
*list = tmp->next;
if (func != NULL) func(tmp);
Comment thread
apaillier-ledger marked this conversation as resolved.
}
}
}

void flist_push_back(s_flist_node **list, s_flist_node *node) {
s_flist_node *tmp;

if ((list != NULL) && (node != NULL)) {
if (*list == NULL) {
*list = node;
} else {
tmp = *list;
if (tmp != NULL) {
for (; tmp->next != NULL; tmp = tmp->next)
;
tmp->next = node;
}
}
}
}

void flist_pop_back(s_flist_node **list, f_list_node_del func) {
s_flist_node *tmp;

if (list != NULL) {
tmp = *list;
if (tmp != NULL) {
// only one element
if (tmp->next == NULL) {
flist_pop_front(list, func);
} else {
for (; tmp->next->next != NULL; tmp = tmp->next)
;
if (func != NULL) func(tmp->next);
tmp->next = NULL;
}
}
}
}

void flist_insert_after(s_flist_node **list, s_flist_node *ref, s_flist_node *node) {
(void) list;
if ((ref != NULL) && (node != NULL)) {
node->next = ref->next;
ref->next = node;
}
}

void flist_remove(s_flist_node **list, s_flist_node *node, f_list_node_del func) {
s_flist_node *it;
s_flist_node *tmp;

if ((list != NULL) && (node != NULL)) {
if (node == *list) {
// first element
flist_pop_front(list, func);
} else {
it = *list;
if (it != NULL) {
for (; it->next != node; it = it->next)
;
tmp = it->next->next;
if (func != NULL) func(it->next);
it->next = tmp;
}
}
}
}

void flist_clear(s_flist_node **list, f_list_node_del func) {
s_flist_node *tmp;
s_flist_node *next;

if (list != NULL) {
tmp = *list;
while (tmp != NULL) {
next = tmp->next;
if (func != NULL) func(tmp);
tmp = next;
}
*list = NULL;
}
}

size_t flist_size(s_flist_node *const *list) {
size_t size = 0;

if (list != NULL) {
for (s_flist_node *tmp = *list; tmp != NULL; tmp = tmp->next) size += 1;
}
return size;
}

void flist_sort(s_flist_node **list, f_list_node_cmp func) {
s_flist_node **tmp;
s_flist_node *a, *b;
bool sorted;

if ((list != NULL) && (func != NULL)) {
do {
sorted = true;
for (tmp = list; (*tmp != NULL) && ((*tmp)->next != NULL); tmp = &(*tmp)->next) {
a = *tmp;
b = a->next;
if (func(a, b) == false) {
*tmp = b;
a->next = b->next;
b->next = a;
sorted = false;
}
}
} while (!sorted);
}
}
96 changes: 96 additions & 0 deletions src/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#pragma once

#include <stdlib.h>
#include <stdbool.h>

/*
* Forward list (singly-linked)
*/
typedef struct flist_node {
struct flist_node *next;
} s_flist_node;

typedef void (*f_list_node_del)(s_flist_node *node);
typedef bool (*f_list_node_cmp)(const s_flist_node *a, const s_flist_node *b);

/**
* Add a new node at the front of the list
*
* @param[in,out] list pointer to the list
* @param[out] node new node to add
*/
void flist_push_front(s_flist_node **list, s_flist_node *node);

/**
* Remove the first node from the list
*
* @param[in,out] list pointer to the list
* @param[in] func pointer to the node deletion function
*/
void flist_pop_front(s_flist_node **list, f_list_node_del func);

/**
* Add a new node at the back of the list
*
* @param[in,out] list pointer to the list
* @param[in,out] node new node to add
*/
void flist_push_back(s_flist_node **list, s_flist_node *node);

/**
* Remove the last node from the list
*
* @param[in,out] list pointer to the list
* @param[in] func pointer to the node deletion function
*/
void flist_pop_back(s_flist_node **list, f_list_node_del func);

/**
* Insert a new node after a given list node (reference)
*
* @param[] list pointer to the list
* @param[in,out] ref reference node
* @param[in,out] node new node to add
*/
void flist_insert_after(s_flist_node **list, s_flist_node *ref, s_flist_node *node);

/**
* Remove a given node from the list
*
* @param[in,out] list pointer to the list
* @param[out] node node to remove
* @param[in] func pointer to the node deletion function
*/
void flist_remove(s_flist_node **list, s_flist_node *node, f_list_node_del func);

/**
* Remove all nodes from the list
*
* @param[in,out] list pointer to the list
* @param[in] func pointer to the node deletion function
*/
void flist_clear(s_flist_node **list, f_list_node_del func);

/**
* Get the list size
*
* @param[in] list pointer to the list
*/
size_t flist_size(s_flist_node *const *list);

/**
* Sort the list
*
* @param[in,out] list pointer to the list
* @param[in] func pointer to the node comparison function
*/
void flist_sort(s_flist_node **list, f_list_node_cmp func);

/*
* List (doubly-linked)
* TODO: add functions
*/
typedef struct list_node {
Comment thread
cedelavergne-ledger marked this conversation as resolved.
s_flist_node _list;
struct list_node *prev;
} s_list_node;
77 changes: 14 additions & 63 deletions src/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@
#include "mem_alloc.h"
#include "os_print.h"

#define SIZE_MEM_BUFFER_MAIN 10240
#define SIZE_MEM_BUFFER_ALT 2048
#define SIZE_MEM_BUFFER (SIZE_MEM_BUFFER_MAIN + SIZE_MEM_BUFFER_ALT)
#define SIZE_MEM_BUFFER (1024 * 12)

static uint8_t mem_buffer[SIZE_MEM_BUFFER] __attribute__((aligned(sizeof(intmax_t))));
static uint16_t mem_legacy_idx;
static mem_ctx_t mem_ctx = NULL;

#ifdef HAVE_MEMORY_PROFILING
#define MP_LOG_PREFIX "==MP "
#endif

bool app_mem_init(void) {
mem_ctx = mem_init(mem_buffer, sizeof(mem_buffer));
void *buf = mem_buffer;
size_t buf_size = sizeof(mem_buffer);

mem_ctx = mem_init(buf, buf_size);
#ifdef HAVE_MEMORY_PROFILING
PRINTF(MP_LOG_PREFIX "init;0x%p;%u\n", buf, buf_size);
#endif
return mem_ctx != NULL;
}

void *app_mem_alloc_impl(size_t size, const char *file, int line) {
void *ptr;
ptr = mem_alloc(mem_ctx, size);
#ifdef HAVE_MEMORY_PROFILING
PRINTF("==MP alloc;%u;0x%p;%s:%u\n", size, ptr, file, line);
PRINTF(MP_LOG_PREFIX "alloc;%u;0x%p;%s:%u\n", size, ptr, file, line);
#else
(void) file;
(void) line;
Expand All @@ -38,66 +45,10 @@ void *app_mem_alloc_impl(size_t size, const char *file, int line) {

void app_mem_free_impl(void *ptr, const char *file, int line) {
#ifdef HAVE_MEMORY_PROFILING
PRINTF("==MP free;0x%p;%s:%u\n", ptr, file, line);
PRINTF(MP_LOG_PREFIX "free;0x%p;%s:%u\n", ptr, file, line);
#else
(void) file;
(void) line;
#endif
mem_free(mem_ctx, ptr);
}

/**
* Initializes the memory buffer index
*/
void mem_legacy_init(void) {
mem_legacy_idx = 0;
// initialize the new allocator to still be able to use it, just in case
mem_ctx = mem_init(mem_buffer + SIZE_MEM_BUFFER_MAIN, SIZE_MEM_BUFFER_ALT);
}

/**
* Resets the memory buffer index
*/
void mem_legacy_reset(void) {
mem_legacy_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_legacy_alloc(size_t size) {
size_t new_idx;

if (__builtin_add_overflow((size_t) mem_legacy_idx, size, &new_idx)) {
PRINTF("Error: overflow detected!\n");
return NULL;
}
// Buffer exceeded
if (new_idx > SIZE_MEM_BUFFER_MAIN) {
PRINTF("Error: mem_alloc(%u) failed!\n", size);
return NULL;
}
mem_legacy_idx += size;
return &mem_buffer[mem_legacy_idx - size];
}

/**
* De-allocates (pop) a chunk of memory buffer by a given size.
*
* @param[in] size Requested deallocation size in bytes
*/
void mem_legacy_dealloc(size_t size) {
// More than is already allocated
if (size > mem_legacy_idx) {
PRINTF("Warning: mem_dealloc(%u) with a value larger than allocated!\n", size);
mem_legacy_idx = 0;
} else {
mem_legacy_idx -= size;
}
}
5 changes: 0 additions & 5 deletions src/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,3 @@
bool app_mem_init(void);
void *app_mem_alloc_impl(size_t size, const char *file, int line);
void app_mem_free_impl(void *ptr, const char *file, int line);

void mem_legacy_init(void);
void mem_legacy_reset(void);
void *mem_legacy_alloc(size_t size);
void mem_legacy_dealloc(size_t size);
Loading
Loading