home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / xplatfrm / tierra / portable.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-26  |  7.0 KB  |  298 lines

  1. /* portable.c  28-10-91  conditionally compiled functions for portability */
  2. /** Tierra Simulator V3.0: Copyright (c) 1991 Thomas S. Ray **/
  3.  
  4. #include "license.h"
  5.  
  6. #ifndef lint
  7. static char     portable_sccsid[] = "%W%    %G%";
  8. #endif
  9.  
  10. #include <stdio.h>
  11. #include "tierra.h"
  12. #include "extern.h"
  13.  
  14. #ifdef SOCKETS
  15. #include "allayer.h"
  16. #endif
  17.  
  18. /* free for arrays of greater than 64K */
  19. void thfree(ptr)
  20. I8s Hp  ptr;
  21. {
  22. #ifdef __TURBOC__
  23.     farfree(ptr);
  24. #endif
  25.  
  26. #ifdef OS2_MC
  27.     DosFreeSeg((I16u) ptr >> 16);
  28. #endif
  29.  
  30. #ifdef unix
  31.  
  32. #ifdef SOCKETS
  33.     ALFree( (char *)ptr );
  34. #else
  35.     free(ptr);
  36. #endif    /* SOCKETS */
  37.  
  38. #endif
  39.  
  40. #ifdef IBM3090
  41.     free(ptr);
  42. #endif
  43. }
  44.  
  45. /* realloc for arrays of greater than 64K */
  46. I8s Hp threalloc(ptr, siz)
  47. I8s Hp  ptr;
  48. I32u    siz;
  49. {
  50. #ifdef __TURBOC__
  51. /* note: due to a bug, Borland's Turbo C V 2.0 farrealloc() does not work */
  52. /*
  53.     if (siz > 65535)
  54.     {   sprintf(mes[0], "You have been caught by the Borland Turbo C V2.0");
  55.         sprintf(mes[1], "farrealloc bug.  To avoid this bug you must make");
  56.         sprintf(mes[2], "the initial size of the Cells array large enough");
  57.         sprintf(mes[3], "that it doesn't have to be reallocated.");
  58.         sprintf(mes[4], "suggestion: CellsSize = SoupSize / 100");
  59.         FEError(5);
  60.         exit(1);
  61.     }
  62. */
  63.     return (I8s  Hp) farrealloc(ptr,siz);
  64. #endif
  65.  
  66. #ifdef OS2_MC
  67.     I16u     NumSegs, Remain, Selector;
  68.     I8s  Hp  tp;
  69.  
  70.     NumSegs = (I16u) siz / 65536ul;
  71.     Remain  = (I16u) siz % 65536ul;
  72.     Selector = ((I16u) ptr >> 16);
  73.     DosReallocHuge(NumSegs,Remain,Selector);
  74.     return (void Hp) ptr;
  75. #endif
  76.  
  77. #ifdef unix
  78.     I8s *  tp;
  79.  
  80. #ifdef SOCKETS
  81.     tp = (I8s *) ALRealloc(ptr,siz);
  82. #else
  83.     tp = (I8s *) realloc(ptr,siz);
  84. #endif    /* SOCKETS */
  85.     if(tp == NULL)
  86.     {
  87. #ifdef SOCKETS
  88.         sprintf(mes[0],"threalloc error: ALRealloc failed");
  89. #else
  90.         sprintf(mes[0],"threalloc error: realloc failed");
  91. #endif    /* SOCKETS */
  92.         if (!hangup)
  93.             FEMessage(1);
  94.         else
  95.         {   sprintf(mes[1],"system being saved to disk");
  96.             FEMessage(2);
  97.         }
  98.         while(hangup) ;
  99. /*      WriteSoup(1); */
  100.         exit(0);
  101.     }
  102.     return tp;
  103.  
  104. #endif /* unix */
  105.  
  106. #ifdef IBM3090
  107.     return (I8s  *) realloc(ptr,siz);
  108. #endif
  109. }
  110.  
  111. /* calloc for arrays of greater than 64K */
  112. I8s Hp thcalloc(num, siz)
  113. I32u   num;
  114. I32u   siz;
  115. {
  116. #ifdef __TURBOC__
  117.     return (I8s Hp) farcalloc(num,siz);
  118. #endif
  119.  
  120. #ifdef OS2_MC
  121.     I32u     HugeSize, i, ASize;
  122.     I16u     NumSegs, Remain, rc;
  123.     I8s Hp  tp;
  124.     SEL      Selector;
  125.  
  126.     HugeSize = num * siz;
  127.     i = HugeSize / 65536ul;
  128.     NumSegs = (I16u) i;
  129.     Remain  = (I16u) HugeSize % 65536ul;
  130.     rc = DosAllocHuge(NumSegs,Remain,&Selector,3 * NumSegs,0);
  131.     if(rc)
  132.     {   sprintf(mes[0],"DosAllocHuge error = %u", rc);
  133.         FEMessage(1);
  134.         exit(1);
  135.     }
  136.     tp = (I8s Hp) ((I32u ) Selector << 16);
  137.     for(i = 0; i < HugeSize; i++)
  138.     {   *(tp + i) = (I8s) 0;
  139.     }
  140.     return (I8s Hp) tp;
  141. #endif
  142.  
  143. #ifdef unix
  144.     I8s *  tp;
  145.  
  146. #ifdef SOCKETS
  147.     tp = (I8s *) ALCalloc(num, siz);
  148. #else
  149.     tp = (I8s *) calloc(num, siz);
  150. #endif    /* SOCKETS */
  151.     if(tp == NULL)
  152.     {
  153. #ifdef SOCKETS
  154.         sprintf(mes[0],"threalloc error: ALCalloc failed");
  155. #else
  156.         sprintf(mes[0],"threalloc error: calloc failed");
  157. #endif    /* SOCKETS */
  158.         if (!hangup)
  159.             FEMessage(1);
  160.         else
  161.         {   sprintf(mes[1],"system being saved to disk");
  162.             FEMessage(2);
  163.         }
  164.         while(hangup) ;
  165. /*      WriteSoup(1); */
  166.         exit(0);
  167.     }
  168.     return tp;
  169.  
  170. #endif /* unix */
  171.  
  172. #ifdef IBM3090
  173.     return (I8s  *) calloc(num,siz);
  174. #endif
  175. }
  176.  
  177. I32u tfread(ptr, size, n, stream)
  178. I8s Hp  ptr;
  179. I32s  size, n;
  180. FILE  *stream;
  181. {   I32u  r_size = 0;
  182. #ifdef __TURBOC__
  183.     I32s  segs, rem, i = 0;
  184.  
  185.     segs = n / (I32s) UINT_MAX;
  186.     rem  = n % (I32s) UINT_MAX;
  187.     if(segs) for(i = 0; i < segs; i++)
  188.         r_size += fread((I8s *) ((I8s Hp) ptr + (i * size *
  189.             (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
  190.     if(rem) r_size += fread((I8s *) ((I8s Hp) ptr + (segs * size *
  191.             (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
  192.     if(r_size != n)
  193.     {   sprintf(mes[0],"tfread inconsistency: n = %ld  r_size = %ld",
  194.             n, r_size);
  195.         FEMessage(1);
  196.     }
  197.     return r_size;
  198. #endif
  199.  
  200. #ifdef OS2_MC
  201.     I32s  segs, rem, i = 0;
  202.  
  203.     segs = n / (I32s) UINT_MAX;
  204.     rem  = n % (I32s) UINT_MAX;
  205.     if(segs) for(i = 0; i < segs; i++)
  206.         r_size += fread((I8s *) ((I8s Hp) ptr + (i * size *
  207.             (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
  208.     if(rem) r_size += fread((I8s *) ((I8s Hp) ptr + (segs * size *
  209.             (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
  210.     if(r_size != n)
  211.     {   sprintf(mes[0],"tfread inconsistency: n = %ld  r_size = %ld",
  212.             n, r_size);
  213.         FEMessage(1);
  214.     }
  215.     return r_size;
  216. #endif
  217.  
  218. #ifdef unix
  219.     r_size = fread(ptr, size, n, stream);
  220.     if(r_size != n)
  221.     {   sprintf(mes[0],"tfread inconsistency: n = %ld  r_size = %ld",
  222.             n, r_size);
  223.         FEMessage(1);
  224.     }
  225.     return r_size;
  226. #endif
  227.  
  228. #ifdef IBM3090
  229.     r_size = fread(ptr, size, n, stream);
  230.     if(r_size != n)
  231.     {   sprintf(mes[0],"tfread inconsistency: n = %ld  r_size = %ld",
  232.             n, r_size);
  233.         FEMessage(1);
  234.     }
  235.     return r_size;
  236. #endif
  237. }
  238.  
  239. I32u tfwrite(ptr, size, n, stream)
  240. I8s Hp  ptr;
  241. I32s  size, n;
  242. FILE *  stream;
  243. {   I32u  r_size = 0;
  244. #ifdef __TURBOC__
  245.     I32s  segs, rem, i = 0;
  246.  
  247.     segs = n / (I32s) UINT_MAX;
  248.     rem  = n % (I32s) UINT_MAX;
  249.     if(segs) for(i = 0; i < segs; i++)
  250.         r_size += fwrite((const I8s *) ((I8s Hp) ptr + (i * size *
  251.             (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
  252.     if(rem) r_size += fwrite((const I8s *) ((I8s Hp) ptr + (segs * size *
  253.         (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
  254.     return r_size;
  255. #endif
  256.  
  257. #ifdef OS2_MC
  258.     I32s  segs, rem, i = 0;
  259.  
  260.     segs = n / (I32s) UINT_MAX;
  261.     rem  = n % (I32s) UINT_MAX;
  262.     if(segs) for(i = 0; i < segs; i++)
  263.         r_size += fwrite((const I8s *) ((I8s Hp) ptr + (i * size *
  264.             (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
  265.     if(rem) r_size += fwrite((const I8s *) ((I8s Hp) ptr + (segs * size *
  266.         (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
  267.     return r_size;
  268. #endif
  269.  
  270. #ifdef unix
  271.     r_size = fwrite(ptr, size, n, stream);
  272.     if(r_size != n)
  273.     {   sprintf(mes[0],"tfwrite inconsistency: n = %ld  r_size = %ld",
  274.             n, r_size);
  275.         FEMessage(1);
  276.     }
  277.     return r_size;
  278. #endif
  279.  
  280. #ifdef IBM3090
  281.     r_size = fwrite(ptr, size, n, stream);
  282.     if(r_size != n)
  283.     {   sprintf(mes[0],"tfwrite inconsistency: n = %ld  r_size = %ld",
  284.             n, r_size);
  285.         FEMessage(1);
  286.     }
  287.     return r_size;
  288. #endif
  289. }
  290.  
  291. I32u ffs(x) 
  292. I32s x;
  293. {
  294. I32u i, j;
  295.     for (i=32, j=1<<i-1; j && !(x&j); i--, j>>=1);
  296.     return i;
  297. }
  298.