~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject/pjlib/src/pj/hash.c

  • Committer: Jackson Doak
  • Date: 2013-07-10 21:04:46 UTC
  • mfrom: (20.1.3 sid)
  • Revision ID: noskcaj@ubuntu.com-20130710210446-y8f587vza807icr9
Properly merged from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: hash.c 3553 2011-05-05 06:14:19Z nanang $ */
2
 
/* 
3
 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
 
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
19
 
 */
20
 
#include <pj/hash.h>
21
 
#include <pj/log.h>
22
 
#include <pj/string.h>
23
 
#include <pj/pool.h>
24
 
#include <pj/os.h>
25
 
#include <pj/ctype.h>
26
 
#include <pj/assert.h>
27
 
 
28
 
/**
29
 
 * The hash multiplier used to calculate hash value.
30
 
 */
31
 
#define PJ_HASH_MULTIPLIER      33
32
 
 
33
 
 
34
 
struct pj_hash_entry
35
 
{
36
 
    struct pj_hash_entry *next;
37
 
    void *key;
38
 
    pj_uint32_t hash;
39
 
    pj_uint32_t keylen;
40
 
    void *value;
41
 
};
42
 
 
43
 
 
44
 
struct pj_hash_table_t
45
 
{
46
 
    pj_hash_entry     **table;
47
 
    unsigned            count, rows;
48
 
    pj_hash_iterator_t  iterator;
49
 
};
50
 
 
51
 
 
52
 
 
53
 
PJ_DEF(pj_uint32_t) pj_hash_calc(pj_uint32_t hash, const void *key, 
54
 
                                 unsigned keylen)
55
 
{
56
 
    PJ_CHECK_STACK();
57
 
 
58
 
    if (keylen==PJ_HASH_KEY_STRING) {
59
 
        const pj_uint8_t *p = (const pj_uint8_t*)key;
60
 
        for ( ; *p; ++p ) {
61
 
            hash = (hash * PJ_HASH_MULTIPLIER) + *p;
62
 
        }
63
 
    } else {
64
 
        const pj_uint8_t *p = (const pj_uint8_t*)key,
65
 
                              *end = p + keylen;
66
 
        for ( ; p!=end; ++p) {
67
 
            hash = (hash * PJ_HASH_MULTIPLIER) + *p;
68
 
        }
69
 
    }
70
 
    return hash;
71
 
}
72
 
 
73
 
PJ_DEF(pj_uint32_t) pj_hash_calc_tolower( pj_uint32_t hval,
74
 
                                          char *result,
75
 
                                          const pj_str_t *key)
76
 
{
77
 
    long i;
78
 
 
79
 
#if defined(PJ_HASH_USE_OWN_TOLOWER) && PJ_HASH_USE_OWN_TOLOWER != 0
80
 
    for (i=0; i<key->slen; ++i) {
81
 
        pj_uint8_t c = key->ptr[i];
82
 
        if (c & 64)
83
 
            result[i] = (char)(c | 32);
84
 
        else
85
 
            result[i] = (char)c;
86
 
        hval = hval * PJ_HASH_MULTIPLIER + result[i];
87
 
    }
88
 
#else
89
 
    for (i=0; i<key->slen; ++i) {
90
 
        result[i] = (char)pj_tolower(key->ptr[i]);
91
 
        hval = hval * PJ_HASH_MULTIPLIER + result[i];
92
 
    }
93
 
#endif
94
 
 
95
 
    return hval;
96
 
}
97
 
 
98
 
 
99
 
PJ_DEF(pj_hash_table_t*) pj_hash_create(pj_pool_t *pool, unsigned size)
100
 
{
101
 
    pj_hash_table_t *h;
102
 
    unsigned table_size;
103
 
    
104
 
    /* Check that PJ_HASH_ENTRY_BUF_SIZE is correct. */
105
 
    PJ_ASSERT_RETURN(sizeof(pj_hash_entry)<=PJ_HASH_ENTRY_BUF_SIZE, NULL);
106
 
 
107
 
    h = PJ_POOL_ALLOC_T(pool, pj_hash_table_t);
108
 
    h->count = 0;
109
 
 
110
 
    PJ_LOG( 6, ("hashtbl", "hash table %p created from pool %s", h, pj_pool_getobjname(pool)));
111
 
 
112
 
    /* size must be 2^n - 1.
113
 
       round-up the size to this rule, except when size is 2^n, then size
114
 
       will be round-down to 2^n-1.
115
 
     */
116
 
    table_size = 8;
117
 
    do {
118
 
        table_size <<= 1;    
119
 
    } while (table_size < size);
120
 
    table_size -= 1;
121
 
    
122
 
    h->rows = table_size;
123
 
    h->table = (pj_hash_entry**)
124
 
               pj_pool_calloc(pool, table_size+1, sizeof(pj_hash_entry*));
125
 
    return h;
126
 
}
127
 
 
128
 
static pj_hash_entry **find_entry( pj_pool_t *pool, pj_hash_table_t *ht, 
129
 
                                   const void *key, unsigned keylen,
130
 
                                   void *val, pj_uint32_t *hval,
131
 
                                   void *entry_buf)
132
 
{
133
 
    pj_uint32_t hash;
134
 
    pj_hash_entry **p_entry, *entry;
135
 
 
136
 
    if (hval && *hval != 0) {
137
 
        hash = *hval;
138
 
        if (keylen==PJ_HASH_KEY_STRING) {
139
 
            keylen = pj_ansi_strlen((const char*)key);
140
 
        }
141
 
    } else {
142
 
        /* This slightly differs with pj_hash_calc() because we need 
143
 
         * to get the keylen when keylen is PJ_HASH_KEY_STRING.
144
 
         */
145
 
        hash=0;
146
 
        if (keylen==PJ_HASH_KEY_STRING) {
147
 
            const pj_uint8_t *p = (const pj_uint8_t*)key;
148
 
            for ( ; *p; ++p ) {
149
 
                hash = hash * PJ_HASH_MULTIPLIER + *p;
150
 
            }
151
 
            keylen = p - (const unsigned char*)key;
152
 
        } else {
153
 
            const pj_uint8_t *p = (const pj_uint8_t*)key,
154
 
                                  *end = p + keylen;
155
 
            for ( ; p!=end; ++p) {
156
 
                hash = hash * PJ_HASH_MULTIPLIER + *p;
157
 
            }
158
 
        }
159
 
 
160
 
        /* Report back the computed hash. */
161
 
        if (hval)
162
 
            *hval = hash;
163
 
    }
164
 
 
165
 
    /* scan the linked list */
166
 
    for (p_entry = &ht->table[hash & ht->rows], entry=*p_entry; 
167
 
         entry; 
168
 
         p_entry = &entry->next, entry = *p_entry)
169
 
    {
170
 
        if (entry->hash==hash && entry->keylen==keylen &&
171
 
            pj_memcmp(entry->key, key, keylen)==0) 
172
 
        {
173
 
            break;      
174
 
        }
175
 
    }
176
 
 
177
 
    if (entry || val==NULL)
178
 
        return p_entry;
179
 
 
180
 
    /* Entry not found, create a new one. 
181
 
     * If entry_buf is specified, use it. Otherwise allocate from pool.
182
 
     */
183
 
    if (entry_buf) {
184
 
        entry = (pj_hash_entry*)entry_buf;
185
 
    } else {
186
 
        /* Pool must be specified! */
187
 
        PJ_ASSERT_RETURN(pool != NULL, NULL);
188
 
 
189
 
        entry = PJ_POOL_ALLOC_T(pool, pj_hash_entry);
190
 
        PJ_LOG(6, ("hashtbl", 
191
 
                   "%p: New p_entry %p created, pool used=%u, cap=%u", 
192
 
                   ht, entry,  pj_pool_get_used_size(pool), 
193
 
                   pj_pool_get_capacity(pool)));
194
 
    }
195
 
    entry->next = NULL;
196
 
    entry->hash = hash;
197
 
    if (pool) {
198
 
        entry->key = pj_pool_alloc(pool, keylen);
199
 
        pj_memcpy(entry->key, key, keylen);
200
 
    } else {
201
 
        entry->key = (void*)key;
202
 
    }
203
 
    entry->keylen = keylen;
204
 
    entry->value = val;
205
 
    *p_entry = entry;
206
 
    
207
 
    ++ht->count;
208
 
    
209
 
    return p_entry;
210
 
}
211
 
 
212
 
PJ_DEF(void *) pj_hash_get( pj_hash_table_t *ht,
213
 
                            const void *key, unsigned keylen,
214
 
                            pj_uint32_t *hval)
215
 
{
216
 
    pj_hash_entry *entry;
217
 
    entry = *find_entry( NULL, ht, key, keylen, NULL, hval, NULL);
218
 
    return entry ? entry->value : NULL;
219
 
}
220
 
 
221
 
PJ_DEF(void) pj_hash_set( pj_pool_t *pool, pj_hash_table_t *ht,
222
 
                          const void *key, unsigned keylen, pj_uint32_t hval,
223
 
                          void *value )
224
 
{
225
 
    pj_hash_entry **p_entry;
226
 
 
227
 
    p_entry = find_entry( pool, ht, key, keylen, value, &hval, NULL);
228
 
    if (*p_entry) {
229
 
        if (value == NULL) {
230
 
            /* delete entry */
231
 
            PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
232
 
            *p_entry = (*p_entry)->next;
233
 
            --ht->count;
234
 
            
235
 
        } else {
236
 
            /* overwrite */
237
 
            (*p_entry)->value = value;
238
 
            PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht, 
239
 
                       *p_entry, value));
240
 
        }
241
 
    }
242
 
}
243
 
 
244
 
PJ_DEF(void) pj_hash_set_np( pj_hash_table_t *ht,
245
 
                             const void *key, unsigned keylen, 
246
 
                             pj_uint32_t hval, pj_hash_entry_buf entry_buf, 
247
 
                             void *value)
248
 
{
249
 
    pj_hash_entry **p_entry;
250
 
 
251
 
    p_entry = find_entry( NULL, ht, key, keylen, value, &hval, 
252
 
                         (void*)entry_buf );
253
 
    if (*p_entry) {
254
 
        if (value == NULL) {
255
 
            /* delete entry */
256
 
            PJ_LOG(6, ("hashtbl", "%p: p_entry %p deleted", ht, *p_entry));
257
 
            *p_entry = (*p_entry)->next;
258
 
            --ht->count;
259
 
            
260
 
        } else {
261
 
            /* overwrite */
262
 
            (*p_entry)->value = value;
263
 
            PJ_LOG(6, ("hashtbl", "%p: p_entry %p value set to %p", ht, 
264
 
                       *p_entry, value));
265
 
        }
266
 
    }
267
 
}
268
 
 
269
 
PJ_DEF(unsigned) pj_hash_count( pj_hash_table_t *ht )
270
 
{
271
 
    return ht->count;
272
 
}
273
 
 
274
 
PJ_DEF(pj_hash_iterator_t*) pj_hash_first( pj_hash_table_t *ht,
275
 
                                           pj_hash_iterator_t *it )
276
 
{
277
 
    it->index = 0;
278
 
    it->entry = NULL;
279
 
 
280
 
    for (; it->index <= ht->rows; ++it->index) {
281
 
        it->entry = ht->table[it->index];
282
 
        if (it->entry) {
283
 
            break;
284
 
        }
285
 
    }
286
 
 
287
 
    return it->entry ? it : NULL;
288
 
}
289
 
 
290
 
PJ_DEF(pj_hash_iterator_t*) pj_hash_next( pj_hash_table_t *ht, 
291
 
                                          pj_hash_iterator_t *it )
292
 
{
293
 
    it->entry = it->entry->next;
294
 
    if (it->entry) {
295
 
        return it;
296
 
    }
297
 
 
298
 
    for (++it->index; it->index <= ht->rows; ++it->index) {
299
 
        it->entry = ht->table[it->index];
300
 
        if (it->entry) {
301
 
            break;
302
 
        }
303
 
    }
304
 
 
305
 
    return it->entry ? it : NULL;
306
 
}
307
 
 
308
 
PJ_DEF(void*) pj_hash_this( pj_hash_table_t *ht, pj_hash_iterator_t *it )
309
 
{
310
 
    PJ_CHECK_STACK();
311
 
    PJ_UNUSED_ARG(ht);
312
 
    return it->entry->value;
313
 
}
314
 
 
315
 
#if 0
316
 
void pj_hash_dump_collision( pj_hash_table_t *ht )
317
 
{
318
 
    unsigned min=0xFFFFFFFF, max=0;
319
 
    unsigned i;
320
 
    char line[120];
321
 
    int len, totlen = 0;
322
 
 
323
 
    for (i=0; i<=ht->rows; ++i) {
324
 
        unsigned count = 0;    
325
 
        pj_hash_entry *entry = ht->table[i];
326
 
        while (entry) {
327
 
            ++count;
328
 
            entry = entry->next;
329
 
        }
330
 
        if (count < min)
331
 
            min = count;
332
 
        if (count > max)
333
 
            max = count;
334
 
        len = pj_snprintf( line+totlen, sizeof(line)-totlen, "%3d:%3d ", i, count);
335
 
        if (len < 1)
336
 
            break;
337
 
        totlen += len;
338
 
 
339
 
        if ((i+1) % 10 == 0) {
340
 
            line[totlen] = '\0';
341
 
            PJ_LOG(4,(__FILE__, line));
342
 
        }
343
 
    }
344
 
 
345
 
    PJ_LOG(4,(__FILE__,"Count: %d, min: %d, max: %d\n", ht->count, min, max));
346
 
}
347
 
#endif
348
 
 
349