Modifying malloc( ) implementation - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Modifying malloc( ) implementation

abheei25abheei25 Posts: 2Member
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,

Comments

  • CytCyt Posts: 557Member
    There is no way of doing this.

    Are you not in control of the caller (the functions which call malloc)? If you are, you could change the calling routines so that they call your own custom made malloc instead of the one provided with the compiler.

    It is also possible to trick the linker into changing all calls to a function (e.g. malloc and free) into other calls (e.g. MyMalloc and MyFree). But again, it depends what you actually have access to and what your exact problem is.
  • Chainsaw666Chainsaw666 Posts: 198Member
    [b][red]This message was edited by Chainsaw666 at 2007-2-16 23:59:34[/red][/b][hr]
    : 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,
    :
    :
    Ultimately, why are you trying to malloc data at a specific location? I don't see any direct advantage, since you have the memory in banks anyway, why do you want to specify addresses? Why not try to malloc the 128M then break it into banks? What is your ultimate goal here, more efficient memory utilization is not very clear to me. As far as your random reassignment idea, I'm sorry to disappoint, but like many brute force methods it will probably not work, 128M seperated into four equal banks still gives you 32M per bank, a rather large amount of memory. The odds of there being enough clear data to allocate a block of memory that big at one specific location is slim to nil unless you have 2GB of memory to access and a whole lot of luck.



  • LundinLundin Posts: 3,711Member
    As mentioned, there are two possible ways:

    - Tell the compiler/linker where to allocate the heap.
    - If not possible, or if you need to use more than 32mb, you have to write your own malloc routine.
Sign In or Register to comment.