Initial commit
[yaffs-website] / node_modules / node-sass / src / libsass / src / memory / SharedPtr.cpp
1 #include "../sass.hpp"
2 #include <iostream>
3 #include <typeinfo>
4
5 #include "SharedPtr.hpp"
6 #include "../ast_fwd_decl.hpp"
7
8 #ifdef DEBUG_SHARED_PTR
9 #include "../debugger.hpp"
10 #endif
11
12 namespace Sass {
13
14   #ifdef DEBUG_SHARED_PTR
15   void SharedObj::dumpMemLeaks() {
16     if (!all.empty()) {
17       std::cerr << "###################################\n";
18       std::cerr << "# REPORTING MISSING DEALLOCATIONS #\n";
19       std::cerr << "###################################\n";
20       for (auto var : all) {
21         if (AST_Node_Ptr ast = Cast<AST_Node>(var)) {
22           debug_ast(ast);
23         } else {
24           std::cerr << "LEAKED " << var << "\n";
25         }
26       }
27     }
28   }
29   std::vector<SharedObj*> SharedObj::all;
30   #endif
31
32   bool SharedObj::taint = false;
33
34   SharedObj::SharedObj()
35   : detached(false)
36     #ifdef DEBUG_SHARED_PTR
37     , dbg(false)
38     #endif
39   {
40       refcounter = 0;
41       #ifdef DEBUG_SHARED_PTR
42         if (taint) all.push_back(this);
43       #endif
44     };
45
46     SharedObj::~SharedObj() {
47       #ifdef DEBUG_SHARED_PTR
48           if (dbg) std::cerr << "Destruct " << this << "\n";
49           if(!all.empty()) { // check needed for MSVC (no clue why?)
50             all.erase(std::remove(all.begin(), all.end(), this), all.end());
51           }
52       #endif
53     };
54
55
56
57   void SharedPtr::decRefCount() {
58     if (node) {
59       -- node->refcounter;
60       #ifdef DEBUG_SHARED_PTR
61         if (node->dbg)  std::cerr << "- " << node << " X " << node->refcounter << " (" << this << ") " << "\n";
62       #endif
63       if (node->refcounter == 0) {
64         #ifdef DEBUG_SHARED_PTR
65           AST_Node_Ptr ptr = Cast<AST_Node>(node);
66           if (node->dbg) std::cerr << "DELETE NODE " << node << "\n";
67         #endif
68         if (!node->detached) {
69           delete(node);
70         }
71       }
72     }
73   }
74
75   void SharedPtr::incRefCount() {
76     if (node) {
77       ++ node->refcounter;
78       node->detached = false;
79       #ifdef DEBUG_SHARED_PTR
80         if (node->dbg) {
81           std::cerr << "+ " << node << " X " << node->refcounter << " (" << this << ") " << "\n";
82         }
83       #endif
84     }
85   }
86
87   SharedPtr::~SharedPtr() {
88     decRefCount();
89   }
90
91
92   // the create constructor
93   SharedPtr::SharedPtr(SharedObj* ptr)
94   : node(ptr) {
95     incRefCount();
96   }
97   // copy assignment operator
98   SharedPtr& SharedPtr::operator=(const SharedPtr& rhs) {
99     void* cur_ptr = (void*) node;
100     void* rhs_ptr = (void*) rhs.node;
101     if (cur_ptr == rhs_ptr) {
102       return *this;
103     }
104     decRefCount();
105     node = rhs.node;
106     incRefCount();
107     return *this;
108   }
109
110   // the copy constructor
111   SharedPtr::SharedPtr(const SharedPtr& obj)
112   : node(obj.node) {
113     incRefCount();
114   }
115
116 }