What is the best way to copy memory for X indexes to many locations within a single array in C? -


what best way copy memory x indexes many locations within single array in c?

the problem trying solved emulated memory cpu emulator. original hardware has type of memory mirroring, , attempting replicate via code.

say have array:

int memory[100] = {0}; 

and have 10 indexes mirrored @ different locations. example if memory[0] changed, index 0, 10, 20, 30... should change value or if memory[3] changed, index 3, 13, 23, 33 should mirrored.

likewise if mirrored location changed other mirror locations should reflect this, such if index 23 changed, 3, 13, 23, 33... etc should reflect this.

another requirement way specify start , ends of mirrored locations are. example index 10-19 mirrored @ index 30-39, again @ 70-79 leaving unmodified space in between segments of mirrored indexes.

would using memcpy fastest/most efficient way of achieving if this, or sort of iterating loop , pointer math better efficiency? how pointer math done calculate start address copy destination? array of pointers holding start addresses live inside memory array best way handle this?

something maybe (this won't compile pseudo code idea of mine):

#define number_of_mirrors 3 #define length_of_mirrors 10 int  memory[100]             = {0}; int *memory_mirror_starts[3] = {&memory[10], &memory[30], &memory[70]};  // when memory needs mirrored for(int = 0; < number_of_mirrors; i++) {    for(int n = 0; n < length_of_mirrors; n++) {        memory_mirror_starts[i][n] = memory_mirror_starts[0][n];    } } 

i think may on right track, wouldn't satisfy requests since copies results of first mirror rest. if write of other mirrors overwritten rather copied other mirrors.

thanks tips , advice.

to specify each mirror starts in memory array, "array of pointers 'memory' works",

int *memory_mirror_starts[3] = {&memory[10], &memory[30], &memory[70]}; // (a) 

or give each offset:

int memory_mirror_starts[3] = { 10, 30, 70 }; // (b) 

then ensure each write given mirror indeed replicated mirrors, without copying whole thing time, have poke function write @ given index in mirror (for each approach, (a) , (b))

void poke(int index, int value) {    int j;    (j=0 ; j<number_of_mirrors ; j++)        memory_mirror_starts[j][index] = value; // (a) } 

or

void poke(int index, int value) {    int j;    (j=0 ; j<number_of_mirrors ; j++)        memory[memory_mirror_starts[j] + index] = value; // (b) } 

having function centralize write access hides mirrors complexity developers , ensures mirrors indeed updated correctly.

note index checked >=0 , < length_of_mirrors.

performance wise,

  • adding inline functions declarations change function calls function code in place, saving calls. poke function being small, code shouldn't bigger.

  • (a) *(*(memory_mirror_starts + j) + index) = value

  • (b) *(memory + *(memory_mirror_starts + j) + index) = value
  • so (a) tad faster, (but optimizers have say, , better test both solutions)

inline:

inline void poke(int index, int value) { ... 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -