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

nasa / trick / 11353097378

15 Oct 2024 07:31PM UTC coverage: 55.836% (-0.06%) from 55.896%
11353097378

Pull #1796

github

web-flow
Merge 1f000ead6 into b2403dcfc
Pull Request #1796: Taking CentOS to the glue factory

12279 of 21991 relevant lines covered (55.84%)

66667.68 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 ) {
4,010✔
11

12
    if (address == 0) {
4,010✔
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);
4,010✔
23

24
    if ((alloc_info != NULL)) {
4,010✔
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);
4,010✔
31
        alloc_info_map.erase( address);
4,010✔
32
        // END PROTECTION of the alloc_info_map.
33
        pthread_mutex_unlock(&mm_mutex);
4,010✔
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 ) {
4,010✔
40
            // The destructor that we just called MAY have deleted addresses
41
            // that are already planned for deletion, say during reset_memory.
42
            // So, keep a record of what we've recently deleted so we don't
43
            // to warn that we can't find it, when reset_memory also tries to
44
            // delete that same address. Same for TRICK_ALLOC_NEW block
45
            deleted_addr_list.push_back(address);
4,004✔
46

47
            if ( alloc_info->alloc_type == TRICK_ALLOC_MALLOC ) {
4,004✔
48
                // This will call a destructor ONLY if alloc_info->type is TRICK_STRUCTURED.
49
                // Otherwise it does nothing.
50
                io_src_destruct_class( alloc_info );
2,573✔
51

52
                free( address);
2,573✔
53
            } else if ( alloc_info->alloc_type == TRICK_ALLOC_NEW ) {
1,431✔
54
                io_src_delete_class( alloc_info );
7✔
55
            }
56
        }
57

58
        if (debug_level) {
4,010✔
59
            std::cout << "Deleted allocation at address " << alloc_info->start << "." << std::endl;
×
60
            std::cout.flush();
×
61
        }
62

63
        if (alloc_info->name ) {
4,010✔
64
            pthread_mutex_lock(&mm_mutex);
3,635✔
65
            variable_map.erase( alloc_info->name);
3,635✔
66
            pthread_mutex_unlock(&mm_mutex);
3,635✔
67
            free(alloc_info->name);
3,635✔
68
        }
69

70
        if ( alloc_info->user_type_name ) {
4,010✔
71
            free(alloc_info->user_type_name);
219✔
72
        }
73

74
        // Delete the alloc_info record.
75
        free(alloc_info);
4,010✔
76

77
    } else {
78

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

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

105
        return 1 ;
×
106
    }
107

108
    return 0;
4,010✔
109
}
110

111
// MEMBER FUNCTION
112
int Trick::MemoryManager::delete_var( std::string name) {
1,774✔
113

114
    VARIABLE_MAP::iterator pos;
1,774✔
115
    ALLOC_INFO *alloc_info;
116

117
    pthread_mutex_lock(&mm_mutex);
1,774✔
118
    pos = variable_map.find( name);
1,774✔
119

120
    if (pos != variable_map.end()) {
1,774✔
121
        alloc_info = pos->second;
1,774✔
122

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

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

141
// MEMBER FUNCTION
142
int Trick::MemoryManager::delete_extern_var( std::string name) {
1✔
143
    // delete_var will remove the external variable from the allocation map
144
    return delete_var(name) ;
1✔
145
}
146

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