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.

245 lines
6.2 KiB

  1. /***** ltl2ba : mem.c *****/
  2. /* Written by Denis Oddoux, LIAFA, France */
  3. /* Copyright (c) 2001 Denis Oddoux */
  4. /* Modified by Paul Gastin, LSV, France */
  5. /* Copyright (c) 2007 Paul Gastin */
  6. /* */
  7. /* This program is free software; you can redistribute it and/or modify */
  8. /* it under the terms of the GNU General Public License as published by */
  9. /* the Free Software Foundation; either version 2 of the License, or */
  10. /* (at your option) any later version. */
  11. /* */
  12. /* This program is distributed in the hope that it will be useful, */
  13. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  14. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  15. /* GNU General Public License for more details. */
  16. /* */
  17. /* You should have received a copy of the GNU General Public License */
  18. /* along with this program; if not, write to the Free Software */
  19. /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
  20. /* */
  21. /* Based on the translation algorithm by Gastin and Oddoux, */
  22. /* presented at the 13th International Conference on Computer Aided */
  23. /* Verification, CAV 2001, Paris, France. */
  24. /* Proceedings - LNCS 2102, pp. 53-65 */
  25. /* */
  26. /* Send bug-reports and/or questions to Paul Gastin */
  27. /* http://www.lsv.ens-cachan.fr/~gastin */
  28. /* */
  29. /* Some of the code in this file was taken from the Spin software */
  30. /* Written by Gerard J. Holzmann, Bell Laboratories, U.S.A. */
  31. #include "ltl2ba.h"
  32. #if 1
  33. #define log(e, u, d) event[e][(int) u] += (long) d;
  34. #else
  35. #define log(e, u, d)
  36. #endif
  37. #define A_LARGE 80
  38. #define A_USER 0x55000000
  39. #define NOTOOBIG 32768
  40. #define POOL 0
  41. #define ALLOC 1
  42. #define FREE 2
  43. #define NREVENT 3
  44. extern unsigned long All_Mem;
  45. extern int tl_verbose;
  46. ATrans *atrans_list = (ATrans *)0;
  47. GTrans *gtrans_list = (GTrans *)0;
  48. BTrans *btrans_list = (BTrans *)0;
  49. int aallocs = 0, afrees = 0, apool = 0;
  50. int gallocs = 0, gfrees = 0, gpool = 0;
  51. int ballocs = 0, bfrees = 0, bpool = 0;
  52. union M {
  53. long size;
  54. union M *link;
  55. };
  56. static union M *freelist[A_LARGE];
  57. static long req[A_LARGE];
  58. static long event[NREVENT][A_LARGE];
  59. void *
  60. tl_emalloc(int U)
  61. { union M *m;
  62. long r, u;
  63. void *rp;
  64. u = (long) ((U-1)/sizeof(union M) + 2);
  65. if (u >= A_LARGE)
  66. { log(ALLOC, 0, 1);
  67. if (tl_verbose)
  68. printf("tl_spin: memalloc %ld bytes\n", u);
  69. m = (union M *) emalloc((int) u*sizeof(union M));
  70. All_Mem += (unsigned long) u*sizeof(union M);
  71. } else
  72. { if (!freelist[u])
  73. { r = req[u] += req[u] ? req[u] : 1;
  74. if (r >= NOTOOBIG)
  75. r = req[u] = NOTOOBIG;
  76. log(POOL, u, r);
  77. freelist[u] = (union M *)
  78. emalloc((int) r*u*sizeof(union M));
  79. All_Mem += (unsigned long) r*u*sizeof(union M);
  80. m = freelist[u] + (r-2)*u;
  81. for ( ; m >= freelist[u]; m -= u)
  82. m->link = m+u;
  83. }
  84. log(ALLOC, u, 1);
  85. m = freelist[u];
  86. freelist[u] = m->link;
  87. }
  88. m->size = (u|A_USER);
  89. for (r = 1; r < u; )
  90. (&m->size)[r++] = 0;
  91. rp = (void *) (m+1);
  92. memset(rp, 0, U);
  93. return rp;
  94. }
  95. void
  96. tfree(void *v)
  97. { union M *m = (union M *) v;
  98. long u;
  99. --m;
  100. if ((m->size&0xFF000000) != A_USER)
  101. Fatal("releasing a free block", (char *)0);
  102. u = (m->size &= 0xFFFFFF);
  103. if (u >= A_LARGE)
  104. { log(FREE, 0, 1);
  105. /* free(m); */
  106. } else
  107. { log(FREE, u, 1);
  108. m->link = freelist[u];
  109. freelist[u] = m;
  110. }
  111. }
  112. ATrans* emalloc_atrans() {
  113. ATrans *result;
  114. if(!atrans_list) {
  115. result = (ATrans *)tl_emalloc(sizeof(GTrans));
  116. result->pos = new_set(1);
  117. result->neg = new_set(1);
  118. result->to = new_set(0);
  119. apool++;
  120. }
  121. else {
  122. result = atrans_list;
  123. atrans_list = atrans_list->nxt;
  124. result->nxt = (ATrans *)0;
  125. }
  126. aallocs++;
  127. return result;
  128. }
  129. void free_atrans(ATrans *t, int rec) {
  130. if(!t) return;
  131. if(rec) free_atrans(t->nxt, rec);
  132. t->nxt = atrans_list;
  133. atrans_list = t;
  134. afrees++;
  135. }
  136. void free_all_atrans() {
  137. ATrans *t;
  138. while(atrans_list) {
  139. t = atrans_list;
  140. atrans_list = t->nxt;
  141. tfree(t->to);
  142. tfree(t->pos);
  143. tfree(t->neg);
  144. tfree(t);
  145. }
  146. }
  147. GTrans* emalloc_gtrans() {
  148. GTrans *result;
  149. if(!gtrans_list) {
  150. result = (GTrans *)tl_emalloc(sizeof(GTrans));
  151. result->pos = new_set(1);
  152. result->neg = new_set(1);
  153. result->final = new_set(0);
  154. gpool++;
  155. }
  156. else {
  157. result = gtrans_list;
  158. gtrans_list = gtrans_list->nxt;
  159. }
  160. gallocs++;
  161. return result;
  162. }
  163. void free_gtrans(GTrans *t, GTrans *sentinel, int fly) {
  164. gfrees++;
  165. if(sentinel && (t != sentinel)) {
  166. free_gtrans(t->nxt, sentinel, fly);
  167. if(fly) t->to->incoming--;
  168. }
  169. t->nxt = gtrans_list;
  170. gtrans_list = t;
  171. }
  172. BTrans* emalloc_btrans() {
  173. BTrans *result;
  174. if(!btrans_list) {
  175. result = (BTrans *)tl_emalloc(sizeof(BTrans));
  176. result->pos = new_set(1);
  177. result->neg = new_set(1);
  178. bpool++;
  179. }
  180. else {
  181. result = btrans_list;
  182. btrans_list = btrans_list->nxt;
  183. }
  184. ballocs++;
  185. return result;
  186. }
  187. void free_btrans(BTrans *t, BTrans *sentinel, int fly) {
  188. bfrees++;
  189. if(sentinel && (t != sentinel)) {
  190. free_btrans(t->nxt, sentinel, fly);
  191. if(fly) t->to->incoming--;
  192. }
  193. t->nxt = btrans_list;
  194. btrans_list = t;
  195. }
  196. void
  197. a_stats(void)
  198. { long p, a, f;
  199. int i;
  200. printf(" size\t pool\tallocs\t frees\n");
  201. for (i = 0; i < A_LARGE; i++)
  202. { p = event[POOL][i];
  203. a = event[ALLOC][i];
  204. f = event[FREE][i];
  205. if(p|a|f)
  206. printf("%5d\t%6ld\t%6ld\t%6ld\n",
  207. i, p, a, f);
  208. }
  209. printf("atrans\t%6d\t%6d\t%6d\n",
  210. apool, aallocs, afrees);
  211. printf("gtrans\t%6d\t%6d\t%6d\n",
  212. gpool, gallocs, gfrees);
  213. printf("btrans\t%6d\t%6d\t%6d\n",
  214. bpool, ballocs, bfrees);
  215. }