Speedup SD card & enlarge your RAM. (#1649)

* FuriHal: sram2 memory manager
* FuriHal: sram2 memory allocator
* FuriHal: allow NULL buffers for txrx in spi hal
* SD card: sector cache
* FuriHal: fix init in memory hal
* RPC: STARTUP instead SERVICE
* Memory: pool "free" command
* Thread: service can be statically allocated in a memory pool

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG
2022-08-27 14:25:47 +10:00
committed by GitHub
parent ab4bb55d0f
commit 99a7d06f71
19 changed files with 410 additions and 53 deletions

View File

@@ -1,6 +1,7 @@
#include "memmgr.h"
#include "common_defines.h"
#include <string.h>
#include <furi_hal_memory.h>
extern void* pvPortMalloc(size_t xSize);
extern void vPortFree(void* pv);
@@ -77,3 +78,18 @@ void* __wrap__realloc_r(struct _reent* r, void* ptr, size_t size) {
UNUSED(r);
return realloc(ptr, size);
}
void* memmgr_alloc_from_pool(size_t size) {
void* p = furi_hal_memory_alloc(size);
if(p == NULL) p = malloc(size);
return p;
}
size_t memmgr_pool_get_free(void) {
return furi_hal_memory_get_free();
}
size_t memmgr_pool_get_max_block(void) {
return furi_hal_memory_max_pool_block();
}

View File

@@ -35,6 +35,28 @@ size_t memmgr_get_total_heap(void);
*/
size_t memmgr_get_minimum_free_heap(void);
/**
* @brief Allocate memory from separate memory pool. That memory can't be freed.
*
* @param size
* @return void*
*/
void* memmgr_alloc_from_pool(size_t size);
/**
* @brief Get free memory pool size
*
* @return size_t
*/
size_t memmgr_pool_get_free(void);
/**
* @brief Get max free block size from memory pool
*
* @return size_t
*/
size_t memmgr_pool_get_max_block(void);
#ifdef __cplusplus
}
#endif

View File

@@ -7,7 +7,9 @@
#include "mutex.h"
#include <task.h>
#include "log.h"
#include <m-string.h>
#include <furi_hal_rtc.h>
#include <furi_hal_console.h>
#define THREAD_NOTIFY_INDEX 1 // Index 0 is used for stream buffers
@@ -20,6 +22,7 @@ struct FuriThreadStdout {
};
struct FuriThread {
bool is_service;
FuriThreadState state;
int32_t ret;
@@ -84,6 +87,11 @@ static void furi_thread_body(void* context) {
furi_assert(thread->state == FuriThreadStateRunning);
furi_thread_set_state(thread, FuriThreadStateStopped);
if(thread->is_service) {
FURI_LOG_E(
"Service", "%s thread exited. Thread memory cannot be reclaimed.", thread->name);
}
// clear thread local storage
__furi_thread_stdout_flush(thread);
furi_assert(pvTaskGetThreadLocalStoragePointer(NULL, 0) != NULL);
@@ -96,7 +104,7 @@ static void furi_thread_body(void* context) {
FuriThread* furi_thread_alloc() {
FuriThread* thread = malloc(sizeof(FuriThread));
string_init(thread->output.buffer);
thread->is_service = false;
return thread;
}
@@ -117,6 +125,10 @@ void furi_thread_set_name(FuriThread* thread, const char* name) {
thread->name = name ? strdup(name) : NULL;
}
void furi_thread_mark_as_service(FuriThread* thread) {
thread->is_service = true;
}
void furi_thread_set_stack_size(FuriThread* thread, size_t stack_size) {
furi_assert(thread);
furi_assert(thread->state == FuriThreadStateStopped);
@@ -168,15 +180,23 @@ void furi_thread_start(FuriThread* thread) {
furi_thread_set_state(thread, FuriThreadStateStarting);
BaseType_t ret = xTaskCreate(
furi_thread_body,
thread->name,
thread->stack_size / 4,
thread,
thread->priority ? thread->priority : FuriThreadPriorityNormal,
&thread->task_handle);
uint32_t stack = thread->stack_size / 4;
UBaseType_t priority = thread->priority ? thread->priority : FuriThreadPriorityNormal;
if(thread->is_service) {
thread->task_handle = xTaskCreateStatic(
furi_thread_body,
thread->name,
stack,
thread,
priority,
memmgr_alloc_from_pool(sizeof(StackType_t) * stack),
memmgr_alloc_from_pool(sizeof(StaticTask_t)));
} else {
BaseType_t ret = xTaskCreate(
furi_thread_body, thread->name, stack, thread, priority, &thread->task_handle);
furi_check(ret == pdPASS);
}
furi_check(ret == pdPASS);
furi_check(thread->task_handle);
}

View File

@@ -73,6 +73,13 @@ void furi_thread_free(FuriThread* thread);
*/
void furi_thread_set_name(FuriThread* thread, const char* name);
/** Mark thread as service
* The service cannot be stopped or removed, and cannot exit from the thread body
*
* @param thread
*/
void furi_thread_mark_as_service(FuriThread* thread);
/** Set FuriThread stack size
*
* @param thread FuriThread instance