I would like to instruct the compiler and the linker on how to allocate the following code (I am using Visual Studio and Intel C++ compiler for Windows)
/* large chunk of code before */ static uint64_t a; static uint64_t b; static uint64_t c; static uint64_t d; static void CALLBACK foo( args ){ /* short code using a, b, c, d */ } /* large chunk of code after */
Specifically, I would like to instruct the compiler and the linker so that the .EXE file would allocate a, b, c, d, foo() contiguously in this order to maximize cache benefit.
The following mods seem to achieve the goal, but the external driver that invokes foo() does not like the fact that foo() is inside a segment, and thus crashes. Maybe I did not use the pragmas correctly.
#pragma code_seg(push, stack1, ".my_text") __declspec(allocate(".my_text")) static uint64_t a; __declspec(allocate(".my_text")) static uint64_t b; __declspec(allocate(".my_text")) static uint64_t c; __declspec(allocate(".my_text")) static uint64_t d; __declspec(code_seg(".my_text")) static void CALLBACK foo( args ){ /* do something using a, b, c, d */ } #pragma code_seg(pop, stack1)
On the other hand, moving foo() outsize the segment works, but I have no guarantee that foo() is allocated soon after a, b, c, d in the .EXE
#pragma code_seg(push, stack1, ".my_text") __declspec(allocate(".my_text")) static uint64_t a; __declspec(allocate(".my_text")) static uint64_t b; __declspec(allocate(".my_text")) static uint64_t c; __declspec(allocate(".my_text")) static uint64_t d; #pragma code_seg(pop, stack1) static void CALLBACK foo( args ){ /* do something using a, b, c, d */ }
Any help is appreciated, thanks.
-Roberto