home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest_04.lha / src / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-08  |  2.2 KB  |  146 lines

  1. /*
  2.  *    misc.c
  3.  *
  4.  *     General mishmash that doesn't belong anywhere else
  5.  *    - fatalerror()        -- abort and die horribly
  6.  *    - overloaded new and delete
  7.  */ 
  8.  
  9. #define _MISC_C
  10.  
  11. #include <stream.h>
  12. #include <osfcn.h>
  13. #include "presto.h"
  14.  
  15. void
  16. error(char *s)
  17. {
  18.     cerr << s << "\n";
  19.     fatalerror();
  20. }
  21.  
  22. void
  23. fatalerror()
  24. {
  25.     cerr << "Aborting....\n";
  26.     cout.flush();
  27.     cerr.flush();
  28.     abort();
  29. }
  30.  
  31. //
  32. // We have to redefine the new and delete functions so they malloc
  33. // in shared memory.
  34. //
  35.  
  36. typedef void (*PFVV)();
  37.  
  38. extern PFVV _new_handler;
  39.  
  40. typedef char*    (*PFUC)(unsigned);        // for malloc
  41. typedef void    (*PFVC)(char*);            // for free
  42.  
  43. extern char* malloc(unsigned);
  44. extern void  free(char*);
  45.  
  46. //
  47. // The memory allocator should be called "malloc" in all
  48. // Presto versions.  That prevents the c-library malloc from being
  49. // linked in and called accidentally.  A call to the c-library
  50. // malloc will cause Topaz Presto to get blown out of the water.
  51. //
  52. #define MALLOC(x)        malloc(x)
  53. #define FREE(x)            free(x)
  54. PFUC    mallocf = malloc;
  55. PFVC    freef = free;
  56.  
  57. extern void* operator new(long size)
  58. {
  59.     char* p;
  60.  
  61.     while ( (p=MALLOC(unsigned(size)))==0 ) {
  62.         if(_new_handler)
  63.             (*_new_handler)();
  64.         else    
  65.             return 0;
  66.     }
  67.     return (void*)p;
  68. }
  69.  
  70.  
  71. extern void operator delete(void* p)
  72. {
  73.     if (p) FREE( (char*)p );
  74. }
  75.  
  76. //
  77. // Dangerous: If you do a set_{malloc/free}_agent from shared to non-shared
  78. // or the other way around, anything which has been malloced in one
  79. // form might get demalloced in another.  Of course, the classes
  80. // can remember this for themselves and make sure to set the
  81. // types correctly.
  82.  
  83. PFUC
  84. set_malloc_agent(PFUC nmalloc)
  85. {
  86.     PFUC omalloc = mallocf;
  87.  
  88.     mallocf = nmalloc;
  89.     return omalloc;
  90. }
  91.  
  92. PFVC
  93. set_free_agents(PFVC nfree)
  94. {
  95.     PFVC ofree = freef;
  96.     freef = nfree;
  97.     return ofree;
  98. }
  99.  
  100. void failed_malloc()
  101. {
  102.     cerr << "operator new failed: out of store\n";
  103.     fatalerror();
  104. }
  105.  
  106. void
  107. thisthread_holdingspinlock()
  108. {
  109.     thisthread->holdingspinlock();
  110. }
  111.  
  112. void
  113. thisthread_releasingspinlock()
  114. {
  115.     thisthread->releasingspinlock();
  116. }
  117.  
  118.  
  119. #ifdef sun
  120. int cpus_online() { return 1; }
  121. char* shmalloc(long s) { return malloc(s); }
  122. void shfree(char* x) { free(x); }
  123. #endif sun
  124.  
  125. #ifdef vax
  126.  
  127. int
  128. cpus_online()
  129. {
  130.     return 1;
  131. }
  132.  
  133. char*
  134. shmalloc(long s)
  135. {
  136.     return malloc(s);
  137. }
  138.  
  139. void
  140. shfree(char* x)
  141. {
  142.     free(x);
  143. }
  144.  
  145. #endif vax
  146.