Hello, I have couple of memory related questions.
1.Does malloc need to be aware of paging? If so does that mean that I have to map every allocation with mapPhysicalToVirtual?
2.Do I need to map my linear frame buffer using malloc so it gets sort of protected from overwriting? (I am already mapping it using physicalToVirtual)
3.Does back buffer for double buffer system need to be mapped using physicalToVirtual as well?
4.How do I test malloc and see if it is all okay?
5.One more question, I can't remember it...
Malloc Paging and Mapping Buffers
Malloc Paging and Mapping Buffers
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Re: Malloc Paging and Mapping Buffers
It may be very loosely aware of page translation and virtual memory. For example, you may have a mechanism to automatically map new pages into the address space when they're accessed. In this case malloc() needs not do anything (or much). If you make this mechanism work in selected regions of the address space, malloc() will only need to enable it once for the region that is reserved for the heap.octacone wrote: 1.Does malloc need to be aware of paging? If so does that mean that I have to map every allocation with mapPhysicalToVirtual?
A somewhat similar approach is when you implement your heap on top of (s)brk. The brk system call may perform the mapping and unmapping and malloc() will only need to call this system call to extend the heap size. ((s)brk works like allocating stack storage by moving the stack pointer, look it up)
Otherwise malloc() will need to do something every time it needs a new page of memory (or can release a page to the system).
malloc() is intended to be used to allocate arbitrary memory for use (see any C reference), which implies address space allocation, page frame allocation and mapping of physical pages into the address space. The frame buffer already is memory and it has a very specific use unlike RAM. It is not supposed to be a store of pages for arbitrary use. It just needs to be mapped into the address space.octacone wrote: 2.Do I need to map my linear frame buffer using malloc so it gets sort of protected from overwriting? (I am already mapping it using physicalToVirtual)
Any kind of memory that software needs to access must be mapped into the address space, whether it's RAM or a memory-mapped device.octacone wrote: 3.Does back buffer for double buffer system need to be mapped using physicalToVirtual as well?
Use your imagination. That's what OS devers are supposed to do.octacone wrote: 4.How do I test malloc and see if it is all okay?
My advice is to test separately: page frame (de)allocation, (un)mapping, address space (de)allocation, malloc()/free(). Don't forget to test the underlying linked lists and sorted trees. Actually, do that first. When it works in a single thread, extend the test for multiple threads concurrently running malloc() and free().
Re: Malloc Paging and Mapping Buffers
Thanks for replying.
Currently I am using this homemade sbrk:
kernelEnd is just a pointer located at the end of my linker file. Is this implementation somewhat correct?
I can't really test multiple threads because I don't have a working multitasking. My paging system is very crude and limited without many functions.
Currently I am using this homemade sbrk:
Code: Select all
void* sbrk(int bytes)
{
char* base;
base = kernelEnd + 0x1000 & ~0xFFF;
base += bytes;
kernelEnd = base;
return base;
}
I can't really test multiple threads because I don't have a working multitasking. My paging system is very crude and limited without many functions.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Re: Malloc Paging and Mapping Buffers
Think! You can test (s)brk as well!