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:
Alexandre Paillier
2022-07-05 17:49:45 +02:00
parent 7dad477413
commit a375e5b07d
6 changed files with 30 additions and 62 deletions

View File

@@ -137,9 +137,15 @@ DEFINES += HAVE_NFT_TESTING_KEY
endif
endif
# Dynamic memory allocator
ifneq ($(TARGET_NAME),TARGET_NANOS)
DEFINES += HAVE_DYN_MEM_ALLOC
endif
# EIP-712
ifneq ($(TARGET_NAME),TARGET_NANOS)
DEFINES += HAVE_EIP712_FULL_SUPPORT HAVE_EIP712_TESTING_KEY
DEFINES += HAVE_EIP712_FULL_SUPPORT
DEFINES += HAVE_EIP712_TESTING_KEY
endif
# Enabling debug PRINTF

View File

@@ -1,16 +1,12 @@
#ifdef HAVE_EIP712_FULL_SUPPORT
#ifdef HAVE_DYN_MEM_ALLOC
#include <stdint.h>
#include "mem.h"
#include "shared_context.h"
#define SIZE_MEM_BUFFER 5120
static uint8_t mem_buffer[SIZE_MEM_BUFFER];
static size_t mem_idx;
#ifdef DEBUG
size_t mem_max;
#endif
/**
@@ -19,9 +15,6 @@ size_t mem_max;
void mem_init(void)
{
mem_idx = 0;
#ifdef DEBUG
mem_max = 0;
#endif
}
/**
@@ -34,6 +27,7 @@ void mem_reset(void)
/**
* 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.
*
@@ -44,22 +38,12 @@ void *mem_alloc(size_t size)
{
if ((mem_idx + size) > SIZE_MEM_BUFFER) // Buffer exceeded
{
#ifdef DEBUG
PRINTF("Memory exhausted!\n");
#endif
return NULL;
}
mem_idx += size;
#ifdef DEBUG
if (mem_idx > mem_max)
{
mem_max = mem_idx;
}
#endif
return &mem_buffer[mem_idx - size];
}
/**
* De-allocates a chunk of memory buffer by a given size.
*
@@ -77,4 +61,4 @@ void mem_dealloc(size_t size)
}
}
#endif // HAVE_EIP712_FULL_SUPPORT
#endif // HAVE_DYN_MEM_ALLOC

View File

@@ -1,7 +1,7 @@
#ifndef MEM_H_
#define MEM_H_
#ifdef HAVE_EIP712_FULL_SUPPORT
#ifdef HAVE_DYN_MEM_ALLOC
#include <stdlib.h>
@@ -10,10 +10,6 @@ void mem_reset(void);
void *mem_alloc(size_t size);
void mem_dealloc(size_t size);
#ifdef DEBUG
extern size_t mem_max;
#endif
#endif // HAVE_EIP712_FULL_SUPPORT
#endif // HAVE_DYN_MEM_ALLOC
#endif // MEM_H_

View File

@@ -1,4 +1,4 @@
#ifdef HAVE_EIP712_FULL_SUPPORT
#ifdef HAVE_DYN_MEM_ALLOC
#include <stdlib.h>
#include <string.h>
@@ -6,22 +6,6 @@
#include "mem.h"
#include "mem_utils.h"
void *mem_alloc_and_copy(const void *data, size_t size)
{
void *mem_ptr;
if ((mem_ptr = mem_alloc(size)) != NULL)
{
memmove(mem_ptr, data, size);
}
return mem_ptr;
}
char *mem_alloc_and_copy_char(char c)
{
return mem_alloc_and_copy(&c, sizeof(char));
}
/**
* Format an unsigned number up to 32-bit into memory into an ASCII string.
*
@@ -79,4 +63,4 @@ void *mem_alloc_and_align(size_t size, size_t alignment)
return mem_alloc(size);
}
#endif // HAVE_EIP712_FULL_SUPPORT
#endif // HAVE_DYN_MEM_ALLOC

16
src_common/mem_utils.h Normal file
View 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_

View File

@@ -1,18 +0,0 @@
#ifndef MEM_UTILS_H_
#define MEM_UTILS_H_
#ifdef HAVE_EIP712_FULL_SUPPORT
#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_copy_char(char c);
void *mem_alloc_and_copy(const void *data, size_t size);
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_EIP712_FULL_SUPPORT
#endif // MEM_UTILS_H_