home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cslio205.zip / INCLUDE / CSHEAP.H < prev    next >
C/C++ Source or Header  |  1997-01-21  |  2KB  |  87 lines

  1. /***********************************************************************
  2.  
  3.                        CSA Library, Free Evaluation Version 2.0.5 
  4.                                        Release: January 22th 1997 
  5.  
  6.        Class to avoid heap fragmentation.
  7.        It allocates and frees memory in chunks of about 2 Kb and
  8.        does its own administration to deal with the many
  9.        small blocks.
  10.  
  11.  
  12.                                            Copyright(c) 1994-1997 
  13.                                                           ComBits 
  14.                                                   The Netherlands 
  15. ***********************************************************************/
  16.  
  17. #ifndef __CSHEAP_H
  18. #define __CSHEAP_H
  19.  
  20. #include "stdio.h"
  21. #include "cstools.h"
  22. #include "cstypes.h"
  23.  
  24. class HEAP
  25. {
  26.    U16 size;    // Chunck size
  27.    U16 nr;      // Number of allocations in a page.
  28.    U16 rs;      // Real chunk size;
  29.    U16 pasi;    // Page size;
  30.    U32 nrbl;    // Number of Blocks allocated.
  31.    U16 is_open;
  32.  
  33.    void *np;    // Next page
  34.    void *fp;    // Chain of full pages.
  35.  
  36.    typedef struct
  37.    {
  38.      void *n;   // next page
  39.      void *p;   // prev page
  40.      void *e;   // empty chain
  41.      U16  count;// number available
  42.    } pahe;      // Page HEader
  43.  
  44.  
  45. protected:
  46.    void connect(void *l,void *r);
  47.    void *new_page(void);
  48.    void unchain(void *&chain,void *p);
  49.    void head_chain(void *&chain,void *p);
  50.    void zap2(void );
  51.  
  52. public:
  53.  
  54.    ~HEAP(void) { if (is_open) close(); }
  55.    HEAP(void)  { is_open=FALSE; fp=np=NULL; nrbl=0; size=0; }
  56.  
  57. ////////////////////// Compatibility functions //////////////////////////
  58. #ifndef _CP_003
  59.    // The GNU compiler generates an error on these functions...
  60.  
  61.    void free(void *p) { vfree(p); }
  62.    void *malloc(void) { return vmalloc(); }
  63. #endif
  64.  
  65. ////////////////////// Init/Creation ////////////////////////////////////
  66.    void init(U16 s,U16 page_size=2048);
  67.  
  68. ////////////////////// Open/Close ///////////////////////////////////////
  69.    void close(void);
  70.    int  open(void);
  71.  
  72. ////////////////////// Malloc/Free //////////////////////////////////////
  73.    void vfree(void *);
  74.    void *vmalloc(void);
  75.  
  76.  
  77.    void empty(void );
  78.    void zap(void );
  79.    U32  blocks(void) { return nrbl; }
  80.    void report(FILE *fp,int sub);
  81.  
  82.  
  83. };
  84.  
  85.  
  86. #endif
  87.