Skip to content

Commit 37e3c9c

Browse files
Removed legacy memory allocator
1 parent f47666a commit 37e3c9c

4 files changed

Lines changed: 1 addition & 99 deletions

File tree

src/mem.c

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@
1111
#include "mem_alloc.h"
1212
#include "os_print.h"
1313

14-
#define SIZE_MEM_BUFFER_MAIN 10240
15-
#define SIZE_MEM_BUFFER_ALT 2048
16-
#define SIZE_MEM_BUFFER (SIZE_MEM_BUFFER_MAIN + SIZE_MEM_BUFFER_ALT)
14+
#define SIZE_MEM_BUFFER (1024 * 12)
1715

1816
static uint8_t mem_buffer[SIZE_MEM_BUFFER] __attribute__((aligned(sizeof(intmax_t))));
19-
static uint16_t mem_legacy_idx;
2017
static mem_ctx_t mem_ctx = NULL;
2118

2219
#ifdef HAVE_MEMORY_PROFILING
@@ -55,59 +52,3 @@ void app_mem_free_impl(void *ptr, const char *file, int line) {
5552
#endif
5653
mem_free(mem_ctx, ptr);
5754
}
58-
59-
/**
60-
* Initializes the memory buffer index
61-
*/
62-
void mem_legacy_init(void) {
63-
mem_legacy_idx = 0;
64-
// initialize the new allocator to still be able to use it, just in case
65-
mem_ctx = mem_init(mem_buffer + SIZE_MEM_BUFFER_MAIN, SIZE_MEM_BUFFER_ALT);
66-
}
67-
68-
/**
69-
* Resets the memory buffer index
70-
*/
71-
void mem_legacy_reset(void) {
72-
mem_legacy_init();
73-
}
74-
75-
/**
76-
* Allocates (push) a chunk of the memory buffer of a given size.
77-
*
78-
* Checks to see if there are enough space left in the memory buffer, returns
79-
* the current location in the memory buffer and moves the index accordingly.
80-
*
81-
* @param[in] size Requested allocation size in bytes
82-
* @return Allocated memory pointer; \ref NULL if not enough space left.
83-
*/
84-
void *mem_legacy_alloc(size_t size) {
85-
size_t new_idx;
86-
87-
if (__builtin_add_overflow((size_t) mem_legacy_idx, size, &new_idx)) {
88-
PRINTF("Error: overflow detected!\n");
89-
return NULL;
90-
}
91-
// Buffer exceeded
92-
if (new_idx > SIZE_MEM_BUFFER_MAIN) {
93-
PRINTF("Error: mem_alloc(%u) failed!\n", size);
94-
return NULL;
95-
}
96-
mem_legacy_idx += size;
97-
return &mem_buffer[mem_legacy_idx - size];
98-
}
99-
100-
/**
101-
* De-allocates (pop) a chunk of memory buffer by a given size.
102-
*
103-
* @param[in] size Requested deallocation size in bytes
104-
*/
105-
void mem_legacy_dealloc(size_t size) {
106-
// More than is already allocated
107-
if (size > mem_legacy_idx) {
108-
PRINTF("Warning: mem_dealloc(%u) with a value larger than allocated!\n", size);
109-
mem_legacy_idx = 0;
110-
} else {
111-
mem_legacy_idx -= size;
112-
}
113-
}

src/mem.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,3 @@
1616
bool app_mem_init(void);
1717
void *app_mem_alloc_impl(size_t size, const char *file, int line);
1818
void app_mem_free_impl(void *ptr, const char *file, int line);
19-
20-
void mem_legacy_init(void);
21-
void mem_legacy_reset(void);
22-
void *mem_legacy_alloc(size_t size);
23-
void mem_legacy_dealloc(size_t size);

src/mem_utils.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,6 @@ const char *mem_alloc_and_format_uint(uint32_t value) {
2828
return mem_ptr;
2929
}
3030

31-
/**
32-
* Align memory by a given value
33-
*
34-
* @param[in] alignment given alignment value
35-
* @return size of the padding required for proper alignment
36-
*/
37-
uint8_t mem_legacy_align(size_t alignment) {
38-
uint8_t diff = (uintptr_t) mem_legacy_alloc(0) % alignment;
39-
40-
if (diff > 0) {
41-
diff = alignment - diff;
42-
mem_legacy_alloc(diff);
43-
}
44-
return diff;
45-
}
46-
47-
/**
48-
* Allocate and align, required when dealing with pointers of multi-bytes data
49-
* like structures that will be dereferenced at runtime.
50-
*
51-
* @param[in] size the size of the data we want to allocate in memory
52-
* @param[in] alignment the byte alignment needed
53-
*
54-
* @return pointer to the memory area, \ref NULL if the allocation failed
55-
*/
56-
void *mem_legacy_alloc_and_align(size_t size, size_t alignment) {
57-
mem_legacy_align(alignment);
58-
return mem_legacy_alloc(size);
59-
}
60-
6131
char *app_mem_strdup(const char *src) {
6232
char *dst;
6333
size_t length = strlen(src) + 1;

src/mem_utils.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,5 @@
22

33
#include <stdint.h>
44

5-
#define MEM_ALLOC_AND_ALIGN_TYPE(type) mem_legacy_alloc_and_align(sizeof(type), __alignof__(type))
6-
75
const char *mem_alloc_and_format_uint(uint32_t value);
8-
uint8_t mem_legacy_align(size_t alignment);
9-
void *mem_legacy_alloc_and_align(size_t size, size_t alignment);
106
char *app_mem_strdup(const char *s);

0 commit comments

Comments
 (0)