PLearn 0.1
|
00001 #include <plearn/base/PMemPool.h> 00002 #include <iostream> 00003 #include <iomanip> 00004 #include <vector> 00005 00006 using namespace std; 00007 using namespace PLearn; 00008 00009 struct MyStruct { 00010 double x[5]; 00011 }; 00012 00013 const int N = 10000000; 00014 const int POOL_SIZE = 10000; 00015 const float GROWTH = 2.0; 00016 00017 template <bool fast_dealloc> 00018 void alloc_from_pool() 00019 { 00020 vector<MyStruct*> all_allocated; 00021 all_allocated.reserve(N); 00022 PObjectPool<MyStruct> pool(POOL_SIZE, GROWTH, fast_dealloc); 00023 00024 cout << "Is pool empty? " << pool.empty() << endl; 00025 00026 for (int i=0; i<N; ++i) { 00027 MyStruct* s = pool.allocate(); 00028 // cout << "Allocating at " << setbase(16) << (void*)s << endl; 00029 all_allocated.push_back(s); 00030 } 00031 00032 cout << "Is pool empty? " << pool.empty() << endl; 00033 00034 for (int i=N/2; i<N; ++i) { 00035 MyStruct* s = all_allocated[i]; 00036 // cout << "Deallocating at " << setbase(16) << (void*)s << endl; 00037 pool.deallocate(s); 00038 } 00039 00040 cout << "Is pool empty? " << pool.empty() << endl; 00041 00042 for (int i=0; i<N/2; ++i) { 00043 MyStruct* s = all_allocated[i]; 00044 // cout << "Deallocating at " << setbase(16) << (void*)s << endl; 00045 pool.deallocate(s); 00046 } 00047 00048 cout << "Is pool empty? " << pool.empty() << endl; 00049 } 00050 00051 void alloc_from_stdalloc() 00052 { 00053 vector<MyStruct*> all_allocated; 00054 all_allocated.reserve(N); 00055 for (int i=0; i<N; ++i) { 00056 all_allocated.push_back(new MyStruct); 00057 } 00058 for (int i=0; i<N; ++i) { 00059 delete all_allocated[i]; 00060 } 00061 } 00062 00063 int main() 00064 { 00065 alloc_from_pool<false>(); 00066 alloc_from_pool<true>(); 00067 alloc_from_stdalloc(); 00068 }; 00069 00070 00071 /* 00072 Local Variables: 00073 mode:c++ 00074 c-basic-offset:4 00075 c-file-style:"stroustrup" 00076 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00077 indent-tabs-mode:nil 00078 fill-column:79 00079 End: 00080 */ 00081 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :