Skip to content

Improved memory allocation for Picos#375

Open
elral wants to merge 23 commits intoMobiFlight:mainfrom
elral:fix_mem_alloc_issue
Open

Improved memory allocation for Picos#375
elral wants to merge 23 commits intoMobiFlight:mainfrom
elral:fix_mem_alloc_issue

Conversation

@elral
Copy link
Copy Markdown
Collaborator

@elral elral commented Mar 28, 2026

Description of changes

For Picos the device buffer is initialized with std::size_t which is 32bit, but only 8bit are used per memory location.
This PR changes the device buffer to uint8_t also for the Pico. But as also classes or 16bit arrays get allocated in the device buffer, the return parameter is now void* and the calling function has to cast the return value to the correct size.
This means that every device.cpp and MFdevice.cpp has to be changed.
The check if the required memory can be allocated in the device buffer is simplified.
Further more it`s now checked if a stepper class can allocate the required memory and returns the result.

For the AVR's there is no change in allocating memory in the device buffer even the same changes in device.cpp apply. It's only differentiated in allocateMem.cpp/h to keep the code readable.

This PR is tested with each device on a Mega and Pico.

Fixes #374

@elral elral requested a review from DocMoebiuz as a code owner March 28, 2026 05:38
@github-actions
Copy link
Copy Markdown

Board and firmware folder for this pull request:
Mobiflight-Firmware.zip

@elral elral changed the title Fixes memory allocation issue for Pico Improved memory allocation for Picos Mar 28, 2026
@DocMoebiuz DocMoebiuz requested a review from Copilot April 6, 2026 08:25
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses #374 by switching the device-buffer allocator to operate in bytes (via a uint8_t buffer) on 32-bit targets like RP2040, adding alignment-aware allocations, and updating device modules to allocate from the device buffer via new macros and null-checks.

Changes:

  • Reworked allocateMem to allocate from an aligned uint8_t device buffer with size/alignment parameters and simplified overflow checking.
  • Updated multiple modules’ setupArray() / internal buffers to allocate using MF_ALLOC_TYPE / MF_ALLOC_BYTES and handle allocation failures.
  • Changed MFStepper::attach() to return bool and added failure propagation to the Stepper registration path.

Reviewed changes

Copilot reviewed 19 out of 20 changed files in this pull request and generated 17 comments.

Show a summary per file
File Description
src/MF_Stepper/Stepper.cpp Switches stepper array allocation to new allocator macros and checks attach failure.
src/MF_Stepper/MFStepper.h Changes attach API to return bool.
src/MF_Stepper/MFStepper.cpp Allocates AccelStepper from device buffer and returns success/failure.
src/MF_Servo/Servos.cpp Switches servo array allocation to MF_ALLOC_TYPE and adds null check.
src/MF_Segment/LedSegment.cpp Switches segment array allocation to MF_ALLOC_TYPE and adds null check.
src/MF_Segment/LedControl_dual.cpp Migrates internal buffers to MF_ALLOC_BYTES and adds null checks.
src/MF_OutputShifter/OutputShifter.cpp Switches output shifter array allocation to MF_ALLOC_TYPE and adds null check.
src/MF_OutputShifter/MFOutputShifter.cpp Migrates _lastState buffer allocation to MF_ALLOC_BYTES.
src/MF_Output/Output.cpp Switches output array allocation to MF_ALLOC_TYPE and adds null check (plus formatting).
src/MF_LCDDisplay/LCDDisplay.cpp Switches LCD array allocation to MF_ALLOC_TYPE and adds null check.
src/MF_InputShifter/MFInputShifter.cpp Migrates _lastState buffer allocation to MF_ALLOC_BYTES.
src/MF_InputShifter/InputShifter.cpp Switches input shifter array allocation to MF_ALLOC_TYPE and adds null check.
src/MF_Encoder/Encoder.cpp Switches encoder array allocation to MF_ALLOC_TYPE and adds null check.
src/MF_DigInMux/DigInMux.cpp Switches dig-in mux array allocation to MF_ALLOC_TYPE and adds null check.
src/MF_CustomDevice/CustomDevice.cpp Switches custom device array allocation to MF_ALLOC_TYPE and adds null check.
src/MF_Button/Button.cpp Switches button array allocation to MF_ALLOC_TYPE and adds null check.
src/MF_Analog/MFAnalog.cpp Whitespace-only change.
src/MF_Analog/Analog.cpp Switches analog array allocation to MF_ALLOC_TYPE and adds null check.
src/allocateMem.h Introduces MF_ALLOC_BYTES / MF_ALLOC_TYPE macros and updates allocator API signatures.
src/allocateMem.cpp Implements aligned byte-buffer allocator and updates available-memory reporting.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

elral and others added 3 commits April 6, 2026 16:37
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 6, 2026

Board and firmware folder for this pull request:
Mobiflight-Firmware.zip

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 6, 2026

Board and firmware folder for this pull request:
Mobiflight-Firmware.zip

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 6, 2026

Board and firmware folder for this pull request:
Mobiflight-Firmware.zip

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 6, 2026

Board and firmware folder for this pull request:
Mobiflight-Firmware.zip

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

Board and firmware folder for this pull request:
Mobiflight-Firmware.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory sizing and checking on non-AVR platforms is off by 4x

2 participants