Walter Walter -4 years ago 90
C Question

C/C++ Windows or Linux map random memory blocks into consecutive order

My requirement is to emulate an old motorola memory controller.

Basically the controller maps 8 x 8Kbyte memory blocks into a 64Kbyte address space for subsequent access by a CPU. The controller has access to upto 1024 such 8K blocks.

At the moment I have accomplished this by using a large flat array like so:

unsigned char Memory[1024][8192];
int MappedMemoryBank[8];
short int Address;

Then to access any particular memory address I use something like this

unsigned char aByte = Memory[MappedMemoryBank[Address>>13]][Address&0x1FFF];

This works fine but as memory references comprise 99% of processing time it is very inefficient.

Is there a memory mapping mechanism where I can allocate a number (1024) of memory blocks and then map them (in any order) into a consecutive memory space?

This would reduce my memory references to:

unsigned char *Memory;
Memory = pointed to first byte of (re-)mapped memory

unsigned char aByte = Memory[Address];

Which would be an order of magnitude faster (I hope)

A solution for either Windows and/or Linux is OK.

Answer Source

I managed to find in Linux there is the mremap() function.

And in Windows is the AWE Address Windowing Exextensions MapUserPhysicalPages function allows this.

I just tested the AWE functions and they work well. Not sure how efficient they are but they work nicely.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download