|
-
November 30th, 2004, 07:26 PM
#1
Implementing malloc...
Yes this is a school project. Yes I do have problems understanding on how to implement it, I would like to bang out any misconceptions or misunderstandings that I have. This is supposed to use a first fit algorithm. Here is the thing...
I pass into that method the size of the memory block that I would like to have. I will most likely need to use sbrk and then brk in order to get a grip on the piece of virtual memory that will be filled in at a later time. I also will need to increase the stack that will hold the desired piece of memory.
My question is. How can I possibly search through the gaps in memory and find the desired piece? How would you implement that (sorry, I don't even know where to begin )?
-
November 30th, 2004, 08:40 PM
#2
Re: Implementing malloc...
I can't recall the exact details. Please correct me if I am wrong. It is roughly something like, for each allocated memory block, the first few bytes (header) have to store the memory block size. Beside that, this header also store a pointer to the next allocated memory block. In other words, the headers form a linked list that you can trace from one allocated memory block to another. Similarly, to mark where are your free memory blocks, free memory blocks also reserve a header with pointer to the next free memory block as well as the size of the present memory block.
-
November 30th, 2004, 09:10 PM
#3
Re: Implementing malloc...
 Originally Posted by Kheun
I can't recall the exact details. Please correct me if I am wrong. It is roughly something like, for each allocated memory block, the first few bytes (header) have to store the memory block size. Beside that, this header also store a pointer to the next allocated memory block. In other words, the headers form a linked list that you can trace from one allocated memory block to another. Similarly, to mark where are your free memory blocks, free memory blocks also reserve a header with pointer to the next free memory block as well as the size of the present memory block.
I'm aware of the fact that a linked list will need to be used (that comes later in the project.) I just don't know how to begin writing the method, do I need to make some special system call or something? Kind confused right now . I'll look up some stuff online and see what I can get.
-
November 30th, 2004, 09:48 PM
#4
Re: Implementing malloc...
 Originally Posted by YourSurrogateGod
do I need to make some special system call or something? Kind confused right now
If you want to use virtual memory for your customize heap, you need to use system calls. For example, in Windows, you can use VirtualAlloc(), VirtualFree(), etc. If not, the easiest is to use a block of global memory, in which you may have to increase the stack size.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|