Modifying malloc( ) implementation, without changing malloc( ) source code
I have a big chunk of memory (128M), which is divided into 4 memory banks.. Now, I am supposed to make efficient memory utilization,and for that purpose, I NEED TO ALLOCATE MEMORY FROM THE ADDRESSES I PROVIDE. Now, here's what the problem... We have a system defined malloc( ) source which I'm not supposed to change.
So, one way I can think of is...
do a malloc and compare with the address I want to allocate memory at.
if not equal, then call malloc again, & free the previous memory
continue this till I reach the address I want to allocate memory at
But this is really very inefficient and slow method..
Since I'm not supposed to change the malloc source code,
Does somebody have any idea how to trick the heap manager, or by some pointer manipulations, or by writing wrapper routines, anyway, so that I can allocate a memory chunk at the address I provide.
Please help,
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
im sorry its not possible.
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
If you have a fixed chunk of memory that already has been allocated, then you do not need to worry about malloc or the system memory manager.
What you want is a memory pool. You need to create one or more classes which keep track of the parts of your chunk of memory which are in use and the parts which are not in use and allow other classes to 'allocate' and 'free' memory in that memory range.
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
I am sorry if I didn't get you properly.
Now, I already have a memory pool. It is divided into say 4 parts, each one a memory bank.
Now, even if I have different classes, they all are ultimately going to call malloc, which would automatically allocate memory from the heap as available, but what I want is write some routine which would allocate that particular chunk at the address I provide
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
Why do they need to call malloc? Why can they not call your own memory management functions?
If for some reason the other functions must call malloc / free, then there are only two options I can see:
Do not link to the default implementation of malloc but write your own implementation of malloc (but it sounds like you are not allowed to)
or
#define malloc mempool_malloc
#define free mempool_free
for the code that needs to use that memory range. This second option is not recommended.
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
Quote:
Originally Posted by abheei25
what I want is write some routine which would allocate that particular chunk at the address I provide
is this adress inside one of your "memory banks" or not?
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
Quote:
Originally Posted by Mitsukai
is this adress inside one of your "memory banks" or not?
Yes, the address provided would be in one of the memory banks, since we are dividing the available space into different memory banks
Quote:
#define malloc mempool_malloc
#define free mempool_free
even if I write different implementation for malloc, it is ultimately the heap manager which allocates the space as available, so I don't think this would work fine(?)
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
k this is what u do..
Code:
void* myalloc(const size_t size)
{
static unsigned char bank[4][128 * 1024];
//manage your **** here
//and return the open memory.
}
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
Quote:
Originally Posted by Mitsukai
Code:
void* myalloc(const size_t size)
{
static unsigned char bank[4][128 * 1024];
//manage your **** here
//and return the open memory.
}
But even after deriving a particular address from the implementation, it is the heap manager that actually allocates the memory, so how would I get the heap manager to allocate space of "size" capacity at the address I provide
Can you please the implementation part??
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
you just asked the hardest part...
i jsut want to ask why u are u doing this, the operating system should take care of the dynamic allocation....
ok.. first u must somehow know where are all the allocations made. u can make another array for this. actually use an array of pair<pointer, size> so u know where evything is allocated... you could also use set instead of an array so you can insert the paird ordered and increases speed for searching for open memory.
i really think you should do this for yourselve, and if you encounter any problems i can help you further.
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
Quote:
Originally Posted by abheei25
how would I get the heap manager to allocate space of "size" capacity at the address I provide
For that, you need to use an OS specific call.
In Windows, you can use VirtualAlloc.
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
Quote:
Originally Posted by Zaccheus
For that, you need to use an OS specific call.
In Windows, you can use
VirtualAlloc.
thats not what he wants, he wants to make a memory manaager hisselve, this adreess hes talking about is in his allocated memory "banks"
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
Quote:
Originally Posted by abheei25
Yes, the address provided would be in one of the memory banks, since we are dividing the available space into different memory banks
even if I write different implementation for malloc, it is ultimately the heap manager which allocates the space as available, so I don't think this would work fine(?)
this is what he sayd. and i gave him the correct answer
Re: Modifying malloc( ) implementation, without changing malloc( ) source code
Quote:
Originally Posted by abheei25
... how would I get the heap manager to allocate space of "size" capacity at the address I provide ...
;)
Let's just see what he says.
:)