Home | History | Annotate | Download | only in platform
      1 /*
      2  * Copyright (C) 2006, 2007, 2009, 2010, 2012 Apple Inc. All rights reserved.
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #ifndef TreeShared_h
     22 #define TreeShared_h
     23 
     24 #include "wtf/Assertions.h"
     25 #include "wtf/MainThread.h"
     26 #include "wtf/Noncopyable.h"
     27 
     28 namespace WebCore {
     29 
     30 #ifndef NDEBUG
     31 template<typename NodeType> class TreeShared;
     32 template<typename NodeType> void adopted(TreeShared<NodeType>*);
     33 #endif
     34 
     35 template<typename NodeType> class TreeShared {
     36     WTF_MAKE_NONCOPYABLE(TreeShared);
     37 protected:
     38     TreeShared()
     39         : m_refCount(1)
     40 #ifndef NDEBUG
     41         , m_deletionHasBegun(false)
     42         , m_inRemovedLastRefFunction(false)
     43         , m_adoptionIsRequired(true)
     44 #endif
     45     {
     46         ASSERT(isMainThread());
     47     }
     48 
     49     ~TreeShared()
     50     {
     51         ASSERT(isMainThread());
     52         ASSERT(!m_refCount);
     53         ASSERT(m_deletionHasBegun);
     54         ASSERT(!m_adoptionIsRequired);
     55     }
     56 
     57 public:
     58     void ref()
     59     {
     60         ASSERT(isMainThread());
     61         ASSERT(!m_deletionHasBegun);
     62         ASSERT(!m_inRemovedLastRefFunction);
     63         ASSERT(!m_adoptionIsRequired);
     64         ++m_refCount;
     65     }
     66 
     67     void deref()
     68     {
     69         ASSERT(isMainThread());
     70         ASSERT(m_refCount >= 0);
     71         ASSERT(!m_deletionHasBegun);
     72         ASSERT(!m_inRemovedLastRefFunction);
     73         ASSERT(!m_adoptionIsRequired);
     74         NodeType* thisNode = static_cast<NodeType*>(this);
     75         if (--m_refCount <= 0 && !thisNode->hasTreeSharedParent()) {
     76 #ifndef NDEBUG
     77             m_inRemovedLastRefFunction = true;
     78 #endif
     79             thisNode->removedLastRef();
     80         }
     81     }
     82 
     83     bool hasOneRef() const
     84     {
     85         ASSERT(!m_deletionHasBegun);
     86         ASSERT(!m_inRemovedLastRefFunction);
     87         return m_refCount == 1;
     88     }
     89 
     90     int refCount() const
     91     {
     92         return m_refCount;
     93     }
     94 
     95 private:
     96     int m_refCount;
     97 
     98 #ifndef NDEBUG
     99 public:
    100     bool m_deletionHasBegun;
    101     bool m_inRemovedLastRefFunction;
    102 private:
    103     friend void adopted<>(TreeShared<NodeType>*);
    104     bool m_adoptionIsRequired;
    105 #endif
    106 };
    107 
    108 #ifndef NDEBUG
    109 
    110 template<typename NodeType> inline void adopted(TreeShared<NodeType>* object)
    111 {
    112     if (!object)
    113         return;
    114     ASSERT(!object->m_deletionHasBegun);
    115     ASSERT(!object->m_inRemovedLastRefFunction);
    116     object->m_adoptionIsRequired = false;
    117 }
    118 
    119 #endif
    120 
    121 }
    122 
    123 #endif // TreeShared.h
    124