"Fossies" - the Fresh Open Source Software Archive

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

    1 // -*- C++ -*-
    2 
    3 #ifndef _FREELISTREAP_H_
    4 #define _FREELISTREAP_H_
    5 
    6 #include "vamcommon.h"
    7 
    8 #include "reapbase.h"
    9 
   10 namespace VAM {
   11 
   12 // FreelistReap: a reap that uses a freelist to recycle freed objects
   13 class FreelistReap : public ReapBase {
   14 
   15 public:
   16 
   17     FreelistReap(size_t size, size_t object_size) {
   18         // the allocation base is right after us
   19         size_t base_ptr = (reinterpret_cast<size_t>(this + 1) + sizeof(double) - 1) & ~(sizeof(double) - 1);
   20         assert(base_ptr % sizeof(double) == 0);
   21 
   22         // calculate the number of allocable objects
   23         size_t num_total = (size - sizeof(*this)) / object_size;
   24 
   25         initReapBase(object_size, num_total, num_total, base_ptr);
   26         _freelist = NULL;
   27     }
   28 
   29     inline void * malloc() {
   30         void * ptr = ReapBase::malloc();
   31 
   32         if (ptr == NULL && _num_free > 0) {
   33             assert(_freelist != NULL);
   34             ptr = _freelist;
   35             _freelist = _freelist->next;
   36             _num_free--;
   37         }
   38 
   39         return ptr;
   40     }
   41 
   42     inline void free(void * ptr) {
   43         assert(_num_free < _num_total);
   44         assert((reinterpret_cast<size_t>(ptr) - _base_ptr) % _object_size == 0);
   45 
   46         FreeObject * free_obj = reinterpret_cast<FreeObject *>(ptr);
   47         free_obj->next = _freelist;
   48         _freelist = free_obj;
   49 
   50         _num_free++;
   51     }
   52 
   53     inline static FreelistReap * listToHeap(list_head * list) {
   54         return container_of(list, FreelistReap, _list);
   55     }
   56 
   57     inline list_head * getList() {
   58         return &_list;
   59     }
   60 
   61 private:
   62 
   63     struct FreeObject {
   64         FreeObject * next;
   65     };
   66 
   67     FreeObject * _freelist;
   68     list_head _list;
   69 
   70 };  // end of class FreelistReap
   71 
   72 };  // end of namespace VAM
   73 
   74 #endif