PageRenderTime 38ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/algorithms_for_tapenade/c_code/adStack.c

http://github.com/b45ch1/hpsc_hanoi_2009_walter
C | 475 lines | 402 code | 38 blank | 35 comment | 61 complexity | 99fb6f83aee109b4fe5ea8c2a177f068 MD5 | raw file
  1. static char adSid[]="$Id: adStack.c 3285 2010-01-05 09:40:52Z llh $";
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #define ONE_BLOCK_SIZE 16384
  6. #ifndef STACK_SIZE_TRACING
  7. #define STACK_SIZE_TRACING 1
  8. #endif
  9. /* The main stack is a double-chain of DoubleChainedBlock objects.
  10. * Each DoubleChainedBlock holds an array[ONE_BLOCK_SIZE] of char. */
  11. typedef struct _doubleChainedBlock{
  12. struct _doubleChainedBlock *prev ;
  13. char *contents ;
  14. struct _doubleChainedBlock *next ;
  15. } DoubleChainedBlock ;
  16. /* Globals that define the current position in the stack: */
  17. static DoubleChainedBlock *curStack = NULL ;
  18. static char *curStackTop = NULL ;
  19. /* Globals that define the current LOOKing position in the stack: */
  20. static DoubleChainedBlock *lookStack = NULL ;
  21. static char *lookStackTop = NULL ;
  22. static long int mmctraffic = 0 ;
  23. static long int mmctrafficM = 0 ;
  24. #ifdef STACK_SIZE_TRACING
  25. long int bigStackSize = 0;
  26. #endif
  27. /* PUSHes "nbChars" consecutive chars from a location starting at address "x".
  28. * Resets the LOOKing position if it was active.
  29. * Checks that there is enough space left to hold "nbChars" chars.
  30. * Otherwise, allocates the necessary space. */
  31. void pushNarray(char *x, unsigned int nbChars) {
  32. unsigned int nbmax = (curStack)?ONE_BLOCK_SIZE-(curStackTop-(curStack->contents)):0 ;
  33. #ifdef STACK_SIZE_TRACING
  34. bigStackSize += nbChars;
  35. #endif
  36. mmctraffic += nbChars ;
  37. while (mmctraffic >= 1000000) {
  38. mmctraffic -= 1000000 ;
  39. mmctrafficM++ ;
  40. }
  41. lookStack = NULL ;
  42. if (nbChars <= nbmax) {
  43. memcpy(curStackTop,x,nbChars) ;
  44. curStackTop+=nbChars ;
  45. } else {
  46. char *inx = x+(nbChars-nbmax) ;
  47. if (nbmax>0) memcpy(curStackTop,inx,nbmax) ;
  48. while (inx>x) {
  49. /* Create new block: */
  50. if ((curStack == NULL) || (curStack->next == NULL)) {
  51. DoubleChainedBlock *newStack ;
  52. char *contents = (char*)malloc(ONE_BLOCK_SIZE*sizeof(char)) ;
  53. newStack = (DoubleChainedBlock*)malloc(sizeof(DoubleChainedBlock)) ;
  54. if ((contents == NULL) || (newStack == NULL)) {
  55. DoubleChainedBlock *stack = curStack ;
  56. int nbBlocks = (stack?-1:0) ;
  57. while(stack) {
  58. stack = stack->prev ;
  59. nbBlocks++ ;
  60. }
  61. printf("Out of memory (allocated %i blocks of %i bytes)\n",
  62. nbBlocks, ONE_BLOCK_SIZE) ;
  63. exit(0);
  64. }
  65. if (curStack != NULL) curStack->next = newStack ;
  66. newStack->prev = curStack ;
  67. newStack->next = NULL ;
  68. newStack->contents = contents ;
  69. curStack = newStack ;
  70. } else
  71. curStack = curStack->next ;
  72. /* new block created! */
  73. inx -= ONE_BLOCK_SIZE ;
  74. if(inx>x)
  75. memcpy(curStack->contents,inx,ONE_BLOCK_SIZE) ;
  76. else {
  77. unsigned int nbhead = (inx-x)+ONE_BLOCK_SIZE ;
  78. curStackTop = curStack->contents ;
  79. memcpy(curStackTop,x,nbhead) ;
  80. curStackTop += nbhead ;
  81. }
  82. }
  83. }
  84. }
  85. /* POPs "nbChars" consecutive chars to a location starting at address "x".
  86. * Resets the LOOKing position if it was active.
  87. * Checks that there is enough data to fill "nbChars" chars.
  88. * Otherwise, pops as many blocks as necessary. */
  89. void popNarray(char *x, unsigned int nbChars) {
  90. unsigned int nbmax = curStackTop-(curStack->contents) ;
  91. #ifdef STACK_SIZE_TRACING
  92. bigStackSize -= nbChars;
  93. #endif
  94. lookStack = NULL ;
  95. if (nbChars <= nbmax) {
  96. curStackTop-=nbChars ;
  97. memcpy(x,curStackTop,nbChars);
  98. } else {
  99. char *tlx = x+nbChars ;
  100. if (nbmax>0) memcpy(x,curStack->contents,nbmax) ;
  101. x+=nbmax ;
  102. while (x<tlx) {
  103. curStack = curStack->prev ;
  104. if (x+ONE_BLOCK_SIZE<tlx) {
  105. memcpy(x,curStack->contents,ONE_BLOCK_SIZE) ;
  106. x += ONE_BLOCK_SIZE ;
  107. } else {
  108. unsigned int nbtail = tlx-x ;
  109. curStackTop=(curStack->contents)+ONE_BLOCK_SIZE-nbtail ;
  110. memcpy(x,curStackTop,nbtail) ;
  111. x = tlx ;
  112. }
  113. }
  114. }
  115. }
  116. /* LOOKs "nbChars" consecutive chars to a location starting at address "x".
  117. * Activates the LOOKing position if it was reset.
  118. * LOOKing is just like POPping, except that the main pointer
  119. * remains in place, so that the value is not POPped.
  120. * Further PUSHs or POPs will start from the same place as if
  121. * no LOOK had been made. */
  122. void lookNarray(char *x, unsigned int nbChars) {
  123. unsigned int nbmax ;
  124. if (lookStack == NULL) {
  125. lookStack = curStack ;
  126. lookStackTop = curStackTop ;
  127. }
  128. nbmax = lookStackTop-(lookStack->contents) ;
  129. if (nbChars <= nbmax) {
  130. lookStackTop-=nbChars ;
  131. memcpy(x,lookStackTop,nbChars);
  132. } else {
  133. char *tlx = x+nbChars ;
  134. if (nbmax>0) memcpy(x,lookStack->contents,nbmax) ;
  135. x+=nbmax ;
  136. while (x<tlx) {
  137. lookStack = lookStack->prev ;
  138. if (x+ONE_BLOCK_SIZE<tlx) {
  139. memcpy(x,lookStack->contents,ONE_BLOCK_SIZE) ;
  140. x += ONE_BLOCK_SIZE ;
  141. } else {
  142. unsigned int nbtail = tlx-x ;
  143. lookStackTop=(lookStack->contents)+ONE_BLOCK_SIZE-nbtail ;
  144. memcpy(x,lookStackTop,nbtail) ;
  145. x = tlx ;
  146. }
  147. }
  148. }
  149. }
  150. void resetadlookstack_() {
  151. lookStack=NULL ;
  152. }
  153. /****** Exported PUSH/POP/LOOK functions for ARRAYS: ******/
  154. void pushcharacterarray_(char *x, unsigned int *n) {
  155. pushNarray(x,*n) ;
  156. }
  157. void popcharacterarray_(char *x, unsigned int *n) {
  158. popNarray(x,*n) ;
  159. }
  160. void lookcharacterarray_(char *x, unsigned int *n) {
  161. lookNarray(x,*n) ;
  162. }
  163. void pushbooleanarray_(char *x, unsigned int *n) {
  164. pushNarray(x,(*n*4)) ;
  165. }
  166. void popbooleanarray_(char *x, unsigned int *n) {
  167. popNarray(x,(*n*4)) ;
  168. }
  169. void lookbooleanarray_(char *x, unsigned int *n) {
  170. lookNarray(x,(*n*4)) ;
  171. }
  172. void pushinteger4array_(char *x, unsigned int *n) {
  173. pushNarray(x,(*n*4)) ;
  174. }
  175. void popinteger4array_(char *x, unsigned int *n) {
  176. popNarray(x,(*n*4)) ;
  177. }
  178. void lookinteger4array_(char *x, unsigned int *n) {
  179. lookNarray(x,(*n*4)) ;
  180. }
  181. void pushinteger8array_(char *x, unsigned int *n) {
  182. pushNarray(x,(*n*8)) ;
  183. }
  184. void popinteger8array_(char *x, unsigned int *n) {
  185. popNarray(x,(*n*8)) ;
  186. }
  187. void lookinteger8array_(char *x, unsigned int *n) {
  188. lookNarray(x,(*n*8)) ;
  189. }
  190. void pushinteger16array_(char *x, unsigned int *n) {
  191. pushNarray(x,(*n*16)) ;
  192. }
  193. void popinteger16array_(char *x, unsigned int *n) {
  194. popNarray(x,(*n*16)) ;
  195. }
  196. void lookinteger16array_(char *x, unsigned int *n) {
  197. lookNarray(x,(*n*16)) ;
  198. }
  199. void pushreal4array_(char *x, unsigned int *n) {
  200. pushNarray(x,(*n*4)) ;
  201. }
  202. void popreal4array_(char *x, unsigned int *n) {
  203. popNarray(x,(*n*4)) ;
  204. }
  205. void lookreal4array_(char *x, unsigned int *n) {
  206. lookNarray(x,(*n*4)) ;
  207. }
  208. void pushreal8array_(char *x, unsigned int *n) {
  209. pushNarray(x,(*n*8)) ;
  210. }
  211. void popreal8array_(char *x, unsigned int *n) {
  212. popNarray(x,(*n*8)) ;
  213. }
  214. void lookreal8array_(char *x, unsigned int *n) {
  215. lookNarray(x,(*n*8)) ;
  216. }
  217. void pushreal16array_(char *x, unsigned int *n) {
  218. pushNarray(x,(*n*16)) ;
  219. }
  220. void popreal16array_(char *x, unsigned int *n) {
  221. popNarray(x,(*n*16)) ;
  222. }
  223. void lookreal16array_(char *x, unsigned int *n) {
  224. lookNarray(x,(*n*16)) ;
  225. }
  226. void pushreal32array_(char *x, unsigned int *n) {
  227. pushNarray(x,(*n*32)) ;
  228. }
  229. void popreal32array_(char *x, unsigned int *n) {
  230. popNarray(x,(*n*32)) ;
  231. }
  232. void lookreal32array_(char *x, unsigned int *n) {
  233. lookNarray(x,(*n*32)) ;
  234. }
  235. void pushcomplex4array_(char *x, unsigned int *n) {
  236. pushNarray(x,(*n*4)) ;
  237. }
  238. void popcomplex4array_(char *x, unsigned int *n) {
  239. popNarray(x,(*n*4)) ;
  240. }
  241. void lookcomplex4array_(char *x, unsigned int *n) {
  242. lookNarray(x,(*n*4)) ;
  243. }
  244. void pushcomplex8array_(char *x, unsigned int *n) {
  245. pushNarray(x,(*n*8)) ;
  246. }
  247. void popcomplex8array_(char *x, unsigned int *n) {
  248. popNarray(x,(*n*8)) ;
  249. }
  250. void lookcomplex8array_(char *x, unsigned int *n) {
  251. lookNarray(x,(*n*8)) ;
  252. }
  253. void pushcomplex16array_(char *x, unsigned int *n) {
  254. pushNarray(x,(*n*16)) ;
  255. }
  256. void popcomplex16array_(char *x, unsigned int *n) {
  257. popNarray(x,(*n*16)) ;
  258. }
  259. void lookcomplex16array_(char *x, unsigned int *n) {
  260. lookNarray(x,(*n*16)) ;
  261. }
  262. void pushcomplex32array_(char *x, unsigned int *n) {
  263. pushNarray(x,(*n*32)) ;
  264. }
  265. void popcomplex32array_(char *x, unsigned int *n) {
  266. popNarray(x,(*n*32)) ;
  267. }
  268. void lookcomplex32array_(char *x, unsigned int *n) {
  269. lookNarray(x,(*n*32)) ;
  270. }
  271. /****** Exported PUSH/POP/LOOK functions for F95 POINTERS: ******/
  272. /* IMPORTANT: Don't forget to add the following interface into each calling routines:
  273. INTERFACE
  274. SUBROUTINE PUSHPOINTER(pp)
  275. REAL, POINTER :: pp
  276. END SUBROUTINE PUSHPOINTER
  277. SUBROUTINE POPPOINTER(pp)
  278. REAL, POINTER :: pp
  279. END SUBROUTINE POPPOINTER
  280. END INTERFACE
  281. */
  282. void pushpointer_(char *ppp) {
  283. pushNarray(ppp, 4) ;
  284. }
  285. void poppointer_(char *ppp) {
  286. popNarray(ppp, 4) ;
  287. }
  288. /************* Debug displays of the state of the stack: ***********/
  289. void printbigbytes(long int nbblocks, long int blocksz, long int nbunits) {
  290. long int a3, b3, res3, res6, res9, res12 ;
  291. int a0, b0, res0 ;
  292. int printzeros = 0 ;
  293. a0 = (int)nbblocks%1000 ;
  294. a3 = nbblocks/1000 ;
  295. b0 = (int)blocksz%1000 ;
  296. b3 = blocksz/1000 ;
  297. res0 = ((int)(nbunits%1000)) + a0*b0 ;
  298. res3 = nbunits/1000 + a3*b0 + a0*b3 ;
  299. res6 = a3*b3 ;
  300. res3 += ((long int)(res0/1000)) ;
  301. res0 = res0%1000 ;
  302. res6 += res3/1000 ;
  303. res3 = res3%1000 ;
  304. res9 = res6/1000 ;
  305. res6 = res6%1000 ;
  306. res12 = res9/1000 ;
  307. res9 = res9%1000 ;
  308. if (res12>0) {
  309. printf("%li ", res12) ;
  310. printzeros = 1 ;
  311. }
  312. if ((res9/100)>0 || printzeros) {
  313. printf("%li",res9/100) ;
  314. printzeros = 1 ;
  315. res9 = res9%100 ;
  316. }
  317. if ((res9/10)>0 || printzeros) {
  318. printf("%li",res9/10) ;
  319. printzeros = 1 ;
  320. res9 = res9%10 ;
  321. }
  322. if (res9>0 || printzeros) {
  323. printf("%li ",res9) ;
  324. printzeros = 1 ;
  325. }
  326. if ((res6/100)>0 || printzeros) {
  327. printf("%li",res6/100) ;
  328. printzeros = 1 ;
  329. res6 = res6%100 ;
  330. }
  331. if ((res6/10)>0 || printzeros) {
  332. printf("%li",res6/10) ;
  333. printzeros = 1 ;
  334. res6 = res6%10 ;
  335. }
  336. if (res6>0 || printzeros) {
  337. printf("%li ",res6) ;
  338. printzeros = 1 ;
  339. }
  340. if ((res3/100)>0 || printzeros) {
  341. printf("%li",res3/100) ;
  342. printzeros = 1 ;
  343. res3 = res3%100 ;
  344. }
  345. if ((res3/10)>0 || printzeros) {
  346. printf("%li",res3/10) ;
  347. printzeros = 1 ;
  348. res3 = res3%10 ;
  349. }
  350. if (res3>0 || printzeros) {
  351. printf("%li ",res3) ;
  352. printzeros = 1 ;
  353. }
  354. if ((res0/100)>0 || printzeros) {
  355. printf("%i",res0/100) ;
  356. printzeros = 1 ;
  357. res0 = res0%100 ;
  358. }
  359. if ((res0/10)>0 || printzeros) {
  360. printf("%i",res0/10) ;
  361. printzeros = 1 ;
  362. res0 = res0%10 ;
  363. }
  364. printf("%i",res0) ;
  365. }
  366. void printctraffic_() {
  367. printf(" C Traffic: ") ;
  368. printbigbytes(mmctrafficM, 1000000, mmctraffic) ;
  369. printf(" bytes\n") ;
  370. }
  371. void printftrafficinc_(long int *mmfM, int *mmfsz, int *mmf) {
  372. printf(" F Traffic: ") ;
  373. printbigbytes(*mmfM, (long int)*mmfsz, (long int)*mmf) ;
  374. printf(" bytes\n") ;
  375. }
  376. void printtopplace_() {
  377. DoubleChainedBlock *stack = curStack ;
  378. int nbBlocks = (stack?-1:0) ;
  379. int remainder = 0;
  380. while(stack) {
  381. stack = stack->prev ;
  382. nbBlocks++ ;
  383. }
  384. if (curStack && curStackTop) remainder = curStackTop-(curStack->contents) ;
  385. printf(" Stack size: ") ;
  386. printbigbytes((long int)nbBlocks, ONE_BLOCK_SIZE, (long int)remainder) ;
  387. printf(" bytes\n") ;
  388. }
  389. void printtopplacenum_(int *n) {
  390. DoubleChainedBlock *stack = curStack ;
  391. int nbBlocks = (stack?-1:0) ;
  392. int remainder = 0;
  393. while(stack) {
  394. stack = stack->prev ;
  395. nbBlocks++ ;
  396. }
  397. if (curStack && curStackTop) remainder = curStackTop-(curStack->contents) ;
  398. printf(" Stack size at location %i : ", *n) ;
  399. printbigbytes((long int)nbBlocks, ONE_BLOCK_SIZE, (long int)remainder) ;
  400. printf(" bytes\n") ;
  401. }
  402. void printstackmax_() {
  403. DoubleChainedBlock *stack = curStack ;
  404. int nbBlocks = (stack?-2:0) ;
  405. int remainder = 0;
  406. long int totalsz ;
  407. while(stack) {
  408. stack = stack->prev ;
  409. nbBlocks++ ;
  410. }
  411. stack = curStack ;
  412. while(stack) {
  413. stack = stack->next ;
  414. nbBlocks++ ;
  415. }
  416. printf(" Max Stack size (%i blocks): ", nbBlocks) ;
  417. printbigbytes((long int)nbBlocks, ONE_BLOCK_SIZE, (long int)0) ;
  418. printf(" bytes\n") ;
  419. }
  420. void printlookingplace_() {
  421. if (lookStack == NULL)
  422. printtopplace_() ;
  423. else {
  424. DoubleChainedBlock *stack = lookStack ;
  425. int nbBlocks = (stack?-1:0) ;
  426. while(stack) {
  427. stack = stack->prev ;
  428. nbBlocks++ ;
  429. }
  430. printf(" Stack look at: ") ;
  431. printbigbytes((long int)nbBlocks, ONE_BLOCK_SIZE,
  432. ((long int)(lookStackTop-(lookStack->contents)))) ;
  433. printf(" bytes\n") ;
  434. }
  435. }