cache.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef __UDT_CACHE_H__
00042 #define __UDT_CACHE_H__
00043
00044 #include <list>
00045 #include <vector>
00046
00047 #include "common.h"
00048 #include "udt.h"
00049
00050 class CCacheItem
00051 {
00052 public:
00053 virtual ~CCacheItem() {}
00054
00055 public:
00056 virtual CCacheItem& operator=(const CCacheItem&) = 0;
00057
00058
00059 virtual bool operator==(const CCacheItem&) = 0;
00060
00061
00062
00063
00064
00065
00066
00067
00068 virtual CCacheItem* clone() = 0;
00069
00070
00071
00072
00073
00074
00075
00076
00077 virtual int getKey() = 0;
00078
00079
00080
00081 virtual void release() {}
00082 };
00083
00084 template<typename T> class CCache
00085 {
00086 public:
00087 CCache(int size = 1024):
00088 m_iMaxSize(size),
00089 m_iHashSize(size * 3),
00090 m_iCurrSize(0)
00091 {
00092 m_vHashPtr.resize(m_iHashSize);
00093 CGuard::createMutex(m_Lock);
00094 }
00095
00096 ~CCache()
00097 {
00098 clear();
00099 CGuard::releaseMutex(m_Lock);
00100 }
00101
00102 public:
00103
00104
00105
00106
00107
00108
00109
00110 int lookup(T* data)
00111 {
00112 CGuard cacheguard(m_Lock);
00113
00114 int key = data->getKey();
00115 if (key < 0)
00116 return -1;
00117 if (key >= m_iMaxSize)
00118 key %= m_iHashSize;
00119
00120 const ItemPtrList& item_list = m_vHashPtr[key];
00121 for (typename ItemPtrList::const_iterator i = item_list.begin(); i != item_list.end(); ++ i)
00122 {
00123 if (*data == ***i)
00124 {
00125
00126 *data = ***i;
00127 return 0;
00128 }
00129 }
00130
00131 return -1;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141 int update(T* data)
00142 {
00143 CGuard cacheguard(m_Lock);
00144
00145 int key = data->getKey();
00146 if (key < 0)
00147 return -1;
00148 if (key >= m_iMaxSize)
00149 key %= m_iHashSize;
00150
00151 T* curr = NULL;
00152
00153 ItemPtrList& item_list = m_vHashPtr[key];
00154 for (typename ItemPtrList::iterator i = item_list.begin(); i != item_list.end(); ++ i)
00155 {
00156 if (*data == ***i)
00157 {
00158
00159 ***i = *data;
00160 curr = **i;
00161
00162
00163 m_StorageList.erase(*i);
00164 item_list.erase(i);
00165
00166
00167 m_StorageList.push_front(curr);
00168 item_list.push_front(m_StorageList.begin());
00169
00170 return 0;
00171 }
00172 }
00173
00174
00175 curr = data->clone();
00176 m_StorageList.push_front(curr);
00177 item_list.push_front(m_StorageList.begin());
00178
00179 ++ m_iCurrSize;
00180 if (m_iCurrSize >= m_iMaxSize)
00181 {
00182
00183 T* last_data = m_StorageList.back();
00184 int last_key = last_data->getKey() % m_iHashSize;
00185
00186 item_list = m_vHashPtr[last_key];
00187 for (typename ItemPtrList::iterator i = item_list.begin(); i != item_list.end(); ++ i)
00188 {
00189 if (*last_data == ***i)
00190 {
00191 item_list.erase(i);
00192 break;
00193 }
00194 }
00195
00196 last_data->release();
00197 delete last_data;
00198 m_StorageList.pop_back();
00199 -- m_iCurrSize;
00200 }
00201
00202 return 0;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212 void setSizeLimit(int size)
00213 {
00214 m_iMaxSize = size;
00215 m_iHashSize = size * 3;
00216 m_vHashPtr.resize(m_iHashSize);
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226 void clear()
00227 {
00228 for (typename std::list<T*>::iterator i = m_StorageList.begin(); i != m_StorageList.end(); ++ i)
00229 {
00230 (*i)->release();
00231 delete *i;
00232 }
00233 m_StorageList.clear();
00234 for (typename std::vector<ItemPtrList>::iterator i = m_vHashPtr.begin(); i != m_vHashPtr.end(); ++ i)
00235 i->clear();
00236 m_iCurrSize = 0;
00237 }
00238
00239 private:
00240 std::list<T*> m_StorageList;
00241 typedef typename std::list<T*>::iterator ItemPtr;
00242 typedef std::list<ItemPtr> ItemPtrList;
00243 std::vector<ItemPtrList> m_vHashPtr;
00244
00245 int m_iMaxSize;
00246 int m_iHashSize;
00247 int m_iCurrSize;
00248
00249 pthread_mutex_t m_Lock;
00250
00251 private:
00252 CCache(const CCache&);
00253 CCache& operator=(const CCache&);
00254 };
00255
00256
00257 class CInfoBlock
00258 {
00259 public:
00260 uint32_t m_piIP[4];
00261 int m_iIPversion;
00262 uint64_t m_ullTimeStamp;
00263 int m_iRTT;
00264 int m_iBandwidth;
00265 int m_iLossRate;
00266 int m_iReorderDistance;
00267 double m_dInterval;
00268 double m_dCWnd;
00269
00270 public:
00271 virtual ~CInfoBlock() {}
00272 virtual CInfoBlock& operator=(const CInfoBlock& obj);
00273 virtual bool operator==(const CInfoBlock& obj);
00274 virtual CInfoBlock* clone();
00275 virtual int getKey();
00276 virtual void release() {}
00277
00278 public:
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 static void convert(const sockaddr* addr, int ver, uint32_t ip[]);
00290 };
00291
00292
00293 #endif