Where to define MORECORE for Newlib?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Where to define MORECORE for Newlib?

Post by max »

Hey ho,

where should I define the MORECORE macro to make newlib use my custom function instead of sbrk? (is it the right way for the following?)

I want to use a different memory model, and for sbrk I must have one contiguous area.. but instead I want to give malloc a different way to get new chunks, but how can I define that?

Thanks :)
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Where to define MORECORE for Newlib?

Post by bluemoon »

max wrote:for sbrk I must have one contiguous area..
Although everybody just make a linear grow for program break, there is nothing stop you to return non-continuous addresses per each call.
(disclaimer, I have not tested it, but the interface of sbrk permit doing so).
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Where to define MORECORE for Newlib?

Post by sortie »

bluemoon: That's wrong, that's not how sbrk works.

I think this was discussed on #osdev at irc.freenode.net yesterday (probably the same author as this thread).
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: Where to define MORECORE for Newlib?

Post by max »

sortie wrote:bluemoon: That's wrong, that's not how sbrk works.

I think this was discussed on #osdev at irc.freenode.net yesterday (probably the same author as this thread).
Yep sortie, that was me :) well actually it really seems like there is no other (usable) solution, so I enabled paging with the following (flat) memory model:

There is one global page directory. It has the following layout (with example values [virtual addresses]):
- 0x00000000 - 0x00177000: Kernel area is identity mapped (calculated with linker symbols and the end of the ramdisk module)
- 0x00177000 - 0x1FEED000: Heap area is mapped as one big, contiguous area of memory (pointing to free chunks somewhere the memory), used as the global heap area

This allows me to just set the initial break to the start of the heap area (here 0x00177000), and then increase (or decrease) that address by the given value, and go out of memory if the end of the heap area is reached.

Just one question: Later, when I want to use DMA, I plan to reserve another identity-mapped area between the kernel and the heap area. So that should be no problem. But, is there anything else (when implementing drivers and stuff) that I maybe didnt plan here, and that could conflict with my memory model?

btw: I know that there is no security for processes, but thats on purpose. ;)
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Where to define MORECORE for Newlib?

Post by jnc100 »

As far as I remember, dlmalloc can be configured to use a mmap interface, rather that sbrk. I believe jemalloc does too. I don't know about others. You may want to investigate using one of these?

Regards,
John.
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: Where to define MORECORE for Newlib?

Post by max »

jnc100 wrote:As far as I remember, dlmalloc can be configured to use a mmap interface, rather that sbrk. I believe jemalloc does too. I don't know about others. You may want to investigate using one of these?

Regards,
John.
That would be a possibility, too. But I've implemented paging now, so that would be a step back :P
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Where to define MORECORE for Newlib?

Post by Kevin »

Hey, you'll be surprised, but you can use paging and a sane interface at the same time. ;)
Developer of tyndur - community OS of Lowlevel (German)
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Where to define MORECORE for Newlib?

Post by pcmattman »

The biggest problem with sbrk() is that it leaves you open to a lot of fragmentation. For example, if you allocate 100 MB, then allocate a couple bytes, and then free the 100 MB, your process is still using 100 MB of fully-paged memory!

mmap allows the heap to have 'holes' of freed memory that can be filled - for the example case, the 100 MB might be one big mmap, then the couple bytes might be another mmap (but perhaps then split into smaller chunks). Then when you free the 100 MB allocation, the 100 MB can be freed.

Paging helps avoid the need for contiguous physical memory. mmap is not a step back; sbrk is a step back :)
Post Reply