Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying malloc implementation

Status
Not open for further replies.

abheei25

Programmer
Feb 16, 2007
3
US
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,
 
Maybe a better question is, why do you need to allocate memory at a specific address? Maybe we can find a better solution.
 
> But this is really very inefficient and slow method..
It also presumes that successive calls to malloc return increasing memory addresses. Some of them do not.

Like cpjust says, state the problem not your solution.

From the way you've worded it, it sounds like you're trying to get an address which is in range of some memory mapped hardware.

Windows uses virtual memory, so this whole "at an address" thing doesn't make sense. Some addresses in the virtual address space never get mapped into your user program, no matter how much you try.

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
In response to cpjust's question, I need to allocate memory at a specific address in order to have efficient memory utilization. In a nutshell, the memory buffer is divided into 4 parts (called memory banks), and each part is accessed by a connector(a hardware component)... allocating memory at a specific address in different memory banks, leads to driver optimization and reduces collisions.

In response to Salem, since the code doesn't run on a windows system, (we have a different processor system, compiler) it doesn't have any issues of virtual memory. Whatever is the memory addressable, its right there on the buffer.
 
I think this should work:
Code:
void * MyMalloc ( SIZE_T requiredSize )
{
  if ( SomeErrorLikeOutOfMemory )
     { return NULL; }
  DoInternalHouseKeeping( );
  return AddressProvidedByMe;
}

#define malloc MyMalloc
Don't forget to do something similar with free (and maybe others like realloc)




Marcel
 
> In response to Salem, since the code doesn't run on a windows system
Since this forum is specifically for Windows programming where the natural assumption is that you're a windows programmer (your post is now off-topic), don't you think this is critical information?

Basically, I see no point in wasting time on trying to answer questions if you're just going to reply, "oh, by the way, this isn't windows at all".
Do you have any other useful information which you might not be telling us, like which embedded RTOS and compiler you're using (now you're here).

Skip the malloc step and just assign a pointer to the base address?
[tt]char *base = (char *)0x8000000;[/tt]

Then if your buffer is 1K into that space, you do
[tt]char *buffer = &base[1024];[/tt]



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Salem: I don't understand why you feel so offended, or why did u feel I was rude. I did not have any intent of offending anybody.

Anyways, the system we have uses ARM processor and it has its own compiler

Also,
***********************************************************
>Skip the malloc step and just assign a pointer to the base address?
char *base = (char *)0x8000000;

Then if your buffer is 1K into that space, you do
char *buffer = &base[1024];

**********************************************************

This would just store the address of 1024th character from base. How will I be able to allocate space for 1k without using malloc in this case?
 
I don't know anything about the ARM processor, so anything else I say here might be total BS, but I'll just take some wild guesses... :p

Are you writing your own OS or using an existing one?
Maybe the OS has a custom memory allocation API (i.e. not standard C/C++) that could do something like requesting a size and specific address?

One idea that's similar to your first one would be to keep mallocing small chunks of memory (but don't free it yet, just keep track of the pointers) and once you get the address you want, then free all the other pointers you made. I'm not sure how evil and resource intensive that would be, but if all else fails, it's worth a shot.
 
I think your question is "how do I write a malloc-like thing that allocates and frees memory, but instead of drawing on one heap, draws on 4 separate heaps whose physical addresses I know".

If so, you're starting on a hard road because your code has got to do everything that malloc does. Salem is right, you can return a pointer pointing to some place in any of your memory banks. Malloc does, in fact, do exactly that. But it also remembers what it has reserved, where, and how much. If you choose to rewrite malloc, you have to do that too. The subject is an enormous and detailed one (chapters of Knuth); you don't want to go there unless you're an expert, or willing to become one.

On a more practical note, at a simple level, if you can't change malloc, and it won't do your job, don't call it; calling it repeatedly until you get the answer you want is utterly pointless. You'll have to call something else. If you are lucky, and your memory allocation calls are always in first-in-last-out order, you needn't do a proper heap allocation system at all. You can simply maintain a record of the top of the occupied space, and return the next memory location along, while moving the top-marker, every time memory is called. If you're less lucky, and memory calling/freeing is more random (but hopefully infrequent), you can simply maintain a list of free blocks, and put anything new in the first free block that's big enough. But you will get fragmentation. If you're feeling really clever, rather than keeping a block-list (for which you'llbe back to malloc!), mark the first block with an invisible pre-block containing its size (which tells you the start of the next block), and then the block-list is actually part of the heap you're maintaining.

Incidentally, this is actually the Visual C++ forum, so some might argue that even malloc is a bit of an off-topic subject, since visual C++ is famous for its garbage collected heap, a different fish altogether.
 
Since this is C++, why not just redefine the ::new operator?

You do know that you can allocate memory in C++ at a specific address using something like
Code:
char* xxx = new (0x80000) char[1024];
Yes - this is valid C++ syntax even though about 90% of C++ programmers have never seen it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top