sipxportlib  Version 3.3
UtlLink.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005 SIPfoundry Inc.
3 // License by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2005 Pingtel Corp.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
9 
10 #ifndef _UTLLINK_H_
11 #define _UTLLINK_H_
12 
13 #include "assert.h"
14 
15 // SYSTEM INCLUDES
16 #include "os/OsDefs.h"
17 #include "os/OsBSem.h"
18 
19 // APPLICATION INCLUDES
20 #include "utl/UtlContainable.h"
21 
22 // DEFINES
23 #ifndef UTLLINK_BLOCK_SIZE
24 #define UTLLINK_BLOCK_SIZE 1000
25 #endif
26 
27 // MACROS
28 // EXTERNAL FUNCTIONS
29 // EXTERNAL VARIABLES
30 // CONSTANTS
31 // STRUCTS
32 // TYPEDEFS
33 // FORWARD DECLARATIONS
34 class UtlLink;
35 class UtlChainPool;
36 
49 class UtlChain
50 {
51 /* //////////////////////////// PUBLIC //////////////////////////////////// */
52  public:
55  prev(NULL),
56  next(NULL)
57  {
58  }
59 
62  {
63  }
64 
65 /* //////////////////////////// PROTECTED //////////////////////////////////// */
66  protected:
67  friend class UtlChainPool;
68  friend class UtlLink;
69  friend class UtlPair;
70  friend class UtlContainer;
71  friend class UtlList;
72  friend class UtlHashMap;
73  friend class UtlHashMapIterator;
74  friend class UtlHashBag;
75  friend class UtlHashBagIterator;
76  friend class UtlChainTest;
77  friend class UtlLinkTest;
78 
81 
82  // ================================================================
90 
93  bool isUnLinked() const
94  {
95  return (!(prev||next));
96  }
97 
99  void unchain()
100  {
101  if (prev)
102  {
103  prev->next = next;
104  }
105  if (next)
106  {
107  next->prev = prev;
108  }
109  prev=NULL;
110  next=NULL;
111  }
112 
114 
117  void chainBefore(UtlChain* existing)
118  {
119  assert(isUnLinked()); // not valid on a link that's in a chain
120 
121  next = existing;
122  if (existing->prev)
123  {
124  prev = existing->prev;
125  }
126  if (prev)
127  {
128  prev->next = this;
129  }
130  existing->prev = this;
131  }
132 
133 
135 
138  void chainAfter(UtlChain* existing)
139  {
140  assert(isUnLinked()); // not valid on a link that's in a chain
141 
142  prev = existing;
143  next = existing->next;
144  if (next)
145  {
146  next->prev = this;
147  }
148  existing->next = this;
149  }
150 
152 
153  // ================================================================
163 
167  {
168  return next;
169  }
170 
173  {
174  return prev;
175  }
176 
178  UtlLink* head() const
179  {
180  return (UtlLink*)next;
181  }
182 
184  UtlLink* tail() const
185  {
186  return (UtlLink*)prev;
187  }
188 
190  void listBefore(UtlChain* list,
191  UtlChain* existing
194  );
202  void listAfter(UtlChain* list,
204  UtlChain* existing
207  );
223 
225 /* //////////////////////////// PRIVATE /////////////////////////////////// */
226  private:
227 };
228 
229 
241 class UtlLink : public UtlChain
242 {
243 /* //////////////////////////// PUBLIC //////////////////////////////////// */
244  public:
245 
246  // ================================================================
251 
254  UtlLink* next() const
255  {
256  return static_cast<UtlLink*>(UtlChain::next);
257  }
258 
260  UtlLink* prev() const
261  {
262  return static_cast<UtlLink*>(UtlChain::prev);
263  }
264 
267  {
268  UtlLink* theLink;
269  for (theLink=const_cast<UtlLink*>(this);
270  theLink && theLink->data != target;
271  theLink=theLink->next())
272  {
273  }
274  return theLink;
275  }
276 
278  UtlLink* findNextHash(unsigned targetHash) const
279  {
280  UtlLink* theLink;
281  for (theLink=const_cast<UtlLink*>(this);
282  theLink && theLink->hash != targetHash;
283  theLink=theLink->next())
284  {
285  }
286  return theLink;
287  }
288 
292  unsigned hash;
293 
295 
296  // ================================================================
299 
301 
306  static size_t totalAllocated();
307 
309 
310 /* //////////////////////////// PROTECTED ///////////////////////////////// */
311  protected:
312  friend class UtlChainPool;
313  friend class UtlContainer;
314  friend class UtlList;
315  friend class UtlListIterator;
316  friend class UtlSList;
317  friend class UtlSListIterator;
318  friend class UtlSortedList;
319  friend class UtlHashBag;
320  friend class UtlHashBagIterator;
321  friend class UtlLinkTest;
322  friend class UtlInit;
323 
324  // ================================================================
331 
334  UtlContainable* unlink();
341  static UtlLink* before(UtlChain* existing, UtlContainable* newData);
343 
345  static UtlLink* after(UtlChain* existing, UtlContainable* newData);
346 
348 
349  // ================================================================
359 
361 
363  static UtlLink* listBefore(UtlChain* list,
364  UtlChain* existing,
367  UtlContainable* newData
368  );
376  static UtlLink* listAfter(UtlChain* list,
378  UtlChain* existing,
381  UtlContainable* newData
382  );
389  UtlContainable* detachFrom(UtlChain* listHead);
398  static UtlLink* findData(UtlChain* list, UtlContainable* target)
400  {
401  return list->next ? static_cast<UtlLink*>(list->next)->findDataRef(target) : NULL;
402  }
403 
405 
406  // ================================================================
411 
414 
420  data(NULL),
421  hash(0)
422  {
423  };
424 
426 
430  {
431  };
432 
434  static UtlLink* get();
435 
437  void release();
438 
440  void rehash();
441 
442 /* //////////////////////////// PRIVATE /////////////////////////////////// */
443  private:
444 
446  static void allocate(size_t blocksize,
447  UtlChain* blockList,
448  UtlChain* pool
449  );
450 
451 
453  static UtlChainPool* spLinkPool;
454 };
455 
457 class UtlPair : public UtlLink
458 {
459  protected:
460  friend class UtlHashMap;
461  friend class UtlHashMapIterator;
462  friend class UtlHashBagIterator;
463  friend class UtlInit;
464 
466 
468  value(NULL)
469  {
470  };
471 
473  {
474  }
475 
477 
479  static UtlPair* get();
480 
482  void release();
483 
484  private:
486  static void allocate(size_t blocksize,
487  UtlChain* blockList,
488  UtlChain* pool
489  );
490 
491 };
492 
493 #endif // _UTLLINK_H_
static UtlChainPool * spPairPool
Definition: UtlLink.h:476
Definition: UtlSListIterator.h:37
UtlChain * prev
backward chain pointer
Definition: UtlLink.h:79
UtlChain * detachFromList(UtlChain *listHead)
Remove a link from a list.
Definition: UtlLink.cpp:105
Definition: UtlSortedList.h:44
void chainBefore(UtlChain *existing)
Insert a new UtlChain before existing.
Definition: UtlLink.h:117
UtlChain * listTail() const
Returns the tail (last) UtlLink on the list (or NULL if the list is empty).
Definition: UtlLink.h:172
UtlChain * next
forward chain pointer
Definition: UtlLink.h:80
UtlLink * tail() const
Returns the tail (last) UtlLink on the list (or NULL if the list is empty).
Definition: UtlLink.h:184
~UtlPair()
Definition: UtlLink.h:472
bool isUnLinked() const
Is this block not linked to anything?
Definition: UtlLink.h:93
Pool of available objects derived from UtlChain.
Definition: UtlChainPool.h:37
UtlLink * head() const
Returns the head (first) UtlLink on the list (or NULL if the list is empty).
Definition: UtlLink.h:178
friend class UtlChainTest
Definition: UtlLink.h:76
#define NULL
Definition: UtlDefs.h:29
Definition: UtlHashMapIterator.h:38
Definition: UtlList.h:59
UtlChain * listHead() const
Returns the head (first) UtlLink on the list (or NULL if the list is empty).
Definition: UtlLink.h:166
Definition: UtlListIterator.h:37
Definition: UtlLink.h:49
Definition: UtlContainable.h:37
void unchain()
Take the link out of its chain.
Definition: UtlLink.h:99
~UtlChain()
Destructor.
Definition: UtlLink.h:61
UtlContainable * value
Definition: UtlLink.h:465
UtlChain()
Constructor initializes to unlinked.
Definition: UtlLink.h:54
friend class UtlLinkTest
Definition: UtlLink.h:77
UtlPair()
Definition: UtlLink.h:467
Definition: UtlSList.h:47
Definition: UtlHashBag.h:41
Utl initializer class. Solves the problem with ordering of constructors & destructors.
Definition: UtlInit.h:40
Definition: UtlHashMap.h:46
Associate a key object (the parent UtlLink data) with its value object.
Definition: UtlLink.h:457
#define assert(exp)
Definition: WinCEFixups.h:37
Definition: UtlHashBagIterator.h:38
Definition: UtlContainer.h:38
void chainAfter(UtlChain *existing)
Insert a new UtlChain after existing..
Definition: UtlLink.h:138
void listBefore(UtlChain *list, UtlChain *existing)
Insert this link into a list before an existing entry (before NULL == at the tail).
Definition: UtlLink.cpp:44
void listAfter(UtlChain *list, UtlChain *existing)
Insert this link into a list after an existing entry (after NULL == at the head). ...
Definition: UtlLink.cpp:75