Skip to content

Commit d49130d

Browse files
authored
Updated to write string literal format for C-string in checkpoint. (#1975)
* Updated to write string literal format for C-string in checkpoint. * Changed one of updated write_var return type back to void to be consistent with all others.
1 parent d8faf92 commit d49130d

2 files changed

Lines changed: 50 additions & 13 deletions

File tree

trick_source/sim_services/CheckPointAgent/ClassicCheckPointerAgent.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ void Trick::ClassicCheckPointAgent::write_decl(std::ostream& chkpnt_os, ALLOC_IN
108108

109109
const char *type_spec;
110110

111+
// Safety check for NULL info pointer or NULL name
112+
if (info == NULL || info->name == NULL) {
113+
return;
114+
}
115+
111116
type_spec = trickTypeCharString(info->type, info->user_type_name);
112117

113118
if (info->stcl == TRICK_EXTERN) {
@@ -828,20 +833,33 @@ std::string Trick::ClassicCheckPointAgent::
828833
alloc_info = mem_mgr->get_alloc_info_of( pointer);
829834

830835
if (alloc_info != NULL) {
831-
int alloc_elem_size;
832-
int alloc_elem_index;
833-
int misalignment;
836+
// Special handling for character strings: prefer string literal format over allocation reference
837+
// The following info for "char *" is for reference purpose:
838+
// curr_dim = 0
839+
// attr.type = TRICK_CHARACTER (base type)
840+
// attr.num_index = 1 (1D array)
841+
// attr.index[0].size = 0 (not static array)
842+
// attr.size = sizeof(char)
843+
// This prevents anonymous allocations from appearing in subsequent checkpoints
844+
if ((attr != NULL) && (attr->type == TRICK_CHARACTER || attr->type == TRICK_UNSIGNED_CHARACTER) && ((curr_dim + 1) == attr->num_index)) {
845+
std::stringstream ss;
846+
write_quoted_str( ss, (const char*)pointer);
847+
reference_string = ss.str();
848+
} else {
849+
int alloc_elem_size;
850+
int alloc_elem_index;
851+
int misalignment;
834852

835-
alloc_elem_size = alloc_info->size;
836-
alloc_elem_index = (int) (((long) pointer - (long) alloc_info->start) / alloc_elem_size);
837-
misalignment = (int) (((long) pointer - (long) alloc_info->start) % alloc_elem_size);
853+
alloc_elem_size = alloc_info->size;
854+
alloc_elem_index = (int) (((long) pointer - (long) alloc_info->start) / alloc_elem_size);
855+
misalignment = (int) (((long) pointer - (long) alloc_info->start) % alloc_elem_size);
838856

839-
// If type-checking AND the type specifiers match AND the type we're looking for
840-
// is either not structured or if it is, the attr-list that describes the contents
841-
// of the structure is the same.
857+
// If type-checking AND the type specifiers match AND the type we're looking for
858+
// is either not structured or if it is, the attr-list that describes the contents
859+
// of the structure is the same.
842860

843-
if ( (attr != NULL) && (attr->type == alloc_info->type) &&
844-
( (attr->type != TRICK_STRUCTURED) || (attr->attr == alloc_info->attr))) {
861+
if ( (attr != NULL) && (attr->type == alloc_info->type) &&
862+
( (attr->type != TRICK_STRUCTURED) || (attr->attr == alloc_info->attr))) {
845863

846864
int ii;
847865
int n_l_ptrs, n_r_ptrs;
@@ -945,6 +963,7 @@ std::string Trick::ClassicCheckPointAgent::
945963
}
946964
}
947965

966+
}
948967
}
949968
} else if ((attr != NULL) && ((curr_dim + 1) == attr->num_index)) {
950969

trick_source/sim_services/MemoryManager/MemoryManager_write_checkpoint.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ void Trick::MemoryManager::execute_checkpoint( std::ostream& out_s ) {
3434
alloc_info = dependencies[ii];
3535
/** Generate temporary names for anonymous variables. */
3636
if (alloc_info->name == NULL) {
37+
// Skip naming individual C-string allocations to eliminate unused declarations
38+
// Individual char arrays (size=1, num_index=1) represent single strings that are
39+
// included in char* arrays, so we don't need separate allocations for them
40+
if ((alloc_info->type == TRICK_CHARACTER || alloc_info->type == TRICK_UNSIGNED_CHARACTER) &&
41+
alloc_info->size == 1 && alloc_info->num_index == 1 && alloc_info->stcl == TRICK_LOCAL) {
42+
// Skip naming this individual char array allocation to prevent unused declarations
43+
continue;
44+
}
45+
3746
if ( alloc_info->stcl == TRICK_LOCAL) {
3847
snprintf( name, sizeof(name), "%s%d", local_anon_var_prefix, local_anon_var_number++);
3948
alloc_info->name = strdup( name);
@@ -70,8 +79,17 @@ void Trick::MemoryManager::execute_checkpoint( std::ostream& out_s ) {
7079

7180
for (int ii = 0 ; ii < n_depends ; ii ++) {
7281
alloc_info = dependencies[ii];
73-
write_var( out_s, alloc_info);
74-
out_s << std::endl;
82+
// Skip naming individual C-string allocations to eliminate unused declarations.
83+
// Individual char arrays (size=1, num_index=1) represent single strings that are
84+
// included in char* arrays, so we don't need separate allocations for them.
85+
// Also, safety check for NULL name before pushing to name stack later.
86+
if (!((alloc_info->type == TRICK_CHARACTER || alloc_info->type == TRICK_UNSIGNED_CHARACTER) &&
87+
alloc_info->size == 1 && alloc_info->num_index == 1 && alloc_info->stcl == TRICK_LOCAL) &&
88+
alloc_info->name != NULL)
89+
{
90+
write_var(out_s, alloc_info);
91+
out_s << std::endl;
92+
}
7593
}
7694

7795
// Free all of the temporary names that were created for the checkpoint.

0 commit comments

Comments
 (0)