home *** CD-ROM | disk | FTP | other *** search
- /* portable.c 28-10-91 conditionally compiled functions for portability */
- /** Tierra Simulator V3.0: Copyright (c) 1991 Thomas S. Ray **/
-
- #include "license.h"
-
- #ifndef lint
- static char portable_sccsid[] = "%W% %G%";
- #endif
-
- #include <stdio.h>
- #include "tierra.h"
- #include "extern.h"
-
- #ifdef SOCKETS
- #include "allayer.h"
- #endif
-
- /* free for arrays of greater than 64K */
- void thfree(ptr)
- I8s Hp ptr;
- {
- #ifdef __TURBOC__
- farfree(ptr);
- #endif
-
- #ifdef OS2_MC
- DosFreeSeg((I16u) ptr >> 16);
- #endif
-
- #ifdef unix
-
- #ifdef SOCKETS
- ALFree( (char *)ptr );
- #else
- free(ptr);
- #endif /* SOCKETS */
-
- #endif
-
- #ifdef IBM3090
- free(ptr);
- #endif
- }
-
- /* realloc for arrays of greater than 64K */
- I8s Hp threalloc(ptr, siz)
- I8s Hp ptr;
- I32u siz;
- {
- #ifdef __TURBOC__
- /* note: due to a bug, Borland's Turbo C V 2.0 farrealloc() does not work */
- /*
- if (siz > 65535)
- { sprintf(mes[0], "You have been caught by the Borland Turbo C V2.0");
- sprintf(mes[1], "farrealloc bug. To avoid this bug you must make");
- sprintf(mes[2], "the initial size of the Cells array large enough");
- sprintf(mes[3], "that it doesn't have to be reallocated.");
- sprintf(mes[4], "suggestion: CellsSize = SoupSize / 100");
- FEError(5);
- exit(1);
- }
- */
- return (I8s Hp) farrealloc(ptr,siz);
- #endif
-
- #ifdef OS2_MC
- I16u NumSegs, Remain, Selector;
- I8s Hp tp;
-
- NumSegs = (I16u) siz / 65536ul;
- Remain = (I16u) siz % 65536ul;
- Selector = ((I16u) ptr >> 16);
- DosReallocHuge(NumSegs,Remain,Selector);
- return (void Hp) ptr;
- #endif
-
- #ifdef unix
- I8s * tp;
-
- #ifdef SOCKETS
- tp = (I8s *) ALRealloc(ptr,siz);
- #else
- tp = (I8s *) realloc(ptr,siz);
- #endif /* SOCKETS */
- if(tp == NULL)
- {
- #ifdef SOCKETS
- sprintf(mes[0],"threalloc error: ALRealloc failed");
- #else
- sprintf(mes[0],"threalloc error: realloc failed");
- #endif /* SOCKETS */
- if (!hangup)
- FEMessage(1);
- else
- { sprintf(mes[1],"system being saved to disk");
- FEMessage(2);
- }
- while(hangup) ;
- /* WriteSoup(1); */
- exit(0);
- }
- return tp;
-
- #endif /* unix */
-
- #ifdef IBM3090
- return (I8s *) realloc(ptr,siz);
- #endif
- }
-
- /* calloc for arrays of greater than 64K */
- I8s Hp thcalloc(num, siz)
- I32u num;
- I32u siz;
- {
- #ifdef __TURBOC__
- return (I8s Hp) farcalloc(num,siz);
- #endif
-
- #ifdef OS2_MC
- I32u HugeSize, i, ASize;
- I16u NumSegs, Remain, rc;
- I8s Hp tp;
- SEL Selector;
-
- HugeSize = num * siz;
- i = HugeSize / 65536ul;
- NumSegs = (I16u) i;
- Remain = (I16u) HugeSize % 65536ul;
- rc = DosAllocHuge(NumSegs,Remain,&Selector,3 * NumSegs,0);
- if(rc)
- { sprintf(mes[0],"DosAllocHuge error = %u", rc);
- FEMessage(1);
- exit(1);
- }
- tp = (I8s Hp) ((I32u ) Selector << 16);
- for(i = 0; i < HugeSize; i++)
- { *(tp + i) = (I8s) 0;
- }
- return (I8s Hp) tp;
- #endif
-
- #ifdef unix
- I8s * tp;
-
- #ifdef SOCKETS
- tp = (I8s *) ALCalloc(num, siz);
- #else
- tp = (I8s *) calloc(num, siz);
- #endif /* SOCKETS */
- if(tp == NULL)
- {
- #ifdef SOCKETS
- sprintf(mes[0],"threalloc error: ALCalloc failed");
- #else
- sprintf(mes[0],"threalloc error: calloc failed");
- #endif /* SOCKETS */
- if (!hangup)
- FEMessage(1);
- else
- { sprintf(mes[1],"system being saved to disk");
- FEMessage(2);
- }
- while(hangup) ;
- /* WriteSoup(1); */
- exit(0);
- }
- return tp;
-
- #endif /* unix */
-
- #ifdef IBM3090
- return (I8s *) calloc(num,siz);
- #endif
- }
-
- I32u tfread(ptr, size, n, stream)
- I8s Hp ptr;
- I32s size, n;
- FILE *stream;
- { I32u r_size = 0;
- #ifdef __TURBOC__
- I32s segs, rem, i = 0;
-
- segs = n / (I32s) UINT_MAX;
- rem = n % (I32s) UINT_MAX;
- if(segs) for(i = 0; i < segs; i++)
- r_size += fread((I8s *) ((I8s Hp) ptr + (i * size *
- (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
- if(rem) r_size += fread((I8s *) ((I8s Hp) ptr + (segs * size *
- (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
- if(r_size != n)
- { sprintf(mes[0],"tfread inconsistency: n = %ld r_size = %ld",
- n, r_size);
- FEMessage(1);
- }
- return r_size;
- #endif
-
- #ifdef OS2_MC
- I32s segs, rem, i = 0;
-
- segs = n / (I32s) UINT_MAX;
- rem = n % (I32s) UINT_MAX;
- if(segs) for(i = 0; i < segs; i++)
- r_size += fread((I8s *) ((I8s Hp) ptr + (i * size *
- (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
- if(rem) r_size += fread((I8s *) ((I8s Hp) ptr + (segs * size *
- (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
- if(r_size != n)
- { sprintf(mes[0],"tfread inconsistency: n = %ld r_size = %ld",
- n, r_size);
- FEMessage(1);
- }
- return r_size;
- #endif
-
- #ifdef unix
- r_size = fread(ptr, size, n, stream);
- if(r_size != n)
- { sprintf(mes[0],"tfread inconsistency: n = %ld r_size = %ld",
- n, r_size);
- FEMessage(1);
- }
- return r_size;
- #endif
-
- #ifdef IBM3090
- r_size = fread(ptr, size, n, stream);
- if(r_size != n)
- { sprintf(mes[0],"tfread inconsistency: n = %ld r_size = %ld",
- n, r_size);
- FEMessage(1);
- }
- return r_size;
- #endif
- }
-
- I32u tfwrite(ptr, size, n, stream)
- I8s Hp ptr;
- I32s size, n;
- FILE * stream;
- { I32u r_size = 0;
- #ifdef __TURBOC__
- I32s segs, rem, i = 0;
-
- segs = n / (I32s) UINT_MAX;
- rem = n % (I32s) UINT_MAX;
- if(segs) for(i = 0; i < segs; i++)
- r_size += fwrite((const I8s *) ((I8s Hp) ptr + (i * size *
- (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
- if(rem) r_size += fwrite((const I8s *) ((I8s Hp) ptr + (segs * size *
- (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
- return r_size;
- #endif
-
- #ifdef OS2_MC
- I32s segs, rem, i = 0;
-
- segs = n / (I32s) UINT_MAX;
- rem = n % (I32s) UINT_MAX;
- if(segs) for(i = 0; i < segs; i++)
- r_size += fwrite((const I8s *) ((I8s Hp) ptr + (i * size *
- (I32s) UINT_MAX)), (I16u) size, (I16u) UINT_MAX, stream);
- if(rem) r_size += fwrite((const I8s *) ((I8s Hp) ptr + (segs * size *
- (I32s) UINT_MAX)), (I16u) size, (I16u) rem, stream);
- return r_size;
- #endif
-
- #ifdef unix
- r_size = fwrite(ptr, size, n, stream);
- if(r_size != n)
- { sprintf(mes[0],"tfwrite inconsistency: n = %ld r_size = %ld",
- n, r_size);
- FEMessage(1);
- }
- return r_size;
- #endif
-
- #ifdef IBM3090
- r_size = fwrite(ptr, size, n, stream);
- if(r_size != n)
- { sprintf(mes[0],"tfwrite inconsistency: n = %ld r_size = %ld",
- n, r_size);
- FEMessage(1);
- }
- return r_size;
- #endif
- }
-
- I32u ffs(x)
- I32s x;
- {
- I32u i, j;
- for (i=32, j=1<<i-1; j && !(x&j); i--, j>>=1);
- return i;
- }
-