other-licenses/7zstub/src/Common/DynamicBuffer.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Common/DynamicBuffer.h
     3 #ifndef __COMMON_DYNAMICBUFFER_H
     4 #define __COMMON_DYNAMICBUFFER_H
     6 #include "Buffer.h"
     8 template <class T> class CDynamicBuffer: public CBuffer<T>
     9 {    
    10   void GrowLength(size_t size)
    11   {
    12     size_t delta;
    13     if (this->_capacity > 64)
    14       delta = this->_capacity / 4;
    15     else if (this->_capacity > 8)
    16       delta = 16;
    17     else
    18       delta = 4;
    19     delta = MyMax(delta, size);
    20     SetCapacity(this->_capacity + delta);
    21   }
    22 public:
    23   CDynamicBuffer(): CBuffer<T>() {};
    24   CDynamicBuffer(const CDynamicBuffer &buffer): CBuffer<T>(buffer) {};
    25   CDynamicBuffer(size_t size): CBuffer<T>(size) {};
    26   CDynamicBuffer& operator=(const CDynamicBuffer &buffer)
    27   {
    28     this->Free();
    29     if(buffer._capacity > 0)
    30     {
    31       SetCapacity(buffer._capacity);
    32       memmove(this->_items, buffer._items, buffer._capacity * sizeof(T));
    33     }
    34     return *this;
    35   }
    36   void EnsureCapacity(size_t capacity)
    37   {
    38     if (this->_capacity < capacity)
    39       GrowLength(capacity - this->_capacity);
    40   }
    41 };
    43 typedef CDynamicBuffer<char> CCharDynamicBuffer;
    44 typedef CDynamicBuffer<wchar_t> CWCharDynamicBuffer;
    45 typedef CDynamicBuffer<unsigned char> CByteDynamicBuffer;
    47 #endif

mercurial