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

nasa / trick / 23870565573

01 Apr 2026 08:58PM UTC coverage: 55.798% (-0.1%) from 55.922%
23870565573

Pull #2011

github

web-flow
Merge 8249d9618 into 5cf9d7558
Pull Request #2011: Standardize code style using clang-format

12574 of 22535 relevant lines covered (55.8%)

311977.1 hits per line

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

82.39
/trick_source/sim_services/MemoryManager/MemoryManager_clear_memory.cpp
1
#include "trick/MemoryManager.hh"
2
#include "trick/bitfield_proto.h"
3
#include <sstream>
4

5
void Trick::MemoryManager::clear_rvalue( void* base_address, ATTRIBUTES* attr, int curr_dim, int offset) {
46,232✔
6

7
    char* final_address;
8
    int remaining_dimensions = attr->num_index - curr_dim;
46,232✔
9

10
    /** @par Detailed Description: */
11

12
    /** @par
13
        If we're referencing a singleton then calculate the address
14
        from the (base) address, the offset and the data-type. Then
15
        set the value at that address to nil.
16
     */
17
    if (remaining_dimensions ==0) {
46,232✔
18

19
        switch (attr->type) {
43,764✔
20
           case TRICK_CHARACTER :
5,321✔
21
           case TRICK_UNSIGNED_CHARACTER :
22
               final_address = (char*)base_address + offset * sizeof(char);
5,321✔
23
               *(char*)final_address = '\0';
5,321✔
24
               break;
5,321✔
25
           case TRICK_BOOLEAN:
9,055✔
26
               final_address = (char*)base_address + offset * sizeof(bool);
9,055✔
27
               *(bool*)final_address = false;
9,055✔
28
               break;
9,055✔
29
           case TRICK_WCHAR :
2✔
30
               final_address = (char*)base_address + offset * sizeof(wchar_t);
2✔
31
               *(wchar_t*)final_address = 0;
2✔
32
               break;
2✔
33
           case TRICK_SHORT :
1,513✔
34
           case TRICK_UNSIGNED_SHORT :
35
               final_address = (char*)base_address + offset * sizeof(short);
1,513✔
36
               *(short*)final_address = 0;
1,513✔
37
               break;
1,513✔
38
           case TRICK_INTEGER :
9,076✔
39
           case TRICK_UNSIGNED_INTEGER :
40
               final_address = (char*)base_address + offset * sizeof(int);
9,076✔
41
               *(int*)final_address = 0;
9,076✔
42
               break;
9,076✔
43
           case TRICK_ENUMERATED :
230✔
44
               if ((size_t)attr->size == sizeof(int)) {
230✔
45
                   final_address = (char*)base_address + offset * sizeof(int);
229✔
46
                   *(int*)final_address = 0;
229✔
47
               } else if ((size_t)attr->size == sizeof(short)) {
1✔
48
                   final_address = (char*)base_address + offset * sizeof(short);
×
49
                   *(short*)final_address = 0;
×
50
               } else if ((size_t)attr->size == sizeof(char)) {
1✔
51
                   final_address = (char*)base_address + offset * sizeof(char);
1✔
52
                   *(char*)final_address = 0;
1✔
53
               } else {
54
                   emitError("INTERNAL-ERROR - Unexpected size of ENUMERATION type.") ;
×
55
               }
56
               break;
230✔
57
           case TRICK_LONG :
70✔
58
           case TRICK_UNSIGNED_LONG :
59
               final_address = (char*)base_address + offset * sizeof(long);
70✔
60
               *(long*)final_address = 0;
70✔
61
               break;
70✔
62
           case TRICK_FLOAT :
4✔
63
               final_address = (char*)base_address + offset * sizeof(float);
4✔
64
               *(float*)final_address = 0.0;
4✔
65
               break;
4✔
66
           case TRICK_DOUBLE :
4,995✔
67
               final_address = (char*)base_address + offset * sizeof(double);
4,995✔
68
               *(double*)final_address = 0.0;
4,995✔
69
               break;
4,995✔
70
           case TRICK_LONG_LONG :
6,294✔
71
           case TRICK_UNSIGNED_LONG_LONG :
72
               final_address = (char*)base_address + offset * sizeof(long long);
6,294✔
73
               *(long long*)final_address = 0;
6,294✔
74
               break;
6,294✔
75
           case TRICK_BITFIELD :
5✔
76
           case TRICK_UNSIGNED_BITFIELD :
77
               final_address = (char*)base_address + offset * (size_t)attr->size;
5✔
78
               if (attr->size == sizeof(int)) {
5✔
79
                   *(unsigned int*)final_address = insert_bitfield_any(
5✔
80
                       *(unsigned int*)final_address, 0, attr->size, attr->index[0].start, attr->index[0].size);
81
               } else if (attr->size == sizeof(short)) {
×
82
                   *(unsigned short*)final_address = insert_bitfield_any(
×
83
                       *(unsigned short*)final_address, 0, attr->size, attr->index[0].start, attr->index[0].size);
×
84
               } else if (attr->size == sizeof(char)) {
×
85
                   *(unsigned char*)final_address = insert_bitfield_any(
×
86
                       *(unsigned char*)final_address, 0, attr->size, attr->index[0].start, attr->index[0].size);
×
87
               } else {
88
                   std::stringstream message;
×
89
                   message << "INTERNAL - Unhandled bitfield struct size (" << attr->size << ") in bitfield assignment.";
×
90
                   emitError(message.str());
×
91
               }
92
               break;
5✔
93
           case TRICK_FILE_PTR :
×
94
               final_address = (char*)base_address + offset * sizeof(void*);
×
95
               *(void**)final_address = (void*)NULL;
×
96
               break;
×
97
           case TRICK_STRING :
5,577✔
98
               final_address = (char*)base_address + offset * sizeof(std::string);
5,577✔
99
               *(std::string*)final_address = "";
5,577✔
100
               break;
5,577✔
101
           case TRICK_STL :
1,622✔
102
               final_address = (char*)base_address + offset * attr->size ;
1,622✔
103
               if ( attr->clear_stl ) {
1,622✔
104
                   (*attr->clear_stl)(final_address) ;
1,579✔
105
               }
106
               break;
1,622✔
107
           default :
×
108
               std::stringstream message;
×
109
               message << "Unhandled Type (" << (int)attr->type << ").";
×
110
               emitError(message.str());
×
111
               break;
×
112
        }
113

114
    /** @par
115
        If on the otherhand we are referencing an array, then we must consider two cases.
116
     */
117
    } else if (remaining_dimensions > 0) {
2,468✔
118
       int extent;
119

120
       extent = attr->index[curr_dim].size ;
2,468✔
121

122
       /**  @par
123
            If the array is unconstrained (i.e., it's a pointer) then we just need to check
124
            whether the pointer is NULL.
125
        */
126
       if ( extent == 0) {
2,468✔
127
           final_address = (char*)base_address;
836✔
128
           *(void**)final_address = NULL;
836✔
129

130
       /** @par
131
           If the array (at this dimension) is constrained (i.e., it's a fixed array )
132
           then it is nil if and only if each of it's sub-elements (at the next dimension,
133
           which can themselves be arrays) are nil. So, for each of the elements in current
134
           dimension, we recursively call is_nil_valued() on each of the sub-elements to
135
           find out whether this array is nil valued and return the result.
136
           */
137
       } else {
138
           int ii;
139

140
           for (ii=0; ii < extent; ii++) {
8,999✔
141
               clear_rvalue( base_address, attr, curr_dim + 1, offset * extent + ii);
7,367✔
142
           }
143
       }
144

145
    } else {
146
        std::stringstream message;
×
147
        message << "This is bad. Remaining dimensions are negative!?.";
×
148
        emitError(message.str());
×
149
    }
150
}
46,232✔
151

152
void Trick::MemoryManager::clear_class( char *address, ATTRIBUTES * A) {
2,420✔
153

154
    int jj;
155
    char *element_addr;
156

157
    /** @par Design Details:
158
     This function is implemented using the following algorithm:
159
     */
160

161
    for (jj = 0; A[jj].name[0] != '\0'; jj++) {
39,875✔
162
        /*
163
         * For all the parameters in this data structure...
164
         */
165

166
        if (currentCheckPointAgent->input_perm_check(&A[jj])) {
37,455✔
167

168
            /**
169
             Determine the address of the structure/class element. If the element is a
170
             C++ static variable, then the element's ATTRIBUTES.offset contains the
171
             it's address.  Otherwise the element's address is the structure/class
172
             base address + the element's ATTRIBUTES.offset.
173
             */
174
            if (A[jj].mods & 2) {
37,444✔
175
                element_addr = (char *) A[jj].offset;
11✔
176
            } else {
177
                element_addr = address + A[jj].offset;
37,433✔
178
            }
179

180
            if (A[jj].type == TRICK_STRUCTURED) {
37,444✔
181
                if (A[jj].num_index != 0) {
729✔
182
                    clear_arrayed_class( element_addr, &A[jj], 0, 0);
145✔
183
                } else {
184
                    if ( A[jj].attr != NULL ) {
584✔
185
                        clear_class( element_addr, (ATTRIBUTES*)(A[jj].attr));
584✔
186
                    }
187
                }
188
            } else {
189
                clear_rvalue( element_addr, &(A[jj]), 0, 0);
36,715✔
190
            }
191

192
        }
193
    }
194
    return;
2,420✔
195
}
196

197
void Trick::MemoryManager::clear_arrayed_class( char* address, ATTRIBUTES* A, int curr_dim, int offset) {
662✔
198

199
    int ii, extent;
200

201
    /** @par Design Details:
202
     This function is implemented using the following algorithm:
203
     */
204

205
    /** If we are looking at the pointer then clear it..*/
206
    if (A->index[curr_dim].size == 0) {
662✔
207
        clear_rvalue( address, A, curr_dim, offset);
565✔
208
    } else {
209
        extent = A->index[curr_dim].size;
97✔
210

211
        for (ii = 0; ii < extent; ii++) {
2,042✔
212

213
            if (curr_dim < A->num_index - 1) {
1,945✔
214
                clear_arrayed_class( address, A, curr_dim + 1, offset * extent + ii);
422✔
215
            } else {
216
                char* element_addr;
217
                element_addr = address + (offset * extent + ii) * A->size ;
1,523✔
218
                clear_class( element_addr, (ATTRIBUTES*)A->attr);
1,523✔
219
            }
220
        }
221

222
    }
223
}
662✔
224

225
// MEMBER FUNCTION
226
void Trick::MemoryManager::clear_var_critical_section( void* address) {
1,993✔
227
    ATTRIBUTES* reference_attr;
228
    ALLOC_INFO* alloc_info;
229
    alloc_info = get_alloc_info_at( address);
1,993✔
230
    if (alloc_info != NULL) {
1,993✔
231
        reference_attr = make_reference_attr( alloc_info);
1,993✔
232
        if (alloc_info->type == TRICK_STRUCTURED ) {
1,993✔
233
            if (alloc_info->num_index != 0) {
408✔
234
                clear_arrayed_class( (char*)(alloc_info->start), reference_attr, 0, 0) ;
95✔
235
            } else {
236
                clear_class( (char*)(alloc_info->start), alloc_info->attr) ;
313✔
237
            }
238
        } else {
239
            clear_rvalue( alloc_info->start, reference_attr, 0, 0 );
1,585✔
240
        }
241
        free_reference_attr( reference_attr);
1,993✔
242
    } else {
243
        std::stringstream message;
×
244
        message << "Cannot clear the variable at address " << address
×
245
                << "because memory manager knows nothing about it." ;
×
246
        emitError(message.str());
×
247
    }
248
}
1,993✔
249

250
// MEMBER FUNCTION
251
void Trick::MemoryManager::clear_var( void* address) {
16✔
252
    pthread_mutex_lock(&mm_mutex);
16✔
253
    clear_var_critical_section(address);
16✔
254
    pthread_mutex_unlock(&mm_mutex);
16✔
255
}
16✔
256

257
// MEMBER FUNCTION
258
void Trick::MemoryManager::clear_var( const char* name) {
1✔
259

260
    VARIABLE_MAP::iterator pos;
1✔
261
    ALLOC_INFO *alloc_info;
262

263
    pthread_mutex_lock(&mm_mutex);
1✔
264
    pos = variable_map.find( name);
1✔
265

266
    if (pos != variable_map.end()) {
1✔
267
        alloc_info = pos->second;
1✔
268
        clear_var_critical_section( alloc_info->start);
1✔
269
    } else {
270
        std::stringstream message;
×
271
        message << "Can't clear variable \"" << name
272
                << "\" because it doesn't exist.";
×
273
        emitError(message.str());
×
274
    }
275
    pthread_mutex_unlock(&mm_mutex);
1✔
276
}
1✔
277

278
// MEMBER FUNCTION
279
void Trick::MemoryManager::clear_all_vars() {
15✔
280

281
    ALLOC_INFO_MAP::iterator pos;
15✔
282
    ALLOC_INFO* alloc_info;
283

284
    pthread_mutex_lock(&mm_mutex);
15✔
285
    for (pos=alloc_info_map.begin() ; pos!=alloc_info_map.end() ; pos++ ) {
1,991✔
286
        alloc_info = pos->second;
1,976✔
287
        clear_var_critical_section( alloc_info->start);
1,976✔
288
    }
289
    pthread_mutex_unlock(&mm_mutex);
15✔
290
}
15✔
291

292
// MEMBER FUNCTION
293
void Trick::MemoryManager::reset_memory() {
11✔
294

295
    ALLOC_INFO_MAP::iterator pos;
11✔
296
    ALLOC_INFO* alloc_info;
297
    std::vector<void*> deletion_list;
11✔
298
    int ii, n_addrs;
299

300
    // Clear the deleted_addr_list at the start of reset memory. As delete_var() is called,
301
    // and resetting_memory == true, the deleted address will be added to the deleted_addr_list.
302
    resetting_memory = true;
11✔
303
    deleted_addr_list.clear();
11✔
304

305
    pthread_mutex_lock(&mm_mutex);
11✔
306
    for (pos=alloc_info_map.begin() ; pos!=alloc_info_map.end() ; pos++ ) {
345✔
307
        alloc_info = pos->second;
334✔
308

309
        if (alloc_info->stcl == TRICK_LOCAL) {
334✔
310
            deletion_list.push_back( alloc_info->start);
59✔
311
        }
312
    }
313
    pthread_mutex_unlock(&mm_mutex);
11✔
314

315
    n_addrs = deletion_list.size();
11✔
316
    for (ii = 0 ; ii < n_addrs ; ii ++) {
70✔
317
        delete_var( deletion_list[ii]);
59✔
318
    }
319

320
    resetting_memory = false;
11✔
321
    deleted_addr_list.clear();
11✔
322

323
    // reset counter to 100mil.  This (hopefully) ensures all alloc'ed ids are after external variables.
324
    alloc_info_map_counter = 100000000 ;
11✔
325
}
11✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc