"Fossies" - the Fresh Open Source Software Archive

Member "heaplayers-351/allocators/vam/twoheap.h" (21 Nov 2005, 1478 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 "twoheap.h" see the Fossies "Dox" file reference documentation.

    1 // -*- C++ -*-
    2 
    3 #ifndef _TWOHEAP_H_
    4 #define _TWOHEAP_H_
    5 
    6 #include "objectheader.h"
    7 
    8 namespace VAM {
    9 
   10 // TwoHeap: a heap that allocates objects from SuperHeap1 if possbile and otherwise from SuperHeap2
   11 template<class SuperHeap1, class SuperHeap2, size_t PartitionSize>
   12 class TwoHeap : public SuperHeap1 {
   13 
   14 public:
   15 
   16     inline void * malloc(size_t size) {
   17         void * ptr = NULL;
   18 
   19         // allocate regular objects from SuperHeap1
   20         if (size <= SuperHeap1::MAX_OBJECT_SIZE) {
   21             ptr = SuperHeap1::malloc(size);
   22         }
   23         // allocate very large objects from SuperHeap2
   24         else {
   25             // FIXME: we are making assumptions about how our SuperHeap works here
   26             // the size must be page-aligned and larger than the underlying partition size
   27             size_t huge_size = size + sizeof(ObjectHeader);
   28             if (huge_size <= PartitionSize)
   29                 huge_size = PartitionSize + PAGE_SIZE;
   30             else
   31                 huge_size = (huge_size + PAGE_SIZE - 1) & PAGE_MASK;
   32 
   33             ObjectHeader * header = reinterpret_cast<ObjectHeader *>(_heap.malloc(huge_size, USE_HEADER_TYPE));
   34             if (header != NULL) {
   35                 header->_size = size;
   36                 ptr = header->getObject();
   37             }
   38         }
   39 
   40         return ptr;
   41     }
   42 
   43     inline void free(void * ptr) {
   44         ObjectHeader * header = ObjectHeader::getHeader(ptr);
   45 
   46         // check where the object was allocated from
   47         if (header->_size <= SuperHeap1::MAX_OBJECT_SIZE) {
   48             SuperHeap1::free(ptr);
   49         }
   50         else {
   51             _heap.free(header);
   52         }
   53     }
   54 
   55 private:
   56 
   57     SuperHeap2 _heap;
   58 
   59 };  // end of class TwoHeap
   60 
   61 };  // end of namespace VAM
   62 
   63 #endif