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

nasa / trick / 4735396045

pending completion
4735396045

push

github

GitHub
Update README.md

12510 of 21441 relevant lines covered (58.35%)

873896.93 hits per line

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

59.02
/trick_source/sim_services/MemoryManager/MemoryManager_delete_var.cpp
1
#include <stdlib.h>
2
#include <iostream>
3

4
#include <sstream>
5
#include <algorithm>
6
#include <dlfcn.h>
7
#include "trick/MemoryManager.hh"
8

9
// MEMBER FUNCTION
10
int Trick::MemoryManager::delete_var(void* address ) {
5,335✔
11

12
    if (address == 0) {
5,335✔
13
        if (debug_level) {
×
14
            std::stringstream message;
×
15
            message << "Cannot delete memory at NULL.";
×
16
            emitWarning(message.str());
×
17
        }
18
        return 1;
×
19
    }
20

21
    // Find the allocation record for this address.
22
    ALLOC_INFO * alloc_info = get_alloc_info_at( address);
5,335✔
23

24
    if ((alloc_info != NULL)) {
5,335✔
25
        /*
26
           Remove the allocation from the map first so we don't have trouble
27
           if io_src_destruct_class or io_src_delete_class calls delete_var recursively.
28
         */
29
        // BEGIN PROTECTION of the alloc_info_map.
30
        pthread_mutex_lock(&mm_mutex);
5,335✔
31
        alloc_info_map.erase( address);
5,335✔
32
        // END PROTECTION of the alloc_info_map.
33
        pthread_mutex_unlock(&mm_mutex);
5,335✔
34

35
        /* Verify that it's OK to free the memory at the given address.
36
           It's OK if an allocation record exists for this address and the
37
           MemoryManager allocated it.
38
         */
39
        if ( alloc_info->stcl == TRICK_LOCAL ) {
5,335✔
40
            if ( alloc_info->alloc_type == TRICK_ALLOC_MALLOC ) {
5,330✔
41
                io_src_destruct_class( alloc_info );
5,325✔
42

43
                // The destructor that we just called MAY have deleted addresses
44
                // that are already planned for deletion, say during reset_memory.
45
                // So, keep a record of what we've recently deleted so we don't
46
                // to warn that we can't find it, when reset_memory also tries to
47
                // delete that same address.
48
                deleted_addr_list.push_back(address);
5,325✔
49

50
                free( address);
5,325✔
51
            } else if ( alloc_info->alloc_type == TRICK_ALLOC_NEW ) {
5✔
52
                io_src_delete_class( alloc_info );
5✔
53
            }
54
        }
55

56
        if (debug_level) {
5,335✔
57
            std::cout << "Deleted allocation at address " << alloc_info->start << "." << std::endl;
×
58
            std::cout.flush();
×
59
        }
60

61
        if (alloc_info->name ) {
5,335✔
62
            pthread_mutex_lock(&mm_mutex);
3,668✔
63
            variable_map.erase( alloc_info->name);
3,668✔
64
            pthread_mutex_unlock(&mm_mutex);
3,668✔
65
            free(alloc_info->name);
3,668✔
66
        }
67

68
        if ( alloc_info->user_type_name ) {
5,335✔
69
            free(alloc_info->user_type_name);
333✔
70
        }
71

72
        // Delete the alloc_info record.
73
        free(alloc_info);
5,335✔
74

75
    } else {
76

77
        // The allocation (address) we're tring to delete may have just been
78
        // deleted by a user's destructor (using delete_var). Check the deleted_addr_list
79
        // to see if the allocation was just deleted. If it was, then there's no
80
        // problem. If it wasn't, then the MemoryManager never knew about it, and
81
        // this call to delete_var() is a problem.
82

83
        if ( resetting_memory == true) {
×
84
            // Check the deleted_addr_list to see whether we recently deleted this address.
85
            std::list<void*>::iterator iter =
86
                std::find( deleted_addr_list.begin(), deleted_addr_list.end(), address );
×
87
            // If we didn't recently delete it, then there's a problem.
88
            if ( iter == deleted_addr_list.end() ) {
×
89
                std::stringstream message;
×
90
                message << "The MemoryManager cannot delete memory at address ["
×
91
                        << address << "] because it has no record of it. Furthermore,"
×
92
                        << " the MemoryManager has not recently deleted it while"
93
                        << " resetting memory for a checkpoint reload.";
×
94
                emitWarning(message.str());
×
95
            }
96
        } else {
97
            std::stringstream message;
×
98
            message << "The MemoryManager cannot delete memory at address ["
×
99
                    << address << "] because it has no record of it.";
×
100
            emitWarning(message.str());
×
101
        }
102

103
        return 1 ;
×
104
    }
105

106
    return 0;
5,335✔
107
}
108

109
// MEMBER FUNCTION
110
int Trick::MemoryManager::delete_var( const char* name) {
1,784✔
111

112
    VARIABLE_MAP::iterator pos;
1,784✔
113
    ALLOC_INFO *alloc_info;
114

115
    pthread_mutex_lock(&mm_mutex);
1,784✔
116
    pos = variable_map.find( name);
1,784✔
117

118
    if (pos != variable_map.end()) {
1,784✔
119
        alloc_info = pos->second;
1,784✔
120

121
        pthread_mutex_unlock(&mm_mutex);
1,784✔
122
        return( delete_var( alloc_info->start));
1,784✔
123
    } else {
124
        std::stringstream message;
×
125
        message << "Cannot delete variable \"" << name
126
                << "\" because it doesn't exist." ;
×
127
        emitError(message.str());
×
128
    }
129
    pthread_mutex_unlock(&mm_mutex);
×
130
    return 1;
×
131
}
132

133
// MEMBER FUNCTION
134
int Trick::MemoryManager::delete_extern_var( void* address) {
1✔
135
    // delete_var will remove the external variable from the allocation map
136
    return delete_var(address) ;
1✔
137
}
138

139
// MEMBER FUNCTION
140
int Trick::MemoryManager::delete_extern_var( const char* name) {
1✔
141
    // delete_var will remove the external variable from the allocation map
142
    return delete_var(name) ;
1✔
143
}
144

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