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

nasa / trick / 31127600353

06 Aug 2026 07:55PM UTC coverage: 56.671%. Remained the same
31127600353

push

github

web-flow
clang setLangDefaults for clang >= 15 (#2164)

* clang setLangDefaults for clang >= 15

* Fix Format

14836 of 26179 relevant lines covered (56.67%)

462575.58 hits per line

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

77.36
/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 ) {
56✔
15

16
    ALLOC_INFO_MAP::iterator pos;
56✔
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;
56✔
24
    out_s.flush();
56✔
25

26
    local_anon_var_number = 0;
56✔
27
    extern_anon_var_number = 0;
56✔
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();
56✔
33
    for (int ii = 0 ; ii < n_depends ; ii ++) {
882✔
34
        alloc_info = dependencies[ii];
826✔
35
        /** Generate temporary names for anonymous variables. */
36
        if (alloc_info->name == NULL) {
826✔
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") ;
×
57
            }
58
        }
59
        get_stl_dependencies(alloc_info);
706✔
60
    }
61

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

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

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

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

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

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

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

116
// Local sort function used in write_checkpoint.
117
static bool alloc_info_id_compare(ALLOC_INFO * lhs, ALLOC_INFO * rhs) { return ( lhs->id < rhs->id ) ; }
4,061✔
118

119
// MEMBER FUNCTION
120
void Trick::MemoryManager::write_checkpoint( std::ostream& out_s) {
17✔
121

122
    ALLOC_INFO_MAP::iterator pos;
17✔
123
    ALLOC_INFO* alloc_info;
124
    dependencies.clear();
17✔
125
    stl_dependencies.clear();
17✔
126

127
    pthread_mutex_lock(&mm_mutex);
17✔
128
    for ( pos=alloc_info_map.begin() ; pos!=alloc_info_map.end() ; pos++ ) {
772✔
129
        alloc_info = pos->second;
755✔
130
        dependencies.push_back(alloc_info);
755✔
131
    }
132

133
    // Sort the dependencies by ALLOC_INFO.id.
134
    std::sort( dependencies.begin() , dependencies.end() , alloc_info_id_compare) ;
17✔
135
    pthread_mutex_unlock(&mm_mutex);
17✔
136

137
    execute_checkpoint( out_s );
17✔
138

139
}
17✔
140

141
// MEMBER FUNCTION
142
void Trick::MemoryManager::write_checkpoint(const char* filename) {
17✔
143

144
   std::ofstream outfile( filename, std::ios::out);
17✔
145

146
    if (outfile.is_open()) {
17✔
147
        write_checkpoint( outfile);
17✔
148
    } else {
149
        std::stringstream message;
×
150
        message << "Couldn't open \"" << filename << "\".";
×
151
        emitError(message.str());
×
152
    }
×
153
}
17✔
154

155
// MEMBER FUNCTION
156
void Trick::MemoryManager::write_checkpoint( std::ostream& out_s, const char* var_name) {
38✔
157

158
    dependencies.clear();
38✔
159
    stl_dependencies.clear();
38✔
160

161
    pthread_mutex_lock(&mm_mutex);
38✔
162
    get_alloc_deps_in_allocation( var_name);
38✔
163
    pthread_mutex_unlock(&mm_mutex);
38✔
164

165
    execute_checkpoint( out_s );
38✔
166
}
38✔
167

168
// MEMBER FUNCTION
169
void Trick::MemoryManager::write_checkpoint(const char* filename, const char* var_name) {
×
170

171
    std::ofstream out_s( filename, std::ios::out);
×
172
    if (out_s.is_open()) {
×
173
        write_checkpoint( out_s, var_name);
×
174
    } else {
175
        std::stringstream message;
×
176
        message << "Couldn't open \"" << filename << "\".";
×
177
        emitError(message.str());
×
178
    }
×
179
}
×
180

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

184
    const char* var_name;
185
    int n_names;
186

187
    dependencies.clear();
1✔
188
    stl_dependencies.clear();
1✔
189

190
    n_names = var_name_list.size();
1✔
191
    for (int ii=0; ii< n_names; ii++) {
3✔
192
        var_name = var_name_list[ii];
2✔
193
        pthread_mutex_lock(&mm_mutex);
2✔
194
        get_alloc_deps_in_allocation(var_name);
2✔
195
        pthread_mutex_unlock(&mm_mutex);
2✔
196
    }
197

198
    execute_checkpoint( out_s );
1✔
199
}
1✔
200

201
// MEMBER FUNCTION
202
void Trick::MemoryManager::write_checkpoint(const char* filename, std::vector<const char*>& var_name_list) {
×
203

204
    std::ofstream out_s( filename, std::ios::out);
×
205

206
    if (out_s.is_open()) {
×
207
        write_checkpoint( out_s, var_name_list);
×
208
    } else {
209
        std::cerr << "ERROR: Couldn't open \""<< filename <<"\"." << std::endl;
×
210
        std::cerr.flush();
×
211
    }
212
}
×
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