mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2025-12-13 05:06:30 +04:00
Coalesce some allocations (#3747)
* View: Coalesce view model allocations * SceneManager: Coalesce AppScene allocations * BufferStream: Coalesce Buffer allocations * ProtocolDict: Coalesce dict allocations * DigitalSignal: Coalesce buffer allocations Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -2,14 +2,13 @@
|
||||
#include <furi.h>
|
||||
|
||||
SceneManager* scene_manager_alloc(const SceneManagerHandlers* app_scene_handlers, void* context) {
|
||||
furi_check(context);
|
||||
furi_check(app_scene_handlers);
|
||||
|
||||
SceneManager* scene_manager = malloc(sizeof(SceneManager));
|
||||
SceneManager* scene_manager =
|
||||
malloc(sizeof(SceneManager) + (sizeof(AppScene) * app_scene_handlers->scene_num));
|
||||
// Set SceneManager context and scene handlers
|
||||
scene_manager->context = context;
|
||||
scene_manager->scene_handlers = app_scene_handlers;
|
||||
// Allocate all scenes
|
||||
scene_manager->scene = malloc(sizeof(AppScene) * app_scene_handlers->scene_num);
|
||||
// Initialize ScaneManager array for navigation
|
||||
SceneManagerIdStack_init(scene_manager->scene_id_stack);
|
||||
|
||||
@@ -21,8 +20,6 @@ void scene_manager_free(SceneManager* scene_manager) {
|
||||
|
||||
// Clear ScaneManager array
|
||||
SceneManagerIdStack_clear(scene_manager->scene_id_stack);
|
||||
// Clear allocated scenes
|
||||
free(scene_manager->scene);
|
||||
// Free SceneManager structure
|
||||
free(scene_manager);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,6 @@ typedef struct {
|
||||
struct SceneManager {
|
||||
SceneManagerIdStack_t scene_id_stack;
|
||||
const SceneManagerHandlers* scene_handlers;
|
||||
AppScene* scene;
|
||||
void* context;
|
||||
AppScene scene[];
|
||||
};
|
||||
|
||||
@@ -76,9 +76,8 @@ void view_allocate_model(View* view, ViewModelType type, size_t size) {
|
||||
if(view->model_type == ViewModelTypeLockFree) {
|
||||
view->model = malloc(size);
|
||||
} else if(view->model_type == ViewModelTypeLocking) {
|
||||
ViewModelLocking* model = malloc(sizeof(ViewModelLocking));
|
||||
ViewModelLocking* model = malloc(sizeof(ViewModelLocking) + size);
|
||||
model->mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
|
||||
model->data = malloc(size);
|
||||
view->model = model;
|
||||
} else {
|
||||
furi_crash();
|
||||
@@ -89,16 +88,11 @@ void view_free_model(View* view) {
|
||||
furi_check(view);
|
||||
if(view->model_type == ViewModelTypeNone) {
|
||||
return;
|
||||
} else if(view->model_type == ViewModelTypeLockFree) {
|
||||
free(view->model);
|
||||
} else if(view->model_type == ViewModelTypeLocking) {
|
||||
ViewModelLocking* model = view->model;
|
||||
furi_mutex_free(model->mutex);
|
||||
free(model->data);
|
||||
free(model);
|
||||
} else {
|
||||
furi_crash();
|
||||
}
|
||||
free(view->model);
|
||||
view->model = NULL;
|
||||
view->model_type = ViewModelTypeNone;
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
#include <furi.h>
|
||||
|
||||
typedef struct {
|
||||
void* data;
|
||||
FuriMutex* mutex;
|
||||
uint8_t data[];
|
||||
} ViewModelLocking;
|
||||
|
||||
struct View {
|
||||
|
||||
@@ -55,7 +55,6 @@ struct DigitalSequence {
|
||||
|
||||
uint32_t size;
|
||||
uint32_t max_size;
|
||||
uint8_t* data;
|
||||
|
||||
LL_DMA_InitTypeDef dma_config_gpio;
|
||||
LL_DMA_InitTypeDef dma_config_timer;
|
||||
@@ -64,19 +63,19 @@ struct DigitalSequence {
|
||||
DigitalSequenceRingBuffer timer_buf;
|
||||
DigitalSequenceSignalBank signals;
|
||||
DigitalSequenceState state;
|
||||
|
||||
uint8_t data[];
|
||||
};
|
||||
|
||||
DigitalSequence* digital_sequence_alloc(uint32_t size, const GpioPin* gpio) {
|
||||
furi_assert(size);
|
||||
furi_assert(gpio);
|
||||
|
||||
DigitalSequence* sequence = malloc(sizeof(DigitalSequence));
|
||||
DigitalSequence* sequence = malloc(sizeof(DigitalSequence) + size);
|
||||
|
||||
sequence->gpio = gpio;
|
||||
sequence->max_size = size;
|
||||
|
||||
sequence->data = malloc(sequence->max_size);
|
||||
|
||||
sequence->dma_config_gpio.PeriphOrM2MSrcAddress = (uint32_t)&gpio->port->BSRR;
|
||||
sequence->dma_config_gpio.MemoryOrM2MDstAddress = (uint32_t)sequence->gpio_buf;
|
||||
sequence->dma_config_gpio.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
|
||||
@@ -107,7 +106,6 @@ DigitalSequence* digital_sequence_alloc(uint32_t size, const GpioPin* gpio) {
|
||||
void digital_sequence_free(DigitalSequence* sequence) {
|
||||
furi_assert(sequence);
|
||||
|
||||
free(sequence->data);
|
||||
free(sequence);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,9 @@
|
||||
#define TAG "DigitalSignal"
|
||||
|
||||
DigitalSignal* digital_signal_alloc(uint32_t max_size) {
|
||||
DigitalSignal* signal = malloc(sizeof(DigitalSignal));
|
||||
DigitalSignal* signal = malloc(sizeof(DigitalSignal) + (max_size * sizeof(uint32_t)));
|
||||
|
||||
signal->max_size = max_size;
|
||||
signal->data = malloc(max_size * sizeof(uint32_t));
|
||||
|
||||
return signal;
|
||||
}
|
||||
@@ -17,7 +16,6 @@ DigitalSignal* digital_signal_alloc(uint32_t max_size) {
|
||||
void digital_signal_free(DigitalSignal* signal) {
|
||||
furi_check(signal);
|
||||
|
||||
free(signal->data);
|
||||
free(signal);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,6 @@ struct DigitalSignal {
|
||||
bool start_level; /**< The level to begin the signal with. */
|
||||
uint32_t size; /**< Current period count contained in the instance. */
|
||||
uint32_t max_size; /**< Maximum period count this instance can hold. */
|
||||
uint32_t* data; /**< Pointer to the array of time periods. */
|
||||
int32_t remainder; /**< Remainder left after converting all periods into timer ticks. */
|
||||
uint32_t data[]; /**< The array of time periods. */
|
||||
};
|
||||
|
||||
@@ -12,8 +12,8 @@ struct BufferStream {
|
||||
FuriStreamBuffer* stream;
|
||||
|
||||
size_t index;
|
||||
Buffer* buffers;
|
||||
size_t max_buffers_count;
|
||||
Buffer buffers[];
|
||||
};
|
||||
|
||||
bool buffer_write(Buffer* buffer, const uint8_t* data, size_t size) {
|
||||
@@ -44,9 +44,8 @@ void buffer_reset(Buffer* buffer) {
|
||||
BufferStream* buffer_stream_alloc(size_t buffer_size, size_t buffers_count) {
|
||||
furi_assert(buffer_size > 0);
|
||||
furi_assert(buffers_count > 0);
|
||||
BufferStream* buffer_stream = malloc(sizeof(BufferStream));
|
||||
BufferStream* buffer_stream = malloc(sizeof(BufferStream) + (sizeof(Buffer) * buffers_count));
|
||||
buffer_stream->max_buffers_count = buffers_count;
|
||||
buffer_stream->buffers = malloc(sizeof(Buffer) * buffer_stream->max_buffers_count);
|
||||
for(size_t i = 0; i < buffer_stream->max_buffers_count; i++) {
|
||||
buffer_stream->buffers[i].occupied = false;
|
||||
buffer_stream->buffers[i].size = 0;
|
||||
@@ -66,7 +65,6 @@ void buffer_stream_free(BufferStream* buffer_stream) {
|
||||
free(buffer_stream->buffers[i].data);
|
||||
}
|
||||
furi_stream_buffer_free(buffer_stream->stream);
|
||||
free(buffer_stream->buffers);
|
||||
free(buffer_stream);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,16 +4,15 @@
|
||||
struct ProtocolDict {
|
||||
const ProtocolBase** base;
|
||||
size_t count;
|
||||
void** data;
|
||||
void* data[];
|
||||
};
|
||||
|
||||
ProtocolDict* protocol_dict_alloc(const ProtocolBase** protocols, size_t count) {
|
||||
furi_check(protocols);
|
||||
|
||||
ProtocolDict* dict = malloc(sizeof(ProtocolDict));
|
||||
ProtocolDict* dict = malloc(sizeof(ProtocolDict) + (sizeof(void*) * count));
|
||||
dict->base = protocols;
|
||||
dict->count = count;
|
||||
dict->data = malloc(sizeof(void*) * dict->count);
|
||||
|
||||
for(size_t i = 0; i < dict->count; i++) {
|
||||
dict->data[i] = dict->base[i]->alloc();
|
||||
@@ -29,7 +28,6 @@ void protocol_dict_free(ProtocolDict* dict) {
|
||||
dict->base[i]->free(dict->data[i]);
|
||||
}
|
||||
|
||||
free(dict->data);
|
||||
free(dict);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user