C++ 清除机制来保存和恢复内存位置列表?

C++ 清除机制来保存和恢复内存位置列表?,c++,C++,我有一个内存位置列表,我需要作为一个组访问,偶尔也需要单独访问。我想提供一个单一的规范点。目前的设计很混乱,我想知道是否有人有一个很好的方法(最好是C++语言)来实现这一点 我有一个巨大的潜在内存位置列表,每个产品(在设计时)将选择多达100个,大致分为两组 在代码中的某一点上,我们将所有内存位置复制到一个缓冲区,然后偶尔从原始位置和存储的缓冲区将它们读回。经过一定的时间(以及大约50000行代码之后),我们将把保存的数据恢复到内存中 当前代码(sorta) 我们目前正在做的事情如下: #def

我有一个内存位置列表,我需要作为一个组访问,偶尔也需要单独访问。我想提供一个单一的规范点。目前的设计很混乱,我想知道是否有人有一个很好的方法(最好是C++语言)来实现这一点

我有一个巨大的潜在内存位置列表,每个产品(在设计时)将选择多达100个,大致分为两组

在代码中的某一点上,我们将所有内存位置复制到一个缓冲区,然后偶尔从原始位置和存储的缓冲区将它们读回。经过一定的时间(以及大约50000行代码之后),我们将把保存的数据恢复到内存中

当前代码(sorta)

我们目前正在做的事情如下:

#define BLOCK_LIST_LOCATIONS1 = 0x1400, 0x1450
#define BLOCK_LIST_LOCATIONS2 = BLOCK_LIST_LOCATIONS1, 0x1000, 0x1002, 0x2040, 0xFFFF

union {

    struct {
        struct {          // Must be kept in sync with BLOCK_LIST_LOCATIONS1
            uint16_t Mem1400;
            uint16_t Mem1450;
        } List1;
        uint16_t Mem1000;       // Must be kept in sync with BLOCK_LIST_LOCATIONS2
        uint16_t Mem1002;
        uint16_t Mem2040;
    } BlockList;

    uint16_t BlockArray[sizeof(BlockList)/sizeof(uint16_t)];

 } MemoryBlock;

uint32_t BASE_PTR = 0x30300000;


// block.cpp
uint16_t BlockMemoryLocationsArray[] = { BLOCK_LIST_LOCATIONS2 };

void SaveAwayMemory() {

    for (int i = 0; BlockMemoryLocationsArray[i] < 0xFFFF; i++) {
       MemoryBlock.BlockArray[i] = *(BASE_PTR + BlockMemoryLocationsArrary[i]);
    }
}

void RestoreFromMemory(bool skipList1) {
    int startingLocation = 0;

    if (skipList1) startingLocation += sizeof(BlockList::List1) / sizeof(uint16_t);

    for (int i = startingLocation; BlockMemoryLocationsArray[i] < 0xFFFF; i++) {
       *(BASE_PTR + BlockMemoryLocationsArrary[i]) = MemoryBlock.BlockArray[i];
    }
}

这是我能在餐巾背面画的

template <int ... Values>
struct Bimap;

template <>
struct Bimap<>
{
    static const int Size = 0; 
};

template <int Value0, int ... Values>
struct Bimap<Value0, Values...>
{
    static const int Val = Value0;
    using Next = Bimap<Values...>;
    static const int Size = 1 + Next::Size;
};

template<typename A, int I>
struct Get;

template<typename A>
struct Get<A, 0>
{
    static const int Val = A::Val;
};

template<typename A, int I>
struct Get
{
    static const int Val = Get<typename A::Next, I-1>::Val;
};

template<typename A, int Val>
struct Find;

template<int Value0, int... Values>
struct Find<Bimap<Value0, Values...>, Value0>
{
    static const int Idx = 0;
};

template<int ValueToFind, int Value0, int... Values>
struct Find<Bimap<Value0, Values...>, ValueToFind>
{
    static const int Idx = 1 + Find<Bimap<Values...>, ValueToFind>::Idx;
};
和使用

MyBlocks[Find<MyLocations, 0x1500>::Idx] = 
  MyBlocks[Find<MyLocations, 0x1400>::Idx] | 0x777;
我希望你能根据自己的具体需要来调整


我几乎可以肯定Boost已经有类似的东西了,但我不想费心去寻找它。

您似乎只对它们的大小使用结构。你真的需要它们吗?@n.m.在我们使用结构的保存/还原之间有一些中间代码。我们从中提取值,修改并写入原始位置。你也可以使用数组索引。只是为了澄清(几年后),数组索引在这种情况下是无用的,因为我们实际上没有0x1000是插槽3的干净映射。而内存地址/名称是我们要为单个读/写操作建立索引的唯一对象。
using MyLocations = Bimap<0x1000, 0x1100, 0x1400, 0x1500>;
Get<MyLocations, 3>::Val == 0x1500
Find<MyLocations, 0x1500>::Idx = 3
uint16_t MyBlocks[MyLocations::Size];
MyBlocks[Find<MyLocations, 0x1500>::Idx] = 
  MyBlocks[Find<MyLocations, 0x1400>::Idx] | 0x777;
MemoryBlock.BlockList.Mem1500 = MemoryBlock.BlockList.Mem1400 | 0x777;