How to check and allocate memory from given adress range

Hi,below is my problem

3. A special hardware unit with some storage in it is connected to your computer and is memory-mapped so that its storage is accessible in the address range 0x55500000 – 0x555fffff. You want to interface this hardware unit to your C++ program so that dynamic memory is allocated in this hardware unit, not in your computer’s memory. Implement a class MyHardwareMemAllocator which has the following function.

void * allocMemoryInMyHardware(int numberOfBytesToAllocate);
which returns a pointer to the allocated memory chunk, or null if unable to allocate.

Clearly state any assumptions you made and why. C library calls like malloc are not allowed.


please help me to solve out this problem.
1)how to allocate memory from given address range.
2)how to check wether this required memory space is available or not for allocating

some body help me
In C/C++ no function is there to allocate memory in specified position/range.

You have to create your own memory allocation function like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
long *nextstart;
void * allocMemoryInMyHardware(int numberOfBytesToAllocate)
{
   long *addr;
   if(firstallocation)
   {
       addr = (long*)0x55500000;
   }
   else if(check whether available space is enough or not)
   {
      addr = nextstart++;
   }
   nextstart=addr+numberOfBytesToAllocate;
}


This is just logic/idea not exact code.
Are you trying to write a device driver ? Because only a device driver can directly communicate with hardware.
@modoran nope this is a asssignment problem.
@tvrameshmc- Il try it and let you know thank you very much
Topic archived. No new replies allowed.