Skip to content

Dynamic memory management #198

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

Merged
merged 11 commits into from
Apr 3, 2025
Merged

Dynamic memory management #198

merged 11 commits into from
Apr 3, 2025

Conversation

bessman
Copy link
Collaborator

@bessman bessman commented Apr 2, 2025

This pull request adds heap based memory management. The previous memory management, which was based on large, statically allocated buffers, is replaced with malloc/free-based management.

Increases sample depth of the oscilloscope and logic analyzer by allowing the user to use more memory than was previously possible.

Summary by Sourcery

Replace static memory management with dynamic memory allocation using malloc/free, enabling more flexible memory usage across the firmware

New Features:

  • Implement dynamic memory allocation for various instrument buffers
  • Add support for heap-based memory management

Enhancements:

  • Modify command function signatures to support dynamic memory allocation
  • Remove static buffer limitations
  • Improve memory efficiency by allocating memory only when needed

Build:

  • Update CMake configuration to include heap allocation
  • Increase heap size to 0x6000 bytes

Copy link
Contributor

sourcery-ai bot commented Apr 2, 2025

Reviewer's Guide by Sourcery

This pull request migrates the PSLab firmware to use dynamic memory allocation. This change replaces the previous static memory allocation scheme with malloc/free-based management. The changes include modifications to the wave generator, interval capture, oscilloscope, and other modules to use dynamic memory allocation. Additionally, the pull request updates the command functions to use dynamically allocated memory for return values and updates the packet handler to free the allocated memory after sending the response. The buffer module was removed, and a heap size was added to the linker flags. Finally, an ESP01 target was added.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Replaced statically allocated wave tables with dynamically allocated ones in the wave generator.
  • Removed the statically allocated wave tables.
  • Added functions to load wave tables into dynamically allocated memory.
  • Modified functions to use the dynamically allocated wave tables.
  • Added functions to stop wave generation and free the allocated memory.
src/instruments/wavegenerator.c
src/instruments/wavegenerator.h
Implemented dynamic memory allocation for sample buffers in the interval capture module.
  • Modified capture functions to allocate sample buffers dynamically.
  • Added error handling for memory allocation failures.
  • Modified capture functions to free allocated memory after capture.
  • Updated the get_state function to return DMA addresses and digital states.
  • Added a fetch_buffer function to return the captured data and free the allocated memory.
src/helpers/interval.c
src/helpers/interval.h
Implemented dynamic memory allocation for sample buffers in the oscilloscope module.
  • Modified capture functions to allocate sample buffers dynamically.
  • Added error handling for memory allocation failures.
  • Modified capture functions to free allocated memory after capture.
  • Updated the fetch_samples function to return the captured data and free the allocated memory.
  • Modified the get_capture_status function to return capture status.
src/instruments/oscilloscope.c
src/instruments/oscilloscope.h
Modified command functions to use dynamically allocated memory for return values.
  • Modified command functions to allocate memory for return values using malloc.
  • Modified command functions to set the rets pointer to the allocated memory.
  • Modified command functions to set the rets_size to the size of the allocated memory.
  • Updated the packet handler to free the allocated memory after sending the response.
src/bus/i2c/i2c.c
src/transport/packet_handler.c
src/transport/uart2.c
src/helpers/device.c
src/instruments/multimeter.c
src/instruments/sensors.c
src/bus/spi/spi.c
src/helpers/rtc.c
Updated the logic analyzer module to use dynamic memory allocation.
  • Removed the dependency on the buffer module.
  • Updated the capture functions to use the interval capture module.
  • Removed the stop function.
src/instruments/logicanalyzer.c
src/instruments/logicanalyzer.h
Updated the power source module to use the new argument passing convention.
  • Updated the SetPower function to use the new argument passing convention.
src/instruments/powersource.c
src/instruments/powersource.h
Updated the SD card module to use the new argument passing convention.
  • Updated the mount, unmount, stat, open, close, write, and read functions to use the new argument passing convention.
src/sdcard/sdcard.c
src/sdcard/sdcard.h
Updated the command table to remove the buffer module.
  • Removed the buffer module from the command table.
src/commands.c
Updated the UART2 module to use the new argument passing convention.
  • Updated the read, write, set_mode, set_baud, flush_rx, and rx_ready functions to use the new argument passing convention.
src/transport/uart2.c
src/transport/uart2.h
Updated the device module to use the new argument passing convention.
  • Updated the get_hw_version, get_fw_version, read_register, and write_register functions to use the new argument passing convention.
src/helpers/device.c
src/helpers/device.h
Updated the RTC module to use the new argument passing convention.
  • Updated the cmd_set_time and cmd_get_time functions to use the new argument passing convention.
src/helpers/rtc.c
src/helpers/rtc.h
Updated the SPI module to use the new argument passing convention.
  • Updated the cmd_open, cmd_close, cmd_set_mode, cmd_set_clock, cmd_set_word_size, cmd_read, cmd_write, and cmd_exchange functions to use the new argument passing convention.
src/bus/spi/spi.c
src/bus/spi/spi.h
Updated the ADC1 module to use the new argument passing convention.
  • Updated the SetOperationMode function to use the new argument passing convention.
src/registers/converters/adc1.c
Updated the pin manager module to use the new argument passing convention.
  • Updated the set_sq_pin_state and get_la_pin_state functions to use the new argument passing convention.
src/registers/system/pin_manager.c
src/registers/system/pin_manager.h
Updated the CTMU module to use the new argument passing convention.
  • Updated the CTMU_start and CTMU_stop functions to use the new argument passing convention.
src/registers/converters/ctmu.c
src/registers/converters/ctmu.h
Updated the sensors module to use the new argument passing convention.
  • Updated the start_counter and get_counter functions to use the new argument passing convention.
src/instruments/sensors.c
src/instruments/sensors.h
Removed the buffer module.
  • Removed the buffer module from the project.
  • Removed the buffer module from the CMakeLists.txt file.
src/helpers/CMakeLists.txt
src/helpers/buffer.c
src/helpers/buffer.h
Added a heap size to the linker flags.
  • Added --heap=0x6000 to the linker flags.
CMakeLists.txt
Added ESP01 target.
  • Added ESP01 option to CMakeLists.txt.
  • Added ESP01 conditional compilation to CMakeLists.txt.
  • Added ESP01 conditional compilation to wavegenerator.c.
  • Added ESP01 conditional compilation to commands.c.
  • Added ESP01 conditional compilation to uart2.c.
CMakeLists.txt
src/instruments/wavegenerator.c
src/commands.c
src/transport/uart2.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bessman bessman changed the base branch from main to develop April 2, 2025 15:00
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

We encountered an error and are unable to review this PR. We have been notified and are working to fix it.

You can try again by commenting this pull request with @sourcery-ai review, or contact us for help.

@bessman
Copy link
Collaborator Author

bessman commented Apr 3, 2025

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @bessman - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The function signatures have changed to pass arguments by double pointer, but some functions still copy the arguments into local variables, which is unnecessary.
  • Consider adding error handling for when malloc returns NULL in functions that allocate memory.
  • It looks like the buffer.c and buffer.h files were removed, but there are still references to BUFFER_sample_buffer that need to be removed.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@bessman
Copy link
Collaborator Author

bessman commented Apr 3, 2025

@sourcery-ai summary
@sourcery-ai guide

@bessman
Copy link
Collaborator Author

bessman commented Apr 3, 2025

  • The function signatures have changed to pass arguments by double pointer, but some functions still copy the arguments into local variables, which is unnecessary.

@sourcery-ai Provide an example from the code of what you mean by this, with an explanation of why you think it is unnecessary.

@bessman bessman merged commit 990a258 into fossasia:develop Apr 3, 2025
3 of 4 checks passed
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.

1 participant