c++ - Clean mechanism to save and restore a list of memory locations? -
c++ - Clean mechanism to save and restore a list of memory locations? -
i have list of memory locations need access both group, , individually. provide single specification point. current design messy, , wondering if had (ideally c++ish) way of approaching this.
i have huge list of potential memory locations, of each product (at design time) select around 100 of them, split more or less evenly 2 groups.
at point in code, re-create of memory locations out buffer, , read them both original location , stored away buffer. after length of time (and ~50,000 lines of code later), restore saved info memory.
current code (sorta)
what doing following:
#define block_list_locations1 = 0x1400, 0x1450 #define block_list_locations2 = block_list_locations1, 0x1000, 0x1002, 0x2040, 0xffff union { struct { struct { // must kept in sync block_list_locations1 uint16_t mem1400; uint16_t mem1450; } list1; uint16_t mem1000; // must kept in sync 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() { (int = 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); (int = startinglocation; blockmemorylocationsarray[i] < 0xffff; i++) { *(base_ptr + blockmemorylocationsarrary[i]) = memoryblock.blockarray[i]; } } this presents number of issues, since it's pain maintain multiple lists synchronized, , errors here tend nasty memory corruption issues.
some odd restrictions: not have heap, we're operating on embedded platform using armcc.
is there relatively clean alternative implementation gives me
access each saved away value individually easy mass save/restore ability specify location oncehigh level code use
saveawaymemory(); // literally thousands of lines of processing... including things next write1550(memoryblock.blocklist.list1.mem1450 | 0x0020); if (read1000() != memoryblock.blocklist.mem1000) // freak out if changed restorefrommemory(true);
this able draw on of napkin.
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 { 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; }; now have anything? if have
using mylocations = bimap<0x1000, 0x1100, 0x1400, 0x1500>; then
get<mylocations, 3>::val == 0x1500 and
find<mylocations, 0x1500>::idx = 3 now can declare
uint16_t myblocks[mylocations::size]; and use
myblocks[find<mylocations, 0x1500>::idx] = myblocks[find<mylocations, 0x1400>::idx] | 0x777; which equivalent former
memoryblock.blocklist.mem1500 = memoryblock.blocklist.mem1400 | 0x777; i hope can adapt exact needs.
i'm sure boost have already, can't bothered find it.
c++ design
Comments
Post a Comment