home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / GCSTRI / GCOBJECT.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  2.0 KB  |  68 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 gcobject.hpp declaration of gcobject class.
  8. // This base class does garbage collection.
  9. // The function Allocate() is used instead of new().
  10.  
  11. #if !defined(GCOBJECT_HPP)
  12. #define GCOBJECT_HPP 
  13.  
  14. #include <stddef.h>
  15.  
  16.  
  17. extern const char handle_alloc_err[];
  18. extern const char gcobject_alloc_err[];
  19.  
  20. struct data_record
  21. {
  22.     size_t alloc_length;    // size of this allocation.
  23.     int ref_count;            // > 0 if allocated, == 0 if deleted.
  24.     data_record ** owner_record; // pointer to owner's pointer to this.
  25.     char data[1];           // dummy address of allocated memory.
  26. };
  27.  
  28. class gcobject
  29. {
  30. public:
  31.     // constructor
  32.     gcobject(size_t buf_size = 10000, int gci = 1000);
  33.  
  34.     // destructor
  35.     ~gcobject();
  36.  
  37.     // allocation function (garbage collected--no deallocator)
  38.     void Allocate(size_t sz, data_record ** owner);
  39.  
  40.     // for incremental garbage collection
  41.     void IncDeleteCount();
  42.  
  43.     // buffer locking and unlocking--need to have one unlock per lock.
  44.     void Lock();
  45.     void Unlock();
  46.  
  47.     // force complete collection of buffer if unlocked.
  48.     void CompactBuffer();
  49.  
  50. #ifdef DEBUG
  51.     void DumpBuffer();    // allows us to see what's in the buffer.
  52. #endif
  53.  
  54. private:
  55.     int lockcount;
  56.     char * buffer_start;            // start of buffer used by Allocate().
  57.     size_t buffer_size;                // size of buffer used by Allocate().
  58.     size_t allocated_size;            // amount of buffer in use.
  59.     int garbage_collection_interval;// destruct count prior to collection.
  60.     int delete_count;                // incremented on destructor calls.
  61.     int CollectGarbage();            // compacts the buffer.
  62. };
  63.  
  64.  
  65. #endif
  66.  
  67. // end of file gcobject.hpp
  68.