Skip to content

Commit 5ae0664

Browse files
committed
Deallocate objects
1 parent 4d5fe4b commit 5ae0664

25 files changed

+170
-41
lines changed

bindgen/CycleDetection.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class CycleDetection {
3636

3737
Type *type = tpeTransl.translate(qtpe);
3838
std::string qtpeString = type->str();
39+
if (type->canBeDeallocated()) {
40+
delete type;
41+
}
3942

4043
// Add the dependence of qtpe
4144
if (contains(qtpeString)) {

bindgen/defines/DefineFinder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void DefineFinder::MacroDefined(const clang::Token &macroNameTok,
5050
std::string varName = (*tokens)[0].getIdentifierInfo()->getName();
5151
ir.addPossibleVarDefine(macroName, varName);
5252
}
53-
std::free(tokens);
53+
delete tokens;
5454
}
5555
}
5656

@@ -70,13 +70,13 @@ DefineFinder::expandDefine(const clang::MacroDirective &md) {
7070
std::vector<clang::Token> *newTokens = expandDefine(
7171
*pp.getLocalMacroDirective(token.getIdentifierInfo()));
7272
if (!newTokens) {
73-
std::free(expandedTokens);
73+
delete expandedTokens;
7474
return nullptr;
7575
}
7676
for (const auto &newToken : *newTokens) {
7777
(*expandedTokens).push_back(newToken);
7878
}
79-
std::free(newTokens);
79+
delete newTokens;
8080
} else {
8181
(*expandedTokens).push_back(token);
8282
}

bindgen/ir/Enum.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
4949
}
5050

5151
std::string Enum::getName() const { return name; }
52+
53+
bool Enum::canBeDeallocated() const { return false; }

bindgen/ir/Enum.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class Enum : public PrimitiveType {
3131

3232
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e);
3333

34+
bool canBeDeallocated() const override;
35+
3436
private:
3537
std::string name; // might be empty
3638
std::vector<Enumerator> enumerators;

bindgen/ir/Function.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Parameter::Parameter(std::string name, Type *type)
55
: TypeAndName(std::move(name), type) {}
66

7-
Function::Function(const std::string &name, std::vector<Parameter> parameters,
7+
Function::Function(const std::string &name, std::vector<Parameter *> parameters,
88
Type *retType, bool isVariadic)
99
: name(name), scalaName(name), parameters(std::move(parameters)),
1010
retType(retType), isVariadic(isVariadic) {}
@@ -16,8 +16,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
1616
s << " def " << handleReservedWords(func.scalaName) << "(";
1717
std::string sep = "";
1818
for (const auto &param : func.parameters) {
19-
s << sep << handleReservedWords(param.getName()) << ": "
20-
<< param.getType()->str();
19+
s << sep << handleReservedWords(param->getName()) << ": "
20+
<< param->getType()->str();
2121
sep = ", ";
2222
}
2323
if (func.isVariadic) {
@@ -34,7 +34,7 @@ bool Function::usesType(Type *type) const {
3434
return true;
3535
}
3636
for (const auto &parameter : parameters) {
37-
if (parameter.getType() == type) {
37+
if (parameter->getType() == type) {
3838
return true;
3939
}
4040
}
@@ -54,7 +54,7 @@ std::string Function::getVarargsParameterName() const {
5454

5555
bool Function::existsParameterWithName(const std::string &parameterName) const {
5656
for (const auto &parameter : parameters) {
57-
if (parameter.getName() == parameterName) {
57+
if (parameter->getName() == parameterName) {
5858
return true;
5959
}
6060
}
@@ -64,3 +64,19 @@ bool Function::existsParameterWithName(const std::string &parameterName) const {
6464
void Function::setScalaName(std::string scalaName) {
6565
this->scalaName = std::move(scalaName);
6666
}
67+
68+
void Function::deallocateTypesThatAreNotInIR() {
69+
if (retType->canBeDeallocated()) {
70+
delete retType;
71+
}
72+
73+
for (const auto &parameter : parameters) {
74+
parameter->deallocateTypesThatAreNotInIR();
75+
}
76+
}
77+
78+
Function::~Function() {
79+
for (const auto &parameter : parameters) {
80+
delete parameter;
81+
}
82+
}

bindgen/ir/Function.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ class Parameter : public TypeAndName {
1313

1414
class Function {
1515
public:
16-
Function(const std::string &name, std::vector<Parameter> parameters,
16+
Function(const std::string &name, std::vector<Parameter *> parameters,
1717
Type *retType, bool isVariadic);
1818

19+
~Function();
20+
1921
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
2022
const Function &func);
2123

@@ -25,14 +27,16 @@ class Function {
2527

2628
void setScalaName(std::string scalaName);
2729

30+
void deallocateTypesThatAreNotInIR();
31+
2832
private:
2933
std::string getVarargsParameterName() const;
3034

3135
bool existsParameterWithName(const std::string &parameterName) const;
3236

3337
std::string name; // real name of the function
3438
std::string scalaName; // not empty
35-
std::vector<Parameter> parameters;
39+
std::vector<Parameter *> parameters;
3640
Type *retType;
3741
bool isVariadic;
3842
};

bindgen/ir/IR.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ IR::IR(std::string libName, std::string linkName, std::string objectName,
66
: libName(std::move(libName)), linkName(std::move(linkName)),
77
objectName(std::move(objectName)), packageName(std::move(packageName)) {}
88

9-
void IR::addFunction(std::string name, std::vector<Parameter> parameters,
9+
void IR::addFunction(std::string name, std::vector<Parameter *> parameters,
1010
Type *retType, bool isVariadic) {
1111
functions.push_back(
1212
new Function(name, std::move(parameters), retType, isVariadic));
@@ -27,15 +27,15 @@ Type *IR::addEnum(std::string name, const std::string &type,
2727
return nullptr;
2828
}
2929

30-
Type *IR::addStruct(std::string name, std::vector<Field> fields,
30+
Type *IR::addStruct(std::string name, std::vector<Field *> fields,
3131
uint64_t typeSize) {
3232
Struct *s = new Struct(std::move(name), std::move(fields), typeSize);
3333
structs.push_back(s);
3434
typeDefs.push_back(s->generateTypeDef());
3535
return typeDefs.back();
3636
}
3737

38-
Type *IR::addUnion(std::string name, std::vector<Field> fields,
38+
Type *IR::addUnion(std::string name, std::vector<Field *> fields,
3939
uint64_t maxSize) {
4040
Union *u = new Union(std::move(name), std::move(fields), maxSize);
4141
unions.push_back(u);
@@ -305,6 +305,12 @@ T IR::getDeclarationWithName(std::vector<T> &declarations,
305305
}
306306

307307
IR::~IR() {
308+
deallocateTypesThatAreNotInIR(functions);
309+
deallocateTypesThatAreNotInIR(typeDefs);
310+
deallocateTypesThatAreNotInIR(structs);
311+
deallocateTypesThatAreNotInIR(unions);
312+
deallocateTypesThatAreNotInIR(variables);
313+
308314
clearVector(functions);
309315
clearVector(typeDefs);
310316
clearVector(structs);
@@ -317,7 +323,13 @@ IR::~IR() {
317323
}
318324

319325
template <typename T> void IR::clearVector(std::vector<T> v) {
320-
for (auto e : v) {
321-
std::free(e);
326+
for (const auto &e : v) {
327+
delete e;
328+
}
329+
}
330+
331+
template <typename T> void IR::deallocateTypesThatAreNotInIR(std::vector<T> v) {
332+
for (const auto &e : v) {
333+
e->deallocateTypesThatAreNotInIR();
322334
}
323335
}

bindgen/ir/IR.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class IR {
1919

2020
~IR();
2121

22-
void addFunction(std::string name, std::vector<Parameter> parameters,
22+
void addFunction(std::string name, std::vector<Parameter *> parameters,
2323
Type *retType, bool isVariadic);
2424

2525
void addTypeDef(std::string name, Type *type);
@@ -33,13 +33,13 @@ class IR {
3333
/**
3434
* @return type alias for the struct
3535
*/
36-
Type *addStruct(std::string name, std::vector<Field> fields,
36+
Type *addStruct(std::string name, std::vector<Field *> fields,
3737
uint64_t typeSize);
3838

3939
/**
4040
* @return type alias for the union
4141
*/
42-
Type *addUnion(std::string name, std::vector<Field> fields,
42+
Type *addUnion(std::string name, std::vector<Field *> fields,
4343
uint64_t maxSize);
4444

4545
void addLiteralDefine(std::string name, std::string literal, Type *type);
@@ -136,6 +136,8 @@ class IR {
136136

137137
template <typename T> void clearVector(std::vector<T> v);
138138

139+
template <typename T> void deallocateTypesThatAreNotInIR(std::vector<T> v);
140+
139141
std::string libName; // name of the library
140142
std::string linkName; // name of the library to link with
141143
std::string objectName; // name of Scala object

bindgen/ir/LiteralDefine.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
99
<< " = " << literalDefine.literal << "\n";
1010
return s;
1111
}
12+
13+
void LiteralDefine::deallocateTypesThatAreNotInIR() {
14+
if (type->canBeDeallocated()) {
15+
delete type;
16+
}
17+
}

bindgen/ir/LiteralDefine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class LiteralDefine : public Define {
1212
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
1313
const LiteralDefine &literalDefine);
1414

15+
void deallocateTypesThatAreNotInIR();
16+
1517
private:
1618
std::string literal;
1719
Type *type;

0 commit comments

Comments
 (0)