sbrk instruction

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
Fukuda

sbrk instruction

Post by Fukuda »

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

Post by Tim »

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".
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:sbrk instruction

Post by df »

sbrk, archaic old **** that should not be in an os ;)
-- Stu --
BI lazy

Re:sbrk instruction

Post by BI lazy »

rather use morecore. it is more friendly to your processes... *ggg*
Fukuda

Re:sbrk instruction

Post by Fukuda »

BI lazy wrote: rather use morecore. it is more friendly to your processes... *ggg*
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.

Code: Select all

void *
_default_morecore(long size)
{
    void *result;

    result = sbrk(size);
    if (result == (void *) -1)
    return NULL;
    return result;
}
Nearly, all the morecore functions that I found is same. Calls sbrk function. And some of you say, dont use sbrk in your os. Should I use morecore and call sbrk in it???

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

Post by Tim »

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 :).
Fukuda

Re:sbrk instruction

Post by Fukuda »

great Tim. ;)
Post Reply