1

If I have a special hardware unit with some storage in it is connected to the computer and is memory mapped, so that its storage is accessible in the address range 0x55500000 – 0x555fffff how an I interface this hardware unit to my C++ program so that dynamic memory is allocated in this hardware unit, not in my computer’s memory? I need to implement a class which has the following function in it.

void * allocMemoryInMyHardware(int numberOfBytesToAllocate);

which returns a pointer to the allocated memory chunk, or null if unable to allocate.

Kadiam
  • 303
  • 4
  • 12
  • 1
    You have to read this hardware's documentation. If the device is memory mapped, you communicate with it, by writing and reading from that memory (which does not necessarily change the device's internal memory state). I think we need more details. – Rafał Rawicki Jul 12 '12 at 07:34
  • @Rafal This is a hypothetical situation – Kadiam Jul 12 '12 at 07:43
  • 1
    So, design a hypothetical device specification or check out some of the existing ones. Especially, you have to define, what 'allocate' means in terms of this unit. If the hardware does not have an operating system it provides no mechanisms for allocating memory and you have to manually secure this fragment of memory from being used. – Rafał Rawicki Jul 12 '12 at 07:49
  • 1
    Solution is OS-specific and requires kernel-mode programming. You need to write driver for this device. Maybe it already has driver? Agree with Rafał Rawick, read device specification. – Alex F Jul 12 '12 at 07:52

2 Answers2

2

You need to write your own allocator. Search internet for a sample code and tweak it. If you have simple requirements, basic allocator can be written from scratch in 2-4 hours. This approach will work if your platform does not have virtual memory management and code can access your range of addresses directly. Otherwise you need to dive into the driver development on your platform.

Typical strategy is to add header to each allocated unit and organize a double linked list for free memory areas. NT heaps work in similar way.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
-1

I think you can use the placement new syntax for this purpose. Using this, you can tell where objects shall be constructed:

char memory[10];
int* i = new (memory) int(42);
Alexandre Hamez
  • 7,725
  • 2
  • 28
  • 39