Eneboo - Documentación para desarrolladores
src/hoard/src/heaplayers/experimental/slotheap.h
Ir a la documentación de este archivo.
00001 /* -*- C++ -*- */
00002 
00003 #ifndef _SLOTHEAP_H_
00004 #define _SLOTHEAP_H_
00005 
00006 // NOTE: All size requests to malloc must be identical!
00007 
00008 /*
00009 
00010   A "slot" allocator.
00011 
00012   All allocations come from a fixed-size chunk of memory
00013   that is carved into a number of pieces.
00014 
00015   The "chunk" class must support the following methods:
00016 
00017   void * getSlot (void); // Returns NULL if there is no slot left.
00018   void putSlot (void *); // Puts a slot back into its chunk.
00019 
00020 */
00021 
00022 #include <assert.h>
00023 
00024 #include "chunkheap.h"
00025 
00026 /* A "slot" heap.
00027 
00028    This heap reserves exactly one "chunk" that is divided into
00029    a number of fixed-size slots. When the chunk is used up,
00030    the heap requests another one. */
00031 
00032 template <int chunkSize, int slotSize, class Super>
00033 class SlotInterface;
00034 
00035 template <int chunkSize, int slotSize, class Super>
00036 class SlotHeap : public SlotInterface<chunkSize, slotSize, ChunkHeap<chunkSize, slotSize, Super> >{};
00037 
00038 template <int chunkSize, int slotSize, class Super>
00039 class SlotInterface : public Super {
00040 public:
00041 
00042   SlotInterface (void)
00043     : currentChunk (new (Super::malloc(chunkSize)) Chunk<chunkSize, slotSize>)
00044   {}
00045   
00046   inline void * malloc (size_t sz) {
00047     assert (sz == slotSize);
00048     // Use up all of the slots in one chunk,
00049     // and get another chunk if we need one.
00050     void * ptr = currentChunk->getSlot();
00051     if (ptr == NULL) {
00052       // This chunk is empty -- get another one.
00053       currentChunk = new (Super::malloc(chunkSize)) Chunk<chunkSize, slotSize>;
00054       ptr = currentChunk->getSlot();
00055     } 
00056     assert (ptr != NULL);
00057     return ptr;
00058   }
00059   
00060   inline void free (void * ptr) {
00061     // If this object belongs to "our" chunk,
00062     // free it directly; otherwise, pass it up.
00063     if (getChunk(ptr) == currentChunk) {
00064       currentChunk->putSlot (ptr);
00065     } else {
00066       Super::free (ptr);
00067     }
00068   }
00069 
00070 private:
00071 
00072   Chunk<chunkSize, slotSize> * currentChunk;
00073 
00074 };
00075 
00076 #endif
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'