Skip to content

make _sbrk() weak (for malloc() from external memory) #5638

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

Closed
bmcdonnell-ionx opened this issue Dec 1, 2017 · 3 comments
Closed

make _sbrk() weak (for malloc() from external memory) #5638

bmcdonnell-ionx opened this issue Dec 1, 2017 · 3 comments

Comments

@bmcdonnell-ionx
Copy link
Contributor

Description

  • Type: Bug | Enhancement
  • Priority: Minor

Bug

Target
LPC40xx

Toolchain:
GCC_ARM

mbed-os sha:
(182bbd5)


Enhancement

Suggested enhancement

In the file mbed-os/platform/mbed_retarget.cpp, please add the attribute MBED_WEAK (or equivalent) to the function definition _sbrk(), to allow the user to override it.

extern "C" MBED_WEAK caddr_t _sbrk(int incr) {
    // [snip...]
}

Reason to enhance or problem with existing solution

An approach sometimes used to access external memory in embedded systems is to have all stack memory use internal RAM, and all dynamic memory (e.g. malloc()) allocate memory from external RAM. This is especially useful during development, to at least temporarily skip over the often difficult task of editing linker files.

Embedded Artists' EALib takes this approach to enable access to an external SDRAM chip. In sdram.cpp, it defines/overrides the _sbrk() function, which is used by malloc(), (etc.), to manage the heap.

This worked in older versions of mbed (at least through rev 120), but I cannot build it in the latest mbed-os, due to _sbrk() already being defined in mbed_retarget.cpp. Please make the mbed-os _sbrk() weak, to allow EALib to use the SDRAM as intended.

Pros

Described above.

Cons

Problematic if another library also defines _sbrk() weakly.

@0xc0170
Copy link
Contributor

0xc0170 commented Dec 4, 2017

@c1728p9 @SenRamakri

@bmcdonnell-ionx
Copy link
Contributor Author

bmcdonnell-ionx commented Dec 6, 2017

#1819 was trying to do the same thing for a different platform, but it takes a different approach. I don't think it's the right approach here, because it's making mbed-os always use the external memory for dynamic memory for that platform, whereas I want to give the user the option to do so.

But I give details here for reference.


Upon closing that issue, @ccli8 said:

I overwrite _sbrk with conditional compile in retarget.cpp. This modification has merged and so I close the issue.

I think he's referring to these commits, in particular the changes to platform/retarget.cpp:
b954780
dfc3240

(EDIT: I think I'm wrong about which commits.)

@bmcdonnell-ionx
Copy link
Contributor Author

PR merged.

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

No branches or pull requests

2 participants