home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 2.ddi / GCOBJECT.ZIP / GCOBJECT.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-09  |  2.8 KB  |  77 lines

  1. /**************************************************************************
  2. These C++ classes are copyright 1989, 1990, by William Herrera.
  3. All those who put this code or its derivatives in a commercial product MUST
  4. mention this copyright in their documentation for users of the products in
  5. which this code or its derivative classes are used.  Otherwise, this code
  6. may be freely distributed and used for any purpose.
  7. William Herrera
  8. contacts:
  9. GENIE
  10. FidoNet c_plusplus
  11. **************************************************************************/
  12. // file gcobject.hpp declaration of gcobject class.
  13. // This base class does garbage collection.
  14. // This class is all static.
  15. // The function Allocate() is used instead of new().
  16. // Derived classes may use this class to overload new(), etc.
  17.  
  18. #ifndef GCOBJECT_HPP
  19. #define GCOBJECT_HPP 1
  20.  
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <dos.h>
  24. #include <limits.h>
  25.  
  26. #ifdef __ZTC__
  27. // Zortech uses cfront version 1 streams for i/o
  28. #include <stream.hpp>
  29. #else ifdef __TURBOC__
  30. // Borland uses cfront version 2 iostreams and fstreams
  31. #include <fstream.h>
  32. #include <alloc.h>
  33. // Borland uses this to define size_t
  34. #endif
  35.  
  36. extern char heap_alloc_err[];
  37. extern char gcobject_alloc_err[];
  38. extern char cr[];
  39.  
  40.  
  41. class gcobject
  42. {
  43. // these are static parts used by the allocation function.
  44.     static char * buffer_start; // start of buffer used by Allocate().
  45.     static unsigned int buffer_size; // size of buffer used by Allocate().
  46.     static unsigned int allocated_size;  // amount of buffer in use.
  47.     static int garbage_collection_interval;    // number of destructor calls
  48.                                 // to be made prior to garbage collection.
  49.     static int delete_count;    // incremented on destructor calls.
  50.     static int CollectGarbage();// compacts the buffer.
  51.     // these are the data parts allocated by Allocate().
  52. protected:
  53.     struct data_record
  54.     {
  55.         size_t alloc_length;    // size of this allocation.
  56.         int ref_count;          // > 0 if allocated, == 0 if deleted.
  57.         data_record ** owner_record;
  58.         // pointer to the owner's pointer to this.
  59.         char data[1];           // dummy address of allocated memory.
  60.     };                            // allocated memory block goes beyond data[].
  61.     static void FatalError(char * p, char * q = cr, int errlevel = 1)
  62.     {
  63.         cerr << p << q;        // output one or two part message
  64.         exit(errlevel);        // and abort at errorlevel default 1.
  65.     }
  66. public:
  67.     static void Initialize(unsigned int buf_size = 10000, int gci = 10000);
  68.     static data_record * Allocate(size_t sz, data_record ** owner);
  69.     static void IncDeleteCount();
  70. #ifdef DEBUG
  71.     static void DumpBuffer();    // allows us to see what's in the buffer.
  72. #endif
  73. };
  74.  
  75. #endif
  76. // end file gcobject.hpp
  77.