home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / GCSTRI / POINTERH.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-25  |  1.3 KB  |  65 lines

  1. /**************************************************************************
  2. These C++ classes are copyright 1989, 1990, 1991 by William Herrera.
  3. I hereby release this source code for free distrubution and use.
  4. If you modify it and distribute it, please indicate any changes you
  5. make as your own and the code as copyrighted above.
  6. **************************************************************************/
  7. // file pointerh.cpp class definitions for PointerHeap class.
  8.  
  9. #include <iostream.h>
  10. #include <stdlib.h>
  11.  
  12. #include "error.hpp"
  13.  
  14. #include "pointerh.hpp"
  15.  
  16. PointerHeap::PointerHeap(int siz) : size(siz) 
  17. {
  18.     heap = new void *[size];
  19.     if(heap == NULL)
  20.         Error("\nCannot allocate memory\n");
  21.     else
  22.     {
  23.  
  24.         last = &heap[size - 1];
  25.         for(void **p = heap; p < last; )
  26.         {
  27.             void ** q = p + 1;
  28.             *p = (void *)q;
  29.             p = q;
  30.         }
  31.         *last = NULL;
  32.         nextAvailable = heap;
  33.     }
  34. }
  35.  
  36. PointerHeap::~PointerHeap()
  37. {
  38.     if(heap != NULL)
  39.         delete heap;
  40. }
  41.  
  42. void ** PointerHeap::Allocate()
  43. {
  44.     void ** retval = NULL;
  45.     if(nextAvailable != NULL)
  46.     {
  47.         retval = nextAvailable;
  48.         nextAvailable = (void **)(*nextAvailable);
  49.     }
  50.     return retval;
  51. }
  52.  
  53. void PointerHeap::Deallocate(void ** p)
  54. {
  55.     if(p >= heap && p <= last)
  56.     {
  57.         void ** temp = nextAvailable;
  58.         nextAvailable = p;
  59.         *p = (void *)temp;
  60.     }
  61. }
  62.  
  63.  
  64. // end of file pointerh.cpp
  65.