Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. Reference counting
  2. ------------------
  3.  
  4. Overview
  5. --------
  6.  
  7.   DOM Nodes are reference counted, so as to ensure they are only destroyed
  8.   when nothing is using them. Each node has a reference count member
  9.   variable, which is a count of external references upon the node. Links
  10.   between nodes in the DOM tree (internal references) are not counted, as
  11.   they are implicitly available by consulting the relevant pointers.
  12.  
  13. Destruction semantics
  14. ---------------------
  15.  
  16.   A simplistic DOM tree might look like the following:
  17.  
  18.                                 Node1
  19.                                  | ^
  20.                                  | |
  21.                    +-------------|-+---------------+
  22.                  +-|-------------+-|-------------+ |
  23.                  | |             | |             | |
  24.                  v |             v |             v |
  25.                 Node2<--------->Node3<--------->Node4
  26.                  | ^
  27.                  | |
  28.            +-----|-+-------+
  29.          +-|-----+-------+ |
  30.          | |             | |
  31.          v |             v |
  32.         Node5<--------->Node6
  33.  
  34.   Thus, each node possesses the following links:
  35.  
  36.     a) A link to its parent
  37.     b) A link to each of its children
  38.     c) A link to the sibling immediately prior to it
  39.     d) A link to the sibling immediately after it
  40.  
  41.   None of these links are reference counted, as the reference can be
  42.   determined implicitly from the pointer value (i.e. a non-NULL pointer
  43.   implies a reference).
  44.  
  45.   A node becomes eligible for destruction when:
  46.  
  47.     a) its reference count variable equals 0
  48.     b) its parent node pointer equals NULL
  49.  
  50.   I.E. There exist no external references upon the node and the node has
  51.   been detached from the tree.
  52.  
  53.   Note that the presence of children or siblings attached to a node has no
  54.   impact upon its eligibility for destruction, as these links are "weak".
  55.  
  56. Destruction process
  57. -------------------
  58.  
  59.   The node destruction process proceeds as follows:
  60.  
  61.     1) Any children of the node are detached from it and an attempt is
  62.        made to destroy them.
  63.     2) The node is destroyed.
  64.  
  65.   If, when attempting to destroy children of the node, a child is found
  66.   to have a non-zero reference count (i.e. an external reference is
  67.   being held upon the child), the child (and its children) is not
  68.   destroyed. The child is added to the list of nodes pending deletion
  69.   and will be destroyed once its reference count reaches zero.
  70.  
  71. Example
  72. -------
  73.  
  74.   This example uses the DOM tree depicted above, and a system state as
  75.   follows:
  76.  
  77.     a) A NodeList collection references Node6. There are no other active
  78.        collections. The NodeList has a reference count of 1.
  79.     b) Node2 (and its subtree) has been removed from the document and
  80.        is referenced solely by the client code that caused it to be
  81.        removed from the document.
  82.  
  83.   The client code unreferences Node2, thus reducing its reference count to
  84.   zero. It is now eligible for destruction. Destruction occurs as follows:
  85.  
  86.     1) Node5 is detached from Node2 and an attempt is made to destroy it.
  87.        a) Node5 has no children and has a reference count of zero, so it
  88.           is destroyed.
  89.     2) Node6 is detached from Node2 and an attempt is made to destroy it.
  90.        a) Node6's reference count is non-zero, so it is added to the list
  91.           of nodes pending deletion.
  92.     3) Node2 has no further children, so it is destroyed.
  93.  
  94.   The client code unreferences the NodeList:
  95.  
  96.     1) The NodeList unreferences the node it's attached to (Node6).
  97.        Node6's reference count is now zero, so it is eligible for
  98.        destruction.
  99.        a) Node6 has no children, so it is destroyed (and removed from the
  100.           list of nodes pending deletion).
  101.     2) The NodeList is destroyed.
  102.  
  103. Destruction of Documents
  104. ------------------------
  105.  
  106.   Assumptions:
  107.  
  108.     1) Nodes within a document do not hold explicit references upon it.
  109.     2) Container data structures which address nodes in a document hold
  110.        an explicit reference upon the document.
  111.        [FIXME: and upon the root node of the subtree they address -- this
  112.         implies that the explicit reference is unnecessary, as the
  113.         addressed node will be added to the list of nodes pending
  114.         deletion]
  115.     3) A document has no parent (i.e. the parent pointer is always NULL).
  116.     4) A given node may be located in either the document tree or the
  117.        list of nodes pending deletion. It may not be located in both
  118.        data structures simultaneously.
  119.     5) Nodes in the list of nodes pending deletion are in use by the
  120.        client.
  121.  
  122.   The above assumptions imply the following:
  123.  
  124.     + If a document has a non-zero reference count, it is in use by
  125.       the client. (1,2)
  126.     + If the document's reference count reaches zero, it is no longer
  127.       in use and is eligible for deletion. (1,2,3)
  128.     + The document destructor must attempt to forcibly delete the
  129.       contents of the document tree as the nodes do not hold a reference
  130.       upon the document. (1)
  131.     + On destruction of a node, it must be removed from the list of nodes
  132.       pending deletion. (4)
  133.     + The document may not be destroyed if the list of nodes pending
  134.       deletion is non-empty after the destructor has attempted to
  135.       destroy the document tree. (4,5)
  136.  
  137.   Therefore, document destruction proceeds as follows:
  138.  
  139.     1) Forcibly destroy the document tree.
  140.     2) If the list of nodes pending deletion is non-empty, stop.
  141.     3) The list of nodes pending deletion is empty, so destroy the
  142.        document.
  143.