prevent problems when application and Crypto++ have different NDEBUG settings

pull/2/head
weidai 2003-07-31 01:57:46 +00:00
parent 083bd3db7c
commit 30f44f9ef8
1 changed files with 20 additions and 22 deletions

View File

@ -114,23 +114,26 @@ public:
{ {
assert(false); assert(false);
} }
size_type max_size() const {return 0;}
}; };
// this allocator can't be used with standard collections // This allocator can't be used with standard collections because
template <class T, unsigned int S, class A = NullAllocator<T> > // they require that all objects of the same allocator type are equivalent.
// So this is for use with SecBlock only.
template <class T, size_t S, class A = NullAllocator<T> >
class FixedSizeAllocatorWithCleanup : public AllocatorBase<T> class FixedSizeAllocatorWithCleanup : public AllocatorBase<T>
{ {
public: public:
CRYPTOPP_INHERIT_ALLOCATOR_TYPES CRYPTOPP_INHERIT_ALLOCATOR_TYPES
FixedSizeAllocatorWithCleanup() : m_allocated(false) {}
pointer allocate(size_type n) pointer allocate(size_type n)
{ {
if (n <= S) if (n <= S && !m_allocated)
{ {
assert(!m_allocated);
#ifndef NDEBUG
m_allocated = true; m_allocated = true;
#endif
return m_array; return m_array;
} }
else else
@ -139,12 +142,9 @@ public:
pointer allocate(size_type n, const void *hint) pointer allocate(size_type n, const void *hint)
{ {
if (n <= S) if (n <= S && !m_allocated)
{ {
assert(!m_allocated);
#ifndef NDEBUG
m_allocated = true; m_allocated = true;
#endif
return m_array; return m_array;
} }
else else
@ -153,13 +153,11 @@ public:
void deallocate(void *p, size_type n) void deallocate(void *p, size_type n)
{ {
if (n <= S) if (p == m_array)
{ {
assert(n <= S);
assert(m_allocated); assert(m_allocated);
assert(p == m_array);
#ifndef NDEBUG
m_allocated = false; m_allocated = false;
#endif
memset(p, 0, n*sizeof(T)); memset(p, 0, n*sizeof(T));
} }
else else
@ -168,23 +166,23 @@ public:
pointer reallocate(pointer p, size_type oldSize, size_type newSize, bool preserve) pointer reallocate(pointer p, size_type oldSize, size_type newSize, bool preserve)
{ {
if (oldSize <= S && newSize <= S) if (p == m_array && newSize <= S)
{
assert(oldSize <= S);
if (oldSize > newSize)
memset(p + newSize, 0, (oldSize-newSize)*sizeof(T));
return p; return p;
}
return StandardReallocate(*this, p, oldSize, newSize, preserve); return StandardReallocate(*this, p, oldSize, newSize, preserve);
} }
size_type max_size() const {return m_fallbackAllocator.max_size();} size_type max_size() const {return STDMAX(m_fallbackAllocator.max_size(), S);}
private: private:
A m_fallbackAllocator;
T m_array[S]; T m_array[S];
A m_fallbackAllocator;
#ifndef NDEBUG
public:
FixedSizeAllocatorWithCleanup() : m_allocated(false) {}
bool m_allocated; bool m_allocated;
#endif
}; };
//! a block of memory allocated using A //! a block of memory allocated using A