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

nasa / trick / 25456501308

06 May 2026 07:29PM UTC coverage: 55.935% (-0.8%) from 56.7%
25456501308

Pull #2011

github

web-flow
Merge 7ad262960 into 7054e405e
Pull Request #2011: Single-file CI and code style adoption

14612 of 26123 relevant lines covered (55.94%)

462107.16 hits per line

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

66.67
/trick_source/sim_services/MemoryManager/MemoryManager_map_external_object.cpp
1
#include <sstream>
2
#include <stdlib.h>
3
#include <string.h>
4

5
#include "trick/MemoryManager.hh"
6
#include "trick/ADefParseContext.hh"
7

8
/**
9
 @par Description:
10
 declare_extern_var describes an externally allocated memory object to the memory manager.
11
 */
12

13
// MEMBER FUNCTION
14
void* Trick::MemoryManager::
4,974✔
15
      declare_extern_var( void* address,
16
                           TRICK_TYPE type,
17
                           std::string user_type_name,
18
                           int n_stars,
19
                           std::string var_name,
20
                           int n_cdims,
21
                           int *cdims) {
22

23
    int ii;
24
    int size;
25
    char* allocation_name;
26
    int n_elems;
27
    ATTRIBUTES* sub_attr;
28
    ALLOC_INFO *new_alloc;
29
    VARIABLE_MAP::iterator variable_pos;
4,974✔
30

31
    /** @par Design Details:
32
     This function is implemented using the following algorithm:
33
     */
34

35
    /** @li Validate Parameters. The address can't be NULL.*/
36
    if (address == NULL) {
4,974✔
37
        emitError("declare_extern_var() called with NULL address.");
×
38
        return ((void*)NULL);
×
39
    }
40

41
    /** @li Determine whether the given variable name is already in use. */
42
    if (var_name != "") {
4,974✔
43
        pthread_mutex_lock(&mm_mutex);
4,941✔
44
        variable_pos = variable_map.find( var_name);
4,941✔
45
        if (variable_pos != variable_map.end()) {
4,941✔
46
            std::stringstream message;
×
47
            message << "Variable \""<< var_name <<"\" already declared.";
×
48
            emitError(message.str());
×
49
            pthread_mutex_unlock(&mm_mutex);
×
50
            return ((void*)NULL);
×
51
        }
×
52
        pthread_mutex_unlock(&mm_mutex);
4,941✔
53
        allocation_name = strdup( var_name.c_str());
4,941✔
54
    } else {
55
        allocation_name = NULL;
33✔
56
    }
57

58

59
    /** @li Calculate the number of elements in this external variable. This is
60
            the product of the sizes of the constrained dimensions. */
61
    n_elems = 1;
4,974✔
62
    for (ii = 0; ii < n_cdims ; ii++ ) {
5,004✔
63
        n_elems = n_elems * cdims[ii];
30✔
64
    }
65
    if (n_elems == 0) {
4,974✔
66
        std::stringstream message;
×
67
        message << "declare_extern_var() can't register \"" << var_name
68
                << "\" because one or more of its constrained dimensions "
69
                << "is zero, thus making its total size zero.";
×
70
        emitError(message.str());
×
71
        return ((void*)NULL);
×
72
    }
×
73

74
    /** @li From the TRICK_TYPE, user_type_name and the number of pointers (asterisks),
75
            determine the size and the attributes of an element. */
76
    if ( get_type_attributes(type, user_type_name, n_stars, sub_attr, size) != 0) {
4,974✔
77
        std::stringstream message;
×
78
        message << "get_type_attributes failed for type "
×
79
                << type << " \"" << user_type_name << "\".";
×
80
        emitError(message.str());
×
81
        return ((void*)NULL);
×
82
    }
×
83

84
    /** @li Allocate and populate an ALLOC_INFO record for the external allocation
85
        (the thingy pointed to by @b address).
86
     */
87
    if ((new_alloc = (ALLOC_INFO*)calloc(1, sizeof(ALLOC_INFO))) != NULL) {
4,974✔
88

89
        new_alloc->start = (void *) address;
4,974✔
90
        new_alloc->end = ((char*)new_alloc->start) + (n_elems * size) - 1;
4,974✔
91
        new_alloc->name = allocation_name;
4,974✔
92
        new_alloc->stcl = TRICK_EXTERN;
4,974✔
93
        new_alloc->alloc_type = TRICK_ALLOC_OTHER;
4,974✔
94
        new_alloc->size = size;
4,974✔
95
        if ( sub_attr != NULL ) {
4,974✔
96
            new_alloc->language = sub_attr->language ;
4,899✔
97
        } else {
98
            new_alloc->language = Language_C ;
75✔
99
        }
100
        new_alloc->type = type;
4,974✔
101

102
        if ((type == TRICK_STRUCTURED) || (type == TRICK_ENUMERATED) || (type == TRICK_OPAQUE_TYPE)) {
4,974✔
103
            new_alloc->user_type_name = strdup( user_type_name.c_str());
4,899✔
104
        } else {
105
            new_alloc->user_type_name = NULL ;
75✔
106
        }
107

108
        new_alloc->attr = sub_attr;
4,974✔
109
        new_alloc->num = n_elems;
4,974✔
110

111
        new_alloc->num_index = 0;
4,974✔
112
        for (ii = 0; ii < n_cdims ; ii++ ) {
5,004✔
113
            new_alloc->index[new_alloc->num_index] = cdims[ii];
30✔
114
            new_alloc->num_index ++ ;
30✔
115
        }
116
        for (ii = 0 ; ii < n_stars ; ii++) {
4,981✔
117
            new_alloc->index[new_alloc->num_index] = 0;
7✔
118
            new_alloc->num_index ++ ;
7✔
119
        }
120

121
        new_alloc->id = extern_alloc_info_map_counter++ ;
4,974✔
122

123
        /** @li Insert the <address, ALLOC_INFO> key-value pair into the alloc_info_map.*/
124
        pthread_mutex_lock(&mm_mutex);
4,974✔
125
        alloc_info_map[address] = new_alloc;
4,974✔
126

127
        /** @li Insert the <variable-name, ALLOC_INFO> key-value pair into the variable map. */
128
        if (new_alloc->name) {
4,974✔
129
            variable_map[new_alloc->name] = new_alloc;
14,823✔
130
        }
131
        pthread_mutex_unlock(&mm_mutex);
4,974✔
132
    } else {
133
        emitError("Out of memory.") ;
×
134
        return ((void*)NULL);
×
135
    }
136

137
    if (debug_level) {
4,974✔
138
        int i;
139
        std::cout << std::endl;
×
140
        std::cout << "Extern declaration: " << new_alloc->num << " element(s) of type(" ;
×
141
        std::cout << trickTypeCharString(type, user_type_name.c_str()) ;
×
142
        for (i=0;i<n_stars;i++) {
×
143
            std::cout << "*";
×
144
        }
145
        std::cout << "), size(" << size << ") @ addr(" << address << ")." ;
×
146
        std::cout << std::endl << std::endl;
×
147
        std::cout.flush();
×
148
    }
149

150
    /** @li Return the address of the allocation. */
151
    return (address);
4,974✔
152
}
153

154
// MEMBER FUNCTION
155
void* Trick::MemoryManager::
98✔
156
      declare_extern_var(void*       address,
157
                          const char* alloc_definition) {
158

159
    void* res_address = NULL;
98✔
160

161
    ADefParseContext* context = NULL;
98✔
162
    std::stringstream alloc_decl_sstream;
98✔
163

164
    /** @par Design Details:
165
     This function is implemented using the following algorithm:
166
     */
167

168
    alloc_decl_sstream << alloc_definition;
98✔
169

170
    /** @li Create a parse context. */
171
    context = new ADefParseContext( &alloc_decl_sstream);
98✔
172

173
    /** @li Call ADEF_parse to parse the declaration. */
174
    if (context != NULL) {
98✔
175
        if ( ADEF_parse( context) == 0) {
98✔
176

177
            /** @li Call the general form of declare_extern_var. */
178
            res_address = declare_extern_var( address,
98✔
179
                                               context->type,
180
                                               context->user_type_name,
98✔
181
                                               context->n_stars,
182
                                               context->var_name,
98✔
183
                                               context->n_cdims,
184
                                               context->cdims);
98✔
185

186
            /** @li Delete the parse context. */
187
            delete( context);
98✔
188
        } else {
189
            std::stringstream message;
×
190
            message << "Invalid declaration \"" << alloc_definition << "\".";
×
191
            emitError(message.str());
×
192
        }
×
193
    }
194
    /** @li Return the address. */
195
    return ( res_address);
98✔
196
}
98✔
197

198
// MEMBER FUNCTION
199
void* Trick::MemoryManager::
1✔
200
      declare_extern_var( void*       address,
201
                          const char* element_definition,
202
                          int         n_elems) {
203

204
    void* res_address = NULL;
1✔
205
    int cdims[8];
206
    int n_cdims;
207

208
    ADefParseContext* context = NULL;
1✔
209
    std::stringstream alloc_decl_sstream;
1✔
210

211
    /** We know that our array will be at least one dimensional and that dimension contains n_elems elements. */
212
    cdims[0] = n_elems;
1✔
213
    n_cdims = 1;
1✔
214

215
    alloc_decl_sstream << element_definition;
1✔
216

217
    /** @li Create a parse context. */
218
    context = new ADefParseContext( &alloc_decl_sstream);
1✔
219

220
    if (context != NULL) {
1✔
221

222
    /** @li Parse the allocation definition and ensure that the dimension is at least one less than the maximum of 8. */
223
        if (( ADEF_parse( context) == 0) && (context->n_cdims < 8) ){
1✔
224
            int ii;
225

226
            /** @li Add the dimensions of the element definition. */
227
            for (ii=0 ; ii < context->n_cdims ; ii++) {
1✔
228
                cdims[ii+1] = context->cdims[ii];
×
229
                n_cdims ++;
×
230
            }
231

232
            /** @li Call the general form of declare_var to perform the allocation. */
233
            res_address = declare_extern_var( address,
1✔
234
                                              context->type,
235
                                              context->user_type_name,
1✔
236
                                              context->n_stars,
237
                                              context->var_name,
1✔
238
                                              n_cdims,
239
                                              cdims);
240

241
            /** @li Delete the parse context. */
242
            delete( context);
1✔
243
        } else {
244
            std::stringstream message;
×
245
            message << "declare_extern_var( \"" << element_definition
246
                    << "\"," << n_elems << ") failed.";
×
247
            emitError(message.str());
×
248
        }
×
249
    }
250
    /** @li Return the address of the allocation. */
251
    return ( res_address);
1✔
252
}
1✔
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