Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Jonathan-R0/TP2-9508
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7
Choose a base ref
...
head repository: Jonathan-R0/TP2-9508
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 12 commits
  • 16 files changed
  • 2 contributors

Commits on Nov 15, 2020

  1. Fix al parser.

    Jonathan-R0 committed Nov 15, 2020
    Copy the full SHA
    4d056ea View commit details
  2. Fixes del linter.

    Jonathan-R0 committed Nov 15, 2020
    Copy the full SHA
    dda718e View commit details
  3. Mas quejas del linter...

    Jonathan-R0 committed Nov 15, 2020
    Copy the full SHA
    37999ad View commit details
  4. Copy the full SHA
    340e8a7 View commit details
  5. Pequeños tweaks.

    Jonathan-R0 committed Nov 15, 2020
    Copy the full SHA
    3ca0de2 View commit details
  6. Copy the full SHA
    48d823d View commit details
  7. Mas quejas del linter.

    Jonathan-R0 committed Nov 15, 2020
    Copy the full SHA
    5f314c7 View commit details

Commits on Nov 17, 2020

  1. Optimizaciones.

    Jonathan-R0 committed Nov 17, 2020
    Copy the full SHA
    5625bed View commit details
  2. Mas quejas del linter.

    Jonathan-R0 committed Nov 17, 2020
    Copy the full SHA
    c71c3b2 View commit details
  3. Tweaks.

    Jonathan-R0 committed Nov 17, 2020
    Copy the full SHA
    8deb012 View commit details
  4. Copy the full SHA
    a73d1e9 View commit details
  5. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    4fa6391 View commit details
Showing with 101 additions and 97 deletions.
  1. +12 −0 README.md
  2. +12 −17 asmline.cpp
  3. +2 −3 asmline.h
  4. +3 −2 eBPF.cpp
  5. +2 −2 eBPF.h
  6. +1 −1 executer.cpp
  7. +3 −2 executer.h
  8. +3 −5 fileFountain.cpp
  9. +0 −2 fileFountain.h
  10. +20 −32 graph.cpp
  11. +3 −0 graph.h
  12. +7 −10 graphFiller.cpp
  13. +2 −1 graphFiller.h
  14. +28 −18 parser.cpp
  15. +1 −1 parser.h
  16. +2 −1 results.cpp
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -48,3 +48,15 @@ Las herramientas más utilizadas en este tp fueron:
- ***Valgrind***: el glorioso programa que tanto nos ayuda a debuggear el código. Con flags como ```--track-origins=yes``` para ver donde se nos generan variables no inicializadas que puedan causar problemas, entre otros.

- ***Gdb***: el debugger de GNU, súper útil para revisar con detalle el código y encontrar más facilmente la causa de problemas como segmentation faults, loops infinitos, entre otros.

## Cambios Importantes de la Reentrega

- El primer gran cambio es la creación de la clase `Executer`. La misma le permite al `main()` delegarle las tareas que antes tenía, para que el mismo no contenga ningún tipo de lógica innecesaria. Este contiene las referencias a los hilos creados y se encarga de crearlos, iniciarlos y luego destruirlos.

- La clase `Asmline`, que antes ingresaba al parser para ser completada con la información que este procesaba, es ahora creada por la misma entidad y devuelta al finalizar de procesar la línea ingresada. La devolución se hace por movimiento. Esto permite eliminar algunos de los setters que utiliza, para poder inicializar el objeto con ingresando datos con el ***Member initializer list***.

- Dentro de la clase `EBPF` se añade una pequeña optimización, donde se pasa el string "filename" hacia el repositorio de respuestas por movimiento. Otro cambio del estilo ocurre en el método `init` de la misma clase, donde ahora se envia la información leída por movimiento, evitando así copias innecesarias.

- En el .cpp de la clase `Graph` se cambian la mayoría de las iteraciones rústicas por usos de funciones de la ***Standard Template Library***.

- Finalmente en la clase `Parser` se realizó una refactorización del método de parseo de líneas. Se evitan utilizan strings y listas de más, que lo único que hacen es comer más memoria, además de aprovechar mejor los métodos de la ***Standard Template Library***.
29 changes: 12 additions & 17 deletions asmline.cpp
Original file line number Diff line number Diff line change
@@ -2,39 +2,34 @@

#include <algorithm>
#include <iostream>
#include <utility>

Asmline::Asmline()
: jumpCodes({"jmp", "ja", "jeq", "jneq", "jne", "jlt", "jle", "jgt", "jge",
Asmline::Asmline(const std::string& label_, const std::string& opCode_)
: opCode(opCode_),
label(label_),
jumpCodes({"jmp", "ja", "jeq", "jneq", "jne", "jlt", "jle", "jgt", "jge",
"jset"}) {}

bool Asmline::isJump() {
return (find(jumpCodes.begin(), jumpCodes.end(), opCode) != jumpCodes.end());
}

bool Asmline::esCortante() {
if (opCode == "ret" || opCode == "jmp" ||
(this->isJump() && labelsToJump.size() == 2))
return true;
return false;
return (opCode == "ret" || opCode == "jmp" ||
(this->isJump() && labelsToJump.size() == 2));
}

std::string Asmline::getOpcode() { return this->opCode; }
std::string Asmline::getLabel() { return this->label; }
std::list<std::string> Asmline::getLabelsToJumpTo() {
return this->labelsToJump;
}
void Asmline::setLabel(std::string labelGiven) { label = labelGiven; }
void Asmline::setOpCode(std::string opCodeGiven) { opCode = opCodeGiven; }
void Asmline::setLabelToJump(std::string labelToJumpGiven) {
labelsToJump.push_back(labelToJumpGiven);
}

void Asmline::setLabelsToJump(std::list<std::string> labelsToJumpGiven) {
if (!this->isJump()) return;
if (labelsToJumpGiven.size() == 1) {
labelsToJump.push_back(labelsToJumpGiven.back());
} else {
labelsToJumpGiven.pop_front();
labelsToJump = std::move(labelsToJumpGiven);
}
Asmline::Asmline(Asmline&& other) {
this->opCode = std::move(other.opCode);
this->label = std::move(other.label);
this->labelsToJump = std::move(other.labelsToJump);
this->jumpCodes = std::move(other.jumpCodes);
}
5 changes: 2 additions & 3 deletions asmline.h
Original file line number Diff line number Diff line change
@@ -12,16 +12,15 @@ class Asmline {
std::list<std::string> jumpCodes;

public:
Asmline();
void setLabel(std::string labelGiven);
void setOpCode(std::string opCodeGiven);
Asmline(const std::string& label_, const std::string& opCode_);
void setLabelToJump(std::string labelToJumpGiven);
std::string getOpcode();
std::string getLabel();
std::list<std::string> getLabelsToJumpTo();
void setLabelsToJump(std::list<std::string> labelsToJumpGiven);
bool isJump();
bool esCortante();
Asmline(Asmline&& other);
};

#endif // ASMLINE_H_
5 changes: 3 additions & 2 deletions eBPF.cpp
Original file line number Diff line number Diff line change
@@ -4,20 +4,21 @@

#include <fstream>
#include <iostream>
#include <utility>

#include "fileFountain.h"
#include "graph.h"
#include "graphFiller.h"
#include "results.h"

void EBPF::init(std::string& filename) {
void EBPF::init(const std::string& filename) {
int i = 1;
std::ifstream reader;
reader.open(filename, std::ifstream::in);
while (reader.good()) {
std::string myText;
std::getline(reader, myText, '\n');
if (myText.size() == 0) continue;
if (myText.empty()) continue;
filler.addInstructionToGraph(std::move(myText), i);
i++;
}
4 changes: 2 additions & 2 deletions eBPF.h
Original file line number Diff line number Diff line change
@@ -18,11 +18,11 @@ class EBPF : public Thread {
Results& results;
FileFountain& fileFountain;
Graphfiller filler;
void init(std::string& filename);
void init(const std::string& filename);

public:
EBPF(Results& r, FileFountain& f);
void run();
void run() override;
void restart();
~EBPF() {}
bool hasCycle();
2 changes: 1 addition & 1 deletion executer.cpp
Original file line number Diff line number Diff line change
@@ -26,4 +26,4 @@ void Executer::run(int argc, char* argv[]) {
delete it;
}
results.printResults();
}
}
5 changes: 3 additions & 2 deletions executer.h
Original file line number Diff line number Diff line change
@@ -11,8 +11,9 @@ class Executer {
std::vector<EBPF*> holders;

public:
Executer(char* argv[]) : numberOfThreads(strtol(argv[1], NULL, 10)) {}
explicit Executer(char* argv[])
: numberOfThreads(strtol(argv[1], NULL, 10)) {}
void run(int argc, char* argv[]);
};

#endif // EXECUTER_H_
#endif // EXECUTER_H_
8 changes: 3 additions & 5 deletions fileFountain.cpp
Original file line number Diff line number Diff line change
@@ -3,20 +3,18 @@
#include <algorithm>
#include <iostream>
#include <mutex>
#include <utility>

FileFountain::FileFountain(int argc, char* argv[]) {
for (int i = argc - 1; i > 1; i--) {
std::string file(argv[i]);
files.push_back(file);
files.push_back(std::string(argv[i]));
}
}

int FileFountain::getNumberOfFiles() { return files.size(); }

std::string FileFountain::getNext() {
std::unique_lock<std::mutex> lock(m);
if (files.size() == 0) return "";
std::string file = std::move(files.back());
std::string file(std::move(files.back()));
files.pop_back();
return file;
}
2 changes: 0 additions & 2 deletions fileFountain.h
Original file line number Diff line number Diff line change
@@ -8,13 +8,11 @@
class FileFountain {
private:
std::vector<std::string> files;
size_t toRead;
std::mutex m;

public:
FileFountain(int argc, char *argv[]);
std::string getNext();
int getNumberOfFiles();
};

#endif // FILEFOUNTAIN_H_
52 changes: 20 additions & 32 deletions graph.cpp
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
#include <list>
#include <map>
#include <string>
#include <utility>

void Graph::addVertex(int node) { nodes[node] = {}; }

@@ -16,12 +17,7 @@ void Graph::addEdge(int from, int to) {

int Graph::size() { return nodes.size(); }

bool Graph::isIn(int node) {
for (auto& i : nodes) {
if (node == i.first) return true;
}
return false;
}
bool Graph::isIn(int node) { return (nodes.find(node)->first != node); }

void Graph::addIfItsNotIn(int node) {
if (!this->isIn(node)) {
@@ -33,41 +29,35 @@ void Graph::clear() { nodes.clear(); }

void Graph::connectLast(int to) { this->addEdge(nodes.size() - 1, to); }

void Graph::disconnectNext(int nodo) {
std::list<int> searching = std::move(nodes[nodo]);
for (int& it : searching) {
if (nodo + 1 == it) {
nodes[nodo].remove(it);
break;
}
}
}
void Graph::disconnectNext(int nodo) { nodes[nodo].remove(nodo + 1); }

void Graph::connect(int from, int to) { nodes[from].push_front(to); }

bool Graph::hasBeenFound(int node, std::list<int>& found) {
return (find(found.begin(), found.end(), node) != found.end());
}

bool Graph::_isCyclic(int start, std::list<int>& found) {
if (hasBeenFound(start, found)) return true;
found.push_front(start);
for (int& i : nodes.at(start)) {
if (Graph::_isCyclic(i, found)) return true;
}
auto begin = nodes.at(start).begin();
auto end = nodes.at(start).end();
if (std::any_of(begin, end,
[&found, this](int i) { return this->_isCyclic(i, found); }))
return true;
found.remove(start);
return false;
}

bool Graph::isCyclic() {
std::list<int> found;
for (auto& it : nodes) {
if (this->_isCyclic(it.first, found)) {
found.clear();
return true;
}
}
return false;
auto begin = nodes.begin();
auto end = nodes.end();
// Lo dejo así por el linter, pero la versión
// anterior hacía menos accesos a memoria :S
return std::any_of(begin, end, [&found, this](iterator_t it) {
return this->_isCyclic(it.first, found);
});
}

bool Graph::hasBeenFound(int node, std::list<int>& found) {
return (find(found.begin(), found.end(), node) != found.end());
}

void Graph::dfs(int start, std::list<int>& found) {
@@ -84,7 +74,5 @@ bool Graph::hasUnusedInstructions() {
std::list<int> found;
if (nodes.size() == 0) return false;
dfs(1, found);
long unsigned int amountFound = found.size();
found.clear();
return (amountFound != nodes.size());
return (found.size() != nodes.size());
}
3 changes: 3 additions & 0 deletions graph.h
Original file line number Diff line number Diff line change
@@ -4,6 +4,9 @@
#include <list>
#include <map>
#include <string>
#include <utility>

typedef std::pair<int, std::list<int>> iterator_t;

class Graph {
private:
17 changes: 7 additions & 10 deletions graphFiller.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "graphFiller.h"

#include <list>
#include <string>
#include <utility>

#include "asmline.h"
#include "parser.h"

@@ -10,19 +14,12 @@ void Graphfiller::restart() {
opGraph.clear();
}

Graphfiller::Graphfiller() {
referenciasColgadas = {};
referenciasReconocidas = {};
aristaACortar = {};
}

void Graphfiller::addInstructionToGraph(std::string line, int lineNumber) {
if (line.size() == 0) return;
Asmline instruction;
parser.parseInstruction(std::move(line), instruction);
Asmline instruction = std::move(parser.parseInstruction(std::move(line)));
opGraph.addVertex(lineNumber);
if (lineNumber != 1) opGraph.connectLast(lineNumber);
std::list<std::string> labelsToJump = instruction.getLabelsToJumpTo();
std::list<std::string> labelsToJump(instruction.getLabelsToJumpTo());
if (instruction.esCortante()) aristaACortar.push_front(lineNumber);
if (instruction.getLabel().size() != 0)
referenciasReconocidas[std::move(instruction.getLabel())] = lineNumber;
@@ -47,4 +44,4 @@ void Graphfiller::connectLostTags() {
std::move(referenciasReconocidas[it.first]));
}
}
}
}
3 changes: 2 additions & 1 deletion graphFiller.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef GRAPHFILLER_H_
#define GRAPHFILLER_H_

#include <list>
#include <map>
#include <string>

#include "graph.h"
#include "parser.h"
@@ -15,7 +17,6 @@ class Graphfiller {
Graph opGraph;

public:
Graphfiller();
void restart();
void connectLostTags();
void addInstructionToGraph(std::string line, int lineNumber);
Loading