home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / !runtime / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  3.5 KB  |  153 lines  |  [TEXT/R*ch]

  1. #include <stdio.h>
  2. #include "config.h"
  3. #include "debugger.h"
  4. #include "misc.h"
  5. #include "io.h"
  6. #ifdef HAS_UI
  7. #include "ui.h"
  8. #endif
  9.  
  10. int verb_gc;
  11. int Volatile something_to_do = 0;
  12. int Volatile force_minor_flag = 0;
  13.  
  14. void force_minor_gc ()
  15. {
  16.   force_minor_flag = 1;
  17.   something_to_do = 1;
  18. }
  19.  
  20. void gc_message (msg, arg)
  21.      char *msg;
  22.      unsigned long arg;
  23. {
  24.   if (verb_gc){
  25. #ifdef HAS_UI
  26.     ui_gc_message(msg, arg);
  27. #else
  28.     fprintf (stderr, msg, arg);
  29.     fflush (stderr);
  30. #endif
  31.   }
  32. }
  33.  
  34. void fatal_error (msg)
  35.      char * msg;
  36. {
  37.   flush_stdouterr();
  38. #ifdef HAS_UI
  39.   ui_fatal_error("%s", msg);
  40. #else
  41.   fprintf (stderr, "%s", msg);
  42.   exit(2);
  43. #endif
  44. }
  45.  
  46. void fatal_error_arg (fmt, arg)
  47.      char * fmt, * arg;
  48. {
  49.   flush_stdouterr();
  50. #ifdef HAS_UI
  51.   ui_fatal_error(fmt, arg);
  52. #else
  53.   fprintf (stderr, fmt, arg);
  54.   exit(2);
  55. #endif
  56. }
  57.  
  58. #ifdef USING_MEMMOV
  59.  
  60. /* This should work on 64-bit machines as well as 32-bit machines.
  61.    It assumes a long is the natural size for memory reads and writes.
  62. */
  63. void memmov (dst, src, length)
  64.      char *dst, *src;
  65.      unsigned long length;
  66. {
  67.   unsigned long i;
  68.  
  69.   if ((unsigned long) dst <= (unsigned long) src){
  70.  
  71.       /* Copy in ascending order. */
  72.     if (((unsigned long) src - (unsigned long) dst) % sizeof (long) != 0){
  73.  
  74.         /* The pointers are not equal modulo sizeof (long).
  75.            Copy byte by byte. */
  76.       for (; length != 0; length--){
  77.     *dst++ = *src++;
  78.       }
  79.     }else{
  80.  
  81.         /* Copy the first few bytes. */
  82.       i = (unsigned long) dst % sizeof (long);
  83.       if (i != 0){
  84.     i = sizeof (long) - i;              /* Number of bytes to copy. */
  85.     if (i > length) i = length;         /* Never copy more than length.*/
  86.     for (; i != 0; i--){
  87.       *dst++ = *src++; --length;
  88.     }
  89.       }                    Assert ((unsigned long) dst % sizeof (long) == 0);
  90.                            Assert ((unsigned long) src % sizeof (long) == 0);
  91.  
  92.       /* Then copy as many entire words as possible. */
  93.       for (i = length / sizeof (long); i > 0; i--){
  94.     *(long *) dst = *(long *) src;
  95.     dst += sizeof (long); src += sizeof (long);
  96.       }
  97.  
  98.       /* Then copy the last few bytes. */
  99.       for (i = length % sizeof (long); i > 0; i--){
  100.     *dst++ = *src++;
  101.       }
  102.     }
  103.   }else{                                       /* Copy in descending order. */
  104.     src += length; dst += length;
  105.     if (((unsigned long) dst - (unsigned long) src) % sizeof (long) != 0){
  106.  
  107.         /* The pointers are not equal modulo sizeof (long).
  108.        Copy byte by byte. */
  109.       for (; length > 0; length--){
  110.     *--dst = *--src;
  111.       }
  112.     }else{
  113.  
  114.         /* Copy the first few bytes. */
  115.       i = (unsigned long) dst % sizeof (long);
  116.       if (i > length) i = length;           /* Never copy more than length. */
  117.       for (; i > 0; i--){
  118.     *--dst = *--src; --length;
  119.       }
  120.  
  121.         /* Then copy as many entire words as possible. */
  122.       for (i = length / sizeof (long); i > 0; i--){
  123.     dst -= sizeof (long); src -= sizeof (long);
  124.     *(long *) dst = *(long *) src;
  125.       }
  126.  
  127.         /* Then copy the last few bytes. */
  128.       for (i = length % sizeof (long); i > 0; i--){
  129.     *--dst = *--src;
  130.       }
  131.     }
  132.   }
  133. }
  134.  
  135. #endif /* USING_MEMMOV */
  136.  
  137. char *aligned_malloc (size, modulo)
  138.      asize_t size;
  139.      int modulo;
  140. {
  141.   char *raw_mem;
  142.   unsigned long aligned_mem;
  143. #ifndef __MWERKS__
  144.   extern char * malloc();
  145. #endif
  146.                                                  Assert (modulo < Page_size);
  147.   raw_mem = malloc (size + Page_size);
  148.   if (raw_mem == NULL) return NULL;
  149.   raw_mem += modulo;        /* Address to be aligned */
  150.   aligned_mem = (((unsigned long) raw_mem / Page_size + 1) * Page_size);
  151.   return (char *) (aligned_mem - modulo);
  152. }
  153.