Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "util/free_list.h"
00012
00013 #include <iostream>
00014
00015 #include "perf/perf.h"
00016
00017
00018 struct type_t {
00019 long value;
00020 smart_ptr<type_t> next;
00021 };
00022
00023
00024 typedef free_list_t<type_t> free_type_t;
00025
00026 typedef std::vector<smart_ptr<type_t> > vec_type_t;
00027
00028
00029
00030
00031
00032
00033
00034
00035 static void
00036 freeVector
00037 (
00038 IN vec_type_t& vec,
00039 IN free_type_t& freeList
00040 )
00041 {
00042 DPRINTF("Returning all objects in vector back to free list...");
00043 for (vec_type_t::iterator i = vec.begin(); i != vec.end(); ++i) {
00044 smart_ptr<type_t>& p = *i;
00045 ASSERT_THROW(p, "null element in our vector");
00046 ASSERT_THROW(!p->next, "should be isolated element in our list");
00047 freeList.returnElement(*i);
00048 }
00049 vec.clear();
00050 }
00051
00052
00053
00054 static void
00055 populateVector
00056 (
00057 IN vec_type_t& vec,
00058 IN free_type_t& freeList,
00059 IN int count
00060 )
00061 {
00062 DPRINTF("Adding %d objects to vector from free list...", count);
00063 ASSERT(count > 0, "bad count: %d", count);
00064 for (int i = 0; i < count; ++i) {
00065 smart_ptr<type_t> p = freeList.getElement();
00066 ASSERT_THROW(p, "got a null element from free list");
00067 ASSERT_THROW(!p->next, "should be isolated element");
00068 vec.push_back(p);
00069 }
00070 }
00071
00072
00073
00074 static void
00075 doTest
00076 (
00077 IN int count
00078 )
00079 {
00080 int divisor = 10;
00081 ASSERT(count > divisor, "Bad count: %d", count);
00082
00083 vec_type_t vec;
00084 vec.reserve(count);
00085
00086 int max = count / divisor;
00087 free_type_t freeList(max);
00088
00089 populateVector(vec, freeList, count);
00090 ASSERT_THROW(!freeList.size(), "should have null size");
00091 ASSERT_THROW(count == (int) vec.size(), "mismatch");
00092
00093 freeVector(vec, freeList);
00094 int nAvail = freeList.size();
00095 ASSERT_THROW(max == nAvail,
00096 "Free list does not have correct number available: " << nAvail);
00097
00098 populateVector(vec, freeList, max);
00099 nAvail = freeList.size();
00100 ASSERT_THROW(!nAvail, "free list should be empty again");
00101
00102 freeVector(vec, freeList);
00103 nAvail = freeList.size();
00104 ASSERT_THROW(max == nAvail, "free list should be full again");
00105
00106 int nRemain = 5;
00107 populateVector(vec, freeList, max - nRemain);
00108 nAvail = freeList.size();
00109 ASSERT_THROW(nRemain == nAvail, "Should have " << nRemain <<
00110 " elements in free list, but list claims " << nAvail);
00111 freeVector(vec, freeList);
00112 nAvail = freeList.size();
00113 ASSERT_THROW(max == nAvail, "free list should be full again");
00114
00115 freeList.clear();
00116 nAvail = freeList.size();
00117 ASSERT_THROW(!nAvail, "Free list should be empty, claims " << nAvail);
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 int
00129 main
00130 (
00131 IN int argc,
00132 IN const char * argv[]
00133 )
00134 {
00135 int retval = 0;
00136 try {
00137 perf::Timer timer("overall timer");
00138
00139 doTest(1000);
00140
00141 } catch (std::exception& e) {
00142 DPRINTF("Exception: %s", e.what());
00143 retval = 1;
00144 }
00145
00146 perf::dumpTimingSummary(std::cerr);
00147
00148 return retval;
00149 }
00150