you don't track which bytes are in use
reread what i posted:
you call malloc, malloc realizes that it needs more memory, so it makes a syscall for another page of memory
malloc then places a number at the base of that block, telling how long the block to be sent to the program is (basically a pointer to the next byte after the last byte the program will use)
then return the pointer to the program (this should point to the byte immediately following the block pointer)
create another block after that one, which contains the remainder of the requested page, and add a pointer to that, to your list of free blocks
next time the program calls malloc, another block will be created by splitting the 'free' block
when free is called, check the bytes before the pointer -- it should be a pointer to the end of the allocated block, if it points to a free block, then combine them (by changeing the end_of_block pointer) -- and remove the other block from the 'free list'
check to see if this block is at the end of another free block, and if so, combine them
add the freed block to your 'free list' -- unless you just added it to the end of an existing free block -- in that case its already in the free list (if you track lengths in the free list then that will need to be modified)
these are variable length blocks, and you don't need to track individual bytes because you track these blocks, (which are not the same as pages, and can be as small as 1 byte)
search for 'dlmalloc' for an example of a functional malloc implementation
How do you write a memory manager
-
- Member
- Posts: 144
- Joined: Tue Oct 26, 2004 11:00 pm
- Location: Australia
Re: How do you write a memory manager
hcker83 I have a piece of code that does allocation for a kernel heap...but it only uses the fist 1Mb.
You can e-mail me if you want it.
For anything outside of your kernel, use a manager, not an allocator.
You can e-mail me if you want it.
For anything outside of your kernel, use a manager, not an allocator.
Two things are infinite: The universe and human stupidity. But I'm not quite sure about the universe.
--- Albert Einstein
--- Albert Einstein
-
- Member
- Posts: 132
- Joined: Wed Nov 03, 2004 12:00 am
- Location: Austria
- Contact:
Re: How do you write a memory manager
hmm...
do ya really need an allocation of 50 bytes ?
the question should be is it really useful to impelement such a method ?
perhaps, but if you want to alloc 50 bytes you first have to alloc one page and then pass on the pointer, and the app writes 50 bytes in there ....
I think it's easier to work with pages, and if the application only needs 50 bytes of a page it's ok
A page is only 4096 bytes "big", in these days, what are 4kb if there 512 MB memory ?
Look at my source code at http://nexus-os.2.ag
there you can find an probably implementation of kmalloc, free,valloc,vfree
do ya really need an allocation of 50 bytes ?
the question should be is it really useful to impelement such a method ?
perhaps, but if you want to alloc 50 bytes you first have to alloc one page and then pass on the pointer, and the app writes 50 bytes in there ....
I think it's easier to work with pages, and if the application only needs 50 bytes of a page it's ok
A page is only 4096 bytes "big", in these days, what are 4kb if there 512 MB memory ?
Look at my source code at http://nexus-os.2.ag
there you can find an probably implementation of kmalloc, free,valloc,vfree
-
- Member
- Posts: 144
- Joined: Tue Oct 26, 2004 11:00 pm
- Location: Australia
Re: How do you write a memory manager
It would be handy to have this. One example of this is memory in the first 1Mb. You are limited in space here and this is the only place you can put a DMA buffer...blackcatcoder wrote: do ya really need an allocation of 50 bytes ?
the question should be is it really useful to impelement such a method ?
Well a good programmer should always aim for code that is efficient in terms of memory usage and CPU usage. Yes we do have 512Mb RAM and 3GHz CPUs...but if your OS wastes memory and CPU, the programs that are running on top of it will also waste CPU and memory and it all adds up in the end.blackcatcoder wrote: A page is only 4096 bytes "big", in these days, what are 4kb if there 512 MB memory ?
Two things are infinite: The universe and human stupidity. But I'm not quite sure about the universe.
--- Albert Einstein
--- Albert Einstein
Re: How do you write a memory manager
but this will often make it less efficient
Well a good programmer should always aim for code that is efficient in terms of memory usage and CPU usage. Yes we do have 512Mb RAM and 3GHz CPUs...but if your OS wastes memory and CPU, the programs that are running on top of it will also waste CPU and memory and it all adds up in the end.
most OSs cannot allocate less than a full page
the application handles malloc and free, and subdevides the pages as needed, and only relies on the OS for allocating and deallocating full pages (as it deturmines neccessary -- syscalls are very expensive, reletivly)
this is wrong -- ISA DMA buffers can be located anywhere in the first 16MB not 1MB (the reason for this is the ISA bus only has 24 address lines) and PCI DMA can be located anywhere in the first 4GB (32 address lines) -- of course unless your targeting your minimun specifications at P6 arch (Pentium Pro, PII), you cannot use more than that anyway
It would be handy to have this. One example of this is memory in the first 1Mb. You are limited in space here and this is the only place you can put a DMA buffer...
-
- Member
- Posts: 132
- Joined: Wed Nov 03, 2004 12:00 am
- Location: Austria
- Contact:
Re: How do you write a memory manager
jep, DMA buffers can be allocated up to 16 MB !
-
- Member
- Posts: 144
- Joined: Tue Oct 26, 2004 11:00 pm
- Location: Australia
Re: How do you write a memory manager
well my aim was backwards compadability in my OS.
I've made things simple and restricted all OS data structures to the first 1Mb (The 8-bit DMA can only use the first 1Mb guys). And besides I haven't been able to find many docs on the 32-bit DMA. All the code I've written for that hasn't worked so far :$
As for efficiency: This is a balance between using memory and CPU effectively. Using more memory can use less CPU, and visa versa. Using best programming practices ensures that you can effectively have both with little compromise. Being able to allocate and deallocate memory within a kernel heap is handy for me, because I can ensure that I'm using as little memory as possible.
Once I've finished testing I might consider using some sort of static allocation mechanism. In the past I've found that static allocation while I'm in testing phase has led me to problems because I've always had difficulty keeping track of where my data structures are in memory, and the linker never seems to be any good at that either.
One day I'll get all that working :p
I've made things simple and restricted all OS data structures to the first 1Mb (The 8-bit DMA can only use the first 1Mb guys). And besides I haven't been able to find many docs on the 32-bit DMA. All the code I've written for that hasn't worked so far :$
As for efficiency: This is a balance between using memory and CPU effectively. Using more memory can use less CPU, and visa versa. Using best programming practices ensures that you can effectively have both with little compromise. Being able to allocate and deallocate memory within a kernel heap is handy for me, because I can ensure that I'm using as little memory as possible.
Once I've finished testing I might consider using some sort of static allocation mechanism. In the past I've found that static allocation while I'm in testing phase has led me to problems because I've always had difficulty keeping track of where my data structures are in memory, and the linker never seems to be any good at that either.
One day I'll get all that working :p
Two things are infinite: The universe and human stupidity. But I'm not quite sure about the universe.
--- Albert Einstein
--- Albert Einstein
Re: How do you write a memory manager
8 bit DMA is only for 8bit ISA -- which you won't find much past the original PC -- even the 286 supported 16bit, so i didn't even consider it
the reason you don't find much on 32bit DMA, is that (iirc) there isn't one -- each device handles its own DMA -- meaning every device must be programed specifically
(this is of course as i remember others talking about it -- havn't implemented it myself)
the reason you don't find much on 32bit DMA, is that (iirc) there isn't one -- each device handles its own DMA -- meaning every device must be programed specifically
(this is of course as i remember others talking about it -- havn't implemented it myself)
-
- Member
- Posts: 144
- Joined: Tue Oct 26, 2004 11:00 pm
- Location: Australia
Re: How do you write a memory manager
Yes that's true, PCI devices can access memory. I wouldn't class this as DMA specifically. That's more like the way shared video memory works...
As far as programming a FDC is concerned, you can't use anything higher than 16MB, unless you're not using DMA.
As far as programming a FDC is concerned, you can't use anything higher than 16MB, unless you're not using DMA.
Two things are infinite: The universe and human stupidity. But I'm not quite sure about the universe.
--- Albert Einstein
--- Albert Einstein