summaryrefslogtreecommitdiff
path: root/db/mork/src/morkBead.cpp
blob: 2b552eff1309ffaf02829aeb1afe31297b3672da (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-  */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef _MDB_
#include "mdb.h"
#endif

#ifndef _MORK_
#include "mork.h"
#endif

#ifndef _MORKNODE_
#include "morkNode.h"
#endif

#ifndef _MORKENV_
#include "morkEnv.h"
#endif

#ifndef _MORKBEAD_
#include "morkBead.h"
#endif

//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789

// ````` ````` ````` ````` ````` 
// { ===== begin morkNode interface =====

/*public virtual*/ void
morkBead::CloseMorkNode(morkEnv* ev) // CloseBead() only if open
{
  if ( this->IsOpenNode() )
  {
    this->MarkClosing();
    this->CloseBead(ev);
    this->MarkShut();
  }
}

/*public virtual*/
morkBead::~morkBead() // assert CloseBead() executed earlier
{
  MORK_ASSERT(mBead_Color==0 || mNode_Usage == morkUsage_kStack );
}

/*public non-poly*/
morkBead::morkBead(mork_color inBeadColor)
: morkNode( morkUsage_kStack )
, mBead_Color( inBeadColor )
{
}

/*public non-poly*/
morkBead::morkBead(const morkUsage& inUsage, nsIMdbHeap* ioHeap, 
  mork_color inBeadColor)
: morkNode( inUsage, ioHeap )
, mBead_Color( inBeadColor )
{
}

/*public non-poly*/
morkBead::morkBead(morkEnv* ev,
  const morkUsage& inUsage, nsIMdbHeap* ioHeap, mork_color inBeadColor)
: morkNode(ev, inUsage, ioHeap)
, mBead_Color( inBeadColor )
{
  if ( ev->Good() )
  {
    if ( ev->Good() )
      mNode_Derived = morkDerived_kBead;
  }
}

/*public non-poly*/ void
morkBead::CloseBead(morkEnv* ev) // called by CloseMorkNode();
{
    if ( this->IsNode() )
    {
      if ( !this->IsShutNode() )
      {
        mBead_Color = 0;
        this->MarkShut();
      }
    }
    else
      this->NonNodeError(ev);
}

// } ===== end morkNode methods =====
// ````` ````` ````` ````` ````` 

//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789

// ````` ````` ````` ````` ````` 
// { ===== begin morkNode interface =====

/*public virtual*/ void
morkBeadMap::CloseMorkNode(morkEnv* ev) // CloseBeadMap() only if open
{
  if ( this->IsOpenNode() )
  {
    this->MarkClosing();
    this->CloseBeadMap(ev);
    this->MarkShut();
  }
}

/*public virtual*/
morkBeadMap::~morkBeadMap() // assert CloseBeadMap() executed earlier
{
  MORK_ASSERT(this->IsShutNode());
}

/*public non-poly*/
morkBeadMap::morkBeadMap(morkEnv* ev,
  const morkUsage& inUsage, nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap)
: morkMap(ev, inUsage, ioHeap, sizeof(morkBead*), /*inValSize*/ 0,
  /*slotCount*/ 11, ioSlotHeap, /*holdChanges*/ morkBool_kFalse)
{
  if ( ev->Good() )
    mNode_Derived = morkDerived_kBeadMap;
}

/*public non-poly*/ void
morkBeadMap::CloseBeadMap(morkEnv* ev) // called by CloseMorkNode();
{
    if ( this->IsNode() )
    {
      this->CutAllBeads(ev);
      this->CloseMap(ev);
      this->MarkShut();
    }
    else
      this->NonNodeError(ev);
}

// } ===== end morkNode methods =====
// ````` ````` ````` ````` ````` 

mork_bool
morkBeadMap::AddBead(morkEnv* ev, morkBead* ioBead)
  // the AddBead() boolean return equals ev->Good().
{
  if ( ioBead && ev->Good() )
  {
    morkBead* oldBead = 0; // old key in the map

    mork_bool put = this->Put(ev, &ioBead, /*val*/ (void*) 0,
      /*key*/ &oldBead, /*val*/ (void*) 0, (mork_change**) 0);
      
    if ( put ) // replaced an existing key?
    {
      if ( oldBead != ioBead ) // new bead was not already in table?
        ioBead->AddStrongRef(ev); // now there's another ref
        
      if ( oldBead && oldBead != ioBead ) // need to release old node?
        oldBead->CutStrongRef(ev);
    }
    else
      ioBead->AddStrongRef(ev); // another ref if not already in table
  }
  else if ( !ioBead )
    ev->NilPointerError();
    
  return ev->Good();
}

mork_bool
morkBeadMap::CutBead(morkEnv* ev, mork_color inColor)
{
  morkBead* oldBead = 0; // old key in the map
  morkBead bead(inColor);
  morkBead* key = &bead;
  
  mork_bool outCutNode = this->Cut(ev, &key, 
    /*key*/ &oldBead, /*val*/ (void*) 0, (mork_change**) 0);
    
  if ( oldBead )
    oldBead->CutStrongRef(ev);
  
  bead.CloseBead(ev);
  return outCutNode;
}

morkBead*
morkBeadMap::GetBead(morkEnv* ev, mork_color inColor)
  // Note the returned bead does NOT have an increase in refcount for this.
{
  morkBead* oldBead = 0; // old key in the map
  morkBead bead(inColor);
  morkBead* key = &bead;

  this->Get(ev, &key, /*key*/ &oldBead, /*val*/ (void*) 0, (mork_change**) 0);
  
  bead.CloseBead(ev);
  return oldBead;
}

mork_num
morkBeadMap::CutAllBeads(morkEnv* ev)
  // CutAllBeads() releases all the referenced beads.
{
  mork_num outSlots = mMap_Slots;
  
  morkBeadMapIter i(ev, this);
  morkBead* b = i.FirstBead(ev);

  while ( b )
  {
    b->CutStrongRef(ev);
    i.CutHereBead(ev);
    b = i.NextBead(ev);
  }
  
  return outSlots;
}


// { ===== begin morkMap poly interface =====
/*virtual*/ mork_bool
morkBeadMap::Equal(morkEnv* ev, const void* inKeyA, const void* inKeyB) const
{
  MORK_USED_1(ev);
  return (*(const morkBead**) inKeyA)->BeadEqual(
    *(const morkBead**) inKeyB);
}

/*virtual*/ mork_u4
morkBeadMap::Hash(morkEnv* ev, const void* inKey) const
{
  MORK_USED_1(ev);
    return (*(const morkBead**) inKey)->BeadHash();
}
// } ===== end morkMap poly interface =====


//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789

 
morkBead* morkBeadMapIter::FirstBead(morkEnv* ev)
{
  morkBead* bead = 0;
  this->First(ev, &bead, /*val*/ (void*) 0);
  return bead;
}

morkBead* morkBeadMapIter::NextBead(morkEnv* ev)
{
  morkBead* bead = 0;
  this->Next(ev, &bead, /*val*/ (void*) 0);
  return bead;
}

morkBead* morkBeadMapIter::HereBead(morkEnv* ev)
{
  morkBead* bead = 0;
  this->Here(ev, &bead, /*val*/ (void*) 0);
  return bead;
}

void morkBeadMapIter::CutHereBead(morkEnv* ev)
{
  this->CutHere(ev, /*key*/ (void*) 0, /*val*/ (void*) 0);
}

//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789

//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789

// ````` ````` ````` ````` ````` 
// { ===== begin morkNode interface =====

/*public virtual*/ void
morkBeadProbeMap::CloseMorkNode(morkEnv* ev) // CloseBeadProbeMap() if open
{
  if ( this->IsOpenNode() )
  {
    this->MarkClosing();
    this->CloseBeadProbeMap(ev);
    this->MarkShut();
  }
}

/*public virtual*/
morkBeadProbeMap::~morkBeadProbeMap() // assert CloseBeadProbeMap() earlier
{
  MORK_ASSERT(this->IsShutNode());
}


/*public non-poly*/
morkBeadProbeMap::morkBeadProbeMap(morkEnv* ev, const morkUsage& inUsage,
    nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap)
: morkProbeMap(ev, inUsage, ioHeap,
  /*inKeySize*/ sizeof(morkBead*), /*inValSize*/ 0,
  ioSlotHeap, /*startSlotCount*/ 11, 
  /*inZeroIsClearKey*/ morkBool_kTrue)
{
  if ( ev->Good() )
    mNode_Derived = morkDerived_kBeadProbeMap;
}

/*public non-poly*/ void
morkBeadProbeMap::CloseBeadProbeMap(morkEnv* ev) // called by CloseMorkNode();
{
    if ( this->IsNode() )
    {
      this->CutAllBeads(ev);
      this->CloseProbeMap(ev);
      this->MarkShut();
    }
    else
      this->NonNodeError(ev);
}

// } ===== end morkNode methods =====
// ````` ````` ````` ````` ````` 

/*virtual*/ mork_test // hit(a,b) implies hash(a) == hash(b)
morkBeadProbeMap::MapTest(morkEnv* ev, const void* inMapKey,
  const void* inAppKey) const
{
  MORK_USED_1(ev);
  const morkBead* key = *(const morkBead**) inMapKey;
  if ( key )
  {
    mork_bool hit = key->BeadEqual(*(const morkBead**) inAppKey);
    return ( hit ) ? morkTest_kHit : morkTest_kMiss;
  }
  else
    return morkTest_kVoid;
}

/*virtual*/ mork_u4 // hit(a,b) implies hash(a) == hash(b)
morkBeadProbeMap::MapHash(morkEnv* ev, const void* inAppKey) const
{
  const morkBead* key = *(const morkBead**) inAppKey;
  if ( key )
    return key->BeadHash();
  else
  {
    ev->NilPointerWarning();
    return 0;
  }
}

/*virtual*/ mork_u4 
morkBeadProbeMap::ProbeMapHashMapKey(morkEnv* ev,
  const void* inMapKey) const
{
  const morkBead* key = *(const morkBead**) inMapKey;
  if ( key )
    return key->BeadHash();
  else
  {
    ev->NilPointerWarning();
    return 0;
  }
}

mork_bool
morkBeadProbeMap::AddBead(morkEnv* ev, morkBead* ioBead)
{
  if ( ioBead && ev->Good() )
  {
    morkBead* bead = 0; // old key in the map
    
    mork_bool put = this->MapAtPut(ev, &ioBead, /*val*/ (void*) 0, 
      /*key*/ &bead, /*val*/ (void*) 0);
          
    if ( put ) // replaced an existing key?
    {
      if ( bead != ioBead ) // new bead was not already in table?
        ioBead->AddStrongRef(ev); // now there's another ref
        
      if ( bead && bead != ioBead ) // need to release old node?
        bead->CutStrongRef(ev);
    }
    else
      ioBead->AddStrongRef(ev); // now there's another ref
  }
  else if ( !ioBead )
    ev->NilPointerError();
    
  return ev->Good();
}

morkBead*
morkBeadProbeMap::GetBead(morkEnv* ev, mork_color inColor)
{
  morkBead* oldBead = 0; // old key in the map
  morkBead bead(inColor);
  morkBead* key = &bead;

  this->MapAt(ev, &key, &oldBead, /*val*/ (void*) 0);
  
  bead.CloseBead(ev);
  return oldBead;
}

mork_num
morkBeadProbeMap::CutAllBeads(morkEnv* ev)
  // CutAllBeads() releases all the referenced bead values.
{
  mork_num outSlots = sMap_Slots;
  
  morkBeadProbeMapIter i(ev, this);
  morkBead* b = i.FirstBead(ev);

  while ( b )
  {
    b->CutStrongRef(ev);
    b = i.NextBead(ev);
  }
  this->MapCutAll(ev);
  
  return outSlots;
}


//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789