-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allocate class instances statically #191
Allocate class instances statically #191
Conversation
Caution Review failedThe pull request is closed. WalkthroughThe recent changes in Changes
Assessment against linked issues
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- src/ayab/main.cpp (1 hunks)
Additional comments not posted (2)
src/ayab/main.cpp (2)
39-45
: LGTM! Improved memory management.Replacing dynamic allocations with stack-allocated instances enhances memory management and performance.
47-53
: LGTM! Correct initialization of static members.The static members of the singleton classes are correctly initialized to point to stack instances.
Avoiding dynamic allocation saves some bookkeeping, but mainly it lets Platform.IO output a more faithful evaluation of the RAM requirements for the firmware.
Issue
The classes that make up the AYAB firmware are currently dynamically allocated with
new
at the top level ofmain.cpp
:ayab-firmware/src/ayab/main.cpp
Lines 50 to 56 in e960ad4
Since these objects are allocated at the start of the program and never freed, there is no advantage to having them dynamically allocated. On the other hand, there are disadvantages:
That last point in particular can hide the fact that the memory requirements for the firmware exceed the target microcontroller's RAM size (2048 bytes for the Atmega328p on an Arduino Uno).
Proposed solution
This PR replaces the use of
new
by the creation of statically allocated instances. The global pointers are then initialized to point to these instances.Before this PR, here is what the output of the PlatformIO builder looks like:
Which looks reasonable. But after including the firmware's main objects in the statically allocated area as this PR does, here is the output:
Revealing that the memory actually available for the stack is much less than was suggested by the earlier output, causing issues like #190.
The memory saved here seems to fix #190 partially. At least, even with stack overflow detection enabled, the firmware can now survive
reqInit
and even knitting, from my quick test.Note that this PR also deletes a series of uninitialized constant pointers that appear to have been remains from an earlier attempt at these global pointers.
Summary by CodeRabbit
new
for singleton instances.