OSDev.org
https://forum.osdev.org/

Malloc Paging and Mapping Buffers
https://forum.osdev.org/viewtopic.php?f=15&t=30777
Page 1 of 1

Author:  Octacone [ Fri Sep 02, 2016 1:24 am ]
Post subject:  Malloc Paging and Mapping Buffers

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...

Author:  alexfru [ Fri Sep 02, 2016 1:53 am ]
Post subject:  Re: Malloc Paging and Mapping Buffers

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?

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.

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

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)

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:
3.Does back buffer for double buffer system need to be mapped using physicalToVirtual as well?

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:
4.How do I test malloc and see if it is all okay?

Use your imagination. That's what OS devers are supposed to do. :)

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().

Author:  Octacone [ Fri Sep 02, 2016 2:15 am ]
Post subject:  Re: Malloc Paging and Mapping Buffers

Thanks for replying. :)
Currently I am using this homemade sbrk:
Code:
void* sbrk(int bytes)
{
   char* base;
   base = kernelEnd + 0x1000 & ~0xFFF;
   base += bytes;
   kernelEnd = base;
   return base;
}

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.

Author:  alexfru [ Fri Sep 02, 2016 2:18 am ]
Post subject:  Re: Malloc Paging and Mapping Buffers

Think! You can test (s)brk as well! :)

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/