Skip to content

Commit

Permalink
Add GlobalVariableContext::{init,append}
Browse files Browse the repository at this point in the history
Also added some throwaway test code to exercise the tls code I ported
  • Loading branch information
jmlapre committed Jan 23, 2025
1 parent fb419f5 commit 5caa1ff
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/sst/elements/mercury/operating_system/process/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,62 @@ GlobalVariable::init(const int size, const char* name, bool tls)
else return glblCtx.append(size, name);
}

void
GlobalVariableContext::init()
{
stackOffset = 0;
allocSize_ = 4096;
globalInits = nullptr;
}

int
GlobalVariableContext::append(const int size, const char* /*name*/)
{
int offset = stackOffset;

int rem = size % 4;
int offsetIncrement = rem ? (size + (4-rem)) : size; //align on 32-bits

bool realloc = false;
while ((offsetIncrement + stackOffset) > allocSize_){
realloc = true;
allocSize_ *= 2; //grow until big enough
}

if (realloc && !activeGlobalMaps_.empty()){
std::cerr << "dynamically loaded global variable overran storage space\n"
"to fix, explicitly set globals_size = " << allocSize_ << " in app params" << "\n";
}

if (realloc || globalInits == nullptr){
char* old = globalInits;
globalInits = new char[allocSize_];
if (old){
//move everything in that was already there
::memcpy(globalInits, old, stackOffset);
delete[] old;
}
}

//printf("Allocated global variable %s of size %d at offset %d - %s\n",
// name, size, offset, (realloc ? "reallocated to fit" : "already fits"));
//fflush(stdout);

stackOffset += offsetIncrement;

return offset;
}

// <JML>
// Temporary fix to trigger some of the code that I ported over
extern "C" {
// This is the variable name in test_tls.cc
extern int my_global;
}

static auto foo = GlobalVariable::init(4, "my_global", true);
// </JML>

void
GlobalVariableContext::callInitFxns(void *globals)
{
Expand Down

0 comments on commit 5caa1ff

Please sign in to comment.