1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #ifndef _MDB_
7 # include "mdb.h"
8 #endif
9
10 #ifndef _MORK_
11 # include "mork.h"
12 #endif
13
14 #ifndef _MORKNODE_
15 # include "morkNode.h"
16 #endif
17
18 #ifndef _MORKENV_
19 # include "morkEnv.h"
20 #endif
21
22 #ifndef _MORKMAP_
23 # include "morkMap.h"
24 #endif
25
26 #ifndef _MORKINTMAP_
27 # include "morkIntMap.h"
28 #endif
29
30 #ifndef _MORKNODEMAP_
31 # include "morkNodeMap.h"
32 #endif
33
34 // 456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
35
36 // ````` ````` ````` ````` `````
37 // { ===== begin morkNode interface =====
38
CloseMorkNode(morkEnv * ev)39 /*public virtual*/ void morkNodeMap::CloseMorkNode(
40 morkEnv* ev) // CloseNodeMap() only if open
41 {
42 if (this->IsOpenNode()) {
43 this->MarkClosing();
44 this->CloseNodeMap(ev);
45 this->MarkShut();
46 }
47 }
48
49 /*public virtual*/
~morkNodeMap()50 morkNodeMap::~morkNodeMap() // assert CloseNodeMap() executed earlier
51 {
52 MORK_ASSERT(this->IsShutNode());
53 }
54
55 /*public non-poly*/
morkNodeMap(morkEnv * ev,const morkUsage & inUsage,nsIMdbHeap * ioHeap,nsIMdbHeap * ioSlotHeap)56 morkNodeMap::morkNodeMap(morkEnv* ev, const morkUsage& inUsage,
57 nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap)
58 : morkIntMap(ev, inUsage, /*valsize*/ sizeof(morkNode*), ioHeap, ioSlotHeap,
59 /*inHoldChanges*/ morkBool_kTrue) {
60 if (ev->Good()) mNode_Derived = morkDerived_kNodeMap;
61 }
62
CloseNodeMap(morkEnv * ev)63 /*public non-poly*/ void morkNodeMap::CloseNodeMap(
64 morkEnv* ev) // called by CloseMorkNode();
65 {
66 if (this->IsNode()) {
67 this->CutAllNodes(ev);
68 this->CloseMap(ev);
69 this->MarkShut();
70 } else
71 this->NonNodeError(ev);
72 }
73
74 // } ===== end morkNode methods =====
75 // ````` ````` ````` ````` `````
76
AddNode(morkEnv * ev,mork_token inToken,morkNode * ioNode)77 mork_bool morkNodeMap::AddNode(morkEnv* ev, mork_token inToken,
78 morkNode* ioNode)
79 // the AddNode() method return value equals ev->Good().
80 {
81 if (ioNode && ev->Good()) {
82 morkNode* node = 0; // old val in the map
83
84 mork_bool put = this->Put(ev, &inToken, &ioNode,
85 /*key*/ (void*)0, &node, (mork_change**)0);
86
87 if (put) // replaced an existing value for key inToken?
88 {
89 if (node && node != ioNode) // need to release old node?
90 node->CutStrongRef(ev);
91 }
92
93 if (ev->Bad() || !ioNode->AddStrongRef(ev)) {
94 // problems adding node or increasing refcount?
95 this->Cut(ev, &inToken, // make sure not in map
96 /*key*/ (void*)0, /*val*/ (void*)0, (mork_change**)0);
97 }
98 } else if (!ioNode)
99 ev->NilPointerError();
100
101 return ev->Good();
102 }
103
CutNode(morkEnv * ev,mork_token inToken)104 mork_bool morkNodeMap::CutNode(morkEnv* ev, mork_token inToken) {
105 morkNode* node = 0; // old val in the map
106 mork_bool outCutNode = this->Cut(ev, &inToken,
107 /*key*/ (void*)0, &node, (mork_change**)0);
108 if (node) node->CutStrongRef(ev);
109
110 return outCutNode;
111 }
112
GetNode(morkEnv * ev,mork_token inToken)113 morkNode* morkNodeMap::GetNode(morkEnv* ev, mork_token inToken)
114 // Note the returned node does NOT have an increase in refcount for this.
115 {
116 morkNode* node = 0; // old val in the map
117 this->Get(ev, &inToken, /*key*/ (void*)0, &node, (mork_change**)0);
118
119 return node;
120 }
121
CutAllNodes(morkEnv * ev)122 mork_num morkNodeMap::CutAllNodes(morkEnv* ev)
123 // CutAllNodes() releases all the reference node values.
124 {
125 mork_num outSlots = mMap_Slots;
126 mork_token key = 0; // old key token in the map
127 morkNode* val = 0; // old val node in the map
128
129 mork_change* c = 0;
130 morkNodeMapIter i(ev, this);
131 for (c = i.FirstNode(ev, &key, &val); c; c = i.NextNode(ev, &key, &val)) {
132 if (val) val->CutStrongRef(ev);
133 i.CutHereNode(ev, /*key*/ (mork_token*)0, /*val*/ (morkNode**)0);
134 }
135
136 return outSlots;
137 }
138
139 // 456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789
140