Skip to content

Commit

Permalink
Revert TLSF
Browse files Browse the repository at this point in the history
This reverts commit 5dddb07
  • Loading branch information
xMasterX committed May 16, 2024
1 parent 97d5b8b commit 0ae78df
Show file tree
Hide file tree
Showing 21 changed files with 625 additions and 609 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,3 @@
path = applications/main/subghz_remote
url = https://github.com/DarkFlippers/SubGHz_Remote.git
branch = ufw_main_app
[submodule "lib/tlsf"]
path = lib/tlsf
url = https://github.com/espressif/tlsf

2 changes: 1 addition & 1 deletion .pvsoptions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--ignore-ccache -C gccarm --rules-config .pvsconfig -e lib/cmsis_core -e lib/tlsf -e lib/fatfs -e lib/fnv1a-hash -e lib/FreeRTOS-Kernel -e lib/heatshrink -e lib/libusb_stm32 -e lib/littlefs -e lib/mbedtls -e lib/microtar -e lib/mlib -e lib/stm32wb_cmsis -e lib/stm32wb_copro -e lib/stm32wb_hal -e lib/u8g2 -e lib/nanopb -e lib/mjs -e */arm-none-eabi/*
--ignore-ccache -C gccarm --rules-config .pvsconfig -e lib/cmsis_core -e lib/fatfs -e lib/fnv1a-hash -e lib/FreeRTOS-Kernel -e lib/heatshrink -e lib/libusb_stm32 -e lib/littlefs -e lib/mbedtls -e lib/microtar -e lib/mlib -e lib/stm32wb_cmsis -e lib/stm32wb_copro -e lib/stm32wb_hal -e lib/u8g2 -e lib/nanopb -e lib/mjs -e */arm-none-eabi/*
262 changes: 4 additions & 258 deletions applications/debug/unit_tests/furi/furi_memmgr_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "../minunit.h"
#include <furi.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>

void test_furi_memmgr(void) {
void* ptr;
Expand Down Expand Up @@ -34,260 +37,3 @@ void test_furi_memmgr(void) {
}
free(ptr);
}

static void test_memmgr_malloc(const size_t allocation_size) {
uint8_t* ptr = NULL;
const char* error_message = NULL;

FURI_CRITICAL_ENTER();

ptr = malloc(allocation_size);

// test that we can allocate memory
if(ptr == NULL) {
error_message = "malloc failed";
}

// test that memory is zero-initialized after allocation
for(size_t i = 0; i < allocation_size; i++) {
if(ptr[i] != 0) {
error_message = "memory is not zero-initialized after malloc";
break;
}
}
memset(ptr, 0x55, allocation_size);
free(ptr);

// test that memory is zero-initialized after free
// we know that allocator can use this memory for inner purposes
// so we check that memory at least partially zero-initialized

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuse-after-free"

size_t zero_count = 0;
for(size_t i = 0; i < allocation_size; i++) {
if(ptr[i] == 0) {
zero_count++;
}
}

#pragma GCC diagnostic pop

// check that at least 75% of memory is zero-initialized
if(zero_count < (allocation_size * 0.75)) {
error_message = "seems that memory is not zero-initialized after free (malloc)";
}

FURI_CRITICAL_EXIT();

if(error_message != NULL) {
mu_fail(error_message);
}
}

static void test_memmgr_realloc(const size_t allocation_size) {
uint8_t* ptr = NULL;
const char* error_message = NULL;

FURI_CRITICAL_ENTER();

ptr = realloc(ptr, allocation_size);

// test that we can allocate memory
if(ptr == NULL) {
error_message = "realloc(NULL) failed";
}

// test that memory is zero-initialized after allocation
for(size_t i = 0; i < allocation_size; i++) {
if(ptr[i] != 0) {
error_message = "memory is not zero-initialized after realloc(NULL)";
break;
}
}

memset(ptr, 0x55, allocation_size);

ptr = realloc(ptr, allocation_size * 2);

// test that we can reallocate memory
if(ptr == NULL) {
error_message = "realloc failed";
}

// test that memory content is preserved
for(size_t i = 0; i < allocation_size; i++) {
if(ptr[i] != 0x55) {
error_message = "memory is not reallocated after realloc";
break;
}
}

// test that remaining memory is zero-initialized
size_t non_zero_count = 0;
for(size_t i = allocation_size; i < allocation_size * 2; i++) {
if(ptr[i] != 0) {
non_zero_count += 1;
}
}

// check that at most of memory is zero-initialized
// we know that allocator not always can restore content size from a pointer
// so we check against small threshold
if(non_zero_count > 4) {
error_message = "seems that memory is not zero-initialized after realloc";
}

uint8_t* null_ptr = realloc(ptr, 0);

// test that we can free memory
if(null_ptr != NULL) {
error_message = "realloc(0) failed";
}

// test that memory is zero-initialized after realloc(0)
// we know that allocator can use this memory for inner purposes
// so we check that memory at least partially zero-initialized

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuse-after-free"

size_t zero_count = 0;
for(size_t i = 0; i < allocation_size; i++) {
if(ptr[i] == 0) {
zero_count++;
}
}

#pragma GCC diagnostic pop

// check that at least 75% of memory is zero-initialized
if(zero_count < (allocation_size * 0.75)) {
error_message = "seems that memory is not zero-initialized after realloc(0)";
}

FURI_CRITICAL_EXIT();

if(error_message != NULL) {
mu_fail(error_message);
}
}

static void test_memmgr_alloc_aligned(const size_t allocation_size, const size_t alignment) {
uint8_t* ptr = NULL;
const char* error_message = NULL;

FURI_CRITICAL_ENTER();

ptr = aligned_alloc(alignment, allocation_size);

// test that we can allocate memory
if(ptr == NULL) {
error_message = "aligned_alloc failed";
}

// test that memory is aligned
if(((uintptr_t)ptr % alignment) != 0) {
error_message = "memory is not aligned after aligned_alloc";
}

// test that memory is zero-initialized after allocation
for(size_t i = 0; i < allocation_size; i++) {
if(ptr[i] != 0) {
error_message = "memory is not zero-initialized after aligned_alloc";
break;
}
}
memset(ptr, 0x55, allocation_size);
free(ptr);

// test that memory is zero-initialized after free
// we know that allocator can use this memory for inner purposes
// so we check that memory at least partially zero-initialized

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuse-after-free"

size_t zero_count = 0;
for(size_t i = 0; i < allocation_size; i++) {
if(ptr[i] == 0) {
zero_count++;
}
}

#pragma GCC diagnostic pop

// check that at least 75% of memory is zero-initialized
if(zero_count < (allocation_size * 0.75)) {
error_message = "seems that memory is not zero-initialized after free (aligned_alloc)";
}

FURI_CRITICAL_EXIT();

if(error_message != NULL) {
mu_fail(error_message);
}
}

void test_furi_memmgr_advanced(void) {
const size_t sizes[] = {50, 100, 500, 1000, 5000, 10000};
const size_t sizes_count = sizeof(sizes) / sizeof(sizes[0]);
const size_t alignments[] = {4, 8, 16, 32, 64, 128, 256, 512, 1024};
const size_t alignments_count = sizeof(alignments) / sizeof(alignments[0]);

// do test without memory fragmentation
{
for(size_t i = 0; i < sizes_count; i++) {
test_memmgr_malloc(sizes[i]);
}

for(size_t i = 0; i < sizes_count; i++) {
test_memmgr_realloc(sizes[i]);
}

for(size_t i = 0; i < sizes_count; i++) {
for(size_t j = 0; j < alignments_count; j++) {
test_memmgr_alloc_aligned(sizes[i], alignments[j]);
}
}
}

// do test with memory fragmentation
{
void* blocks[sizes_count];
void* guards[sizes_count - 1];

// setup guards
for(size_t i = 0; i < sizes_count; i++) {
blocks[i] = malloc(sizes[i]);
if(i < sizes_count - 1) {
guards[i] = malloc(sizes[i]);
}
}

for(size_t i = 0; i < sizes_count; i++) {
free(blocks[i]);
}

// do test
for(size_t i = 0; i < sizes_count; i++) {
test_memmgr_malloc(sizes[i]);
}

for(size_t i = 0; i < sizes_count; i++) {
test_memmgr_realloc(sizes[i]);
}

for(size_t i = 0; i < sizes_count; i++) {
for(size_t j = 0; j < alignments_count; j++) {
test_memmgr_alloc_aligned(sizes[i], alignments[j]);
}
}

// cleanup guards
for(size_t i = 0; i < sizes_count - 1; i++) {
free(guards[i]);
}
}
}
2 changes: 0 additions & 2 deletions applications/debug/unit_tests/furi/furi_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ void test_furi_concurrent_access(void);
void test_furi_pubsub(void);

void test_furi_memmgr(void);
void test_furi_memmgr_advanced(void);

static int foo = 0;

Expand Down Expand Up @@ -38,7 +37,6 @@ MU_TEST(mu_test_furi_memmgr) {
// this test is not accurate, but gives a basic understanding
// that memory management is working fine
test_furi_memmgr();
test_furi_memmgr_advanced();
}

MU_TEST_SUITE(test_suite) {
Expand Down
48 changes: 3 additions & 45 deletions applications/services/cli/cli_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,58 +435,16 @@ void cli_command_free(Cli* cli, FuriString* args, void* context) {
printf("Minimum heap size: %zu\r\n", memmgr_get_minimum_free_heap());
printf("Maximum heap block: %zu\r\n", memmgr_heap_get_max_free_block());

printf("Aux pool total free: %zu\r\n", memmgr_aux_pool_get_free());
printf("Aux pool max free block: %zu\r\n", memmgr_pool_get_max_block());
}

typedef struct {
void* addr;
size_t size;
} FreeBlockInfo;

#define FREE_BLOCK_INFO_MAX 128

typedef struct {
FreeBlockInfo free_blocks[FREE_BLOCK_INFO_MAX];
size_t free_blocks_count;
} FreeBlockContext;

static bool free_block_walker(void* pointer, size_t size, bool used, void* context) {
FreeBlockContext* free_blocks = (FreeBlockContext*)context;
if(!used) {
if(free_blocks->free_blocks_count < FREE_BLOCK_INFO_MAX) {
free_blocks->free_blocks[free_blocks->free_blocks_count].addr = pointer;
free_blocks->free_blocks[free_blocks->free_blocks_count].size = size;
free_blocks->free_blocks_count++;
} else {
return false;
}
}
return true;
printf("Pool free: %zu\r\n", memmgr_pool_get_free());
printf("Maximum pool block: %zu\r\n", memmgr_pool_get_max_block());
}

void cli_command_free_blocks(Cli* cli, FuriString* args, void* context) {
UNUSED(cli);
UNUSED(args);
UNUSED(context);

FreeBlockContext* free_blocks = malloc(sizeof(FreeBlockContext));
free_blocks->free_blocks_count = 0;

memmgr_heap_walk_blocks(free_block_walker, free_blocks);

for(size_t i = 0; i < free_blocks->free_blocks_count; i++) {
printf(
"A %p S %zu\r\n",
(void*)free_blocks->free_blocks[i].addr,
free_blocks->free_blocks[i].size);
}

if(free_blocks->free_blocks_count == FREE_BLOCK_INFO_MAX) {
printf("... and more\r\n");
}

free(free_blocks);
memmgr_heap_printf_free_blocks();
}

void cli_command_i2c(Cli* cli, FuriString* args, void* context) {
Expand Down
Loading

0 comments on commit 0ae78df

Please sign in to comment.