cuddLCache.c 41.2 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9
/**CFile***********************************************************************

  FileName    [cuddLCache.c]

  PackageName [cudd]

  Synopsis    [Functions for local caches.]

  Description [Internal procedures included in this module:
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
                <ul>
                <li> cuddLocalCacheInit()
                <li> cuddLocalCacheQuit()
                <li> cuddLocalCacheInsert()
                <li> cuddLocalCacheLookup()
                <li> cuddLocalCacheClearDead()
                <li> cuddLocalCacheClearAll()
                <li> cuddLocalCacheProfile()
                <li> cuddHashTableInit()
                <li> cuddHashTableQuit()
                <li> cuddHashTableInsert()
                <li> cuddHashTableLookup()
                <li> cuddHashTableInsert2()
                <li> cuddHashTableLookup2()
                <li> cuddHashTableInsert3()
                <li> cuddHashTableLookup3()
                </ul>
            Static procedures included in this module:
                <ul>
                <li> cuddLocalCacheResize()
                <li> ddLCHash()
                <li> cuddLocalCacheAddToList()
                <li> cuddLocalCacheRemoveFromList()
                <li> cuddHashTableResize()
                <li> cuddHashTableAlloc()
                </ul> ]
Alan Mishchenko committed
36 37 38 39 40

  SeeAlso     []

  Author      [Fabio Somenzi]

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
  Copyright   [Copyright (c) 1995-2004, Regents of the University of Colorado

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither the name of the University of Colorado nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.]
Alan Mishchenko committed
72 73 74

******************************************************************************/

75
#include "misc/util/util_hack.h"
76
#include "cuddInt.h"
Alan Mishchenko committed
77

78 79 80
ABC_NAMESPACE_IMPL_START


81

Alan Mishchenko committed
82 83 84 85
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/

86
#define DD_MAX_HASHTABLE_DENSITY 2      /* tells when to resize a table */
Alan Mishchenko committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

#ifndef lint
103
static char rcsid[] DD_UNUSED = "$Id: cuddLCache.c,v 1.24 2009/03/08 02:49:02 fabio Exp $";
Alan Mishchenko committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#endif

/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/

/**Macro***********************************************************************

  Synopsis    [Computes hash function for keys of two operands.]

  Description []

  SideEffects [None]

  SeeAlso     [ddLCHash3 ddLCHash]

******************************************************************************/
#if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
#define ddLCHash2(f,g,shift) \
123 124
((((unsigned)(ptruint)(f) * DD_P1 + \
   (unsigned)(ptruint)(g)) * DD_P2) >> (shift))
Alan Mishchenko committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#else
#define ddLCHash2(f,g,shift) \
((((unsigned)(f) * DD_P1 + (unsigned)(g)) * DD_P2) >> (shift))
#endif


/**Macro***********************************************************************

  Synopsis    [Computes hash function for keys of three operands.]

  Description []

  SideEffects [None]

  SeeAlso     [ddLCHash2 ddLCHash]

******************************************************************************/
142
#define ddLCHash3(f,g,h,shift)  ddCHash2(f,g,h,shift)
Alan Mishchenko committed
143 144 145 146 147 148 149 150


/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

151 152 153 154 155 156
static void cuddLocalCacheResize (DdLocalCache *cache);
DD_INLINE static unsigned int ddLCHash (DdNodePtr *key, unsigned int keysize, int shift);
static void cuddLocalCacheAddToList (DdLocalCache *cache);
static void cuddLocalCacheRemoveFromList (DdLocalCache *cache);
static int cuddHashTableResize (DdHashTable *hash);
DD_INLINE static DdHashItem * cuddHashTableAlloc (DdHashTable *hash);
Alan Mishchenko committed
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

/**AutomaticEnd***************************************************************/


/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Initializes a local computed table.]

  Description [Initializes a computed table.  Returns a pointer the
  the new local cache in case of success; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [cuddInitCache]

******************************************************************************/
DdLocalCache *
cuddLocalCacheInit(
  DdManager * manager /* manager */,
  unsigned int  keySize /* size of the key (number of operands) */,
  unsigned int  cacheSize /* Initial size of the cache */,
  unsigned int  maxCacheSize /* Size of the cache beyond which no resizing occurs */)
{
    DdLocalCache *cache;
    int logSize;

Alan Mishchenko committed
192
    cache = ABC_ALLOC(DdLocalCache,1);
Alan Mishchenko committed
193
    if (cache == NULL) {
194 195
        manager->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
196 197 198 199 200 201 202 203 204 205
    }
    cache->manager = manager;
    cache->keysize = keySize;
    cache->itemsize = (keySize + 1) * sizeof(DdNode *);
#ifdef DD_CACHE_PROFILE
    cache->itemsize += sizeof(ptrint);
#endif
    logSize = cuddComputeFloorLog2(ddMax(cacheSize,manager->slots/2));
    cacheSize = 1 << logSize;
    cache->item = (DdLocalCacheItem *)
206
        ABC_ALLOC(char, cacheSize * cache->itemsize);
Alan Mishchenko committed
207
    if (cache->item == NULL) {
208 209 210
        manager->errorCode = CUDD_MEMORY_OUT;
        ABC_FREE(cache);
        return(NULL);
Alan Mishchenko committed
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
    }
    cache->slots = cacheSize;
    cache->shift = sizeof(int) * 8 - logSize;
    cache->maxslots = ddMin(maxCacheSize,manager->slots);
    cache->minHit = manager->minHit;
    /* Initialize to avoid division by 0 and immediate resizing. */
    cache->lookUps = (double) (int) (cacheSize * cache->minHit + 1);
    cache->hits = 0;
    manager->memused += cacheSize * cache->itemsize + sizeof(DdLocalCache);

    /* Initialize the cache. */
    memset(cache->item, 0, cacheSize * cache->itemsize);

    /* Add to manager's list of local caches for GC. */
    cuddLocalCacheAddToList(cache);

    return(cache);

} /* end of cuddLocalCacheInit */


/**Function********************************************************************

  Synopsis    [Shuts down a local computed table.]

  Description [Initializes the computed table. It is called by
  Cudd_Init. Returns a pointer the the new local cache in case of
  success; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [cuddLocalCacheInit]

******************************************************************************/
void
cuddLocalCacheQuit(
  DdLocalCache * cache /* cache to be shut down */)
{
    cache->manager->memused -=
250
        cache->slots * cache->itemsize + sizeof(DdLocalCache);
Alan Mishchenko committed
251
    cuddLocalCacheRemoveFromList(cache);
Alan Mishchenko committed
252 253
    ABC_FREE(cache->item);
    ABC_FREE(cache);
Alan Mishchenko committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

    return;

} /* end of cuddLocalCacheQuit */


/**Function********************************************************************

  Synopsis    [Inserts a result in a local cache.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
void
cuddLocalCacheInsert(
  DdLocalCache * cache,
  DdNodePtr * key,
  DdNode * value)
{
    unsigned int posn;
    DdLocalCacheItem *entry;

    posn = ddLCHash(key,cache->keysize,cache->shift);
    entry = (DdLocalCacheItem *) ((char *) cache->item +
282
                                  posn * cache->itemsize);
Alan Mishchenko committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    memcpy(entry->key,key,cache->keysize * sizeof(DdNode *));
    entry->value = value;
#ifdef DD_CACHE_PROFILE
    entry->count++;
#endif

} /* end of cuddLocalCacheInsert */


/**Function********************************************************************

  Synopsis [Looks up in a local cache.]

  Description [Looks up in a local cache. Returns the result if found;
  it returns NULL if no result is found.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
DdNode *
cuddLocalCacheLookup(
  DdLocalCache * cache,
  DdNodePtr * key)
{
    unsigned int posn;
    DdLocalCacheItem *entry;
    DdNode *value;

    cache->lookUps++;
    posn = ddLCHash(key,cache->keysize,cache->shift);
    entry = (DdLocalCacheItem *) ((char *) cache->item +
316
                                  posn * cache->itemsize);
Alan Mishchenko committed
317
    if (entry->value != NULL &&
318 319 320 321 322 323 324
        memcmp(key,entry->key,cache->keysize*sizeof(DdNode *)) == 0) {
        cache->hits++;
        value = Cudd_Regular(entry->value);
        if (value->ref == 0) {
            cuddReclaim(cache->manager,value);
        }
        return(entry->value);
Alan Mishchenko committed
325 326 327 328 329
    }

    /* Cache miss: decide whether to resize */

    if (cache->slots < cache->maxslots &&
330 331
        cache->hits > cache->lookUps * cache->minHit) {
        cuddLocalCacheResize(cache);
Alan Mishchenko committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    }

    return(NULL);

} /* end of cuddLocalCacheLookup */


/**Function********************************************************************

  Synopsis [Clears the dead entries of the local caches of a manager.]

  Description [Clears the dead entries of the local caches of a manager.
  Used during garbage collection.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
void
cuddLocalCacheClearDead(
  DdManager * manager)
{
    DdLocalCache *cache = manager->localCaches;
    unsigned int keysize;
    unsigned int itemsize;
    unsigned int slots;
    DdLocalCacheItem *item;
    DdNodePtr *key;
    unsigned int i, j;

    while (cache != NULL) {
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        keysize = cache->keysize;
        itemsize = cache->itemsize;
        slots = cache->slots;
        item = cache->item;
        for (i = 0; i < slots; i++) {
            if (item->value != NULL) {
                if (Cudd_Regular(item->value)->ref == 0) {
                    item->value = NULL;
                } else {
                    key = item->key;
                    for (j = 0; j < keysize; j++) {
                        if (Cudd_Regular(key[j])->ref == 0) {
                            item->value = NULL;
                            break;
                        }
                    }
                }
Alan Mishchenko committed
381
            }
382
            item = (DdLocalCacheItem *) ((char *) item + itemsize);
Alan Mishchenko committed
383
        }
384
        cache = cache->next;
Alan Mishchenko committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    }
    return;

} /* end of cuddLocalCacheClearDead */


/**Function********************************************************************

  Synopsis [Clears the local caches of a manager.]

  Description [Clears the local caches of a manager.
  Used before reordering.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
void
cuddLocalCacheClearAll(
  DdManager * manager)
{
    DdLocalCache *cache = manager->localCaches;

    while (cache != NULL) {
410 411
        memset(cache->item, 0, cache->slots * cache->itemsize);
        cache = cache->next;
Alan Mishchenko committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    }
    return;

} /* end of cuddLocalCacheClearAll */


#ifdef DD_CACHE_PROFILE

#define DD_HYSTO_BINS 8

/**Function********************************************************************

  Synopsis    [Computes and prints a profile of a local cache usage.]

  Description [Computes and prints a profile of a local cache usage.
  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
cuddLocalCacheProfile(
  DdLocalCache * cache)
{
    double count, mean, meansq, stddev, expected;
    long max, min;
    int imax, imin;
    int i, retval, slots;
    long *hystogram;
    int nbins = DD_HYSTO_BINS;
    int bin;
    long thiscount;
    double totalcount;
    int nzeroes;
    DdLocalCacheItem *entry;
    FILE *fp = cache->manager->out;

    slots = cache->slots;

    meansq = mean = expected = 0.0;
    max = min = (long) cache->item[0].count;
    imax = imin = nzeroes = 0;
    totalcount = 0.0;

Alan Mishchenko committed
458
    hystogram = ABC_ALLOC(long, nbins);
Alan Mishchenko committed
459
    if (hystogram == NULL) {
460
        return(0);
Alan Mishchenko committed
461 462
    }
    for (i = 0; i < nbins; i++) {
463
        hystogram[i] = 0;
Alan Mishchenko committed
464 465 466
    }

    for (i = 0; i < slots; i++) {
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
        entry = (DdLocalCacheItem *) ((char *) cache->item +
                                      i * cache->itemsize);
        thiscount = (long) entry->count;
        if (thiscount > max) {
            max = thiscount;
            imax = i;
        }
        if (thiscount < min) {
            min = thiscount;
            imin = i;
        }
        if (thiscount == 0) {
            nzeroes++;
        }
        count = (double) thiscount;
        mean += count;
        meansq += count * count;
        totalcount += count;
        expected += count * (double) i;
        bin = (i * nbins) / slots;
        hystogram[bin] += thiscount;
Alan Mishchenko committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    }
    mean /= (double) slots;
    meansq /= (double) slots;
    stddev = sqrt(meansq - mean*mean);

    retval = fprintf(fp,"Cache stats: slots = %d average = %g ", slots, mean);
    if (retval == EOF) return(0);
    retval = fprintf(fp,"standard deviation = %g\n", stddev);
    if (retval == EOF) return(0);
    retval = fprintf(fp,"Cache max accesses = %ld for slot %d\n", max, imax);
    if (retval == EOF) return(0);
    retval = fprintf(fp,"Cache min accesses = %ld for slot %d\n", min, imin);
    if (retval == EOF) return(0);
    retval = fprintf(fp,"Cache unused slots = %d\n", nzeroes);
    if (retval == EOF) return(0);

    if (totalcount) {
505 506 507 508 509 510 511 512 513 514
        expected /= totalcount;
        retval = fprintf(fp,"Cache access hystogram for %d bins", nbins);
        if (retval == EOF) return(0);
        retval = fprintf(fp," (expected bin value = %g)\n# ", expected);
        if (retval == EOF) return(0);
        for (i = nbins - 1; i>=0; i--) {
            retval = fprintf(fp,"%ld ", hystogram[i]);
            if (retval == EOF) return(0);
        }
        retval = fprintf(fp,"\n");
Alan Mishchenko committed
515 516 517
        if (retval == EOF) return(0);
    }

Alan Mishchenko committed
518
    ABC_FREE(hystogram);
Alan Mishchenko committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
    return(1);

} /* end of cuddLocalCacheProfile */
#endif


/**Function********************************************************************

  Synopsis    [Initializes a hash table.]

  Description [Initializes a hash table. Returns a pointer to the new
  table if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [cuddHashTableQuit]

******************************************************************************/
DdHashTable *
cuddHashTableInit(
  DdManager * manager,
  unsigned int  keySize,
  unsigned int  initSize)
{
    DdHashTable *hash;
    int logSize;

#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
Alan Mishchenko committed
550
    hash = ABC_ALLOC(DdHashTable, 1);
Alan Mishchenko committed
551
    if (hash == NULL) {
552 553
        manager->errorCode = CUDD_MEMORY_OUT;
        return(NULL);
Alan Mishchenko committed
554 555 556 557 558 559
    }
    hash->keysize = keySize;
    hash->manager = manager;
    hash->memoryList = NULL;
    hash->nextFree = NULL;
    hash->itemsize = (keySize + 1) * sizeof(DdNode *) +
560
        sizeof(ptrint) + sizeof(DdHashItem *);
Alan Mishchenko committed
561 562 563 564 565
    /* We have to guarantee that the shift be < 32. */
    if (initSize < 2) initSize = 2;
    logSize = cuddComputeFloorLog2(initSize);
    hash->numBuckets = 1 << logSize;
    hash->shift = sizeof(int) * 8 - logSize;
Alan Mishchenko committed
566
    hash->bucket = ABC_ALLOC(DdHashItem *, hash->numBuckets);
Alan Mishchenko committed
567
    if (hash->bucket == NULL) {
568 569 570
        manager->errorCode = CUDD_MEMORY_OUT;
        ABC_FREE(hash);
        return(NULL);
Alan Mishchenko committed
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
    }
    memset(hash->bucket, 0, hash->numBuckets * sizeof(DdHashItem *));
    hash->size = 0;
    hash->maxsize = hash->numBuckets * DD_MAX_HASHTABLE_DENSITY;
#ifdef __osf__
#pragma pointer_size restore
#endif
    return(hash);

} /* end of cuddHashTableInit */


/**Function********************************************************************

  Synopsis    [Shuts down a hash table.]

  Description [Shuts down a hash table, dereferencing all the values.]

  SideEffects [None]

  SeeAlso     [cuddHashTableInit]

******************************************************************************/
void
cuddHashTableQuit(
  DdHashTable * hash)
{
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
    unsigned int i;
    DdManager *dd = hash->manager;
    DdHashItem *bucket;
    DdHashItem **memlist, **nextmem;
    unsigned int numBuckets = hash->numBuckets;

    for (i = 0; i < numBuckets; i++) {
609 610 611 612 613
        bucket = hash->bucket[i];
        while (bucket != NULL) {
            Cudd_RecursiveDeref(dd, bucket->value);
            bucket = bucket->next;
        }
Alan Mishchenko committed
614 615 616 617
    }

    memlist = hash->memoryList;
    while (memlist != NULL) {
618 619 620
        nextmem = (DdHashItem **) memlist[0];
        ABC_FREE(memlist);
        memlist = nextmem;
Alan Mishchenko committed
621 622
    }

Alan Mishchenko committed
623 624
    ABC_FREE(hash->bucket);
    ABC_FREE(hash);
Alan Mishchenko committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
#ifdef __osf__
#pragma pointer_size restore
#endif

    return;

} /* end of cuddHashTableQuit */


/**Function********************************************************************

  Synopsis    [Inserts an item in a hash table.]

  Description [Inserts an item in a hash table when the key has more than
  three pointers.  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [[cuddHashTableInsert1 cuddHashTableInsert2 cuddHashTableInsert3
  cuddHashTableLookup]

******************************************************************************/
int
cuddHashTableInsert(
  DdHashTable * hash,
  DdNodePtr * key,
  DdNode * value,
  ptrint count)
{
    int result;
    unsigned int posn;
    DdHashItem *item;
    unsigned int i;

#ifdef DD_DEBUG
    assert(hash->keysize > 3);
#endif

    if (hash->size > hash->maxsize) {
664 665
        result = cuddHashTableResize(hash);
        if (result == 0) return(0);
Alan Mishchenko committed
666 667 668 669 670 671 672 673
    }
    item = cuddHashTableAlloc(hash);
    if (item == NULL) return(0);
    hash->size++;
    item->value = value;
    cuddRef(value);
    item->count = count;
    for (i = 0; i < hash->keysize; i++) {
674
        item->key[i] = key[i];
Alan Mishchenko committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
    }
    posn = ddLCHash(key,hash->keysize,hash->shift);
    item->next = hash->bucket[posn];
    hash->bucket[posn] = item;

    return(1);

} /* end of cuddHashTableInsert */


/**Function********************************************************************

  Synopsis    [Looks up a key in a hash table.]

  Description [Looks up a key consisting of more than three pointers
  in a hash table.  Returns the value associated to the key if there
  is an entry for the given key in the table; NULL otherwise. If the
  entry is present, its reference counter is decremented if not
  saturated. If the counter reaches 0, the value of the entry is
694
  dereferenced, and the entry is returned to the free list.]
Alan Mishchenko committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720

  SideEffects [None]

  SeeAlso     [cuddHashTableLookup1 cuddHashTableLookup2 cuddHashTableLookup3
  cuddHashTableInsert]

******************************************************************************/
DdNode *
cuddHashTableLookup(
  DdHashTable * hash,
  DdNodePtr * key)
{
    unsigned int posn;
    DdHashItem *item, *prev;
    unsigned int i, keysize;

#ifdef DD_DEBUG
    assert(hash->keysize > 3);
#endif

    posn = ddLCHash(key,hash->keysize,hash->shift);
    item = hash->bucket[posn];
    prev = NULL;

    keysize = hash->keysize;
    while (item != NULL) {
721 722 723 724 725 726 727
        DdNodePtr *key2 = item->key;
        int equal = 1;
        for (i = 0; i < keysize; i++) {
            if (key[i] != key2[i]) {
                equal = 0;
                break;
            }
Alan Mishchenko committed
728
        }
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
        if (equal) {
            DdNode *value = item->value;
            cuddSatDec(item->count);
            if (item->count == 0) {
                cuddDeref(value);
                if (prev == NULL) {
                    hash->bucket[posn] = item->next;
                } else {
                    prev->next = item->next;
                }
                item->next = hash->nextFree;
                hash->nextFree = item;
                hash->size--;
            }
            return(value);
Alan Mishchenko committed
744
        }
745 746
        prev = item;
        item = item->next;
Alan Mishchenko committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
    }
    return(NULL);

} /* end of cuddHashTableLookup */


/**Function********************************************************************

  Synopsis    [Inserts an item in a hash table.]

  Description [Inserts an item in a hash table when the key is one pointer.
  Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [cuddHashTableInsert cuddHashTableInsert2 cuddHashTableInsert3
  cuddHashTableLookup1]

******************************************************************************/
int
cuddHashTableInsert1(
  DdHashTable * hash,
  DdNode * f,
  DdNode * value,
  ptrint count)
{
    int result;
    unsigned int posn;
    DdHashItem *item;

#ifdef DD_DEBUG
    assert(hash->keysize == 1);
#endif

    if (hash->size > hash->maxsize) {
782 783
        result = cuddHashTableResize(hash);
        if (result == 0) return(0);
Alan Mishchenko committed
784 785 786 787 788 789 790 791
    }
    item = cuddHashTableAlloc(hash);
    if (item == NULL) return(0);
    hash->size++;
    item->value = value;
    cuddRef(value);
    item->count = count;
    item->key[0] = f;
792
    posn = ddLCHash2(cuddF2L(f),cuddF2L(f),hash->shift);
Alan Mishchenko committed
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
    item->next = hash->bucket[posn];
    hash->bucket[posn] = item;

    return(1);

} /* end of cuddHashTableInsert1 */


/**Function********************************************************************

  Synopsis    [Looks up a key consisting of one pointer in a hash table.]

  Description [Looks up a key consisting of one pointer in a hash table.
  Returns the value associated to the key if there is an entry for the given
  key in the table; NULL otherwise. If the entry is present, its reference
  counter is decremented if not saturated. If the counter reaches 0, the
809
  value of the entry is dereferenced, and the entry is returned to the free
Alan Mishchenko committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
  list.]

  SideEffects [None]

  SeeAlso     [cuddHashTableLookup cuddHashTableLookup2 cuddHashTableLookup3
  cuddHashTableInsert1]

******************************************************************************/
DdNode *
cuddHashTableLookup1(
  DdHashTable * hash,
  DdNode * f)
{
    unsigned int posn;
    DdHashItem *item, *prev;

#ifdef DD_DEBUG
    assert(hash->keysize == 1);
#endif

830
    posn = ddLCHash2(cuddF2L(f),cuddF2L(f),hash->shift);
Alan Mishchenko committed
831 832 833 834
    item = hash->bucket[posn];
    prev = NULL;

    while (item != NULL) {
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
        DdNodePtr *key = item->key;
        if (f == key[0]) {
            DdNode *value = item->value;
            cuddSatDec(item->count);
            if (item->count == 0) {
                cuddDeref(value);
                if (prev == NULL) {
                    hash->bucket[posn] = item->next;
                } else {
                    prev->next = item->next;
                }
                item->next = hash->nextFree;
                hash->nextFree = item;
                hash->size--;
            }
            return(value);
Alan Mishchenko committed
851
        }
852 853
        prev = item;
        item = item->next;
Alan Mishchenko committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
    }
    return(NULL);

} /* end of cuddHashTableLookup1 */


/**Function********************************************************************

  Synopsis    [Inserts an item in a hash table.]

  Description [Inserts an item in a hash table when the key is
  composed of two pointers. Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [cuddHashTableInsert cuddHashTableInsert1 cuddHashTableInsert3
  cuddHashTableLookup2]

******************************************************************************/
int
cuddHashTableInsert2(
  DdHashTable * hash,
  DdNode * f,
  DdNode * g,
  DdNode * value,
  ptrint count)
{
    int result;
    unsigned int posn;
    DdHashItem *item;

#ifdef DD_DEBUG
    assert(hash->keysize == 2);
#endif

    if (hash->size > hash->maxsize) {
890 891
        result = cuddHashTableResize(hash);
        if (result == 0) return(0);
Alan Mishchenko committed
892 893 894 895 896 897 898 899 900
    }
    item = cuddHashTableAlloc(hash);
    if (item == NULL) return(0);
    hash->size++;
    item->value = value;
    cuddRef(value);
    item->count = count;
    item->key[0] = f;
    item->key[1] = g;
901
    posn = ddLCHash2(cuddF2L(f),cuddF2L(g),hash->shift);
Alan Mishchenko committed
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
    item->next = hash->bucket[posn];
    hash->bucket[posn] = item;

    return(1);

} /* end of cuddHashTableInsert2 */


/**Function********************************************************************

  Synopsis    [Looks up a key consisting of two pointers in a hash table.]

  Description [Looks up a key consisting of two pointer in a hash table.
  Returns the value associated to the key if there is an entry for the given
  key in the table; NULL otherwise. If the entry is present, its reference
  counter is decremented if not saturated. If the counter reaches 0, the
918
  value of the entry is dereferenced, and the entry is returned to the free
Alan Mishchenko committed
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
  list.]

  SideEffects [None]

  SeeAlso     [cuddHashTableLookup cuddHashTableLookup1 cuddHashTableLookup3
  cuddHashTableInsert2]

******************************************************************************/
DdNode *
cuddHashTableLookup2(
  DdHashTable * hash,
  DdNode * f,
  DdNode * g)
{
    unsigned int posn;
    DdHashItem *item, *prev;

#ifdef DD_DEBUG
    assert(hash->keysize == 2);
#endif

940
    posn = ddLCHash2(cuddF2L(f),cuddF2L(g),hash->shift);
Alan Mishchenko committed
941 942 943 944
    item = hash->bucket[posn];
    prev = NULL;

    while (item != NULL) {
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
        DdNodePtr *key = item->key;
        if ((f == key[0]) && (g == key[1])) {
            DdNode *value = item->value;
            cuddSatDec(item->count);
            if (item->count == 0) {
                cuddDeref(value);
                if (prev == NULL) {
                    hash->bucket[posn] = item->next;
                } else {
                    prev->next = item->next;
                }
                item->next = hash->nextFree;
                hash->nextFree = item;
                hash->size--;
            }
            return(value);
Alan Mishchenko committed
961
        }
962 963
        prev = item;
        item = item->next;
Alan Mishchenko committed
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    }
    return(NULL);

} /* end of cuddHashTableLookup2 */


/**Function********************************************************************

  Synopsis    [Inserts an item in a hash table.]

  Description [Inserts an item in a hash table when the key is
  composed of three pointers. Returns 1 if successful; 0 otherwise.]

  SideEffects [None]

  SeeAlso     [cuddHashTableInsert cuddHashTableInsert1 cuddHashTableInsert2
  cuddHashTableLookup3]

******************************************************************************/
int
cuddHashTableInsert3(
  DdHashTable * hash,
  DdNode * f,
  DdNode * g,
  DdNode * h,
  DdNode * value,
  ptrint count)
{
    int result;
    unsigned int posn;
    DdHashItem *item;

#ifdef DD_DEBUG
    assert(hash->keysize == 3);
#endif

    if (hash->size > hash->maxsize) {
1001 1002
        result = cuddHashTableResize(hash);
        if (result == 0) return(0);
Alan Mishchenko committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    }
    item = cuddHashTableAlloc(hash);
    if (item == NULL) return(0);
    hash->size++;
    item->value = value;
    cuddRef(value);
    item->count = count;
    item->key[0] = f;
    item->key[1] = g;
    item->key[2] = h;
1013
    posn = ddLCHash3(cuddF2L(f),cuddF2L(g),cuddF2L(h),hash->shift);
Alan Mishchenko committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
    item->next = hash->bucket[posn];
    hash->bucket[posn] = item;

    return(1);

} /* end of cuddHashTableInsert3 */


/**Function********************************************************************

  Synopsis    [Looks up a key consisting of three pointers in a hash table.]

  Description [Looks up a key consisting of three pointers in a hash table.
  Returns the value associated to the key if there is an entry for the given
  key in the table; NULL otherwise. If the entry is present, its reference
  counter is decremented if not saturated. If the counter reaches 0, the
1030
  value of the entry is dereferenced, and the entry is returned to the free
Alan Mishchenko committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
  list.]

  SideEffects [None]

  SeeAlso     [cuddHashTableLookup cuddHashTableLookup1 cuddHashTableLookup2
  cuddHashTableInsert3]

******************************************************************************/
DdNode *
cuddHashTableLookup3(
  DdHashTable * hash,
  DdNode * f,
  DdNode * g,
  DdNode * h)
{
    unsigned int posn;
    DdHashItem *item, *prev;

#ifdef DD_DEBUG
    assert(hash->keysize == 3);
#endif

1053
    posn = ddLCHash3(cuddF2L(f),cuddF2L(g),cuddF2L(h),hash->shift);
Alan Mishchenko committed
1054 1055 1056 1057
    item = hash->bucket[posn];
    prev = NULL;

    while (item != NULL) {
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
        DdNodePtr *key = item->key;
        if ((f == key[0]) && (g == key[1]) && (h == key[2])) {
            DdNode *value = item->value;
            cuddSatDec(item->count);
            if (item->count == 0) {
                cuddDeref(value);
                if (prev == NULL) {
                    hash->bucket[posn] = item->next;
                } else {
                    prev->next = item->next;
                }
                item->next = hash->nextFree;
                hash->nextFree = item;
                hash->size--;
            }
            return(value);
Alan Mishchenko committed
1074
        }
1075 1076
        prev = item;
        item = item->next;
Alan Mishchenko committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    }
    return(NULL);

} /* end of cuddHashTableLookup3 */


/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Resizes a local cache.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static void
cuddLocalCacheResize(
  DdLocalCache * cache)
{
    DdLocalCacheItem *item, *olditem, *entry, *old;
    int i, shift;
    unsigned int posn;
    unsigned int slots, oldslots;
1107 1108
    extern DD_OOMFP MMoutOfMemory;
    DD_OOMFP saveHandler;
Alan Mishchenko committed
1109 1110 1111 1112 1113 1114 1115

    olditem = cache->item;
    oldslots = cache->slots;
    slots = cache->slots = oldslots << 1;

#ifdef DD_VERBOSE
    (void) fprintf(cache->manager->err,
1116 1117
                   "Resizing local cache from %d to %d entries\n",
                   oldslots, slots);
Alan Mishchenko committed
1118
    (void) fprintf(cache->manager->err,
1119 1120
                   "\thits = %.0f\tlookups = %.0f\thit ratio = %5.3f\n",
                   cache->hits, cache->lookUps, cache->hits / cache->lookUps);
Alan Mishchenko committed
1121 1122 1123 1124 1125
#endif

    saveHandler = MMoutOfMemory;
    MMoutOfMemory = Cudd_OutOfMem;
    cache->item = item =
1126
        (DdLocalCacheItem *) ABC_ALLOC(char, slots * cache->itemsize);
Alan Mishchenko committed
1127 1128 1129 1130
    MMoutOfMemory = saveHandler;
    /* If we fail to allocate the new table we just give up. */
    if (item == NULL) {
#ifdef DD_VERBOSE
1131
        (void) fprintf(cache->manager->err,"Resizing failed. Giving up.\n");
Alan Mishchenko committed
1132
#endif
1133 1134 1135 1136 1137
        cache->slots = oldslots;
        cache->item = olditem;
        /* Do not try to resize again. */
        cache->maxslots = oldslots - 1;
        return;
Alan Mishchenko committed
1138 1139 1140 1141 1142 1143 1144 1145 1146
    }
    shift = --(cache->shift);
    cache->manager->memused += (slots - oldslots) * cache->itemsize;

    /* Clear new cache. */
    memset(item, 0, slots * cache->itemsize);

    /* Copy from old cache to new one. */
    for (i = 0; (unsigned) i < oldslots; i++) {
1147 1148 1149 1150 1151 1152 1153 1154
        old = (DdLocalCacheItem *) ((char *) olditem + i * cache->itemsize);
        if (old->value != NULL) {
            posn = ddLCHash(old->key,cache->keysize,shift);
            entry = (DdLocalCacheItem *) ((char *) item +
                                          posn * cache->itemsize);
            memcpy(entry->key,old->key,cache->keysize*sizeof(DdNode *));
            entry->value = old->value;
        }
Alan Mishchenko committed
1155 1156
    }

Alan Mishchenko committed
1157
    ABC_FREE(olditem);
Alan Mishchenko committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186

    /* Reinitialize measurements so as to avoid division by 0 and
    ** immediate resizing.
    */
    cache->lookUps = (double) (int) (slots * cache->minHit + 1);
    cache->hits = 0;

} /* end of cuddLocalCacheResize */


/**Function********************************************************************

  Synopsis    [Computes the hash value for a local cache.]

  Description [Computes the hash value for a local cache. Returns the
  bucket index.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
DD_INLINE
static unsigned int
ddLCHash(
  DdNodePtr * key,
  unsigned int keysize,
  int shift)
{
1187
    unsigned int val = (unsigned int) (ptrint) key[0] * DD_P2;
Alan Mishchenko committed
1188 1189 1190
    unsigned int i;

    for (i = 1; i < keysize; i++) {
1191
        val = val * DD_P1 + (int) (ptrint) key[i];
Alan Mishchenko committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
    }

    return(val >> shift);

} /* end of ddLCHash */


/**Function********************************************************************

  Synopsis    [Inserts a local cache in the manager list.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static void
cuddLocalCacheAddToList(
  DdLocalCache * cache)
{
    DdManager *manager = cache->manager;

    cache->next = manager->localCaches;
    manager->localCaches = cache;
    return;

} /* end of cuddLocalCacheAddToList */


/**Function********************************************************************

  Synopsis    [Removes a local cache from the manager list.]

  Description []

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
static void
cuddLocalCacheRemoveFromList(
  DdLocalCache * cache)
{
    DdManager *manager = cache->manager;
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
    DdLocalCache **prevCache, *nextCache;
#ifdef __osf__
#pragma pointer_size restore
#endif

    prevCache = &(manager->localCaches);
    nextCache = manager->localCaches;

    while (nextCache != NULL) {
1252 1253 1254 1255 1256 1257
        if (nextCache == cache) {
            *prevCache = nextCache->next;
            return;
        }
        prevCache = &(nextCache->next);
        nextCache = nextCache->next;
Alan Mishchenko committed
1258
    }
1259
    return;                     /* should never get here */
Alan Mishchenko committed
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296

} /* end of cuddLocalCacheRemoveFromList */


/**Function********************************************************************

  Synopsis    [Resizes a hash table.]

  Description [Resizes a hash table. Returns 1 if successful; 0
  otherwise.]

  SideEffects [None]

  SeeAlso     [cuddHashTableInsert]

******************************************************************************/
static int
cuddHashTableResize(
  DdHashTable * hash)
{
    int j;
    unsigned int posn;
    DdHashItem *item;
    DdHashItem *next;
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
    DdNode **key;
    int numBuckets;
    DdHashItem **buckets;
    DdHashItem **oldBuckets = hash->bucket;
#ifdef __osf__
#pragma pointer_size restore
#endif
    int shift;
    int oldNumBuckets = hash->numBuckets;
1297 1298
    extern DD_OOMFP MMoutOfMemory;
    DD_OOMFP saveHandler;
Alan Mishchenko committed
1299 1300 1301 1302 1303 1304 1305 1306 1307

    /* Compute the new size of the table. */
    numBuckets = oldNumBuckets << 1;
    saveHandler = MMoutOfMemory;
    MMoutOfMemory = Cudd_OutOfMem;
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
Alan Mishchenko committed
1308
    buckets = ABC_ALLOC(DdHashItem *, numBuckets);
Alan Mishchenko committed
1309 1310
    MMoutOfMemory = saveHandler;
    if (buckets == NULL) {
1311 1312
        hash->maxsize <<= 1;
        return(1);
Alan Mishchenko committed
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
    }

    hash->bucket = buckets;
    hash->numBuckets = numBuckets;
    shift = --(hash->shift);
    hash->maxsize <<= 1;
    memset(buckets, 0, numBuckets * sizeof(DdHashItem *));
#ifdef __osf__
#pragma pointer_size restore
#endif
    if (hash->keysize == 1) {
1324 1325 1326 1327 1328
        for (j = 0; j < oldNumBuckets; j++) {
            item = oldBuckets[j];
            while (item != NULL) {
                next = item->next;
                key = item->key;
1329
                posn = ddLCHash2(cuddF2L(key[0]), cuddF2L(key[0]), shift);
1330 1331 1332 1333
                item->next = buckets[posn];
                buckets[posn] = item;
                item = next;
            }
Alan Mishchenko committed
1334 1335
        }
    } else if (hash->keysize == 2) {
1336 1337 1338 1339 1340
        for (j = 0; j < oldNumBuckets; j++) {
            item = oldBuckets[j];
            while (item != NULL) {
                next = item->next;
                key = item->key;
1341
                posn = ddLCHash2(cuddF2L(key[0]), cuddF2L(key[1]), shift);
1342 1343 1344 1345
                item->next = buckets[posn];
                buckets[posn] = item;
                item = next;
            }
Alan Mishchenko committed
1346 1347
        }
    } else if (hash->keysize == 3) {
1348 1349 1350 1351 1352
        for (j = 0; j < oldNumBuckets; j++) {
            item = oldBuckets[j];
            while (item != NULL) {
                next = item->next;
                key = item->key;
1353
                posn = ddLCHash3(cuddF2L(key[0]), cuddF2L(key[1]), cuddF2L(key[2]), shift);
1354 1355 1356 1357
                item->next = buckets[posn];
                buckets[posn] = item;
                item = next;
            }
Alan Mishchenko committed
1358 1359
        }
    } else {
1360 1361 1362 1363 1364 1365 1366 1367 1368
        for (j = 0; j < oldNumBuckets; j++) {
            item = oldBuckets[j];
            while (item != NULL) {
                next = item->next;
                posn = ddLCHash(item->key, hash->keysize, shift);
                item->next = buckets[posn];
                buckets[posn] = item;
                item = next;
            }
Alan Mishchenko committed
1369 1370
        }
    }
Alan Mishchenko committed
1371
    ABC_FREE(oldBuckets);
Alan Mishchenko committed
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
    return(1);

} /* end of cuddHashTableResize */


/**Function********************************************************************

  Synopsis    [Fast storage allocation for items in a hash table.]

  Description [Fast storage allocation for items in a hash table. The
  first 4 bytes of a chunk contain a pointer to the next block; the
  rest contains DD_MEM_CHUNK spaces for hash items.  Returns a pointer to
  a new item if successful; NULL is memory is full.]

  SideEffects [None]

  SeeAlso     [cuddAllocNode cuddDynamicAllocNode]

******************************************************************************/
DD_INLINE
static DdHashItem *
cuddHashTableAlloc(
  DdHashTable * hash)
{
    int i;
    unsigned int itemsize = hash->itemsize;
1398 1399
    extern DD_OOMFP MMoutOfMemory;
    DD_OOMFP saveHandler;
Alan Mishchenko committed
1400 1401 1402 1403 1404 1405 1406
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
    DdHashItem **mem, *thisOne, *next, *item;

    if (hash->nextFree == NULL) {
1407 1408 1409 1410
        saveHandler = MMoutOfMemory;
        MMoutOfMemory = Cudd_OutOfMem;
        mem = (DdHashItem **) ABC_ALLOC(char,(DD_MEM_CHUNK+1) * itemsize);
        MMoutOfMemory = saveHandler;
Alan Mishchenko committed
1411 1412 1413
#ifdef __osf__
#pragma pointer_size restore
#endif
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
        if (mem == NULL) {
            if (hash->manager->stash != NULL) {
                ABC_FREE(hash->manager->stash);
                hash->manager->stash = NULL;
                /* Inhibit resizing of tables. */
                hash->manager->maxCacheHard = hash->manager->cacheSlots - 1;
                hash->manager->cacheSlack = - (int) (hash->manager->cacheSlots + 1);
                for (i = 0; i < hash->manager->size; i++) {
                    hash->manager->subtables[i].maxKeys <<= 2;
                }
                hash->manager->gcFrac = 0.2;
                hash->manager->minDead =
                    (unsigned) (0.2 * (double) hash->manager->slots);
Alan Mishchenko committed
1427 1428 1429 1430
#ifdef __osf__
#pragma pointer_size save
#pragma pointer_size short
#endif
1431
                mem = (DdHashItem **) ABC_ALLOC(char,(DD_MEM_CHUNK+1) * itemsize);
Alan Mishchenko committed
1432 1433 1434
#ifdef __osf__
#pragma pointer_size restore
#endif
1435 1436 1437 1438 1439 1440
            }
            if (mem == NULL) {
                (*MMoutOfMemory)((long)((DD_MEM_CHUNK + 1) * itemsize));
                hash->manager->errorCode = CUDD_MEMORY_OUT;
                return(NULL);
            }
Alan Mishchenko committed
1441 1442
        }

1443 1444
        mem[0] = (DdHashItem *) hash->memoryList;
        hash->memoryList = mem;
Alan Mishchenko committed
1445

1446 1447 1448 1449 1450 1451 1452
        thisOne = (DdHashItem *) ((char *) mem + itemsize);
        hash->nextFree = thisOne;
        for (i = 1; i < DD_MEM_CHUNK; i++) {
            next = (DdHashItem *) ((char *) thisOne + itemsize);
            thisOne->next = next;
            thisOne = next;
        }
Alan Mishchenko committed
1453

1454
        thisOne->next = NULL;
Alan Mishchenko committed
1455 1456 1457 1458 1459 1460 1461

    }
    item = hash->nextFree;
    hash->nextFree = item->next;
    return(item);

} /* end of cuddHashTableAlloc */
1462 1463


1464 1465
ABC_NAMESPACE_IMPL_END