Moved the memory allocator and its util functions out of the EIP712 subdirectory and gave it its own compilation flag
Also removed dead code
This commit is contained in:
64
src_common/mem.c
Normal file
64
src_common/mem.c
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifdef HAVE_DYN_MEM_ALLOC
|
||||
|
||||
#include <stdint.h>
|
||||
#include "mem.h"
|
||||
|
||||
#define SIZE_MEM_BUFFER 5120
|
||||
|
||||
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 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 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
src_common/mem.h
Normal file
15
src_common/mem.h
Normal file
@@ -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_
|
||||
66
src_common/mem_utils.c
Normal file
66
src_common/mem_utils.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#ifdef HAVE_DYN_MEM_ALLOC
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "mem.h"
|
||||
#include "mem_utils.h"
|
||||
|
||||
/**
|
||||
* Format an unsigned number up to 32-bit into memory into an ASCII string.
|
||||
*
|
||||
* @param[in] value Value to write in memory
|
||||
* @param[out] length number of characters written to memory
|
||||
*
|
||||
* @return pointer to memory area or \ref NULL if the allocation failed
|
||||
*/
|
||||
char *mem_alloc_and_format_uint(uint32_t value, uint8_t *const length)
|
||||
{
|
||||
char *mem_ptr;
|
||||
uint32_t value_copy;
|
||||
uint8_t size;
|
||||
|
||||
size = 1; // minimum size, even if 0
|
||||
value_copy = value;
|
||||
while (value_copy >= 10)
|
||||
{
|
||||
value_copy /= 10;
|
||||
size += 1;
|
||||
}
|
||||
// +1 for the null character
|
||||
if ((mem_ptr = mem_alloc(sizeof(char) * (size + 1))))
|
||||
{
|
||||
snprintf(mem_ptr, (size + 1), "%u", value);
|
||||
mem_dealloc(sizeof(char)); // to skip the null character
|
||||
if (length != NULL)
|
||||
{
|
||||
*length = size;
|
||||
}
|
||||
}
|
||||
return mem_ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate and align, required when dealing with pointers of multi-bytes data
|
||||
* like structures that will be dereferenced at runtime.
|
||||
*
|
||||
* @param[in] size the size of the data we want to allocate in memory
|
||||
* @param[in] alignment the byte alignment needed
|
||||
*
|
||||
* @return pointer to the memory area, \ref NULL if the allocation failed
|
||||
*/
|
||||
void *mem_alloc_and_align(size_t size, size_t alignment)
|
||||
{
|
||||
uint8_t align_diff = (uintptr_t)mem_alloc(0) % alignment;
|
||||
|
||||
if (align_diff > 0) // alignment needed
|
||||
{
|
||||
if (mem_alloc(alignment - align_diff) == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return mem_alloc(size);
|
||||
}
|
||||
|
||||
#endif // HAVE_DYN_MEM_ALLOC
|
||||
16
src_common/mem_utils.h
Normal file
16
src_common/mem_utils.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef MEM_UTILS_H_
|
||||
#define MEM_UTILS_H_
|
||||
|
||||
#ifdef HAVE_DYN_MEM_ALLOC
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MEM_ALLOC_AND_ALIGN_TYPE(type) mem_alloc_and_align(sizeof(type), __alignof__(type))
|
||||
|
||||
char *mem_alloc_and_format_uint(uint32_t value, uint8_t *const written_chars);
|
||||
void *mem_alloc_and_align(size_t size, size_t alignment);
|
||||
|
||||
#endif // HAVE_DYN_MEM_ALLOC
|
||||
|
||||
#endif // MEM_UTILS_H_
|
||||
Reference in New Issue
Block a user