OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Apr 24, 2017 5:01 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Malloc Paging and Mapping Buffers
PostPosted: Fri Sep 02, 2016 1:24 am 
Offline
Member
Member

Joined: Fri Aug 07, 2015 6:13 am
Posts: 655
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...

_________________
Working on Basic OS, C++ Combined Kernel. OSDev is thrilling and exciting!
What does it take to learn Assembly? Your entire lifetime.


Top
 Profile  
 
 Post subject: Re: Malloc Paging and Mapping Buffers
PostPosted: Fri Sep 02, 2016 1:53 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 752
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().


Top
 Profile  
 
 Post subject: Re: Malloc Paging and Mapping Buffers
PostPosted: Fri Sep 02, 2016 2:15 am 
Offline
Member
Member

Joined: Fri Aug 07, 2015 6:13 am
Posts: 655
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.

_________________
Working on Basic OS, C++ Combined Kernel. OSDev is thrilling and exciting!
What does it take to learn Assembly? Your entire lifetime.


Top
 Profile  
 
 Post subject: Re: Malloc Paging and Mapping Buffers
PostPosted: Fri Sep 02, 2016 2:18 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 752
Think! You can test (s)brk as well! :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group