Page 1 of 1
Porting Newlib
Posted: Tue Mar 03, 2009 6:22 am
by AndrewAPrice
I have a few questions about some of the wrappers defined in
Porting Newlib.
First of all;
I'm imagining that "incr" is how many bytes to allocate, and it returns the address of the newly allocated space. Will this (or how can I make it) be aligned to a size (e.g. allocate in multiples of 4kb pages). (How) does it free memory?
Second question:
Code: Select all
int execve(char *name, char **argv, char **env)
int fork()
I don't have fork (and didn't plan on having it - my process manager creates a new process and loads the executable image straight in to it), so is it possible to make newlib fit with this, or would it be too much trouble and I might aswell add fork/execve?
Re: Porting Newlib
Posted: Tue Mar 03, 2009 6:32 am
by pcmattman
The incr argument to sbrk can be any arbitrary number, it does not need to be page aligned. In my experience it is page aligned
generally, as in, most of the time on my system. You should still be able to handle the odd case out though.
As for freeing memory - I've never had the newlib sbrk() called with a negative incr variable. Generally it's left up to the OS to clean up.
Execve merely loads the given executable image (technically, it's supposed to load and replace the current address space).
The easiest thing to do if your system has no support is:
Code: Select all
int fork()
{
errno = ENOSYS;
return -1;
}
Of course, if an application you port that uses newlib requires fork, you're in a bit of trouble.
Re: Porting Newlib
Posted: Tue Mar 03, 2009 6:43 am
by JamesM
Sbrk often gets called with non-page-aligned values. Do not rely on the values being page aligned.
Re: Porting Newlib
Posted: Tue Mar 03, 2009 7:13 am
by AndrewAPrice
pcmattman wrote:As for freeing memory - I've never had the newlib sbrk() called with a negative incr variable. Generally it's left up to the OS to clean up.
How do I know when to clean up? I'm assuming newlib calls sbrk() to get memory from the OS for it's malloc/free implementation, but what call does it make to release memory?
You've convinced me on adding fork.
Re: Porting Newlib
Posted: Tue Mar 03, 2009 7:58 am
by JamesM
MessiahAndrw wrote:pcmattman wrote:As for freeing memory - I've never had the newlib sbrk() called with a negative incr variable. Generally it's left up to the OS to clean up.
How do I know when to clean up? I'm assuming newlib calls sbrk() to get memory from the OS for it's malloc/free implementation, but what call does it make to release memory?
You've convinced me on adding fork.
To release memory newlib calls sbrk with a negative argument, to move the breakpoint lower in memory.
Re: Porting Newlib
Posted: Tue Mar 03, 2009 8:05 pm
by AndrewAPrice
So basically for sbrk, I keep an address which marks the end of my heap. When sbrk is called, I increment/decrement this address and as it passes page boundaries I allocate/free the page?
Also, this means that pages must be in a linear order (in virtual memory)? This could be an issue because if I'm also allocating/releasing pages outside of sbrk I would end up fragmenting the virtual memory.
Is it possible to replace newlib's allocator with mine (which works with my paging as-in) and ignore sbrk?
Re: Porting Newlib
Posted: Wed Mar 04, 2009 1:28 am
by xyzzy
If you have an mmap()-style interface for managing the virtual address space, you can patch malloc in newlib use that rather than sbrk. I did it when I used newlib for my OS.
Re: Porting Newlib
Posted: Wed Mar 04, 2009 8:13 am
by AndrewAPrice
I would still really like to swap malloc/free with another (nedmalloc) which should be easy enough (find out where newlib's allocator is and remove it, then link with mine).
So I was wondering if anything else relies on sbrk or there's something people in a similar situation can tell me will break?
Re: Porting Newlib
Posted: Wed Mar 04, 2009 2:05 pm
by xyzzy
MessiahAndrw wrote:I would still really like to swap malloc/free with another (nedmalloc) which should be easy enough (find out where newlib's allocator is and remove it, then link with mine).
So I was wondering if anything else relies on sbrk or there's something people in a similar situation can tell me will break?
I don't even have an sbrk/brk function. I replaced the malloc implementation in newlib with a dlmalloc that uses mmap and friends by placing some files in my system target directory in the source tree. IIRC there's another port, probably the Linux one, that does the same - have a look at it to see how its done.
Re: Porting Newlib
Posted: Wed Mar 04, 2009 4:51 pm
by JamesM
AlexExtreme wrote:MessiahAndrw wrote:I would still really like to swap malloc/free with another (nedmalloc) which should be easy enough (find out where newlib's allocator is and remove it, then link with mine).
So I was wondering if anything else relies on sbrk or there's something people in a similar situation can tell me will break?
I don't even have an sbrk/brk function. I replaced the malloc implementation in newlib with a dlmalloc that uses mmap and friends by placing some files in my system target directory in the source tree. IIRC there's another port, probably the Linux one, that does the same - have a look at it to see how its done.
IIRC there's a MALLOC_PROVIDED flag of some sort that you can set, in which case it will skip compiling its own malloc.