diff --git a/applications/services/gui/scene_manager.c b/applications/services/gui/scene_manager.c index 7a0a29137..11acc0796 100644 --- a/applications/services/gui/scene_manager.c +++ b/applications/services/gui/scene_manager.c @@ -2,14 +2,13 @@ #include 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); } diff --git a/applications/services/gui/scene_manager_i.h b/applications/services/gui/scene_manager_i.h index fca798d12..85f7e7bee 100644 --- a/applications/services/gui/scene_manager_i.h +++ b/applications/services/gui/scene_manager_i.h @@ -17,6 +17,6 @@ typedef struct { struct SceneManager { SceneManagerIdStack_t scene_id_stack; const SceneManagerHandlers* scene_handlers; - AppScene* scene; void* context; + AppScene scene[]; }; diff --git a/applications/services/gui/view.c b/applications/services/gui/view.c index 51d023543..3ac85794a 100644 --- a/applications/services/gui/view.c +++ b/applications/services/gui/view.c @@ -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; } diff --git a/applications/services/gui/view_i.h b/applications/services/gui/view_i.h index 3e895bd94..1cc84c745 100644 --- a/applications/services/gui/view_i.h +++ b/applications/services/gui/view_i.h @@ -9,8 +9,8 @@ #include typedef struct { - void* data; FuriMutex* mutex; + uint8_t data[]; } ViewModelLocking; struct View { diff --git a/lib/digital_signal/digital_sequence.c b/lib/digital_signal/digital_sequence.c index 24dddb77d..3e1643abb 100644 --- a/lib/digital_signal/digital_sequence.c +++ b/lib/digital_signal/digital_sequence.c @@ -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); } diff --git a/lib/digital_signal/digital_signal.c b/lib/digital_signal/digital_signal.c index 585250c26..dd8600a40 100644 --- a/lib/digital_signal/digital_signal.c +++ b/lib/digital_signal/digital_signal.c @@ -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); } diff --git a/lib/digital_signal/digital_signal_i.h b/lib/digital_signal/digital_signal_i.h index e473c80c0..cd2f7fa3b 100644 --- a/lib/digital_signal/digital_signal_i.h +++ b/lib/digital_signal/digital_signal_i.h @@ -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. */ }; diff --git a/lib/toolbox/buffer_stream.c b/lib/toolbox/buffer_stream.c index e8cb334d5..dccc97f1c 100644 --- a/lib/toolbox/buffer_stream.c +++ b/lib/toolbox/buffer_stream.c @@ -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); } diff --git a/lib/toolbox/protocols/protocol_dict.c b/lib/toolbox/protocols/protocol_dict.c index 71fa4fe28..34cbcd13a 100644 --- a/lib/toolbox/protocols/protocol_dict.c +++ b/lib/toolbox/protocols/protocol_dict.c @@ -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); }