File: SharedPtr.cpp

package info (click to toggle)
golang-github-wellington-go-libsass 0.9.2%2Bgit20181130.4ef5b9d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,128 kB
  • sloc: cpp: 28,607; ansic: 839; makefile: 44
file content (114 lines) | stat: -rw-r--r-- 2,650 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "../sass.hpp"
#include <iostream>
#include <typeinfo>

#include "SharedPtr.hpp"
#include "../ast_fwd_decl.hpp"

#ifdef DEBUG_SHARED_PTR
#include "../debugger.hpp"
#endif

namespace Sass {

  #ifdef DEBUG_SHARED_PTR
  void SharedObj::dumpMemLeaks() {
    if (!all.empty()) {
      std::cerr << "###################################\n";
      std::cerr << "# REPORTING MISSING DEALLOCATIONS #\n";
      std::cerr << "###################################\n";
      for (SharedObj* var : all) {
        if (AST_Node_Ptr ast = dynamic_cast<AST_Node*>(var)) {
          debug_ast(ast);
        } else {
          std::cerr << "LEAKED " << var << "\n";
        }
      }
    }
  }
  std::vector<SharedObj*> SharedObj::all;
  #endif

  bool SharedObj::taint = false;

  SharedObj::SharedObj()
  : detached(false)
    #ifdef DEBUG_SHARED_PTR
    , dbg(false)
    #endif
  {
    refcounter = 0;
    #ifdef DEBUG_SHARED_PTR
      if (taint) all.push_back(this);
    #endif
  };

  SharedObj::~SharedObj() {
    #ifdef DEBUG_SHARED_PTR
      if (dbg) std::cerr << "Destruct " << this << "\n";
      if(!all.empty()) { // check needed for MSVC (no clue why?)
        all.erase(std::remove(all.begin(), all.end(), this), all.end());
      }
    #endif
  };

  void SharedPtr::decRefCount() {
    if (node) {
      -- node->refcounter;
      #ifdef DEBUG_SHARED_PTR
        if (node->dbg)  std::cerr << "- " << node << " X " << node->refcounter << " (" << this << ") " << "\n";
      #endif
      if (node->refcounter == 0) {
        #ifdef DEBUG_SHARED_PTR
          // AST_Node_Ptr ast = dynamic_cast<AST_Node*>(node);
          if (node->dbg) std::cerr << "DELETE NODE " << node << "\n";
        #endif
        if (!node->detached) {
          delete(node);
        }
      }
    }
  }

  void SharedPtr::incRefCount() {
    if (node) {
      ++ node->refcounter;
      node->detached = false;
      #ifdef DEBUG_SHARED_PTR
        if (node->dbg) {
          std::cerr << "+ " << node << " X " << node->refcounter << " (" << this << ") " << "\n";
        }
      #endif
    }
  }

  SharedPtr::~SharedPtr() {
    decRefCount();
  }


  // the create constructor
  SharedPtr::SharedPtr(SharedObj* ptr)
  : node(ptr) {
    incRefCount();
  }
  // copy assignment operator
  SharedPtr& SharedPtr::operator=(const SharedPtr& rhs) {
    void* cur_ptr = (void*) node;
    void* rhs_ptr = (void*) rhs.node;
    if (cur_ptr == rhs_ptr) {
      return *this;
    }
    decRefCount();
    node = rhs.node;
    incRefCount();
    return *this;
  }

  // the copy constructor
  SharedPtr::SharedPtr(const SharedPtr& obj)
  : node(obj.node) {
    incRefCount();
  }

}