home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_02 / 3n02008a < prev    next >
Text File  |  1991-12-26  |  3KB  |  99 lines

  1. #ifndef CHALLOC_H
  2. #define CHALLOC_H
  3.  
  4.  
  5. #include <assert.h>
  6. #include <stdlib.h>
  7.  
  8. // challoc.h - header file for "chunk" memory allocator
  9.  
  10. class   ChunkRoot
  11.     {
  12. public:
  13.     ChunkRoot(size_t ElementSize_);
  14.     ~ChunkRoot();
  15.     enum    {
  16.         NUMBER_OF_BINS = 10, CHUNK_SIZE = 4096,
  17.         MAX_OBJECT = CHUNK_SIZE/4
  18.         };
  19.  
  20.  
  21. private: friend class ChunkAllocated;
  22.     struct  ChunkRootLink
  23.         {
  24.         ChunkRootLink   *Next;
  25.         union
  26.             {
  27.             long double LDAlign;
  28.             double      DAlign;
  29.             long        LAlign;
  30.             void        *VAlign;
  31.             };
  32.         void    *Chunk() { return (void *)&LDAlign; }
  33.         };
  34.     void            AddChunk();
  35.     ChunkRootLink   *Root;
  36.     ChunkAllocated  *FreeList;
  37.     size_t          ElementSize;
  38.     size_t          ElementsPerChunk;
  39.     };
  40.  
  41. class   ChunkAllocated
  42.     {
  43. friend class ChunkRoot;
  44. public:
  45.     void    *operator new(size_t SizeInBytes);
  46.     void    operator delete(void *);
  47. protected:
  48.     union
  49.         {
  50.         ChunkAllocated  *Next;
  51.         ChunkRoot       *Root;
  52.         };
  53. static  ChunkRoot   Roots[ChunkRoot::NUMBER_OF_BINS];
  54.     };
  55.  
  56. inline void ChunkAllocated::operator delete(void *Instance_)
  57.     {
  58.     ChunkAllocated  *Instance   = (ChunkAllocated *)Instance_;
  59.     ChunkRoot       *Root       = Instance->Root;
  60.     Instance->Next              = Root->FreeList;
  61.     Root->FreeList              = Instance;
  62.     }
  63.  
  64. inline void * ChunkAllocated::operator new(size_t SizeInBytes)
  65.     {
  66.     ChunkRoot       *Root;
  67.     ChunkAllocated  *NewInstance;
  68.  
  69.     assert(SizeInBytes < ChunkRoot::MAX_OBJECT);
  70.     if(SizeInBytes <= 2)
  71.         Root    = &Roots[0];
  72.     else if(SizeInBytes <= 4)
  73.         Root    = &Roots[1];
  74.     else if(SizeInBytes <= 8)
  75.         Root    = &Roots[2];
  76.     else if(SizeInBytes <= 16)
  77.         Root    = &Roots[3];
  78.     else if(SizeInBytes <= 32)
  79.         Root    = &Roots[4];
  80.     else if(SizeInBytes <= 64)
  81.         Root    = &Roots[5];
  82.     else if(SizeInBytes <= 128)
  83.         Root    = &Roots[6];
  84.     else if(SizeInBytes <= 256)
  85.         Root    = &Roots[7];
  86.     else if(SizeInBytes <= 512)
  87.         Root    = &Roots[8];
  88.     else
  89.         Root    = &Roots[9];
  90.     if(!Root->FreeList)
  91.         Root->AddChunk();
  92.     NewInstance         = Root->FreeList;
  93. // Point back to root, so operator delete can find it
  94.     Root->FreeList      = NewInstance->Next;
  95.     NewInstance->Root   = Root;
  96.     return (void *)NewInstance;
  97.     }
  98. #endif
  99.