Changeset 16622 in webkit for trunk/JavaScriptCore/wtf/Vector.h
- Timestamp:
- Sep 28, 2006, 2:07:39 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/Vector.h
r16533 r16622 219 219 220 220 template<typename T> 221 class VectorBuffer<T, 0> 222 { 221 class VectorBuffer<T, 0> { 223 222 public: 224 223 VectorBuffer() 224 : m_buffer(0), m_capacity(0) 225 { 226 } 227 228 VectorBuffer(size_t capacity) 229 #if !ASSERT_DISABLED 225 230 : m_capacity(0) 226 , m_buffer(0) 227 { 228 } 229 230 VectorBuffer(size_t capacity) 231 : m_capacity(0) 231 #endif 232 232 { 233 233 allocateBuffer(capacity); … … 239 239 } 240 240 241 void deallocateBuffer(T* buffer)241 static void deallocateBuffer(T* buffer) 242 242 { 243 243 fastFree(buffer); … … 250 250 if (newCapacity > std::numeric_limits<size_t>::max() / sizeof(T)) 251 251 abort(); 252 m_buffer = reinterpret_cast<T*>(fastMalloc(newCapacity * sizeof(T)));252 m_buffer = static_cast<T*>(fastMalloc(newCapacity * sizeof(T))); 253 253 } 254 254 … … 257 257 size_t capacity() const { return m_capacity; } 258 258 259 T* releaseBuffer() 260 { 261 T* buffer = m_buffer; 262 m_buffer = 0; 263 m_capacity = 0; 264 return buffer; 265 } 266 259 267 protected: 260 268 VectorBuffer(T* buffer, size_t capacity) 261 : m_capacity(capacity) 262 , m_buffer(buffer) 263 { 264 } 265 269 : m_buffer(buffer), m_capacity(capacity) 270 { 271 } 272 273 T* m_buffer; 274 275 private: 266 276 size_t m_capacity; 267 T *m_buffer; 268 }; 269 270 template<typename T, size_t inlineCapacity> 271 class VectorBuffer : public VectorBuffer<T, 0> { 277 }; 278 279 template<typename T, size_t inlineCapacity> 280 class VectorBuffer : private VectorBuffer<T, 0> { 272 281 private: 273 282 typedef VectorBuffer<T, 0> BaseBuffer; 274 283 public: 275 VectorBuffer() 284 VectorBuffer() 276 285 : BaseBuffer(inlineBuffer(), inlineCapacity) 277 286 { … … 282 291 { 283 292 if (capacity > inlineCapacity) 284 BaseBuffer::allocateBuffer(capacity);293 allocateBuffer(capacity); 285 294 } 286 295 287 296 ~VectorBuffer() 288 297 { 289 if ( BaseBuffer::buffer() == inlineBuffer())298 if (buffer() == inlineBuffer()) 290 299 BaseBuffer::m_buffer = 0; 291 300 } … … 297 306 } 298 307 308 using BaseBuffer::allocateBuffer; 309 310 using BaseBuffer::buffer; 311 using BaseBuffer::capacity; 312 313 T* releaseBuffer() 314 { 315 if (buffer() == inlineBuffer()) 316 return 0; 317 return BaseBuffer::releaseBuffer(); 318 } 319 299 320 private: 300 321 static const size_t m_inlineBufferSize = inlineCapacity * sizeof(T); 301 T *inlineBuffer() { return reinterpret_cast<T *>(&m_inlineBuffer); } 322 T* inlineBuffer() { return reinterpret_cast<T*>(&m_inlineBuffer); } 323 302 324 char m_inlineBuffer[m_inlineBufferSize]; 303 325 }; … … 409 431 void fill(const T& val) { fill(val, size()); } 410 432 433 T* releaseBuffer(); 434 411 435 private: 412 436 void expandCapacity(size_t newMinCapacity); … … 598 622 599 623 template<typename T, size_t inlineCapacity> 624 T* Vector<T, inlineCapacity>::releaseBuffer() 625 { 626 T* buffer = m_impl.releaseBuffer(); 627 if (!buffer && m_size) { 628 // If the vector had some data, but no buffer to release, 629 // that means it was using the inline buffer. In that case, 630 // we create a brand new buffer so the caller always gets one. 631 size_t bytes = m_size * sizeof(T); 632 buffer = static_cast<T*>(fastMalloc(bytes)); 633 memcpy(buffer, data(), bytes); 634 } 635 m_size = 0; 636 return buffer; 637 } 638 639 template<typename T, size_t inlineCapacity> 600 640 void deleteAllValues(const Vector<T, inlineCapacity>& collection) 601 641 {
Note:
See TracChangeset
for help on using the changeset viewer.