Skip to content

Commit

Permalink
Separated chunk_treap/gc_graph visualizers into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
m4drat committed Nov 21, 2023
1 parent 9cec0be commit 3950275
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 424 deletions.
2 changes: 2 additions & 0 deletions libmemplusplus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ list(APPEND MPP_SOURCES
src/heuristics/heuristics.cpp
src/containers/chunk_treap.cpp
src/containers/gc_graph.cpp
src/containers/visualizers/gc_graph_visualizer.cpp
src/containers/visualizers/chunk_treap_visualizer.cpp
src/containers/node.cpp
src/containers/vertex.cpp
src/shared_gcptr.cpp
Expand Down
23 changes: 10 additions & 13 deletions libmemplusplus/include/mpplib/shared_gcptr-imp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ namespace mpp {
CheckInvalidInitialization(t_newData);
#endif

// Create temp object
// Create temp object.
SharedGcPtr tmp(t_newData);

// Swap with temp object
// Swap with temp object.
tmp.Swap(*this);
return *this;
}
Expand All @@ -208,12 +208,11 @@ namespace mpp {
SharedGcPtr<Type>& SharedGcPtr<Type>::operator=(std::nullptr_t t_newData)
{
PROFILE_FUNCTION();
// Just delete reference
this->DeleteReference();
return *this;
}

// comparisons operators
// comparisons operators.
template<class Type>
bool SharedGcPtr<Type>::operator==(const SharedGcPtr& t_other) const noexcept
{
Expand Down Expand Up @@ -303,7 +302,7 @@ namespace mpp {

auto& gcPtrs = g_memoryManager->GetGC().GetGcPtrs();

// Delete shared ptr from list of all active gc ptrs
// Delete shared ptr from list of all active gc ptrs.
auto toErase = std::find(gcPtrs.begin(), gcPtrs.end(), this);
if (toErase != gcPtrs.end()) {
gcPtrs.erase(toErase);
Expand Down Expand Up @@ -362,14 +361,14 @@ namespace mpp {
template<class Type>
void SharedGcPtr<Type>::DecrementRefCounter()
{
// If m_references isn't nullptr
// If m_references isn't nullptr.
if (m_references) {
// De
// Decrement references count.
--(*m_references);

// Destroy shared ptr and the object
// Destroy shared ptr and the object.
if (*m_references <= 0) {
// Delete references variable
// Delete references variable.
delete m_references;
m_references = nullptr;

Expand All @@ -386,15 +385,13 @@ namespace mpp {
void SharedGcPtr<Type>::Swap(SharedGcPtr& t_other)
{
PROFILE_FUNCTION();
// Current object ptr is nullptr
// t_other's object ptr isn't nullptr
// Current object ptr is nullptr, t_other's object ptr isn't nullptr.
if (this->m_objectPtr == nullptr && t_other.m_objectPtr != nullptr) {
t_other.DeleteFromGcList();
AddToGcList();
}

// Current object ptr isn't nullptr
// t_other's object ptr is nullptr
// Current object ptr isn't nullptr, t_other's object ptr is nullptr.
if (this->m_objectPtr != nullptr && t_other.m_objectPtr == nullptr) {
DeleteFromGcList();
t_other.AddToGcList();
Expand Down
2 changes: 1 addition & 1 deletion libmemplusplus/include/mpplib/shared_gcptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace mpp {
* @tparam Type of user object.
*/
template<class Type>
class SharedGcPtr
class SharedGcPtr final
: public SharedGcPtrBase<Type>
, public SharedGcPtrArray<std::is_array<Type>::value>
, public GcPtr
Expand Down
93 changes: 0 additions & 93 deletions libmemplusplus/src/containers/chunk_treap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,99 +100,6 @@ namespace mpp {
return m_freedMemory;
}

std::ostream& ChunkTreap::GenerateGraphvizLayout(std::ostream& t_out,
const std::string& t_treapName,
Node* t_root) const
{
PROFILE_FUNCTION();

std::function<std::ostream&(Node*, std::ostream&)> DumpNode =
[](Node* t_node, std::ostream& t_out) -> std::ostream& {
t_out << " [label=\"chunk = " << reinterpret_cast<void*>(t_node->chunk) << "\n"
<< "chunk.size = " << t_node->chunk->GetSize() << std::endl;
t_out << "priority = " << t_node->priority << "\"];";
return t_out;
};

std::function<void(Node*, uint32_t, std::ostream&)> GenerateNull =
[&DumpNode](Node* t_node, uint32_t t_nullCount, std::ostream& t_out) {
t_out << " null" << t_nullCount << " [shape=point];" << std::endl;
t_out << " " << (reinterpret_cast<intptr_t>(t_node->chunk) ^ t_node->priority);
DumpNode(t_node, t_out) << std::endl;
t_out << " " << (reinterpret_cast<intptr_t>(t_node->chunk) ^ t_node->priority)
<< " -> null" << t_nullCount << ";\n"
<< std::endl;
};

std::function<void(Node*, std::ostream&)> GenerateRecursive =
[&GenerateNull, &GenerateRecursive, &DumpNode](Node* t_node, std::ostream& t_out) {
static uint32_t nullCount = 0;

if (t_node->leftChild != nullptr) {
t_out << " "
<< (reinterpret_cast<intptr_t>(t_node->chunk) ^ t_node->priority);
DumpNode(t_node, t_out) << std::endl;

t_out << " "
<< (reinterpret_cast<intptr_t>(t_node->leftChild->chunk) ^
t_node->leftChild->priority);
DumpNode(t_node->leftChild, t_out) << std::endl;

t_out << " "
<< (reinterpret_cast<intptr_t>(t_node->chunk) ^ t_node->priority)
<< " -> "
<< (reinterpret_cast<intptr_t>(t_node->leftChild->chunk) ^
t_node->leftChild->priority)
<< ";\n"
<< std::endl;
GenerateRecursive(t_node->leftChild, t_out);
} else {
GenerateNull(t_node, nullCount++, t_out);
}

if (t_node->rightChild != nullptr) {
t_out << " "
<< (reinterpret_cast<intptr_t>(t_node->chunk) ^ t_node->priority);
DumpNode(t_node, t_out) << std::endl;

t_out << " "
<< (reinterpret_cast<intptr_t>(t_node->rightChild->chunk) ^
t_node->rightChild->priority);
DumpNode(t_node->rightChild, t_out) << std::endl;

t_out << " "
<< (reinterpret_cast<intptr_t>(t_node->chunk) ^ t_node->priority)
<< " -> "
<< (reinterpret_cast<intptr_t>(t_node->rightChild->chunk) ^
t_node->rightChild->priority)
<< ";\n"
<< std::endl;
GenerateRecursive(t_node->rightChild, t_out);
} else {
GenerateNull(t_node, nullCount++, t_out);
}
};

t_out << "digraph " << t_treapName << " {\n";
t_out << " node [shape=rectangle]\n";

Node* startRoot = (t_root == nullptr) ? m_root : t_root;

if (startRoot == nullptr) {
t_out << std::endl;
} else if (startRoot->leftChild == nullptr && startRoot->rightChild == nullptr) {
t_out << " " << (reinterpret_cast<intptr_t>(startRoot->chunk) ^ startRoot->priority)
<< " [label=\"chunk: " << reinterpret_cast<void*>(startRoot->chunk)
<< "\npriority: " << startRoot->priority << "\"];" << std::endl;
} else {
GenerateRecursive(startRoot, t_out);
}

t_out << "}" << std::endl;

return t_out;
}

Chunk* ChunkTreap::MinSizeChunk() const
{
Node* currentNode = m_root;
Expand Down
Loading

0 comments on commit 3950275

Please sign in to comment.