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

  1. #include "presto.h"
  2.  
  3. FreeStacks::FreeStacks()
  4. {
  5.     fs_lock = new Spinlock();
  6.     fs_sp = 0;
  7. }
  8.  
  9.  
  10. FreeStacks::~FreeStacks()
  11. {
  12.  
  13.     fs_lock->lock();
  14.  
  15.     register int end = fs_sp - 1;
  16.  
  17.     while (end >= 0)    {
  18.         fs_free[end]->destroy();
  19.         end--;
  20.     }        
  21.     fs_lock->unlock();
  22.     delete fs_lock;
  23. }
  24.     
  25.     
  26. //
  27. // find a stack having the right size (or greater)
  28. //
  29. Stack*
  30. FreeStacks::find(int sz)
  31. {
  32.     Stack *s = 0;
  33.     register int end;
  34.     
  35.     fs_lock->lock();
  36.  
  37.     end = fs_sp - 1;
  38.     while (end >= 0)    {
  39.         if (fs_free[end]->limit() >= sz)    {
  40.             s = fs_free[end];
  41.             fs_free[end] = fs_free[--fs_sp];
  42.             break;
  43.         }
  44.         end--;
  45.     }
  46.     fs_lock->unlock();
  47.     return s;
  48. }
  49.  
  50. void
  51. FreeStacks::free(Stack *s)
  52. {
  53.     fs_lock->lock();
  54.     
  55.     if (fs_sp == MAXFREESTACKS)    {
  56.         fs_lock->unlock();
  57.         s->destroy();
  58.     }
  59.     else    {
  60.         fs_free[fs_sp++] = s;
  61.         fs_lock->unlock();
  62.     }
  63. }
  64.  
  65.  
  66. static shared_t stackcnt = 0;
  67. static shared_t FreeStacks freestacks;
  68. //
  69. // freestacks really belongs as a static member of Stack, but we can't
  70. // have static members with constructors.
  71. //
  72.  
  73. Stack::Stack(int sz)
  74. {
  75.     register Stack *s = 0;
  76.  
  77.     if (this)    {
  78.         error("Can only allocate a stack on the heap");
  79.     }
  80.     
  81.     if (sz)    {
  82.         sz = sz &(~0x200);        // force page size boundaries
  83.         s = freestacks.find(sz);
  84.     } 
  85.     if (s)    {
  86.         this = s;
  87.     } else {
  88.         this = (Stack*)new char[sizeof(Stack)];
  89.         stackcnt++;
  90.         st_base = (int*)new char[sz];
  91.         st_size = sz;
  92.         st_limit = sz;
  93.     };
  94. }
  95.  
  96. #ifdef notdef
  97. #define DONTREUSESTACKS
  98. #endif
  99. Stack::~Stack()
  100. {
  101.     freestacks.free(this);
  102.     this = 0;        // prevent regular dealloc
  103. }
  104.     
  105. int
  106. Stack::numstacksbuilt()
  107.     return stackcnt;
  108. }        
  109.     
  110.  
  111.  
  112.     
  113.  
  114.             
  115.             
  116.