fastalloc.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00040 #ifndef COL_FASTALLOC_H
00041 #define COL_FASTALLOC_H
00042 
00043 #include "fastlib/base/base.h"
00044 
00050 template<size_t item_size>
00051 class SlabAllocator {
00052  private:
00053   // TODO: what if we want to free the entire thing?
00054   static char *freelist_;
00055   //static char *slab_list_;
00056   
00057  public:
00058   static void AllocSlab();
00059   
00060   static char *Alloc() {
00061     if (unlikely(freelist_ == NULL)) {
00062       AllocSlab();
00063     }
00064     char *item = freelist_;
00065     freelist_ = *reinterpret_cast<char **>(freelist_);
00066     return item;
00067   }
00068   
00069   static void Free(char *item) {
00070     *reinterpret_cast<char**>(item) = freelist_;
00071     freelist_ = item;
00072   }
00073 };
00074 
00075 template<size_t item_size>
00076 void SlabAllocator<item_size>::AllocSlab() {
00077   int slab_items = 128; // TODO: hard-coded constant
00078   size_t real_item_size = stride_align(item_size, char *);
00079   size_t size = real_item_size * slab_items + sizeof(char *);
00080   char *slab = mem::Alloc<char>(size);
00081   
00082   // slab_items must be at least 2
00083   
00084   *reinterpret_cast<char **>(slab) = freelist_;
00085   
00086   --slab_items;
00087   
00088   do {
00089     char *prev = slab;
00090     slab += real_item_size;
00091     *reinterpret_cast<char **>(slab) = prev;
00092   } while (--slab_items);
00093   
00094   freelist_ = slab;
00095   //slab += real_item_size;
00096   //
00097   //*reinterpret_cast<char **>(slab) = slab_list_;
00098   //slab_list_ = slab;
00099 }
00100 
00101 template<size_t item_size>
00102 char *SlabAllocator<item_size>::freelist_ = 0;
00103 
00124 #define fast_new(T) new(SlabAllocator<sizeof(T)>::Alloc()) T
00125 
00137 template<typename T>
00138 inline void fast_delete(T *ptr) {
00139   ptr->~T();
00140   SlabAllocator<sizeof(T)>::Free(reinterpret_cast<char*>(ptr));
00141 }
00142 
00143 #endif
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3