Skip to content

Commit

Permalink
Fix the alloc_type of allocations performed by declare_var. #1529 (#1639
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jmpenn authored Jan 24, 2024
1 parent 7547d36 commit bfb6419
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
8 changes: 8 additions & 0 deletions include/trick/io_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
#include "trick/attributes.h"
#include "trick/parameter_types.h"

/**
* enum TRICK_STCL - indicates whether or not the MemoryManager allocated the object.
* -------------------------------------------------------------------------------------------
* TRICK_LOCAL - The memory manager allocated the object, via a form of declare_var..().
* TRICK_EXTERN - The memory manager did not allocate the object, but was given a pointer to
* an object via a form of declare_extern_var..().
* -------------------------------------------------------------------------------------------
*/
typedef enum {
TRICK_LOCAL = 0,
TRICK_EXTERN = 1,
Expand Down
4 changes: 2 additions & 2 deletions trick_source/sim_services/MemoryManager/MemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ Trick::MemoryManager::~MemoryManager() {
io_src_delete_class( ai_ptr );
}
}
free(ai_ptr->name);
free(ai_ptr->user_type_name);
if (ai_ptr->name) { free(ai_ptr->name); }
if (ai_ptr->user_type_name) { free(ai_ptr->user_type_name); }
free(ai_ptr) ;
}
alloc_info_map.clear() ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void* Trick::MemoryManager::declare_var( TRICK_TYPE type,
char* allocation_name;
int n_elems;
Language language;
TRICK_ALLOC_TYPE allocation_type;
void* address;
ATTRIBUTES* sub_attr;
ALLOC_INFO *new_alloc;
Expand Down Expand Up @@ -126,6 +127,8 @@ void* Trick::MemoryManager::declare_var( TRICK_TYPE type,
return ((void*)NULL);
}
language = Language_CPP;
/* io_src_allocate_class allocates objects using calloc (only). */
allocation_type = TRICK_ALLOC_MALLOC;

} else if ((type == TRICK_STRING) && (n_stars == 0 ) ) {

Expand Down Expand Up @@ -157,6 +160,7 @@ void* Trick::MemoryManager::declare_var( TRICK_TYPE type,
new_alloc->size = size;
new_alloc->language = language;
new_alloc->type = type;
new_alloc->alloc_type = allocation_type;

if ((type == TRICK_STRUCTURED) || (type == TRICK_ENUMERATED)) {
new_alloc->user_type_name = strdup( user_type_name.c_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ int Trick::MemoryManager::delete_var(void* address ) {
*/
if ( alloc_info->stcl == TRICK_LOCAL ) {
if ( alloc_info->alloc_type == TRICK_ALLOC_MALLOC ) {

// This will call a destructor ONLY if alloc_info->type is TRICK_STRUCTURED.
// Otherwise it does nothing.
io_src_destruct_class( alloc_info );

// The destructor that we just called MAY have deleted addresses
Expand Down
13 changes: 13 additions & 0 deletions trick_source/sim_services/MemoryManager/test/MM_delete_var.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
PURPOSE: (Testing)
*/
#include <stddef.h>

class CountMe {
public:
int a;
static size_t count;
CountMe() { count ++; }
~CountMe() { count --; }
} ;

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "trick/MemoryManager.hh"
#include "MM_test.hh"
#include <iostream>
#include "MM_delete_var.hh"
size_t CountMe::count = 0;

/*
Test Fixture.
Expand All @@ -21,6 +23,27 @@ class MM_delete_var_unittest : public ::testing::Test {
================================================================================
*/

TEST_F(MM_delete_var_unittest, CXX_object_constructor_destructor) {

// ===========================================================================================
// This test determines:
// 1) whether a class's constructor is called when declare_var is called and,
// 2) whether a class's destructor is being called when delete_var is called.
// ===========================================================================================

EXPECT_TRUE(std::is_pod<CountMe>::value == false);

EXPECT_EQ(0, CountMe::count);
CountMe* cm1_p = (CountMe*)memmgr->declare_var("CountMe cm1");
EXPECT_EQ(1, CountMe::count);
CountMe* cm2_p = (CountMe*)memmgr->declare_var("CountMe cm2");
EXPECT_EQ(2, CountMe::count);
memmgr->delete_var("cm1");
EXPECT_EQ(1, CountMe::count);
memmgr->delete_var("cm2");
EXPECT_EQ(0, CountMe::count);
}

TEST_F(MM_delete_var_unittest, var_exists) {

int exists;
Expand Down
4 changes: 3 additions & 1 deletion trick_source/sim_services/MemoryManager/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ io_headers = MM_user_defined_types.hh \
MM_write_checkpoint.hh \
MM_get_enumerated.hh \
MM_ref_name_from_address.hh \
MM_stl_testbed.hh
MM_stl_testbed.hh \
MM_delete_var.hh

# List of .cpp files produced by ICG from the io_headers.
io_source = $(patsubst %.hh,io_%.cpp,$(io_headers))
Expand Down Expand Up @@ -119,6 +120,7 @@ $(TESTS) : %: %.o
# ----------------------------------------------------------------------------------
# The following unittest programs are also dependent on the indicated object files.
# ----------------------------------------------------------------------------------
MM_delete_var_unittest : io_MM_delete_var.o
MM_declare_var_unittest : io_MM_user_defined_types.o
MM_declare_var_2_unittest : io_MM_user_defined_types.o
MM_declare_extern_var_unittest : io_MM_user_defined_types.o
Expand Down

0 comments on commit bfb6419

Please sign in to comment.