Allocating memory

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
Jerry

Allocating memory

Post by Jerry »

Since I need to write my own mallocfunction, which is the best way of doing it? Since both applications and the kernel should be able to do this.. Should I use software interrupts to allocate memory? Suppose I have an application running, and I want to allocate some memory..in some way I need to tell the OS and get a pointer to the memorylocation.. but how can I do this? To let other applications use methods in my kernel
proxy

Re:Allocating memory

Post by proxy »

the typical unix (should i say POSIX?) model is to have a syscall similar to sbrk which adjusts the data segment of the address space of a user space program, after this is done, malloc is a user space function which will manage this new memory. The kernel can of course adjust and map pages directly.

so basically (this is a simple example), suppose you need 4k of memory and your current data section does not have enough room for it. You would do a syscall "sbrk' (or whatever) to exapnd your address space by at least 1 page and map a new page in. After that, your user space malloc library can manage that as it feel free.

Usually if the allocations are less that a page, sbrk will map the next page but keep a pointer to the "end' of your address space so if the next request lands in the same page, it just updates the pointer.

proxy
Jerry

Re:Allocating memory

Post by Jerry »

How do I do syscalls? Software interrupts? call gates?
Ozguxxx

Re:Allocating memory

Post by Ozguxxx »

Yes I think software interrupts is the best way to handle system calls, honestly I dont know any other method. Use one interrupt and set one register to request different functions, like real mode biod interrupts. For passing parameters you can use registers or kernel can directly examine user stack (which is the way I am doing) to get parameters to system call.
Jerry

Re:Allocating memory

Post by Jerry »

Alright thank you! so I should do something like this?
in my malloc-function:
push size_you_wanna_allocate
int xxx
; The kernel is finding som free space and pushes the adress.
pop [adress]..
Post Reply