"Fossies" - the Fresh Open Source Software Archive

Member "heaplayers-351/heaplayers/experimental/strictslotheap.h" (3 May 2006, 1616 Bytes) of package /linux/misc/old/heaplayers_3_5_1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "strictslotheap.h" see the Fossies "Dox" file reference documentation.

    1 /* -*- C++ -*- */
    2 
    3 #ifndef _LAZYSLOTHEAP_H_
    4 #define _LAZYSLOTHEAP_H_
    5 
    6 /*
    7   This heap manages memory in units of Chunks.
    8   malloc returns a slot within a chunk,
    9   while free returns slots back to a chunk.
   10 
   11   We completely exhaust the first chunk before we ever get another one.
   12   Once a chunk is COMPLETELY empty, it is returned to the superheap.
   13 */
   14 
   15 #include <assert.h>
   16 #include <new.h>
   17 
   18 #include "chunk.h"
   19 
   20 template <int chunkSize, int slotSize, class Super>
   21 class LazySlotHeap : public Super {
   22 public:
   23 
   24   LazySlotHeap (void)
   25     : myChunk (new (Super::malloc (chunkSize)) Chunk<chunkSize, slotSize>())
   26   {}
   27 
   28   virtual ~LazySlotHeap (void)
   29   {
   30     // Give up our chunk.
   31     Super::free (myChunk);
   32   }
   33 
   34   inline void * malloc (size_t sz) {
   35     assert (sz <= chunkSize);
   36     void * ptr = myChunk->getSlot();
   37     if (ptr == NULL) {
   38       myChunk = new (Super::malloc (chunkSize)) Chunk<chunkSize, slotSize>();
   39       ptr = myChunk->getSlot();
   40       assert (ptr != NULL);
   41     }
   42     return ptr;
   43   }
   44 
   45   inline void free (void * ptr) {
   46     /// Return a slot to its chunk.
   47     Chunk<chunkSize, slotSize> * ch = Chunk<chunkSize, slotSize>::getChunk (ptr);
   48     ch->putSlot (ptr);
   49     // Once the chunk is completely empty, free it.
   50     if (ch->getNumSlotsAvailable() == ch->getNumSlots()) {
   51       if (ch == myChunk) {
   52     // If this was 'our' chunk, get another one.
   53     myChunk = new (Super::malloc (chunkSize)) Chunk<chunkSize, slotSize>();
   54       }
   55       Super::free (ch);
   56     }
   57   }
   58 
   59 protected:
   60 
   61   inline static size_t size (void * ptr)
   62   {
   63       return slotSize;
   64   }
   65 
   66 
   67 private:
   68 
   69   Chunk<chunkSize, slotSize> * myChunk;
   70 
   71 };
   72 
   73 
   74 #endif