You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

808 lines
20 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddRef.c]
  3. PackageName [cudd]
  4. Synopsis [Functions that manipulate the reference counts.]
  5. Description [External procedures included in this module:
  6. <ul>
  7. <li> Cudd_Ref()
  8. <li> Cudd_RecursiveDeref()
  9. <li> Cudd_IterDerefBdd()
  10. <li> Cudd_DelayedDerefBdd()
  11. <li> Cudd_RecursiveDerefZdd()
  12. <li> Cudd_Deref()
  13. <li> Cudd_CheckZeroRef()
  14. </ul>
  15. Internal procedures included in this module:
  16. <ul>
  17. <li> cuddReclaim()
  18. <li> cuddReclaimZdd()
  19. <li> cuddClearDeathRow()
  20. <li> cuddShrinkDeathRow()
  21. <li> cuddIsInDeathRow()
  22. <li> cuddTimesInDeathRow()
  23. </ul>
  24. ]
  25. SeeAlso []
  26. Author [Fabio Somenzi]
  27. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  28. All rights reserved.
  29. Redistribution and use in source and binary forms, with or without
  30. modification, are permitted provided that the following conditions
  31. are met:
  32. Redistributions of source code must retain the above copyright
  33. notice, this list of conditions and the following disclaimer.
  34. Redistributions in binary form must reproduce the above copyright
  35. notice, this list of conditions and the following disclaimer in the
  36. documentation and/or other materials provided with the distribution.
  37. Neither the name of the University of Colorado nor the names of its
  38. contributors may be used to endorse or promote products derived from
  39. this software without specific prior written permission.
  40. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  41. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  42. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  43. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  44. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  45. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  46. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  48. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  50. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  51. POSSIBILITY OF SUCH DAMAGE.]
  52. ******************************************************************************/
  53. #include "util.h"
  54. #include "cuddInt.h"
  55. /*---------------------------------------------------------------------------*/
  56. /* Constant declarations */
  57. /*---------------------------------------------------------------------------*/
  58. /*---------------------------------------------------------------------------*/
  59. /* Stucture declarations */
  60. /*---------------------------------------------------------------------------*/
  61. /*---------------------------------------------------------------------------*/
  62. /* Type declarations */
  63. /*---------------------------------------------------------------------------*/
  64. /*---------------------------------------------------------------------------*/
  65. /* Variable declarations */
  66. /*---------------------------------------------------------------------------*/
  67. #ifndef lint
  68. static char rcsid[] DD_UNUSED = "$Id: cuddRef.c,v 1.29 2012/02/05 01:07:19 fabio Exp $";
  69. #endif
  70. /*---------------------------------------------------------------------------*/
  71. /* Macro declarations */
  72. /*---------------------------------------------------------------------------*/
  73. /**AutomaticStart*************************************************************/
  74. /*---------------------------------------------------------------------------*/
  75. /* Static function prototypes */
  76. /*---------------------------------------------------------------------------*/
  77. /**AutomaticEnd***************************************************************/
  78. /*---------------------------------------------------------------------------*/
  79. /* Definition of exported functions */
  80. /*---------------------------------------------------------------------------*/
  81. /**Function********************************************************************
  82. Synopsis [Increases the reference count of a node, if it is not
  83. saturated.]
  84. Description []
  85. SideEffects [None]
  86. SeeAlso [Cudd_RecursiveDeref Cudd_Deref]
  87. ******************************************************************************/
  88. void
  89. Cudd_Ref(
  90. DdNode * n)
  91. {
  92. n = Cudd_Regular(n);
  93. cuddSatInc(n->ref);
  94. } /* end of Cudd_Ref */
  95. /**Function********************************************************************
  96. Synopsis [Decreases the reference count of node n.]
  97. Description [Decreases the reference count of node n. If n dies,
  98. recursively decreases the reference counts of its children. It is
  99. used to dispose of a DD that is no longer needed.]
  100. SideEffects [None]
  101. SeeAlso [Cudd_Deref Cudd_Ref Cudd_RecursiveDerefZdd]
  102. ******************************************************************************/
  103. void
  104. Cudd_RecursiveDeref(
  105. DdManager * table,
  106. DdNode * n)
  107. {
  108. DdNode *N;
  109. int ord;
  110. DdNodePtr *stack = table->stack;
  111. int SP = 1;
  112. unsigned int live = table->keys - table->dead;
  113. if (live > table->peakLiveNodes) {
  114. table->peakLiveNodes = live;
  115. }
  116. N = Cudd_Regular(n);
  117. do {
  118. #ifdef DD_DEBUG
  119. assert(N->ref != 0);
  120. #endif
  121. if (N->ref == 1) {
  122. N->ref = 0;
  123. table->dead++;
  124. #ifdef DD_STATS
  125. table->nodesDropped++;
  126. #endif
  127. if (cuddIsConstant(N)) {
  128. table->constants.dead++;
  129. N = stack[--SP];
  130. } else {
  131. ord = table->perm[N->index];
  132. stack[SP++] = Cudd_Regular(cuddE(N));
  133. table->subtables[ord].dead++;
  134. N = cuddT(N);
  135. }
  136. } else {
  137. cuddSatDec(N->ref);
  138. N = stack[--SP];
  139. }
  140. } while (SP != 0);
  141. } /* end of Cudd_RecursiveDeref */
  142. /**Function********************************************************************
  143. Synopsis [Decreases the reference count of BDD node n.]
  144. Description [Decreases the reference count of node n. If n dies,
  145. recursively decreases the reference counts of its children. It is
  146. used to dispose of a BDD that is no longer needed. It is more
  147. efficient than Cudd_RecursiveDeref, but it cannot be used on
  148. ADDs. The greater efficiency comes from being able to assume that no
  149. constant node will ever die as a result of a call to this
  150. procedure.]
  151. SideEffects [None]
  152. SeeAlso [Cudd_RecursiveDeref Cudd_DelayedDerefBdd]
  153. ******************************************************************************/
  154. void
  155. Cudd_IterDerefBdd(
  156. DdManager * table,
  157. DdNode * n)
  158. {
  159. DdNode *N;
  160. int ord;
  161. DdNodePtr *stack = table->stack;
  162. int SP = 1;
  163. unsigned int live = table->keys - table->dead;
  164. if (live > table->peakLiveNodes) {
  165. table->peakLiveNodes = live;
  166. }
  167. N = Cudd_Regular(n);
  168. do {
  169. #ifdef DD_DEBUG
  170. assert(N->ref != 0);
  171. #endif
  172. if (N->ref == 1) {
  173. N->ref = 0;
  174. table->dead++;
  175. #ifdef DD_STATS
  176. table->nodesDropped++;
  177. #endif
  178. ord = table->perm[N->index];
  179. stack[SP++] = Cudd_Regular(cuddE(N));
  180. table->subtables[ord].dead++;
  181. N = cuddT(N);
  182. } else {
  183. cuddSatDec(N->ref);
  184. N = stack[--SP];
  185. }
  186. } while (SP != 0);
  187. } /* end of Cudd_IterDerefBdd */
  188. /**Function********************************************************************
  189. Synopsis [Decreases the reference count of BDD node n.]
  190. Description [Enqueues node n for later dereferencing. If the queue
  191. is full decreases the reference count of the oldest node N to make
  192. room for n. If N dies, recursively decreases the reference counts of
  193. its children. It is used to dispose of a BDD that is currently not
  194. needed, but may be useful again in the near future. The dereferencing
  195. proper is done as in Cudd_IterDerefBdd.]
  196. SideEffects [None]
  197. SeeAlso [Cudd_RecursiveDeref Cudd_IterDerefBdd]
  198. ******************************************************************************/
  199. void
  200. Cudd_DelayedDerefBdd(
  201. DdManager * table,
  202. DdNode * n)
  203. {
  204. DdNode *N;
  205. int ord;
  206. DdNodePtr *stack;
  207. int SP;
  208. unsigned int live = table->keys - table->dead;
  209. if (live > table->peakLiveNodes) {
  210. table->peakLiveNodes = live;
  211. }
  212. n = Cudd_Regular(n);
  213. #ifdef DD_DEBUG
  214. assert(n->ref != 0);
  215. #endif
  216. #ifdef DD_NO_DEATH_ROW
  217. N = n;
  218. #else
  219. if (cuddIsConstant(n) || n->ref > 1) {
  220. #ifdef DD_DEBUG
  221. assert(n->ref != 1 && (!cuddIsConstant(n) || n == DD_ONE(table)));
  222. #endif
  223. cuddSatDec(n->ref);
  224. return;
  225. }
  226. N = table->deathRow[table->nextDead];
  227. if (N != NULL) {
  228. #endif
  229. #ifdef DD_DEBUG
  230. assert(!Cudd_IsComplement(N));
  231. #endif
  232. stack = table->stack;
  233. SP = 1;
  234. do {
  235. #ifdef DD_DEBUG
  236. assert(N->ref != 0);
  237. #endif
  238. if (N->ref == 1) {
  239. N->ref = 0;
  240. table->dead++;
  241. #ifdef DD_STATS
  242. table->nodesDropped++;
  243. #endif
  244. ord = table->perm[N->index];
  245. stack[SP++] = Cudd_Regular(cuddE(N));
  246. table->subtables[ord].dead++;
  247. N = cuddT(N);
  248. } else {
  249. cuddSatDec(N->ref);
  250. N = stack[--SP];
  251. }
  252. } while (SP != 0);
  253. #ifndef DD_NO_DEATH_ROW
  254. }
  255. table->deathRow[table->nextDead] = n;
  256. /* Udate insertion point. */
  257. table->nextDead++;
  258. table->nextDead &= table->deadMask;
  259. #if 0
  260. if (table->nextDead == table->deathRowDepth) {
  261. if (table->deathRowDepth < table->looseUpTo / 2) {
  262. extern void (*MMoutOfMemory)(long);
  263. void (*saveHandler)(long) = MMoutOfMemory;
  264. DdNodePtr *newRow;
  265. MMoutOfMemory = Cudd_OutOfMem;
  266. newRow = REALLOC(DdNodePtr,table->deathRow,2*table->deathRowDepth);
  267. MMoutOfMemory = saveHandler;
  268. if (newRow == NULL) {
  269. table->nextDead = 0;
  270. } else {
  271. int i;
  272. table->memused += table->deathRowDepth;
  273. i = table->deathRowDepth;
  274. table->deathRowDepth <<= 1;
  275. for (; i < table->deathRowDepth; i++) {
  276. newRow[i] = NULL;
  277. }
  278. table->deadMask = table->deathRowDepth - 1;
  279. table->deathRow = newRow;
  280. }
  281. } else {
  282. table->nextDead = 0;
  283. }
  284. }
  285. #endif
  286. #endif
  287. } /* end of Cudd_DelayedDerefBdd */
  288. /**Function********************************************************************
  289. Synopsis [Decreases the reference count of ZDD node n.]
  290. Description [Decreases the reference count of ZDD node n. If n dies,
  291. recursively decreases the reference counts of its children. It is
  292. used to dispose of a ZDD that is no longer needed.]
  293. SideEffects [None]
  294. SeeAlso [Cudd_Deref Cudd_Ref Cudd_RecursiveDeref]
  295. ******************************************************************************/
  296. void
  297. Cudd_RecursiveDerefZdd(
  298. DdManager * table,
  299. DdNode * n)
  300. {
  301. DdNode *N;
  302. int ord;
  303. DdNodePtr *stack = table->stack;
  304. int SP = 1;
  305. N = n;
  306. do {
  307. #ifdef DD_DEBUG
  308. assert(N->ref != 0);
  309. #endif
  310. cuddSatDec(N->ref);
  311. if (N->ref == 0) {
  312. table->deadZ++;
  313. #ifdef DD_STATS
  314. table->nodesDropped++;
  315. #endif
  316. #ifdef DD_DEBUG
  317. assert(!cuddIsConstant(N));
  318. #endif
  319. ord = table->permZ[N->index];
  320. stack[SP++] = cuddE(N);
  321. table->subtableZ[ord].dead++;
  322. N = cuddT(N);
  323. } else {
  324. N = stack[--SP];
  325. }
  326. } while (SP != 0);
  327. } /* end of Cudd_RecursiveDerefZdd */
  328. /**Function********************************************************************
  329. Synopsis [Decreases the reference count of node.]
  330. Description [Decreases the reference count of node. It is primarily
  331. used in recursive procedures to decrease the ref count of a result
  332. node before returning it. This accomplishes the goal of removing the
  333. protection applied by a previous Cudd_Ref.]
  334. SideEffects [None]
  335. SeeAlso [Cudd_RecursiveDeref Cudd_RecursiveDerefZdd Cudd_Ref]
  336. ******************************************************************************/
  337. void
  338. Cudd_Deref(
  339. DdNode * node)
  340. {
  341. node = Cudd_Regular(node);
  342. cuddSatDec(node->ref);
  343. } /* end of Cudd_Deref */
  344. /**Function********************************************************************
  345. Synopsis [Checks the unique table for nodes with non-zero reference
  346. counts.]
  347. Description [Checks the unique table for nodes with non-zero
  348. reference counts. It is normally called before Cudd_Quit to make sure
  349. that there are no memory leaks due to missing Cudd_RecursiveDeref's.
  350. Takes into account that reference counts may saturate and that the
  351. basic constants and the projection functions are referenced by the
  352. manager. Returns the number of nodes with non-zero reference count.
  353. (Except for the cases mentioned above.)]
  354. SideEffects [None]
  355. SeeAlso []
  356. ******************************************************************************/
  357. int
  358. Cudd_CheckZeroRef(
  359. DdManager * manager)
  360. {
  361. int size;
  362. int i, j;
  363. int remain; /* the expected number of remaining references to one */
  364. DdNodePtr *nodelist;
  365. DdNode *node;
  366. DdNode *sentinel = &(manager->sentinel);
  367. DdSubtable *subtable;
  368. int count = 0;
  369. int index;
  370. #ifndef DD_NO_DEATH_ROW
  371. cuddClearDeathRow(manager);
  372. #endif
  373. /* First look at the BDD/ADD subtables. */
  374. remain = 1; /* reference from the manager */
  375. size = manager->size;
  376. remain += 2 * size; /* reference from the BDD projection functions */
  377. for (i = 0; i < size; i++) {
  378. subtable = &(manager->subtables[i]);
  379. nodelist = subtable->nodelist;
  380. for (j = 0; (unsigned) j < subtable->slots; j++) {
  381. node = nodelist[j];
  382. while (node != sentinel) {
  383. if (node->ref != 0 && node->ref != DD_MAXREF) {
  384. index = (int) node->index;
  385. if (node != manager->vars[index]) {
  386. count++;
  387. } else {
  388. if (node->ref != 1) {
  389. count++;
  390. }
  391. }
  392. }
  393. node = node->next;
  394. }
  395. }
  396. }
  397. /* Then look at the ZDD subtables. */
  398. size = manager->sizeZ;
  399. if (size) /* references from ZDD universe */
  400. remain += 2;
  401. for (i = 0; i < size; i++) {
  402. subtable = &(manager->subtableZ[i]);
  403. nodelist = subtable->nodelist;
  404. for (j = 0; (unsigned) j < subtable->slots; j++) {
  405. node = nodelist[j];
  406. while (node != NULL) {
  407. if (node->ref != 0 && node->ref != DD_MAXREF) {
  408. index = (int) node->index;
  409. if (node == manager->univ[manager->permZ[index]]) {
  410. if (node->ref > 2) {
  411. count++;
  412. }
  413. } else {
  414. count++;
  415. }
  416. }
  417. node = node->next;
  418. }
  419. }
  420. }
  421. /* Now examine the constant table. Plusinfinity, minusinfinity, and
  422. ** zero are referenced by the manager. One is referenced by the
  423. ** manager, by the ZDD universe, and by all projection functions.
  424. ** All other nodes should have no references.
  425. */
  426. nodelist = manager->constants.nodelist;
  427. for (j = 0; (unsigned) j < manager->constants.slots; j++) {
  428. node = nodelist[j];
  429. while (node != NULL) {
  430. if (node->ref != 0 && node->ref != DD_MAXREF) {
  431. if (node == manager->one) {
  432. if ((int) node->ref != remain) {
  433. count++;
  434. }
  435. } else if (node == manager->zero ||
  436. node == manager->plusinfinity ||
  437. node == manager->minusinfinity) {
  438. if (node->ref != 1) {
  439. count++;
  440. }
  441. } else {
  442. count++;
  443. }
  444. }
  445. node = node->next;
  446. }
  447. }
  448. return(count);
  449. } /* end of Cudd_CheckZeroRef */
  450. /*---------------------------------------------------------------------------*/
  451. /* Definition of internal functions */
  452. /*---------------------------------------------------------------------------*/
  453. /**Function********************************************************************
  454. Synopsis [Brings children of a dead node back.]
  455. Description []
  456. SideEffects [None]
  457. SeeAlso [cuddReclaimZdd]
  458. ******************************************************************************/
  459. void
  460. cuddReclaim(
  461. DdManager * table,
  462. DdNode * n)
  463. {
  464. DdNode *N;
  465. int ord;
  466. DdNodePtr *stack = table->stack;
  467. int SP = 1;
  468. double initialDead = table->dead;
  469. N = Cudd_Regular(n);
  470. #ifdef DD_DEBUG
  471. assert(N->ref == 0);
  472. #endif
  473. do {
  474. if (N->ref == 0) {
  475. N->ref = 1;
  476. table->dead--;
  477. if (cuddIsConstant(N)) {
  478. table->constants.dead--;
  479. N = stack[--SP];
  480. } else {
  481. ord = table->perm[N->index];
  482. stack[SP++] = Cudd_Regular(cuddE(N));
  483. table->subtables[ord].dead--;
  484. N = cuddT(N);
  485. }
  486. } else {
  487. cuddSatInc(N->ref);
  488. N = stack[--SP];
  489. }
  490. } while (SP != 0);
  491. N = Cudd_Regular(n);
  492. cuddSatDec(N->ref);
  493. table->reclaimed += initialDead - table->dead;
  494. } /* end of cuddReclaim */
  495. /**Function********************************************************************
  496. Synopsis [Brings children of a dead ZDD node back.]
  497. Description []
  498. SideEffects [None]
  499. SeeAlso [cuddReclaim]
  500. ******************************************************************************/
  501. void
  502. cuddReclaimZdd(
  503. DdManager * table,
  504. DdNode * n)
  505. {
  506. DdNode *N;
  507. int ord;
  508. DdNodePtr *stack = table->stack;
  509. int SP = 1;
  510. N = n;
  511. #ifdef DD_DEBUG
  512. assert(N->ref == 0);
  513. #endif
  514. do {
  515. cuddSatInc(N->ref);
  516. if (N->ref == 1) {
  517. table->deadZ--;
  518. table->reclaimed++;
  519. #ifdef DD_DEBUG
  520. assert(!cuddIsConstant(N));
  521. #endif
  522. ord = table->permZ[N->index];
  523. stack[SP++] = cuddE(N);
  524. table->subtableZ[ord].dead--;
  525. N = cuddT(N);
  526. } else {
  527. N = stack[--SP];
  528. }
  529. } while (SP != 0);
  530. cuddSatDec(n->ref);
  531. } /* end of cuddReclaimZdd */
  532. /**Function********************************************************************
  533. Synopsis [Shrinks the death row.]
  534. Description [Shrinks the death row by a factor of four.]
  535. SideEffects [None]
  536. SeeAlso [cuddClearDeathRow]
  537. ******************************************************************************/
  538. void
  539. cuddShrinkDeathRow(
  540. DdManager *table)
  541. {
  542. #ifndef DD_NO_DEATH_ROW
  543. int i;
  544. if (table->deathRowDepth > 3) {
  545. for (i = table->deathRowDepth/4; i < table->deathRowDepth; i++) {
  546. if (table->deathRow[i] == NULL) break;
  547. Cudd_IterDerefBdd(table,table->deathRow[i]);
  548. table->deathRow[i] = NULL;
  549. }
  550. table->deathRowDepth /= 4;
  551. table->deadMask = table->deathRowDepth - 1;
  552. if ((unsigned) table->nextDead > table->deadMask) {
  553. table->nextDead = 0;
  554. }
  555. table->deathRow = REALLOC(DdNodePtr, table->deathRow,
  556. table->deathRowDepth);
  557. }
  558. #endif
  559. } /* end of cuddShrinkDeathRow */
  560. /**Function********************************************************************
  561. Synopsis [Clears the death row.]
  562. Description []
  563. SideEffects [None]
  564. SeeAlso [Cudd_DelayedDerefBdd Cudd_IterDerefBdd Cudd_CheckZeroRef
  565. cuddGarbageCollect]
  566. ******************************************************************************/
  567. void
  568. cuddClearDeathRow(
  569. DdManager *table)
  570. {
  571. #ifndef DD_NO_DEATH_ROW
  572. int i;
  573. for (i = 0; i < table->deathRowDepth; i++) {
  574. if (table->deathRow[i] == NULL) break;
  575. Cudd_IterDerefBdd(table,table->deathRow[i]);
  576. table->deathRow[i] = NULL;
  577. }
  578. #ifdef DD_DEBUG
  579. for (; i < table->deathRowDepth; i++) {
  580. assert(table->deathRow[i] == NULL);
  581. }
  582. #endif
  583. table->nextDead = 0;
  584. #endif
  585. } /* end of cuddClearDeathRow */
  586. /**Function********************************************************************
  587. Synopsis [Checks whether a node is in the death row.]
  588. Description [Checks whether a node is in the death row. Returns the
  589. position of the first occurrence if the node is present; -1
  590. otherwise.]
  591. SideEffects [None]
  592. SeeAlso [Cudd_DelayedDerefBdd cuddClearDeathRow]
  593. ******************************************************************************/
  594. int
  595. cuddIsInDeathRow(
  596. DdManager *dd,
  597. DdNode *f)
  598. {
  599. #ifndef DD_NO_DEATH_ROW
  600. int i;
  601. for (i = 0; i < dd->deathRowDepth; i++) {
  602. if (f == dd->deathRow[i]) {
  603. return(i);
  604. }
  605. }
  606. #endif
  607. return(-1);
  608. } /* end of cuddIsInDeathRow */
  609. /**Function********************************************************************
  610. Synopsis [Counts how many times a node is in the death row.]
  611. Description []
  612. SideEffects [None]
  613. SeeAlso [Cudd_DelayedDerefBdd cuddClearDeathRow cuddIsInDeathRow]
  614. ******************************************************************************/
  615. int
  616. cuddTimesInDeathRow(
  617. DdManager *dd,
  618. DdNode *f)
  619. {
  620. int count = 0;
  621. #ifndef DD_NO_DEATH_ROW
  622. int i;
  623. for (i = 0; i < dd->deathRowDepth; i++) {
  624. count += f == dd->deathRow[i];
  625. }
  626. #endif
  627. return(count);
  628. } /* end of cuddTimesInDeathRow */
  629. /*---------------------------------------------------------------------------*/
  630. /* Definition of static functions */
  631. /*---------------------------------------------------------------------------*/