summaryrefslogtreecommitdiff
path: root/ace/Hash_Purgable_Map_Manager_T.cpp
blob: 5652e1b318f97208837dd13442efbd96d79f49b9 (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    ace
//
// = FILENAME
//    Hash_Purgable_Map_Manager_T.cpp
//
// = AUTHOR
//    Kirthika Parameswaran  <kirthika@cs.wustl.edu>
//
// ============================================================================

#ifndef ACE_HASH_PURGABLE_MAP_MANAGER_T_CPP
#define ACE_HASH_PURGABLE_MAP_MANAGER_T_CPP

#include "ace/Hash_Purgable_Map_Manager_T.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#if !defined (__ACE_INLINE__)
# include "ace/Hash_Purgable_Map_Manager_T.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(ace, Hash_Purgable_Map_Manager_T, "$Id$")

// Initialisation.
template <class EXT_ID, class INT_ID> 
ACE_Hash_Purgable_Map_Entry<EXT_ID, INT_ID>::ACE_Hash_Purgable_Map_Entry (ACE_Hash_Map_Entry<EXT_ID, INT_ID> *next,
                                                                          ACE_Hash_Map_Entry<EXT_ID, INT_ID> *prev,
                                                                          int purge_tag)
  : ACE_Hash_Map_Entry<EXT_ID, INT_ID> (next, 
                                        prev),
    purge_tag_ (purge_tag)
{
}

template <class EXT_ID, class INT_ID>
ACE_Hash_Purgable_Map_Entry<EXT_ID, INT_ID>::ACE_Hash_Purgable_Map_Entry (const EXT_ID &ext_id,
                                                                          const INT_ID &int_id,
                                                                          ACE_Hash_Map_Entry<EXT_ID, INT_ID> *next,
                                                                          ACE_Hash_Map_Entry<EXT_ID, INT_ID> *prev,
                                                                          int purge_tag)
  : ACE_Hash_Map_Entry<EXT_ID, INT_ID> (ext_id,
                                        int_id,
                                        next,
                                        prev),
    purge_tag_ (purge_tag)
{
}
#if ! defined (ACE_HAS_BROKEN_NOOP_DTORS)
template <class EXT_ID, class INT_ID> 
ACE_Hash_Purgable_Map_Entry<EXT_ID, INT_ID>::~ACE_Hash_Purgable_Map_Entry (void)
{
}
#endif /* ! defined (ACE_HAS_BROKEN_NOOP_DTORS) */

template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> ACE_Hash_Map_Entry<EXT_ID, INT_ID>*
ACE_Hash_Purgable_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>:: create_entry (ACE_Hash_Map_Entry<EXT_ID, INT_ID> *next,
                                                                                                   ACE_Hash_Map_Entry<EXT_ID, INT_ID> *prev)
{
  ACE_Hash_Purgable_Map_Entry<EXT_ID, INT_ID> *new_entry;
  void *ptr;
  
  // Memory is  allocated for the entry.
  ACE_ALLOCATOR_RETURN (ptr,
                        this->allocator_->malloc (sizeof (ACE_Hash_Purgable_Map_Entry<EXT_ID, INT_ID>)),
                        0);
  
  // A entry is created with its purge_tag value set to the current
  // timer value.
  new_entry = new (ptr) ACE_Hash_Purgable_Map_Entry<EXT_ID, INT_ID> (next,
                                                                     prev,
                                                                     this->timer_);
                                                           
  // The timer ticks on...;)
  ++this->timer_;
  
  return new_entry;
}

template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> ACE_Hash_Map_Entry<EXT_ID, INT_ID>*
ACE_Hash_Purgable_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>:: create_entry (const EXT_ID &ext_id,
                                                                                                   const INT_ID &int_id,
                                                                                                   ACE_Hash_Map_Entry<EXT_ID, INT_ID> *next,
                                                                                                   ACE_Hash_Map_Entry<EXT_ID, INT_ID> *prev)
{
  ACE_Hash_Purgable_Map_Entry<EXT_ID, INT_ID> *new_entry;
  void *ptr;

  // Memory is  allocated for the entry.
  ACE_ALLOCATOR_RETURN (ptr,
                        this->allocator_->malloc (sizeof (ACE_Hash_Purgable_Map_Entry<EXT_ID, INT_ID>)),
                        0);

  // A entry is created with its purge_tag value set to the current
  // timer value.
  new_entry = new (ptr) ACE_Hash_Purgable_Map_Entry<EXT_ID, INT_ID> (ext_id,
                                                                     int_id,
                                                                     next,
                                                                     prev,
                                                                     this->timer_);
  // The timer moves on.
  ++this->timer_;
  
  return new_entry;
}

// This method finds the appropriate entry and also sets its purge_tag
// to the current timer value. 
template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
ACE_Hash_Purgable_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::shared_find (const EXT_ID &ext_id,
                                                                                                 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry,
                                                                                                 u_long &loc)
{
  // The entry is filled in depending upon the ext_id searched in the map.
  int result =
    ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::shared_find (ext_id,
                                                                                            entry,
                                                                                            loc);
  if (result == -1)
    return -1;
 
  // On a successful find ..
  if (result == 0)
    {
      PURGABLE_ENTRY *purgable_entry = ACE_dynamic_cast (PURGABLE_ENTRY *&,
                                                         entry);
      
       // Set the purge_tag to the current timer value showing the
       // recent use of the entry. The timer is then incremented.
       purgable_entry->set_purge_tag (this->timer_);
       ++this->timer_;
     }

  return result;
}

template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int 
ACE_Hash_Purgable_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::purge (void)
{
  // The minimum value has to be large enough to be able to fall
  // thru the loop to find lesser valued entries.
  unsigned int min= ULONG_MAX;
  
  ENTRY *entry = 0;
  PURGABLE_ENTRY *purgable_entry = 0;
  PURGABLE_ENTRY *purgable_entry_to_remove = 0;
  
  // The iterator moves thru the map searching for the entry with the
  // lowest purge_tag. Such an entry is stored and later removing by
  // unbinding it from the map.
#if defined (__IBMCPP__) && (__IBMCPP__ >= 400)
  for (typename ACE_Hash_Map_Manager_Ex <EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::ITERATOR iter (*this);
       iter.next (entry) !=0;
       iter.advance ())
#else
  for (ITERATOR iter (*this);
       iter.next (entry) !=0;
       iter.advance ())
#endif /* defined (__IBMCPP__) && (__IBMCPP__ >= 400) */
    {
      purgable_entry = ACE_dynamic_cast (PURGABLE_ENTRY *,
                                         entry);

      // Ah! an entry with a lower purge_tag...
      if (min > purgable_entry->get_purge_tag ())
        {
          min = purgable_entry->get_purge_tag ();
          purgable_entry_to_remove = purgable_entry;
        }
    }
  
  // The entry is removed from the map only if it is non-NULL.
  if (purgable_entry_to_remove != 0) 
    return this->unbind_i (purgable_entry_to_remove);
  else
    return 0;
}

#endif /* ACE_HASH_PURGABLE_MAP_MANAGER_T_CPP */