Skip to content

Optimize _sbrk_r when no increment is needed #14

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SilentButeo2
Copy link

During debug I hit the _sbrk_r() function, but the incr parameter was 0. Callstack points out it is mallinfo() that initiates the call.
Because the current code goes into a critical section (ST) or suspends all tasks (NXP) but doesn't change anything in between, this is optimized away.

Unless the _sbrk_r is also used do some limit checks, I think this adjustment is safe.
Tested with an ST setup.

Copy link
Owner

@DRNadler DRNadler left a comment

Choose a reason for hiding this comment

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

Thanks! Some questions though:

  1. How often is mallinfo called? Is this really worth adding code to optimize?
  2. If so, please add a comment about WHY this is worthwhile optimizing ie Happens when called from mallinfo twice per week
  3. Didn't you mean to cast the return value to (void*), not (char*)?
    Thanks again,
    Best Regards, Dave

@StefanBalt
Copy link

1. How often is mallinfo called? Is this really worth adding code to optimize?

I do not know why mallinfo() should be relevant as only sbrk() is changed.

2. If so, please add a comment about WHY this is worthwhile optimizing ie _Happens when called from mallinfo twice per week_

I think mallinfo() does not matter here.
sbrk() is called any time an allocation does not fit into the memory previously requested via sbrk().
Typically this happens a lot at the start of the program and fewer the longer it runs.

I am not sure if this optimization is needed but it seems cheap with only a few lines added.

3. Didn't you mean to cast the return value to (void*), not (char*)?

All returns in sbrk() cast to (char*), so I guess this is for consistency reasons.

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.

4 participants