24 #ifndef TAO_CRYPT_BLOCK_HPP
25 #define TAO_CRYPT_BLOCK_HPP
34 #include "algorithm.hpp"
38 namespace STL = STL_NAMESPACE;
50 typedef size_t size_type;
51 typedef ptrdiff_t difference_type;
53 typedef const T* const_pointer;
55 typedef const T& const_reference;
57 pointer address(reference r)
const {
return (&r);}
58 const_pointer address(const_reference r)
const {
return (&r); }
59 void construct(pointer p,
const T& val) {
new (p) T(val);}
60 void destroy(pointer p) {p->~T();}
61 size_type max_size()
const {
return ~size_type(0)/
sizeof(T);}
67 template<
typename T,
class A>
68 typename A::pointer StdReallocate(
A& a, T* p,
typename A::size_type oldSize,
69 typename A::size_type newSize,
bool preserve)
71 if (oldSize == newSize)
76 typename A::pointer newPointer = b.allocate(newSize, 0);
77 memcpy(newPointer, p,
sizeof(T) * min((word32) oldSize, (word32) newSize));
78 a.deallocate(p, oldSize);
83 a.deallocate(p, oldSize);
84 return a.allocate(newSize, 0);
94 typedef typename AllocatorBase<T>::pointer pointer;
95 typedef typename AllocatorBase<T>::size_type size_type;
97 pointer allocate(size_type
n,
const void* = 0)
99 if (n > this->max_size())
106 void deallocate(
void* p, size_type n)
108 memset(p, 0, n *
sizeof(T));
109 tcArrayDelete((T*)p);
112 pointer reallocate(T* p, size_type oldSize, size_type newSize,
115 return StdReallocate(*
this, p, oldSize, newSize, preserve);
125 template<
typename T,
class A = AllocatorWithCleanup<T> >
128 explicit Block(word32 s = 0) : sz_(s), buffer_(allocator_.allocate(sz_))
131 Block(
const T* buff, word32 s) : sz_(s), buffer_(allocator_.allocate(sz_))
132 { memcpy(buffer_, buff, sz_ *
sizeof(T)); }
134 Block(
const Block& that) : sz_(that.sz_), buffer_(allocator_.allocate(sz_))
135 { memcpy(buffer_, that.buffer_, sz_ *
sizeof(T)); }
143 T& operator[] (word32
i) {
return buffer_[
i]; }
144 const T& operator[] (word32 i)
const {
return buffer_[
i]; }
146 T* operator+ (word32 i) {
return buffer_ +
i; }
147 const T* operator+ (word32 i)
const {
return buffer_ +
i; }
149 word32 size()
const {
return sz_; }
151 T* get_buffer()
const {
return buffer_; }
152 T* begin()
const {
return get_buffer(); }
154 void CleanGrow(word32 newSize)
157 buffer_ = allocator_.reallocate(buffer_, sz_, newSize,
true);
158 memset(buffer_ + sz_, 0, (newSize - sz_) *
sizeof(T));
163 void CleanNew(word32 newSize)
166 memset(buffer_, 0, sz_ *
sizeof(T));
169 void New(word32 newSize)
171 buffer_ = allocator_.reallocate(buffer_, sz_, newSize,
false);
175 void resize(word32 newSize)
177 buffer_ = allocator_.reallocate(buffer_, sz_, newSize,
true);
181 void Swap(
Block& other) {
182 STL::swap(sz_, other.sz_);
183 STL::swap(buffer_, other.buffer_);
184 STL::swap(allocator_, other.allocator_);
187 ~
Block() { allocator_.deallocate(buffer_, sz_); }
202 #endif // TAO_CRYPT_BLOCK_HPP