sbrk instruction
-
Fukuda
sbrk instruction
Im implementing malloc function for my os, but I dont understand this sbrk stuff. I search net, but get lost in huge, most of nonsense, results. Ill be glad if someone explains and gives a link about it. thanks.
-
Tim
Re:sbrk instruction
sbrk is how malloc gets its memory from the OS when it has run out.
sbrk originates from Old Unix, where a program's address space looked like this:
(low memory) | Code | Data +BSS | (high memory)
sbrk(n) adjusts the position of the end of the BSS by n bytes. Clearly this method only works for the layout above; it doesn't consider fragmentation at all.
A better, and more flexible, way is to have the kernel provide a function whereby malloc can say "give me a chunk of n bytes anywhere".
sbrk originates from Old Unix, where a program's address space looked like this:
(low memory) | Code | Data +BSS | (high memory)
sbrk(n) adjusts the position of the end of the BSS by n bytes. Clearly this method only works for the layout above; it doesn't consider fragmentation at all.
A better, and more flexible, way is to have the kernel provide a function whereby malloc can say "give me a chunk of n bytes anywhere".
-
Fukuda
Re:sbrk instruction
As I understood sbrk is a bad stuff to use in an os. I should use 'morecore' instead of 'sbrk'. Here is a morecore function that I found on net to study. Sorry, I dont know the owner so can not give credits.BI lazy wrote: rather use morecore. it is more friendly to your processes... *ggg*
Code: Select all
void *
_default_morecore(long size)
{
void *result;
result = sbrk(size);
if (result == (void *) -1)
return NULL;
return result;
}
Indeed, Im implementing my malloc function. While doing so, I imagine that I have a big memory block to give piece by piece by malloc function. And now, I dont know how to get these big chunks. (I plan to use sbrk) I enabled paging and at map all virtual addrs on to their physical location. And I put a symbol at the end of my kernel end and know the memory after kernel is free(I dont use, so it should be, shouldt it?). Now how to get big chunks from that space to use with malloc? Every tutorial I found not interested in this. So I stuck here. I study lots of sbrk function, (not morecore because all morecore functions I found calls sbrk and I dont understand why I should use morecore instead of sbrk). All sbrk functions breaks free memory into pieces after a blank(dont know why) from end of kernel symbol.
If anyone have a good tutorial, Ill be glad. And explanations welcome. Thanks.
-
Tim
Re:sbrk instruction
Come up with a page-based virtual memory memory allocator in your kernel, which can allocate a group of pages, return the starting virtual address to the application (to morecore/malloc), and allocate and map physical pages when they are needed.
www.osdever.net, Memory Management 2 by somebody who might be familiar
.
www.osdever.net, Memory Management 2 by somebody who might be familiar
