summaryrefslogtreecommitdiffstats
path: root/private/ole32/dcomss/objex/shrmem/dcom95/gentable.cxx
blob: 890ae5e21c7906d2c2752a175f4f84a4eb0ba89d (plain) (blame)
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
/*++

Copyright (c) 1995 Microsoft Corporation

Module Name:

    Table.cxx

Abstract:

    Implementation of the CResolverHashTable and CTableElement.

Author:

    Mario Goertzel    [MarioGo]

Revision History:

    MarioGo     02-15-95    Bits 'n pieces
    MarioGo     12-18-95    Changed from UUID to generic object keys
    SatishT     02-12-96    Modified for use with shared memory

--*/

#include<or.hxx>

#if DBG

void
CResolverHashTable::IsValid()
{
    IsGoodBasedPtr(_buckets);

    ASSERT(_fInitialized == TRUE || _fInitialized == FALSE);
    ASSERT(!_fInitialized || _cBuckets > 0);
    ASSERT(_cBuckets > 0 || _cElements == 0);
    ASSERT(_cBuckets >=0 && _cBuckets < MAX_BUCKETS);

    if (_fInitialized) 
    {
        for (USHORT i = 0; i < _cBuckets; i++)
        {
            _buckets[i].IsValid();
        }
    }
}

#endif

ORSTATUS
CResolverHashTable::PrivAdd(        // never called before Init()
    IN CTableElement *pElement
    )
{
    VALIDATE_METHOD

    ORSTATUS status = OR_OK;

    ISearchKey& sk = *pElement;      // auto conversion to ISearchKey

    DWORD hash = sk.Hash() % _cBuckets;

    ComDebOut((DEB_ITRACE, "Adding %x at hash = %d\n",
                                 OR_OFFSET(pElement),hash));

    _buckets[hash].Insert(status,pElement);

    _cElements++;

    return status;
}



CResolverHashTable::CResolverHashTable(UINT start_size)
{
    ComDebOut((DEB_ITRACE, "Constructing CResolverHashTable Size = %d\n",start_size));

    _cBuckets = start_size;
    _cElements = 0;
    _buckets = NULL;
    _fInitialized = FALSE;

    VALIDATE_METHOD
}


ORSTATUS
CResolverHashTable::Init()
{
    ORSTATUS status;

    ASSERT(!_fInitialized);

    VALIDATE_METHOD

    NEW_OR_BASED_ARRAY(_buckets,CTableElementList,_cBuckets);

    if (NULL == _buckets)
    {
        status = OR_NOMEM;
    }
    else
    {
        status = OR_OK;
        _fInitialized = TRUE;
    }

    return status;
}


CResolverHashTable::~CResolverHashTable()
{
    RemoveAll();
}


CTableElement *
CResolverHashTable::Lookup(
    IN ISearchKey &id
    )
{
    ComDebOut((DEB_ITRACE, "Entering Lookup\n"));

    VALIDATE_METHOD

    if (!_fInitialized) return NULL;  // nothing to look in

    DWORD hash = id.Hash();
    hash %= _cBuckets;

    return _buckets[hash].Find(id);
}

ORSTATUS
CResolverHashTable::Add(
    IN CTableElement *pElement
    )
{
    ComDebOut((DEB_ITRACE, "Entering Add for %x in %x\n",
                                     OR_OFFSET(pElement),OR_OFFSET(this)));


    VALIDATE_METHOD

    ORSTATUS status = OR_OK;

    if (!_fInitialized) status = Init();     // set up buckets

    if (status != OR_OK) return status;

    status = PrivAdd(pElement);     // do the basic Add

    if (status != OR_OK) return status;

    if (_cElements > _cBuckets)     // now see if the table is overpopulated
        {
        // Try to grow the table.  If the allocation fails no need to worry,
        // everything still works but might be a bit slower.

        CTableElementList OR_BASED * psll;
        NEW_OR_BASED_ARRAY(psll,CTableElementList,_cBuckets * 2);


        // The tricky part here is to avoid getting OR_NOMEM error while moving
        // between tables.  We do that by recycling the links in the old lists

        if (psll)    
        {
            UINT i, uiBucketsOld = _cBuckets;
            CTableElement *pte;
            CTableElementList::Link *pLink;
            CTableElementList OR_BASED *psllOld = _buckets;

            OrDbgDetailPrint(("OR: Growing table: %p\n", this));

            // Change to the larger array of buckets.
            _cBuckets *= 2;
            _buckets = psll;

            // Move every element from the old table into the large table.

            for(i = 0; i < uiBucketsOld; i++)
            {
                while (pLink = psllOld[i].PopLink())  // uses specialized private operations
                {                                     // to avoid both memory allocation
                                                      // and reference counting problems
                
                    ISearchKey& sk = *pLink->_pData;  // auto conversion to ISearchKey
                    _buckets[sk.Hash() % _cBuckets].PushLink(pLink);
                }
            }
        }
    }

    ComDebOut((DEB_ITRACE, "Leaving Add\n"));

    return status;
}

CTableElement *
CResolverHashTable::Remove(
    IN ISearchKey &id
    )
/*++

Routine Description:

    Looks up and removes an element from the table.

Arguments:

    id - The key to match the element to be removed

Return Value:

    NULL - The element was not in the table

    non-NULL - A pointer to the element which was removed.

--*/

{
   VALIDATE_METHOD

   if (!_fInitialized) return NULL;  // nothing to remove

    DWORD hash = id.Hash() % _cBuckets;

    CTableElement *pTE = _buckets[hash].Remove(id);

    if (pTE)
    {
        _cElements--;
    }

    return pTE;
}


void
CResolverHashTable::RemoveAll()
{
    VALIDATE_METHOD

    if (!_fInitialized) return;  // nothing to remove

    ASSERT(_buckets);
    DWORD _currentBucketIndex = 0;
    CTableElement *pTE;

    while (_currentBucketIndex < _cBuckets)
    {
        while (pTE = _buckets[_currentBucketIndex].Pop())
        {
            _cElements--;
        }

        _currentBucketIndex++;
    }

    ASSERT(_cElements==0);

    DELETE_OR_BASED_ARRAY(CTableElementList,_buckets,_cBuckets);
    _buckets = NULL;
    _fInitialized = FALSE;
}