SHARE
TWEET

Standard allocator vs simple segregated storage

chemikadze Apr 24th, 2011 164 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * comparsion of standard allocator and simple segregated storage allocator
  3.  * requires c++0x in line 25 (for type size assertions)
  4. */
  5.  
  6. // removes type size check for your own risk
  7. #define CHECK_SIZE
  8.  
  9. #include <iostream>
  10. #include <sys/timeb.h>
  11. #include <cstdlib>
  12.  
  13. typedef long double ldouble;
  14. typedef unsigned int uint;
  15.  
  16. ldouble ftime()
  17. {
  18.         timeb curr;
  19.         ftime(&curr);
  20.         return curr.time+curr.millitm/1000.0;
  21. }
  22.  
  23. template <class T, size_t size>
  24. class pool {
  25. public:
  26.         pool()
  27.         {
  28. #ifdef CHECK_SIZE
  29.                 static_assert(sizeof(T)>=sizeof(T*), "Chunk size is less than machine word size!");
  30. #endif
  31.                 storage_ = reinterpret_cast<T*>(malloc(size*sizeof(T)));
  32.                 T* ptr;
  33.                 for (ptr = storage_; ptr != storage_ + (size-1); ++ptr) {
  34.                         *reinterpret_cast<T**>(ptr) = ptr+1;
  35.                 }
  36.                 *reinterpret_cast<T**>(ptr) = 0;
  37.                 free_chunk_ = storage_;
  38.         }
  39.        
  40.         ~pool()
  41.         {
  42.                 free(storage_);
  43.         }
  44.        
  45.         T* get()
  46.         {
  47.                 if (!free_chunk_) {
  48.                         throw std::bad_alloc();
  49.                 }
  50.                 T* result = free_chunk_;
  51.                 free_chunk_ = *reinterpret_cast<T**>(free_chunk_);
  52.                 return result;
  53.         }
  54.        
  55.         void free(T* block)
  56.         {
  57.                 *reinterpret_cast<T**>(block) = free_chunk_;
  58.                 free_chunk_ = block;
  59.         }
  60.        
  61.        
  62. private:
  63.         T* free_chunk_;
  64.         T* storage_;
  65. };
  66.  
  67. template <typename T>
  68. void test(int N)
  69. {
  70.         T** ptrs = new T*[N];
  71.        
  72.         // test standard allocator
  73.         ldouble start = ftime();
  74.         for (uint i = 0; i<N; ++i) {
  75.                 ptrs[i] = new T;
  76.         }
  77.        
  78.         for (uint i = 0; i<N; ++i) {
  79.                 delete ptrs[i];
  80.         }
  81.         std::cout << "Standard allocator with " << N << " objects: " << ftime()-start << " seconds" << std::endl;
  82.        
  83.         pool<T, 50000000> pool_;
  84.        
  85.         start = ftime();
  86.         for (uint i = 0; i<N; ++i) {
  87.                 ptrs[i] = new(pool_.get()) T;
  88.         }
  89.        
  90.         for (uint i = 0; i<N; ++i) {
  91.                 ptrs[i]->~T();
  92.                 pool_.free(ptrs[i]);
  93.         }
  94.         std::cout << "Simple segregated storage allocator with " << N << " objects: " << ftime()-start << " seconds" << std::endl;
  95.        
  96.         delete ptrs;
  97. }
  98.  
  99. int main(int argc, char **argv)
  100. {
  101.         uint N;
  102.        
  103.         if (argc > 1) {
  104.                 N = atoi(argv[1]);
  105.         }
  106.         else {
  107.                 N = 10000000;
  108.         }
  109.        
  110.         test<long int>(N);
  111.        
  112.     return 0;
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top