00001 // -*- c++ -*- 00002 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 00003 00004 // O S C L _ R E F C O U N T E R _ M E M F R A G 00005 00006 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 00007 00023 #ifndef OSCL_REFCOUNTER_MEMFRAG_H_INCLUDED 00024 #define OSCL_REFCOUNTER_MEMFRAG_H_INCLUDED 00025 00026 #ifndef OSCL_BASE_H_INCLUDED 00027 #include "oscl_base.h" 00028 #endif 00029 00030 #ifndef OSCL_REFCOUNTER_H_INCLUDED 00031 #include "oscl_refcounter.h" 00032 #endif 00033 00034 00039 class OsclRefCounterMemFrag 00040 { 00041 public: 00042 00053 OsclRefCounterMemFrag(OsclMemoryFragment &m, OsclRefCounter *r, 00054 uint32 in_capacity) : 00055 memfrag(m), refcnt(r), capacity(in_capacity) 00056 // no need to increment refcnt--it should already be done. 00057 {} 00058 00062 OsclRefCounterMemFrag(const OsclRefCounterMemFrag &x) : 00063 memfrag(x.memfrag), refcnt(x.refcnt), capacity(x.capacity) 00064 { 00065 if (refcnt) 00066 { 00067 refcnt->addRef(); 00068 } 00069 } 00070 00074 OsclRefCounterMemFrag() 00075 { 00076 memfrag.ptr = 0; 00077 memfrag.len = 0; 00078 refcnt = 0; 00079 capacity = 0; 00080 } 00081 00082 00086 OsclRefCounterMemFrag& operator= (const OsclRefCounterMemFrag &x) 00087 { 00088 if (this == &x) 00089 { 00090 // protect against self-assignment 00091 return *this; 00092 } 00093 00094 // remove ref for current memfrag 00095 if (refcnt) 00096 { 00097 refcnt->removeRef(); 00098 } 00099 00100 // copy assigned object 00101 memfrag = x.memfrag; 00102 refcnt = x.refcnt; 00103 capacity = x.capacity; 00104 00105 // add ref for new memfrag 00106 if (refcnt) 00107 { 00108 refcnt->addRef(); 00109 } 00110 00111 return *this; 00112 } 00113 00121 ~OsclRefCounterMemFrag() 00122 { 00123 if (refcnt) 00124 { 00125 refcnt->removeRef(); 00126 } 00127 } 00128 00133 OsclRefCounter* getRefCounter() 00134 { 00135 return refcnt; 00136 } 00137 00142 OsclMemoryFragment& getMemFrag() 00143 { 00144 return memfrag; 00145 } 00146 00150 OsclAny* getMemFragPtr() 00151 { 00152 return memfrag.ptr; 00153 } 00154 00161 uint32 getMemFragSize() 00162 { 00163 return memfrag.len; 00164 } 00165 00171 uint32 getCapacity() 00172 { 00173 return capacity; 00174 } 00175 00179 uint32 getCount() 00180 { 00181 return (refcnt) ? refcnt->getCount() : 0; 00182 } 00183 00184 00185 00186 private: 00187 00188 OsclMemoryFragment memfrag; 00189 OsclRefCounter *refcnt; 00190 uint32 capacity; 00191 }; 00192 00193 00197 #endif // OSCL_REFCOUNTER_MEMFRAG_H