1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 #ifndef OMX_PROXY_VECTOR_H_INCLUDED 19 #define OMX_PROXY_VECTOR_H_INCLUDED 20 21 #ifndef OSCL_DEFALLOC_H_INCLUDED 22 #include "oscl_defalloc.h" 23 #endif 24 25 #ifndef OSCL_BASE_H_INCLUDED 26 #include "oscl_base.h" 27 #endif 28 29 #ifndef OSCL_BASE_ALLOC_H_INCLUDED 30 #include "oscl_base_alloc.h" 31 #endif 32 33 #ifndef PV_OMXDEFS_H_INCLUDED 34 #include "pv_omxdefs.h" 35 #endif 36 37 #if PROXY_INTERFACE 38 39 //Memory allocator/deallocator class that don't use OSCL TLS 40 class Oscl_Vector_Allocator : public Oscl_DefAlloc 41 { 42 public: ~Oscl_Vector_Allocator()43 virtual ~Oscl_Vector_Allocator() {}; 44 allocate(const uint32 size)45 OsclAny* allocate(const uint32 size) 46 { 47 OsclAny* tmp = iDefAlloc.allocate(size); 48 49 return tmp; 50 } 51 deallocate(OsclAny * p)52 OsclAny deallocate(OsclAny* p) 53 { 54 iDefAlloc.deallocate(p); 55 } 56 private: 57 _OsclBasicAllocator iDefAlloc; 58 }; 59 60 /** Added a new class with similar functionality but a different fail mechanism 61 * than the oscl_try & oscl_leave methods 62 * New class so that we need not make private members of oscl_vector as protected 63 * These routines can be used without oscl initializations, 64 * as they don't depend upon TLS 65 **/ 66 67 template<class T, class Alloc> 68 class Oscl_Vector_New 69 { 70 public: 71 typedef T value_type; 72 typedef T* pointer; 73 typedef const T& const_reference; 74 typedef T* iterator; 75 Oscl_Vector_New()76 Oscl_Vector_New() : 77 elems(0), numelems(0), bufsize(0) 78 {} 79 Oscl_Vector_New(uint32 n)80 Oscl_Vector_New(uint32 n) : 81 numelems(0), bufsize(n) 82 { 83 elems = defAlloc.ALLOCATE(bufsize); 84 } 85 Oscl_Vector_New(const Oscl_Vector_New<T,Alloc> & x)86 Oscl_Vector_New(const Oscl_Vector_New<T, Alloc>& x) 87 { 88 numelems = x.numelems; 89 bufsize = x.numelems; // only allocate enough for current elements 90 elems = defAlloc.ALLOCATE(bufsize); 91 uninitialized_copy(x.begin(), x.end(), begin()); 92 } 93 ~Oscl_Vector_New()94 virtual ~Oscl_Vector_New() 95 { 96 if (elems) 97 { 98 destroy(begin(), end()); 99 defAlloc.deallocate(elems); 100 } 101 } 102 103 Oscl_Vector_New<T, Alloc>& operator=(const Oscl_Vector_New<T, Alloc>& x) 104 { 105 if (&x != this) 106 { 107 if (x.size() > capacity()) 108 { 109 // allocate space and copy 110 T* tmp = defAlloc.ALLOCATE(x.end() - x.begin()); 111 uninitialized_copy(x.begin(), x.end(), tmp); 112 destroy(begin(), end()); 113 if (elems) 114 defAlloc.deallocate(elems); 115 elems = tmp; 116 bufsize = x.size(); 117 } 118 else if (size() >= x.size()) 119 { 120 iterator i = copy(x.begin(), x.end(), begin()); 121 destroy(i, end()); 122 } 123 else 124 { 125 copy(x.begin(), x.begin() + size(), begin()); 126 uninitialized_copy(x.begin() + size(), x.end(), end()); 127 } 128 numelems = x.size(); 129 } 130 return *this; 131 } 132 size()133 uint32 size() const 134 { 135 return numelems; 136 } empty()137 bool empty() const 138 { 139 return numelems == 0; 140 } capacity()141 uint32 capacity() const 142 { 143 return bufsize; 144 } 145 reserve(uint32 n)146 bool reserve(uint32 n) 147 { 148 if (n > bufsize) 149 { 150 T *oldelems = elems; 151 elems = defAlloc.ALLOCATE(n); 152 153 if (elems == NULL) 154 return false; 155 156 for (uint32 i = 0; i < numelems; i++) 157 construct(&elems[i], oldelems[i]); 158 159 if (oldelems) 160 { 161 destroy(oldelems, oldelems + numelems); 162 defAlloc.deallocate(oldelems); 163 } 164 bufsize = n; 165 } 166 return true; 167 } 168 push_back(const T & x)169 bool push_back(const T& x) 170 { 171 if (numelems == bufsize) 172 { 173 uint32 new_bufsize = (bufsize) ? 2 * bufsize : 2; 174 if ((status = reserve(new_bufsize)) == false) 175 return false; 176 } 177 construct(end(), x); 178 numelems++; 179 return true; 180 } 181 clear()182 void clear() 183 { 184 erase(begin(), end()); 185 } 186 187 T& operator[](uint32 n) 188 { 189 return (*(begin() + n)); 190 } 191 192 const T& operator[](uint32 n) const 193 { 194 return (*(begin() + n)); 195 } 196 destroy()197 void destroy() 198 { 199 if (elems) 200 { 201 destroy(begin(), end()); 202 defAlloc.deallocate(elems); 203 elems = NULL; 204 numelems = 0; 205 bufsize = 0; 206 } 207 } 208 begin()209 iterator begin() const 210 { 211 return elems; 212 } 213 end()214 iterator end() const 215 { 216 return elems + numelems; 217 } 218 erase(iterator pos)219 iterator erase(iterator pos) 220 { 221 if (pos + 1 != end()) copy(pos + 1, end(), pos); 222 numelems--; 223 destroy(end()); 224 return pos; 225 } 226 erase(iterator first,iterator last)227 iterator erase(iterator first, iterator last) 228 { 229 iterator it = copy(last, end(), first); 230 destroy(it, end()); 231 numelems -= (last - first); 232 return first; 233 } 234 235 private: 236 bool status; 237 T *elems; 238 uint32 numelems; 239 uint32 bufsize; 240 Oscl_TAlloc<T, Alloc> defAlloc; 241 construct(pointer p,const_reference x)242 void construct(pointer p, const_reference x) 243 { 244 OSCL_PLACEMENT_NEW(p, value_type(x)); 245 } 246 copy(iterator first,iterator last,iterator result)247 iterator copy(iterator first, iterator last, iterator result) 248 { 249 while (first != last) *result++ = *first++; 250 return result; 251 } uninitialized_copy(iterator first,iterator last,iterator result)252 iterator uninitialized_copy(iterator first, iterator last, iterator result) 253 { 254 while (first != last) construct(result++, *first++); 255 return result; 256 } 257 destroy(iterator first)258 void destroy(iterator first) 259 { 260 OSCL_UNUSED_ARG(first); 261 first->~T(); 262 } destroy(iterator first,iterator last)263 void destroy(iterator first, iterator last) 264 { 265 while (first != last) 266 { 267 first->~T(); 268 first++; 269 } 270 } 271 272 }; 273 #endif //PROXY_INTERFACE 274 #endif 275 276