home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************
- These C++ classes are copyright 1989, 1990, by William Herrera.
- All those who put this code or its derivatives in a commercial product MUST
- mention this copyright in their documentation for users of the products in
- which this code or its derivative classes are used. Otherwise, this code
- may be freely distributed and used for any purpose.
- William Herrera
- contacts:
- GENIE
- FidoNet c_plusplus
- **************************************************************************/
- // file gcobject.hpp declaration of gcobject class.
- // This base class does garbage collection.
- // This class is all static.
- // The function Allocate() is used instead of new().
- // Derived classes may use this class to overload new(), etc.
-
- #ifndef GCOBJECT_HPP
- #define GCOBJECT_HPP 1
-
- #include <stdlib.h>
- #include <string.h>
- #include <dos.h>
- #include <limits.h>
-
- #ifdef __ZTC__
- // Zortech uses cfront version 1 streams for i/o
- #include <stream.hpp>
- #else ifdef __TURBOC__
- // Borland uses cfront version 2 iostreams and fstreams
- #include <fstream.h>
- #include <alloc.h>
- // Borland uses this to define size_t
- #endif
-
- extern char heap_alloc_err[];
- extern char gcobject_alloc_err[];
- extern char cr[];
-
-
- class gcobject
- {
- // these are static parts used by the allocation function.
- static char * buffer_start; // start of buffer used by Allocate().
- static unsigned int buffer_size; // size of buffer used by Allocate().
- static unsigned int allocated_size; // amount of buffer in use.
- static int garbage_collection_interval; // number of destructor calls
- // to be made prior to garbage collection.
- static int delete_count; // incremented on destructor calls.
- static int CollectGarbage();// compacts the buffer.
- // these are the data parts allocated by Allocate().
- protected:
- struct data_record
- {
- size_t alloc_length; // size of this allocation.
- int ref_count; // > 0 if allocated, == 0 if deleted.
- data_record ** owner_record;
- // pointer to the owner's pointer to this.
- char data[1]; // dummy address of allocated memory.
- }; // allocated memory block goes beyond data[].
- static void FatalError(char * p, char * q = cr, int errlevel = 1)
- {
- cerr << p << q; // output one or two part message
- exit(errlevel); // and abort at errorlevel default 1.
- }
- public:
- static void Initialize(unsigned int buf_size = 10000, int gci = 10000);
- static data_record * Allocate(size_t sz, data_record ** owner);
- static void IncDeleteCount();
- #ifdef DEBUG
- static void DumpBuffer(); // allows us to see what's in the buffer.
- #endif
- };
-
- #endif
- // end file gcobject.hpp
-