stmm.c 14.5 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10
/*
 * Revision Control Information
 *
 * /projects/hsis/CVS/utilities/st/st.c,v
 * serdar
 * 1.1
 * 1993/07/29 01:00:13
 *
 */
#include <stdio.h>
Alan Mishchenko committed
11
#include "extra.h"
Alan Mishchenko committed
12
#include "stmm.h"
Alan Mishchenko committed
13
#include "port_type.h"
Alan Mishchenko committed
14

Alan Mishchenko committed
15 16 17 18
#ifndef ABS
#  define ABS(a)            ((a) < 0 ? -(a) : (a))
#endif

Alan Mishchenko committed
19 20
#define STMM_NUMCMP(x,y) ((x) != (y))
#define STMM_NUMHASH(x,size) (ABS((long)x)%(size))
Alan Mishchenko committed
21 22
//#define STMM_PTRHASH(x,size) ((int)((PORT_PTRUINT_T)(x)>>2)%size) //  64-bit bug fix 9/17/2007
#define STMM_PTRHASH(x,size) ((int)(((PORT_PTRUINT_T)(x)>>2)%size))
Alan Mishchenko committed
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
#define EQUAL(func, x, y) \
    ((((func) == stmm_numcmp) || ((func) == stmm_ptrcmp)) ?\
      (STMM_NUMCMP((x),(y)) == 0) : ((*func)((x), (y)) == 0))


#define do_hash(key, table)\
    ((table->hash == stmm_ptrhash) ? STMM_PTRHASH((key),(table)->num_bins) :\
     (table->hash == stmm_numhash) ? STMM_NUMHASH((key), (table)->num_bins) :\
     (*table->hash)((key), (table)->num_bins))

static int rehash ();
int stmm_numhash (), stmm_ptrhash (), stmm_numcmp (), stmm_ptrcmp ();

stmm_table *
stmm_init_table_with_params (compare, hash, size, density, grow_factor,
                 reorder_flag)
    int (*compare) ();
    int (*hash) ();
    int size;
    int density;
    double grow_factor;
    int reorder_flag;
{
    int i;
    stmm_table *new;

    new = ALLOC (stmm_table, 1);
Alan Mishchenko committed
50 51
    if (new == NULL) {
    return NULL;
Alan Mishchenko committed
52 53 54 55 56 57 58 59 60 61 62 63
    }
    new->compare = compare;
    new->hash = hash;
    new->num_entries = 0;
    new->max_density = density;
    new->grow_factor = grow_factor;
    new->reorder_flag = reorder_flag;
    if (size <= 0) {
    size = 1;
    }
    new->num_bins = size;
    new->bins = ALLOC (stmm_table_entry *, size);
Alan Mishchenko committed
64
    if (new->bins == NULL) {
Alan Mishchenko committed
65
    FREE (new);
Alan Mishchenko committed
66
    return NULL;
Alan Mishchenko committed
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
    }
    for (i = 0; i < size; i++) {
    new->bins[i] = 0;
    }

    // added by alanmi
    new->pMemMan = Extra_MmFixedStart(sizeof (stmm_table_entry));
    return new;
}

stmm_table *
stmm_init_table (compare, hash)
    int (*compare) ();
    int (*hash) ();
{
    return stmm_init_table_with_params (compare, hash,
                    STMM_DEFAULT_INIT_TABLE_SIZE,
                    STMM_DEFAULT_MAX_DENSITY,
                    STMM_DEFAULT_GROW_FACTOR,
                    STMM_DEFAULT_REORDER_FLAG);
}

void
stmm_free_table (table)
    stmm_table *table;
{
/*
    register stmm_table_entry *ptr, *next;
    int i;
    for ( i = 0; i < table->num_bins; i++ )
    {
        ptr = table->bins[i];
Alan Mishchenko committed
99
        while ( ptr != NULL )
Alan Mishchenko committed
100 101 102 103 104 105 106 107 108 109
        {
            next = ptr->next;
            FREE( ptr );
            ptr = next;
        }
    }
*/
    // no need to deallocate entries because they are in the memory manager now
    // added by alanmi
    if ( table->pMemMan )
Alan Mishchenko committed
110
        Extra_MmFixedStop (table->pMemMan);
Alan Mishchenko committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    FREE (table->bins);
    FREE (table);
}

// this function recycles all the bins
void
stmm_clean (table)
    stmm_table *table;
{
    int i;
    // clean the bins
    for (i = 0; i < table->num_bins; i++)
        table->bins[i] = NULL;
    // reset the parameters
    table->num_entries = 0;
    // restart the memory manager
    Extra_MmFixedRestart (table->pMemMan);
}


#define PTR_NOT_EQUAL(table, ptr, user_key)\
Alan Mishchenko committed
132
(ptr != NULL && !EQUAL(table->compare, user_key, (ptr)->key))
Alan Mishchenko committed
133 134 135 136 137 138 139

#define FIND_ENTRY(table, hash_val, key, ptr, last) \
    (last) = &(table)->bins[hash_val];\
    (ptr) = *(last);\
    while (PTR_NOT_EQUAL((table), (ptr), (key))) {\
    (last) = &(ptr)->next; (ptr) = *(last);\
    }\
Alan Mishchenko committed
140
    if ((ptr) != NULL && (table)->reorder_flag) {\
Alan Mishchenko committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    *(last) = (ptr)->next;\
    (ptr)->next = (table)->bins[hash_val];\
    (table)->bins[hash_val] = (ptr);\
    }

int
stmm_lookup (table, key, value)
    stmm_table *table;
    register char *key;
    char **value;
{
    int hash_val;
    register stmm_table_entry *ptr, **last;

    hash_val = do_hash (key, table);

    FIND_ENTRY (table, hash_val, key, ptr, last);

Alan Mishchenko committed
159
    if (ptr == NULL) {
Alan Mishchenko committed
160 161 162
    return 0;
    }
    else {
Alan Mishchenko committed
163
    if (value != NULL)
Alan Mishchenko committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    {
        *value = ptr->record;
    }
    return 1;
    }
}

int
stmm_lookup_int (table, key, value)
    stmm_table *table;
    register char *key;
    int *value;
{
    int hash_val;
    register stmm_table_entry *ptr, **last;

    hash_val = do_hash (key, table);

    FIND_ENTRY (table, hash_val, key, ptr, last);

Alan Mishchenko committed
184
    if (ptr == NULL) {
Alan Mishchenko committed
185 186 187
    return 0;
    }
    else {
Alan Mishchenko committed
188
    if (value != 0)
Alan Mishchenko committed
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
    {
        *value = (long) ptr->record;
    }
    return 1;
    }
}

// This macro contained a line
//    new = ALLOC(stmm_table_entry, 1);
// which was modified by alanmi


/* This macro does not check if memory allocation fails. Use at you own risk */
#define ADD_DIRECT(table, key, value, hash_val, new)\
{\
    if (table->num_entries/table->num_bins >= table->max_density) {\
    rehash(table);\
    hash_val = do_hash(key,table);\
    }\
    \
    new = (stmm_table_entry *)Extra_MmFixedEntryFetch( table->pMemMan );\
    \
    new->key = key;\
    new->record = value;\
    new->next = table->bins[hash_val];\
    table->bins[hash_val] = new;\
    table->num_entries++;\
}

int
stmm_insert (table, key, value)
    register stmm_table *table;
    register char *key;
    char *value;
{
    int hash_val;
    stmm_table_entry *new;
    register stmm_table_entry *ptr, **last;

    hash_val = do_hash (key, table);

    FIND_ENTRY (table, hash_val, key, ptr, last);

Alan Mishchenko committed
232
    if (ptr == NULL) {
Alan Mishchenko committed
233 234 235 236 237 238 239 240 241
    if (table->num_entries / table->num_bins >= table->max_density) {
        if (rehash (table) == STMM_OUT_OF_MEM) {
        return STMM_OUT_OF_MEM;
        }
        hash_val = do_hash (key, table);
    }

//              new = ALLOC( stmm_table_entry, 1 );
    new = (stmm_table_entry *) Extra_MmFixedEntryFetch (table->pMemMan);
Alan Mishchenko committed
242
    if (new == NULL) {
Alan Mishchenko committed
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
        return STMM_OUT_OF_MEM;
    }

    new->key = key;
    new->record = value;
    new->next = table->bins[hash_val];
    table->bins[hash_val] = new;
    table->num_entries++;
    return 0;
    }
    else {
    ptr->record = value;
    return 1;
    }
}

int
stmm_add_direct (table, key, value)
    stmm_table *table;
    char *key;
    char *value;
{
    int hash_val;
    stmm_table_entry *new;

    hash_val = do_hash (key, table);
    if (table->num_entries / table->num_bins >= table->max_density) {
    if (rehash (table) == STMM_OUT_OF_MEM) {
        return STMM_OUT_OF_MEM;
    }
    }
    hash_val = do_hash (key, table);

//      new = ALLOC( stmm_table_entry, 1 );
    new = (stmm_table_entry *) Extra_MmFixedEntryFetch (table->pMemMan);
Alan Mishchenko committed
278
    if (new == NULL) {
Alan Mishchenko committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    return STMM_OUT_OF_MEM;
    }

    new->key = key;
    new->record = value;
    new->next = table->bins[hash_val];
    table->bins[hash_val] = new;
    table->num_entries++;
    return 1;
}

int
stmm_find_or_add (table, key, slot)
    stmm_table *table;
    char *key;
    char ***slot;
{
    int hash_val;
    stmm_table_entry *new, *ptr, **last;

    hash_val = do_hash (key, table);

    FIND_ENTRY (table, hash_val, key, ptr, last);

Alan Mishchenko committed
303
    if (ptr == NULL) {
Alan Mishchenko committed
304 305 306 307 308 309 310 311 312
    if (table->num_entries / table->num_bins >= table->max_density) {
        if (rehash (table) == STMM_OUT_OF_MEM) {
        return STMM_OUT_OF_MEM;
        }
        hash_val = do_hash (key, table);
    }

    // new = ALLOC( stmm_table_entry, 1 );
    new = (stmm_table_entry *) Extra_MmFixedEntryFetch (table->pMemMan);
Alan Mishchenko committed
313
    if (new == NULL) {
Alan Mishchenko committed
314 315 316 317 318 319 320 321
        return STMM_OUT_OF_MEM;
    }

    new->key = key;
    new->record = (char *) 0;
    new->next = table->bins[hash_val];
    table->bins[hash_val] = new;
    table->num_entries++;
Alan Mishchenko committed
322
    if (slot != NULL)
Alan Mishchenko committed
323 324 325 326
         *slot = &new->record;
    return 0;
    }
    else {
Alan Mishchenko committed
327
    if (slot != NULL)
Alan Mishchenko committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
         *slot = &ptr->record;
    return 1;
    }
}

int
stmm_find (table, key, slot)
    stmm_table *table;
    char *key;
    char ***slot;
{
    int hash_val;
    stmm_table_entry *ptr, **last;

    hash_val = do_hash (key, table);

    FIND_ENTRY (table, hash_val, key, ptr, last);

Alan Mishchenko committed
346
    if (ptr == NULL) {
Alan Mishchenko committed
347 348 349
    return 0;
    }
    else {
Alan Mishchenko committed
350
    if (slot != NULL)
Alan Mishchenko committed
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
    {
        *slot = &ptr->record;
    }
    return 1;
    }
}

static int
rehash (table)
    register stmm_table *table;
{
    register stmm_table_entry *ptr, *next, **old_bins;
    int i, old_num_bins, hash_val, old_num_entries;

    /* save old values */
    old_bins = table->bins;
    old_num_bins = table->num_bins;
    old_num_entries = table->num_entries;

    /* rehash */
    table->num_bins = (int) (table->grow_factor * old_num_bins);
    if (table->num_bins % 2 == 0) {
    table->num_bins += 1;
    }
    table->num_entries = 0;
    table->bins = ALLOC (stmm_table_entry *, table->num_bins);
Alan Mishchenko committed
377
    if (table->bins == NULL) {
Alan Mishchenko committed
378 379 380 381 382 383 384 385 386 387 388 389 390
    table->bins = old_bins;
    table->num_bins = old_num_bins;
    table->num_entries = old_num_entries;
    return STMM_OUT_OF_MEM;
    }
    /* initialize */
    for (i = 0; i < table->num_bins; i++) {
    table->bins[i] = 0;
    }

    /* copy data over */
    for (i = 0; i < old_num_bins; i++) {
    ptr = old_bins[i];
Alan Mishchenko committed
391
    while (ptr != NULL) {
Alan Mishchenko committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
        next = ptr->next;
        hash_val = do_hash (ptr->key, table);
        ptr->next = table->bins[hash_val];
        table->bins[hash_val] = ptr;
        table->num_entries++;
        ptr = next;
    }
    }
    FREE (old_bins);

    return 1;
}

stmm_table *
stmm_copy (old_table)
    stmm_table *old_table;
{
    stmm_table *new_table;
    stmm_table_entry *ptr, /* *newptr, *next, */ *new;
    int i, /*j, */ num_bins = old_table->num_bins;

    new_table = ALLOC (stmm_table, 1);
Alan Mishchenko committed
414 415
    if (new_table == NULL) {
    return NULL;
Alan Mishchenko committed
416 417 418 419
    }

    *new_table = *old_table;
    new_table->bins = ALLOC (stmm_table_entry *, num_bins);
Alan Mishchenko committed
420
    if (new_table->bins == NULL) {
Alan Mishchenko committed
421
    FREE (new_table);
Alan Mishchenko committed
422
    return NULL;
Alan Mishchenko committed
423 424 425 426 427 428 429
    }

    // allocate the memory manager for the new table
    new_table->pMemMan =
    Extra_MmFixedStart (sizeof (stmm_table_entry));

    for (i = 0; i < num_bins; i++) {
Alan Mishchenko committed
430
    new_table->bins[i] = NULL;
Alan Mishchenko committed
431
    ptr = old_table->bins[i];
Alan Mishchenko committed
432
    while (ptr != NULL) {
Alan Mishchenko committed
433 434 435 436 437
//                      new = ALLOC( stmm_table_entry, 1 );
        new =
        (stmm_table_entry *) Extra_MmFixedEntryFetch (new_table->
                                pMemMan);

Alan Mishchenko committed
438
        if (new == NULL) {
Alan Mishchenko committed
439 440 441 442
/*
                for ( j = 0; j <= i; j++ )
                {
                    newptr = new_table->bins[j];
Alan Mishchenko committed
443
                    while ( newptr != NULL )
Alan Mishchenko committed
444 445 446 447 448 449 450
                    {
                        next = newptr->next;
                        FREE( newptr );
                        newptr = next;
                    }
                }
*/
Alan Mishchenko committed
451
        Extra_MmFixedStop (new_table->pMemMan);
Alan Mishchenko committed
452 453 454

        FREE (new_table->bins);
        FREE (new_table);
Alan Mishchenko committed
455
        return NULL;
Alan Mishchenko committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
        }
        *new = *ptr;
        new->next = new_table->bins[i];
        new_table->bins[i] = new;
        ptr = ptr->next;
    }
    }
    return new_table;
}

int
stmm_delete (table, keyp, value)
    register stmm_table *table;
    register char **keyp;
    char **value;
{
    int hash_val;
    char *key = *keyp;
    register stmm_table_entry *ptr, **last;

    hash_val = do_hash (key, table);

    FIND_ENTRY (table, hash_val, key, ptr, last);

Alan Mishchenko committed
480
    if (ptr == NULL) {
Alan Mishchenko committed
481 482 483 484
    return 0;
    }

    *last = ptr->next;
Alan Mishchenko committed
485
    if (value != NULL)
Alan Mishchenko committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
     *value = ptr->record;
    *keyp = ptr->key;
//      FREE( ptr );
    Extra_MmFixedEntryRecycle (table->pMemMan, (char *) ptr);

    table->num_entries--;
    return 1;
}

int
stmm_delete_int (table, keyp, value)
    register stmm_table *table;
    register long *keyp;
    char **value;
{
    int hash_val;
    char *key = (char *) *keyp;
    register stmm_table_entry *ptr, **last;

    hash_val = do_hash (key, table);

    FIND_ENTRY (table, hash_val, key, ptr, last);

Alan Mishchenko committed
509
    if (ptr == NULL) {
Alan Mishchenko committed
510 511 512 513
    return 0;
    }

    *last = ptr->next;
Alan Mishchenko committed
514
    if (value != NULL)
Alan Mishchenko committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
     *value = ptr->record;
    *keyp = (long) ptr->key;
//      FREE( ptr );
    Extra_MmFixedEntryRecycle (table->pMemMan, (char *) ptr);

    table->num_entries--;
    return 1;
}

int
stmm_foreach (table, func, arg)
    stmm_table *table;
    enum stmm_retval (*func) ();
    char *arg;
{
    stmm_table_entry *ptr, **last;
    enum stmm_retval retval;
    int i;

    for (i = 0; i < table->num_bins; i++) {
    last = &table->bins[i];
    ptr = *last;
Alan Mishchenko committed
537
    while (ptr != NULL) {
Alan Mishchenko committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
        retval = (*func) (ptr->key, ptr->record, arg);
        switch (retval) {
        case STMM_CONTINUE:
        last = &ptr->next;
        ptr = *last;
        break;
        case STMM_STOP:
        return 0;
        case STMM_DELETE:
        *last = ptr->next;
        table->num_entries--;    /* cstevens@ic */
//                              FREE( ptr );
        Extra_MmFixedEntryRecycle (table->pMemMan, (char *) ptr);

        ptr = *last;
        }
    }
    }
    return 1;
}

int
stmm_strhash (string, modulus)
    register char *string;
    int modulus;
{
    register int val = 0;
    register int c;

    while ((c = *string++) != '\0') {
    val = val * 997 + c;
    }

    return ((val < 0) ? -val : val) % modulus;
}

int
stmm_numhash (x, size)
    char *x;
    int size;
{
    return STMM_NUMHASH (x, size);
}

int
stmm_ptrhash (x, size)
    char *x;
    int size;
{
    return STMM_PTRHASH (x, size);
}

int
stmm_numcmp (x, y)
    char *x;
    char *y;
{
    return STMM_NUMCMP (x, y);
}

int
stmm_ptrcmp (x, y)
    char *x;
    char *y;
{
    return STMM_NUMCMP (x, y);
}

stmm_generator *
stmm_init_gen (table)
    stmm_table *table;
{
    stmm_generator *gen;

    gen = ALLOC (stmm_generator, 1);
Alan Mishchenko committed
613 614
    if (gen == NULL) {
    return NULL;
Alan Mishchenko committed
615 616
    }
    gen->table = table;
Alan Mishchenko committed
617
    gen->entry = NULL;
Alan Mishchenko committed
618 619 620 621 622 623 624 625 626 627 628 629 630
    gen->index = 0;
    return gen;
}


int
stmm_gen (gen, key_p, value_p)
    stmm_generator *gen;
    char **key_p;
    char **value_p;
{
    register int i;

Alan Mishchenko committed
631
    if (gen->entry == NULL) {
Alan Mishchenko committed
632 633
    /* try to find next entry */
    for (i = gen->index; i < gen->table->num_bins; i++) {
Alan Mishchenko committed
634
        if (gen->table->bins[i] != NULL) {
Alan Mishchenko committed
635 636 637 638 639
        gen->index = i + 1;
        gen->entry = gen->table->bins[i];
        break;
        }
    }
Alan Mishchenko committed
640
    if (gen->entry == NULL) {
Alan Mishchenko committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
        return 0;        /* that's all folks ! */
    }
    }
    *key_p = gen->entry->key;
    if (value_p != 0) {
    *value_p = gen->entry->record;
    }
    gen->entry = gen->entry->next;
    return 1;
}


int
stmm_gen_int (gen, key_p, value_p)
    stmm_generator *gen;
    char **key_p;
    long *value_p;
{
    register int i;

Alan Mishchenko committed
661
    if (gen->entry == NULL) {
Alan Mishchenko committed
662 663
    /* try to find next entry */
    for (i = gen->index; i < gen->table->num_bins; i++) {
Alan Mishchenko committed
664
        if (gen->table->bins[i] != NULL) {
Alan Mishchenko committed
665 666 667 668 669
        gen->index = i + 1;
        gen->entry = gen->table->bins[i];
        break;
        }
    }
Alan Mishchenko committed
670
    if (gen->entry == NULL) {
Alan Mishchenko committed
671 672 673 674
        return 0;        /* that's all folks ! */
    }
    }
    *key_p = gen->entry->key;
Alan Mishchenko committed
675
    if (value_p != 0)
Alan Mishchenko committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689
    {
    *value_p = (long) gen->entry->record;
    }
    gen->entry = gen->entry->next;
    return 1;
}


void
stmm_free_gen (gen)
    stmm_generator *gen;
{
    FREE (gen);
}