home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Python 1.3 / source code / Mac / mwerks / mwerksglue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-17  |  2.4 KB  |  166 lines  |  [TEXT/R*ch]

  1. /*
  2. ** Glue code for MetroWerks CodeWarrior, which misses
  3. ** unix-like routines for file-access.
  4. */
  5.  
  6. #ifdef __MWERKS__
  7. #include <Types.h>
  8. #include <Files.h>
  9. #include <Strings.h>
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <errno.h>
  14.  
  15. /* #define DBGMALLOC /**/
  16.  
  17. #ifdef DBGMALLOC
  18. #define NMALLOC 50000
  19. static long m_index;
  20. static long m_addrs[NMALLOC];
  21. static long m_sizes[NMALLOC];
  22. static long m_lastaddr;
  23.  
  24. #define SSLOP 2
  25. #define SLOP (SSLOP+0)
  26.  
  27. void
  28. m_abort() {
  29.     printf("ABORT\n");
  30.     exit(1);
  31. }
  32.  
  33. void *
  34. checkrv(ptr, size)
  35.     void *ptr;
  36.     int size;
  37. {
  38.     long b = (long)ptr, e = (long)ptr + size+SSLOP;
  39.     int i;
  40.     
  41.     if ( m_index >= NMALLOC ) {
  42.         printf("too many mallocs\n");
  43.         m_abort();
  44.     }
  45.     m_lastaddr = (long)ptr;
  46.     for(i=0; i<m_index; i++) {
  47.         if ( m_addrs[i] > 0 && b < m_addrs[i]+m_sizes[i] && e > m_addrs[i] ) {
  48.             printf("overlapping block with %d\n", i);
  49.             m_abort();
  50.         }
  51.     }
  52.     m_sizes[m_index] = size;
  53.     m_addrs[m_index++] = (long)ptr;
  54.     *(short *)ptr = m_index-1;
  55.     return (void *)((char *)ptr+SSLOP);
  56.     
  57. }
  58.  
  59. void *
  60. checkfree(ptr)
  61.     void *ptr;
  62. {
  63.     int i;
  64.     
  65.     if ( ptr == 0 ) {
  66.         printf("free null\n");
  67.         m_abort();
  68.     }
  69.     m_lastaddr = (long)ptr;
  70.     for(i=0; i<m_index; i++) {
  71.         if ( m_addrs[i] == (long)ptr-SSLOP ) {
  72.             m_addrs[i] = -m_addrs[i];
  73.             ptr = (void *)((char *)ptr-SSLOP);
  74.             if ( *(short *)ptr != i ) {
  75.                 int saved_i = i, saved_ptr = *(short *)ptr;
  76.                 
  77.                 printf("Wrong starter value\n");
  78.                 m_abort();
  79.             }
  80.             return ptr;
  81.         }
  82.     }
  83.     printf("free unknown\n");
  84.     m_abort();
  85.     return 0;
  86. }
  87.  
  88. void *
  89. m_malloc(size)
  90. {
  91.     void *ptr;
  92.     
  93.     ptr = malloc(size+SLOP);
  94.     if ( !ptr ) {
  95.         printf("malloc failure\n");
  96.         m_abort();
  97.     }
  98.     return checkrv(ptr, size);
  99. }
  100.  
  101. void *
  102. m_realloc(optr, size)
  103.     void *optr;
  104. {
  105.     void *ptr;
  106.     
  107.     ptr = checkfree(ptr);
  108.     ptr = realloc(ptr, size+SLOP);
  109.     if ( !ptr ) {
  110.         printf("realloc failure\n");
  111.         m_abort();
  112.     }
  113.     return checkrv(ptr, size);
  114. }
  115.  
  116. void *
  117. m_calloc(size, nelem)
  118. {
  119.     void *ptr;
  120.     
  121.     ptr = calloc(1, nelem*size+SLOP);
  122.     return checkrv(ptr, nelem*size);
  123. }
  124.  
  125. void
  126. m_free(ptr)
  127.     void *ptr;
  128. {
  129.     
  130.     ptr = checkfree(ptr);
  131.     free(ptr);
  132. }
  133. #endif /* DBGMALLOC */
  134.  
  135. #ifdef CW4
  136. int
  137. fileno(fp)
  138.     FILE *fp;
  139. {
  140.     if (fp==stdin) return 0;
  141.     else if (fp==stdout) return 1;
  142.     else if (fp==stderr) return 2;
  143.     else return 3;
  144. }
  145.  
  146. int
  147. isatty(fd)
  148.     int fd;
  149. {
  150.     return (fd >= 0 && fd <= 2);
  151. }
  152.  
  153. int
  154. unlink(old)
  155.     char *old;
  156. {
  157.     OSErr err;
  158.     
  159.     if ((err=FSDelete((ConstStr255Param)Pstring(old), 0)) == noErr)
  160.         return 0;
  161.     errno= err;
  162.     return -1;
  163. }
  164. #endif /* CW4 */
  165.  
  166. #endif /* __MWERKS__ */