c - MapViewOfFile offset - how to use it -
developing game ("jogo" in pt), server can host 5 simultaneous games, client access via mapped memory.
so here's have:
server:
#define max_jogos 5 typedef struct{ ... } sjogo; typedef struct{ sjogo * ps; } sglobals; sjogo jogo[max_jogos]; //global sglobals globals[max_jogos]; //global handle hmapfile; //global int _tmain(int argc, lptstr argv[]) { ... hmapfile = createfilemapping(invalid_handle_value, null, page_readwrite, 0, sizeof(sjogo)*max_jogos, szname); //create map games .... } //called when new game created void createview(int index){ //create view 1 game , store pointer //### need apply offset here ### globals.ps[index] = (sjogo * )mapviewoffile(hmapfile, file_map_all_access, 0, 0, sizeof(sjogo); } //called thread on event set void copyjogo(int index){ //use stored pointer update jogo copymemory((pvoid)globals[index].ps, &jogo[index], sizeof(sjogo)); }
client:
handle hmapfile; //global sjogo * ps; //global int _tmain(int argc, lptstr argv[]) { ... hmapfile = openfilemapping(file_map_all_access, false, szname); ps = (sjogo *)mapviewoffile(cdata.hmapfile, file_map_all_access, 0, 0, sizeof(sjogo)); //### need respective offset here ### }
i have tried creating view of "sizeof(sjogo)*max_jogos" , incrementing pointer+=sizeof(sjogo) * index; didn't manage succeed, turn you, can me learn use offset?
i have searched quite persistently , found example here on stackoverflow it's c++ , couldn't adapt it.
the high-order dword offset sizeof(sjogo) correct? don't know granularity or how apply low-order dword...
can me? thank you.
edit:
the code below returning when = 1 (null), doing wrong?
int _tmain(int argc, lptstr argv[]) { .... hmapfile = createfilemapping(invalid_handle_value, null, page_readwrite, 0, sizeof(sjogo)*max_jogos, szname); if (hmapfile == null) {...} dword offset = 0; (i = 0; < max_jogos; i++) { if (mapviewoffile(hmapfile, file_map_all_access, 0, offset, sizeof(sjogo)) == null) { _tprintf(text("erro mapviewoffile i: %d\n"), i); closehandle(hmapfile); return; } offset += sizeof(sjogo); } }
edit 2:
solved problem above, found solution here:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366548%28v=vs.85%29.aspx
i wasn't taking account allocation granularity on offset, causing mapviewoffile return null on second attempt.
the link above shows clear example on how apply offset.
the example in msdn should fit you. can find here
basically in createfilemapping
declare amount of memory share, in mapviewoffile
create 'viewport' on memory. can have dimensions equal or smaller quantity requested.
this memory can read or write.
on client side can open mapping openfilemapping
should map quantity of memory want access, size of single structure. can iterate while mapviewoffile
returns valid address access elements of array:
sjogo *ps = null; dword ofset = 0; while((ps= (sjogo *)mapviewoffile(cdata.hmapfile, file_map_all_access, 0, offset, sizeof(sjogo))) { //do current element pointed ps offset += sizeof(sjogo); }
here start @ offset 0 in mapped memory, increment offset size of our structure. moves mapping next element of array of structures.
when exceed number of elements file mapping return null.
this not efficient way handle file mapping, want consider create super structure holding number of entries available , entries coded vla (variable lenght array) @ end of structure:
typedef struct{ int nentries; //holds number of entries sjogo ps[]; } sglobals; sglobals globals;
Comments
Post a Comment