"Fossies" - the Fresh Open Source Software Archive

Member "heaplayers-351/allocators/fragmentation/nfheap.h" (1 Nov 2004, 1007 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 "nfheap.h" see the Fossies "Dox" file reference documentation.

    1 #ifndef _NFHEAP_H_
    2 #define _NFHEAP_H_
    3 
    4 #include "dllisttype.h"
    5 
    6 namespace HL {
    7 
    8 //class NFHeap: a heap using the next-fit policy
    9 template<int Type, class SuperHeap>
   10 class NFHeap : public SuperHeap {
   11   public:
   12     inline NFHeap() : next_start(NULL) {}
   13 
   14     inline void * malloc(size_t size) {
   15         void * ptr = free_list.getListNext(reinterpret_cast<DLListEntry *>(next_start));
   16         while (ptr != NULL && getSize(ptr) < size) {
   17             ptr = free_list.getListNext(reinterpret_cast<DLListEntry *>(ptr));
   18         }
   19 
   20         if (ptr == NULL) {
   21             ptr = SuperHeap::malloc(size);
   22             assert(ptr != NULL);
   23         }
   24         else {
   25             assert(getSize(ptr) >= size);
   26             free_list.remove(reinterpret_cast<DLListEntry *>(ptr));
   27         }
   28 
   29         return ptr;
   30     }
   31 
   32     inline void free(void * ptr) {
   33         free_list.insert(reinterpret_cast<DLListEntry *>(ptr));
   34     }
   35 
   36     inline void remove(void * ptr) {
   37         free_list.remove(reinterpret_cast<DLListEntry *>(ptr));
   38     }
   39 
   40   private:
   41     DLListType<Type> free_list;
   42     void * next_start;
   43 
   44 };  //end of class NFHeap
   45 };  //end of namespace HL
   46 
   47 #endif