• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nasa / trick / 42255919351

06 Nov 2026 04:49PM UTC coverage: 57.017% (+0.01%) from 57.005%
42255919351

Pull #2278

github

web-flow
Merge 434aa99f7 into b3eb8c583
Pull Request #2278: feat: Add TMM/MM calls for determining whether any errors while writing a checkpoint

20 of 58 new or added lines in 7 files covered. (34.48%)

14 existing lines in 4 files now uncovered.

15020 of 26343 relevant lines covered (57.02%)

479401.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/trick_source/sim_services/MemoryManager/MemoryManager_write_checkpoint.cpp
1
#include <fstream>
2
#include <sstream>
3
#include <string.h>
4
#include <stdlib.h>  // free()
5
#include <algorithm> // std::sort()
6
#include "trick/MemoryManager.hh"
7

8
// GreenHills stuff
9
#if ( __ghs )
10
#include "ghs_stubs.h"
11
#endif
12

13
// MEMBER FUNCTION
14
void Trick::MemoryManager::execute_checkpoint( std::ostream& out_s ) {
63 ✔
15

16
    ALLOC_INFO_MAP::iterator pos;
63 ✔
17
    ALLOC_INFO* alloc_info;
18
    char name[256];
19
    int local_anon_var_number;
20
    int extern_anon_var_number;
21

22
    // 1) Generate declaration statements for each the allocations that we are managing.
23
    out_s << "// Variable Declarations." << std::endl;
63 ✔
24
    out_s.flush();
63 ✔
25

26
    local_anon_var_number = 0;
63 ✔
27
    extern_anon_var_number = 0;
63 ✔
28

29
    // Give names to the anonymous declarations
30
    // Also search each allocation for STLs.
31
    // STLs will be added to the dependencies vector.
32
    int n_depends = dependencies.size();
63 ✔
33
    for (int ii = 0 ; ii < n_depends ; ii ++) {
895 ✔
34
        alloc_info = dependencies[ii];
832 ✔
35
        /** Generate temporary names for anonymous variables. */
36
        if (alloc_info->name == NULL) {
832 ✔
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->size == 1 && alloc_info->num_index == 1
296 ✔
41
                && alloc_info->stcl == TRICK_LOCAL)
120 ✔
42
            {
43
                // Skip naming this individual char array allocation to prevent unused declarations
44
                continue;
120 ✔
45
            }
46

47
            if ( alloc_info->stcl == TRICK_LOCAL) {
176 ✔
48
                snprintf( name, sizeof(name), "%s%d", local_anon_var_prefix, local_anon_var_number++);
176 ✔
49
                alloc_info->name = strdup( name);
176 ✔
50
            } else if (alloc_info->stcl == TRICK_EXTERN) {
×
51
                snprintf( name, sizeof(name), "%s%d", extern_anon_var_prefix, extern_anon_var_number++);
×
52
                alloc_info->name = strdup( name);
×
53
                /** @b NOTE: We should not write declarations for external
54
                    anonymous variables, because we should not reload them.*/
55
            } else {
56
                emitError("write_checkpoint: This is bad. ALLOC_INFO object is messed up.\n") ;
×
NEW
57
                checkpoint_write_success = false;
×
58
            }
59
        }
60
        get_stl_dependencies(alloc_info);
712 ✔
61
    }
62

63
    // Write a declaration statement for all of the LOCAL variables,
64
    n_depends = dependencies.size();
63 ✔
65
    for (int ii = 0 ; ii < n_depends ; ii ++) {
3,342 ✔
66
        alloc_info = dependencies[ii];
3,279 ✔
67
        if ( alloc_info->stcl == TRICK_LOCAL) {
3,279 ✔
68
            currentCheckPointAgent->write_decl( out_s, alloc_info);
2,850 ✔
69
        }
70
    }
71

72
    // Write a "clear_all_vars" command.
73
    if (reduced_checkpoint) {
63 ✔
74
        out_s << std::endl << std::endl << "// Clear all allocations to 0." << std::endl;
63 ✔
75
        out_s << "clear_all_vars();" << std::endl;
63 ✔
76
    }
77

78
    // 2) Dump the contents of each of the dynamic and mapped allocations.
79
    out_s << std::endl << std::endl << "// Variable Assignments." << std::endl;
63 ✔
80
    out_s.flush();
63 ✔
81

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

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

99
    for (int ii = 0 ; ii < n_depends ; ii ++) {
3,342 ✔
100
        alloc_info = dependencies[ii];
3,279 ✔
101
        // If the temporary-variable prefix occurs at the beginning of the name ...
102
        if ((alloc_info->name != NULL) &&
3,279 ✔
103
            (( strstr( alloc_info->name, local_anon_var_prefix ) == alloc_info->name ) ||
3,159 ✔
104
             ( strstr( alloc_info->name, extern_anon_var_prefix) == alloc_info->name ))) {
2,983 ✔
105
                free( alloc_info->name);
176 ✔
106
                alloc_info->name = NULL;
176 ✔
107
        }
108
    }
109

110
    // Delete the variables created by STLs. Remove memory in reverse order.
111
    std::vector<ALLOC_INFO*>::reverse_iterator it ;
63 ✔
112
    for ( it = stl_dependencies.rbegin() ; it != stl_dependencies.rend() ; it++ ) {
2,510 ✔
113
        delete_var((*it)->start) ;
2,447 ✔
114
    }
115

116
    out_s.flush();
63 ✔
117
    if (out_s.fail())
63 ✔
118
    {
NEW
119
        emitError("write_checkpoint: An error occurred while writing to the checkpoint output stream.\n");
×
NEW
120
        checkpoint_write_success = false;
×
121
    }
122
    if (!checkpoint_write_success)
63 ✔
123
    {
124
        std::stringstream message;
4 ✔
125
        message << "write_checkpoint: Checkpoint was written with errors and may not restore correctly."
126
                << " See get_checkpoint_write_state() / TMM_get_checkpoint_write_state().";
4 ✔
127
        emitWarning(message.str());
4 ✔
128
    }
4 ✔
129
}
63 ✔
130

131
// Reset the checkpoint write error state before starting a new checkpoint.
132
void Trick::MemoryManager::begin_checkpoint_write() { checkpoint_write_success = true; }
63 ✔
133

134
// MEMBER FUNCTION
135
bool Trick::MemoryManager::get_checkpoint_write_state() { return checkpoint_write_success; }
9 ✔
136

137
// MEMBER FUNCTION
138
void Trick::MemoryManager::set_checkpoint_write_state(bool state) { checkpoint_write_success = state; }
3 ✔
139

140
// Local sort function used in write_checkpoint.
141
static bool alloc_info_id_compare(ALLOC_INFO * lhs, ALLOC_INFO * rhs) { return ( lhs->id < rhs->id ) ; }
4,178 ✔
142

143
// MEMBER FUNCTION
144
void Trick::MemoryManager::write_checkpoint( std::ostream& out_s) {
18 ✔
145

146
    ALLOC_INFO_MAP::iterator pos;
18 ✔
147
    ALLOC_INFO* alloc_info;
148
    begin_checkpoint_write();
18 ✔
149
    dependencies.clear();
18 ✔
150
    stl_dependencies.clear();
18 ✔
151

152
    pthread_mutex_lock(&mm_mutex);
18 ✔
153
    for ( pos=alloc_info_map.begin() ; pos!=alloc_info_map.end() ; pos++ ) {
774 ✔
154
        alloc_info = pos->second;
756 ✔
155
        dependencies.push_back(alloc_info);
756 ✔
156
    }
157

158
    // Sort the dependencies by ALLOC_INFO.id.
159
    std::sort( dependencies.begin() , dependencies.end() , alloc_info_id_compare) ;
18 ✔
160
    pthread_mutex_unlock(&mm_mutex);
18 ✔
161

162
    execute_checkpoint( out_s );
18 ✔
163

164
}
18 ✔
165

166
// MEMBER FUNCTION
167
void Trick::MemoryManager::write_checkpoint(const char* filename) {
19 ✔
168

169
   std::ofstream outfile( filename, std::ios::out);
19 ✔
170

171
    if (outfile.is_open()) {
19 ✔
172
        write_checkpoint( outfile);
18 ✔
173
    } else {
174
        std::stringstream message;
1 ✔
175
        message << "Couldn't open \"" << filename << "\".";
1 ✔
176
        emitError(message.str());
1 ✔
177
        checkpoint_write_success = false;
1 ✔
178
    }
1 ✔
179
}
19 ✔
180

181
// MEMBER FUNCTION
182
void Trick::MemoryManager::write_checkpoint( std::ostream& out_s, const char* var_name) {
44 ✔
183
    begin_checkpoint_write();
44 ✔
184
    dependencies.clear();
44 ✔
185
    stl_dependencies.clear();
44 ✔
186

187
    pthread_mutex_lock(&mm_mutex);
44 ✔
188
    get_alloc_deps_in_allocation( var_name);
44 ✔
189
    pthread_mutex_unlock(&mm_mutex);
44 ✔
190

191
    execute_checkpoint( out_s );
44 ✔
192
}
44 ✔
193

194
// MEMBER FUNCTION
195
void Trick::MemoryManager::write_checkpoint(const char* filename, const char* var_name) {
×
196

197
    std::ofstream out_s( filename, std::ios::out);
×
198
    if (out_s.is_open()) {
×
199
        write_checkpoint( out_s, var_name);
×
200
    } else {
201
        std::stringstream message;
×
202
        message << "Couldn't open \"" << filename << "\".";
×
203
        emitError(message.str());
×
NEW
204
        checkpoint_write_success = false;
×
205
    }
×
206
}
×
207

208
// MEMBER FUNCTION
209
void Trick::MemoryManager::write_checkpoint( std::ostream& out_s, std::vector<const char*>& var_name_list) {
1 ✔
210

211
    const char* var_name;
212
    int n_names;
213

214
    begin_checkpoint_write();
1 ✔
215
    dependencies.clear();
1 ✔
216
    stl_dependencies.clear();
1 ✔
217

218
    n_names = var_name_list.size();
1 ✔
219
    for (int ii=0; ii< n_names; ii++) {
3 ✔
220
        var_name = var_name_list[ii];
2 ✔
221
        pthread_mutex_lock(&mm_mutex);
2 ✔
222
        get_alloc_deps_in_allocation(var_name);
2 ✔
223
        pthread_mutex_unlock(&mm_mutex);
2 ✔
224
    }
225

226
    execute_checkpoint( out_s );
1 ✔
227
}
1 ✔
228

229
// MEMBER FUNCTION
230
void Trick::MemoryManager::write_checkpoint(const char* filename, std::vector<const char*>& var_name_list) {
×
231

232
    std::ofstream out_s( filename, std::ios::out);
×
233

234
    if (out_s.is_open()) {
×
235
        write_checkpoint( out_s, var_name_list);
×
236
    } else {
237
        std::cerr << "ERROR: Couldn't open \""<< filename <<"\"." << std::endl;
×
238
        std::cerr.flush();
×
NEW
239
        checkpoint_write_success = false;
×
240
    }
241
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc