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

nasa / trick / 25459968639

06 May 2026 08:42PM UTC coverage: 55.916% (-0.8%) from 56.7%
25459968639

Pull #2011

github

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

14607 of 26123 relevant lines covered (55.92%)

466416.66 hits per line

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

52.07
/trick_source/sim_services/MemoryManager/MemoryManager_realloc.cpp
1
#include "trick/MemoryManager.hh"
2
#include <dlfcn.h>
3
#include <stdlib.h>
4
#include <sstream>
5
#include <string.h>
6

7
// PRIVATE MEMBER FUNCTION
8
void Trick::MemoryManager::recursive_array_copy( void *s_base,
218✔
9
                                                 void *d_base,
10
                                                 int *s_cdims,
11
                                                 int *d_cdims,
12
                                                 size_t size,
13
                                                 int n,
14
                                                 int cur,
15
                                                 int s_off,
16
                                                 int d_off) {
17
    char *s_addr, *d_addr;
18
    int ii;
19

20
    if (cur == n) {
218✔
21
        s_addr = (char*)s_base + s_off * size;
171✔
22
        d_addr = (char*)d_base + d_off * size;
171✔
23
        memcpy( d_addr, s_addr, size);
171✔
24
    } else {
25

26
        for (ii=0 ; (ii<s_cdims[cur])&&(ii<d_cdims[cur]) ; ii++ ) {
222✔
27
              recursive_array_copy( s_base,
175✔
28
                                    d_base,
29
                                    s_cdims,
30
                                    d_cdims,
31
                                    size,
32
                                    n,
33
                                    cur+1,
34
                                    s_off * s_cdims[cur] + ii,
175✔
35
                                    d_off * d_cdims[cur] + ii);
175✔
36
        }
37
    }
38
}
218✔
39

40

41
// PRIVATE MEMBER FUNCTION
42
void Trick::MemoryManager::array_copy( void *s_base,
43✔
43
                                       void *d_base,
44
                                       int *s_cdims,
45
                                       int *d_cdims,
46
                                       size_t size,
47
                                       int n) {
48

49
   recursive_array_copy( s_base, d_base, s_cdims, d_cdims, size, n, 0, 0, 0);
43✔
50
}
43✔
51

52

53
// PUBLIC MEMBER FUNCTION
54
void* Trick::MemoryManager::resize_array( void *address, int n_cdims, int *cdims) {
43✔
55

56
    ALLOC_INFO *alloc_info;
57
    int ii;
58
    int pre_n_cdims;
59
    int new_n_elems;
60
    int n_stars;
61
    void *new_address;
62

63
    /** @li Find the ALLOC_INFO record for the object at the given address.*/
64
    pthread_mutex_lock(&mm_mutex);
43✔
65
    alloc_info = get_alloc_info_at( address);
43✔
66

67
    if (alloc_info == NULL) {
43✔
68
        emitError("Address passed to resize_array is NULL.") ;
×
69
        pthread_mutex_unlock(&mm_mutex);
×
70
        return ( (void*)NULL);
×
71
    }
72

73
    if (alloc_info->stcl != TRICK_LOCAL) {
43✔
74
        std::stringstream message;
×
75
        message << "Cannot resize the array at [" << address << "] because it's EXTERN.";
×
76
        emitError(message.str());
×
77
        pthread_mutex_unlock(&mm_mutex);
×
78
        return ( (void*)NULL);
×
79
    }
×
80

81
    if (alloc_info->type == TRICK_STRING) {
43✔
82
        std::stringstream message;
×
83
        message << "resize_array doesn't support STL strings.";
×
84
        emitError(message.str());
×
85
        pthread_mutex_unlock(&mm_mutex);
×
86
        return ( (void*)NULL);
×
87
    }
×
88

89
    /** @li Determine the number of constrained dimensions in the existing allocation.*/
90
    pre_n_cdims = 0;
43✔
91
    for (ii=0 ; ii<alloc_info->num_index ; ii++) {
125✔
92
       if (alloc_info->index[ii]!=0) {
82✔
93
           pre_n_cdims++;
45✔
94
       }
95
    }
96
    n_stars = alloc_info->num_index - pre_n_cdims;
43✔
97
    pthread_mutex_unlock(&mm_mutex);
43✔
98

99
    /** @li Verify that the number of constrained dimensions in the existing allocation
100
        are the same as those in the proposed re-allocation.*/
101
    if (n_cdims != pre_n_cdims) {
43✔
102
        if (pre_n_cdims == 0) {
×
103
            std::stringstream message;
×
104
            message << "resize_array: The object at address [" << address
×
105
                    << "] is not an array. If you want to be able to resize "
106
                    << "it then declare it as an array (of at least one).";
×
107
            emitError(message.str());
×
108
        } else {
×
109
            std::stringstream message;
×
110
            message << "resize_array: The number of dimensions specified, [" << n_cdims
×
111
                    << "], doesn't match the number of dimensions of the object at "
×
112
                    << address <<", [" << pre_n_cdims << "].";
×
113
            emitError(message.str());
×
114
        }
×
115
        return ( (void*)NULL);
×
116
    }
117

118
    /** @li Calculate the new number of elements to be allocated. */
119
    new_n_elems = 1;
43✔
120
    for (ii = 0; ii < n_cdims ; ii++ ) {
88✔
121
        new_n_elems = new_n_elems * cdims[ii];
45✔
122
    }
123
    if (new_n_elems == 0) {
43✔
124
        emitError("One or more of the constrained dimensions is zero.");
×
125
        return ((void*)NULL);
×
126
    }
127

128
    /** @li Allocate new memory. */
129
    pthread_mutex_lock(&mm_mutex);
43✔
130
    if ( (alloc_info->type == TRICK_STRUCTURED) &&
43✔
131
         (alloc_info->language == Language_CPP) &&
37✔
132
         (alloc_info->num_index == n_cdims) ) {
×
133

134
        if ((new_address = io_src_allocate_class( alloc_info->user_type_name, new_n_elems)) == NULL) {
×
135
            std::stringstream message;
×
136
            message << "io_src_allocate_class(" << alloc_info->user_type_name << "," << new_n_elems << ") failed to allocate any memory.";
×
137
            emitError(message.str());
×
138
            pthread_mutex_unlock(&mm_mutex);
×
139
            return ((void*)NULL);
×
140
        }
×
141
    } else if ((alloc_info->type == TRICK_STRING) && (n_stars == 0 ) ) {
43✔
142
        new_address = new (std::nothrow) std::string[new_n_elems];
×
143
        if (new_address == NULL) {
×
144
            pthread_mutex_unlock(&mm_mutex);
×
145
            return ((void*)NULL);
×
146
        }
147
    } else {
148
        if ( (new_address = calloc( (size_t)new_n_elems, (size_t)alloc_info->size ) ) == NULL) {
43✔
149
            emitError("Out of memory.") ;
×
150
            pthread_mutex_unlock(&mm_mutex);
×
151
            return ((void*)NULL);
×
152
        }
153
    }
154

155
    /** @li Copy the data from the previous array to the new array, constrained by the common extents.*/
156
    array_copy( alloc_info->start,
43✔
157
                new_address,
158
                alloc_info->index,
43✔
159
                cdims,
160
                alloc_info->size,
43✔
161
                n_cdims);
162

163
    /** @li Delete the previous memory allocation. */
164
    free( address);
43✔
165

166
    // Remove the old <address, ALLOC_INFO*> key-value pair from the alloc_info_map.
167
    alloc_info_map.erase( address);
43✔
168

169
    /** @li Update the ALLOC_INFO record with new start and end addresses, with
170
            new extents and with the new number of elements.*/
171
    alloc_info->start = new_address;
43✔
172
    alloc_info->end   = ( (char*)alloc_info->start) + (new_n_elems * alloc_info->size) - 1;
43✔
173
    for (ii=0 ; ii < n_cdims ; ii++) {
88✔
174
        alloc_info->index[ii] = cdims[ii];
45✔
175
    }
176
    alloc_info->num = new_n_elems;
43✔
177

178
    /** @li Insert the new <address, ALLOC_INFO> key-value pair into the alloc_info_map.*/
179
    alloc_info_map[alloc_info->start] = alloc_info;
43✔
180
    pthread_mutex_unlock(&mm_mutex);
43✔
181

182
    /** @li If debug is enabled, show what happened.*/
183
    if (debug_level) {
43✔
184
        int i;
185
        std::cout << std::endl;
×
186
        std::cout << "Re-allocation: " << alloc_info->num << " element(s) of type(" ;
×
187
        std::cout << trickTypeCharString( alloc_info->type, alloc_info->user_type_name ) ;
×
188
        for (i=0;i<n_stars;i++) std::cout << "*";
×
189
        std::cout << "," << alloc_info->type << "), size(" << alloc_info->size << ") @ addr(" << alloc_info->start << ")." ;
×
190
        std::cout << std::endl << std::endl;
×
191
        std::cout.flush();
×
192
    }
193
    return (new_address);
43✔
194
}
195

196

197
// PUBLIC MEMBER FUNCTION
198
void* Trick::MemoryManager::resize_array( void* address, int num) {
38✔
199
    int dimension = num;
38✔
200

201
    return( resize_array( address, 1, &dimension));
76✔
202
}
203

204

205
// PUBLIC MEMBER FUNCTION
206
void* Trick::MemoryManager::resize_array( const char* name, int n_cdims, int *cdims) {
2✔
207

208
    VARIABLE_MAP::iterator pos;
2✔
209
    ALLOC_INFO *alloc_info;
210

211
    pthread_mutex_lock(&mm_mutex);
2✔
212
    pos = variable_map.find( name);
4✔
213

214
    if (pos != variable_map.end()) {
2✔
215
        alloc_info = pos->second;
2✔
216
        pthread_mutex_unlock(&mm_mutex);
2✔
217
        return( resize_array( alloc_info->start, n_cdims, cdims));
2✔
218
    } else {
219
        std::stringstream message;
×
220
        message << "Cannot resize variable \"" << name << "\" because it doesn't exist.";
×
221
        emitError(message.str());
×
222
    }
×
223
    pthread_mutex_unlock(&mm_mutex);
×
224
    return NULL ;
×
225
}
226

227

228
// PUBLIC MEMBER FUNCTION
229
void* Trick::MemoryManager::resize_array( const char* name, int num) {
1✔
230
    int dimension = num;
1✔
231

232
    return( resize_array( name, 1, &dimension));
2✔
233
}
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