Skip to content

Commit

Permalink
Clean up BPatch::createEnum
Browse files Browse the repository at this point in the history
This removes the unnecessary construction of a separate (thread-safe!)
container of pairs and the associated memory leaks. It also explicitly
makes the enum's underlying type a four-byte signed int. Redundant code
from the overload which computes the enum values was replaced by using
std::iota and calling the three-argument createEnum.
  • Loading branch information
Tim Haines committed Nov 24, 2021
1 parent 424ee54 commit 338b78e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
43 changes: 16 additions & 27 deletions dyninstAPI/src/BPatch.C
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#endif

#include <fstream>
#include <numeric>

using namespace std;
using namespace SymtabAPI;
Expand Down Expand Up @@ -1392,20 +1393,18 @@ BPatch_type * BPatch::createEnum( const char * name,
if (elementNames.size() != elementIds.size()) {
return NULL;
}
string typeName = name;
dyn_c_vector<pair<string, int> *>elements;
for (unsigned int i=0; i < elementNames.size(); i++)
elements.push_back(new pair<string, int>(elementNames[i], elementIds[i]));

boost::shared_ptr<Type> typ(typeEnum::create( typeName, elements));
if (!typ) return NULL;

BPatch_type *newType = new BPatch_type(typ);
if (!newType) return NULL;

APITypes->addType(newType);

return(newType);
// Make the underlying type a 4-byte signed int
auto underlying_type = boost::make_shared<Type>(std::move(typeScalar(4, "int", true)));

typeEnum tenum{underlying_type, name};
for(auto i=0UL; i<elementNames.size(); i++) {
tenum.addConstant(elementNames[i], elementIds[i]);
}

BPatch_type *newType = new BPatch_type(boost::make_shared<Type>(std::move(tenum)));
APITypes->addType(newType);
return newType;
}


Expand All @@ -1421,20 +1420,10 @@ BPatch_type * BPatch::createEnum( const char * name,
BPatch_type * BPatch::createEnum( const char * name,
BPatch_Vector<char *> &elementNames)
{
string typeName = name;
dyn_c_vector<pair<string, int> *>elements;
for (unsigned int i=0; i < elementNames.size(); i++)
elements.push_back(new pair<string, int>(elementNames[i], i));

boost::shared_ptr<Type> typ(typeEnum::create( typeName, elements));
if (!typ) return NULL;

BPatch_type *newType = new BPatch_type(typ);
if (!newType) return NULL;

APITypes->addType(newType);

return(newType);
// We were only given names, so assume sequentially-ordered values
BPatch_Vector<int> ids(elementNames.size());
std::iota(ids.begin(), ids.end(), 0);
return createEnum(name, elementNames, ids);
}

/*
Expand Down
1 change: 1 addition & 0 deletions symtabAPI/h/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ class SYMTAB_EXPORT typeEnum : public derivedType {
typeEnum() = default;
typeEnum(typeId_t ID, std::string name = "");
typeEnum(std::string name);
typeEnum(boost::shared_ptr<Type> underlying_type, std::string name);
typeEnum(boost::shared_ptr<Type> underlying_type, std::string name, typeId_t ID);
typeEnum(boost::shared_ptr<Type> underlying_type, std::string name, typeId_t ID, scoped_t) :
typeEnum(underlying_type, std::move(name), ID) { is_scoped_=true; }
Expand Down
2 changes: 2 additions & 0 deletions symtabAPI/src/Type.C
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ typeEnum::typeEnum(boost::shared_ptr<Type> underlying_type, std::string name, ty
derivedType(name, ID, underlying_type->getSize(), dataEnum) {
baseType_ = underlying_type;
}
typeEnum::typeEnum(boost::shared_ptr<Type> underlying_type, std::string name) :
typeEnum(underlying_type, name, ::getUniqueTypeId()) {}

typeEnum *typeEnum::create(std::string &name, dyn_c_vector< std::pair<std::string, int> *> &constants, Symtab *obj)
{
Expand Down

0 comments on commit 338b78e

Please sign in to comment.