Blame src/memory/SharedPtr.cpp

Packit Service 7770af
#include "../sass.hpp"
Packit Service 7770af
#include <iostream>
Packit Service 7770af
#include <typeinfo>
Packit Service 7770af
Packit Service 7770af
#include "SharedPtr.hpp"
Packit Service 7770af
#include "../ast_fwd_decl.hpp"
Packit Service 7770af
Packit Service 7770af
#ifdef DEBUG_SHARED_PTR
Packit Service 7770af
#include "../debugger.hpp"
Packit Service 7770af
#endif
Packit Service 7770af
Packit Service 7770af
namespace Sass {
Packit Service 7770af
Packit Service 7770af
  #ifdef DEBUG_SHARED_PTR
Packit Service 7770af
  void SharedObj::dumpMemLeaks() {
Packit Service 7770af
    if (!all.empty()) {
Packit Service 7770af
      std::cerr << "###################################\n";
Packit Service 7770af
      std::cerr << "# REPORTING MISSING DEALLOCATIONS #\n";
Packit Service 7770af
      std::cerr << "###################################\n";
Packit Service 7770af
      for (SharedObj* var : all) {
Packit Service 7770af
        if (AST_Node_Ptr ast = dynamic_cast<AST_Node*>(var)) {
Packit Service 7770af
          debug_ast(ast);
Packit Service 7770af
        } else {
Packit Service 7770af
          std::cerr << "LEAKED " << var << "\n";
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
  std::vector<SharedObj*> SharedObj::all;
Packit Service 7770af
  #endif
Packit Service 7770af
Packit Service 7770af
  bool SharedObj::taint = false;
Packit Service 7770af
Packit Service 7770af
  SharedObj::SharedObj()
Packit Service 7770af
  : detached(false)
Packit Service 7770af
    #ifdef DEBUG_SHARED_PTR
Packit Service 7770af
    , dbg(false)
Packit Service 7770af
    #endif
Packit Service 7770af
  {
Packit Service 7770af
    refcounter = 0;
Packit Service 7770af
    #ifdef DEBUG_SHARED_PTR
Packit Service 7770af
      if (taint) all.push_back(this);
Packit Service 7770af
    #endif
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  SharedObj::~SharedObj() {
Packit Service 7770af
    #ifdef DEBUG_SHARED_PTR
Packit Service 7770af
      if (dbg) std::cerr << "Destruct " << this << "\n";
Packit Service 7770af
      if(!all.empty()) { // check needed for MSVC (no clue why?)
Packit Service 7770af
        all.erase(std::remove(all.begin(), all.end(), this), all.end());
Packit Service 7770af
      }
Packit Service 7770af
    #endif
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  void SharedPtr::decRefCount() {
Packit Service 7770af
    if (node) {
Packit Service 7770af
      -- node->refcounter;
Packit Service 7770af
      #ifdef DEBUG_SHARED_PTR
Packit Service 7770af
        if (node->dbg)  std::cerr << "- " << node << " X " << node->refcounter << " (" << this << ") " << "\n";
Packit Service 7770af
      #endif
Packit Service 7770af
      if (node->refcounter == 0) {
Packit Service 7770af
        #ifdef DEBUG_SHARED_PTR
Packit Service 7770af
          // AST_Node_Ptr ast = dynamic_cast<AST_Node*>(node);
Packit Service 7770af
          if (node->dbg) std::cerr << "DELETE NODE " << node << "\n";
Packit Service 7770af
        #endif
Packit Service 7770af
        if (!node->detached) {
Packit Service 7770af
          delete(node);
Packit Service 7770af
        }
Packit Service 7770af
      }
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  void SharedPtr::incRefCount() {
Packit Service 7770af
    if (node) {
Packit Service 7770af
      ++ node->refcounter;
Packit Service 7770af
      node->detached = false;
Packit Service 7770af
      #ifdef DEBUG_SHARED_PTR
Packit Service 7770af
        if (node->dbg) {
Packit Service 7770af
          std::cerr << "+ " << node << " X " << node->refcounter << " (" << this << ") " << "\n";
Packit Service 7770af
        }
Packit Service 7770af
      #endif
Packit Service 7770af
    }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  SharedPtr::~SharedPtr() {
Packit Service 7770af
    decRefCount();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
Packit Service 7770af
  // the create constructor
Packit Service 7770af
  SharedPtr::SharedPtr(SharedObj* ptr)
Packit Service 7770af
  : node(ptr) {
Packit Service 7770af
    incRefCount();
Packit Service 7770af
  }
Packit Service 7770af
  // copy assignment operator
Packit Service 7770af
  SharedPtr& SharedPtr::operator=(const SharedPtr& rhs) {
Packit Service 7770af
    void* cur_ptr = (void*) node;
Packit Service 7770af
    void* rhs_ptr = (void*) rhs.node;
Packit Service 7770af
    if (cur_ptr == rhs_ptr) {
Packit Service 7770af
      return *this;
Packit Service 7770af
    }
Packit Service 7770af
    decRefCount();
Packit Service 7770af
    node = rhs.node;
Packit Service 7770af
    incRefCount();
Packit Service 7770af
    return *this;
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // the copy constructor
Packit Service 7770af
  SharedPtr::SharedPtr(const SharedPtr& obj)
Packit Service 7770af
  : node(obj.node) {
Packit Service 7770af
    incRefCount();
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
}