summaryrefslogtreecommitdiff
path: root/secblock.h
diff options
context:
space:
mode:
authorweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2003-07-31 01:57:46 +0000
committerweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2003-07-31 01:57:46 +0000
commitb973517f3305d7e518fea35b5c3175e544ddbe77 (patch)
tree62963b4baf401ac86e126b4364a4d04b32b5eab0 /secblock.h
parent5b11a240c1f5a607b031cc7a2feb4a6af34c8282 (diff)
downloadcryptopp-b973517f3305d7e518fea35b5c3175e544ddbe77.tar.gz
prevent problems when application and Crypto++ have different NDEBUG settings
git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@123 57ff6487-cd31-0410-9ec3-f628ee90f5f0
Diffstat (limited to 'secblock.h')
-rw-r--r--secblock.h42
1 files changed, 20 insertions, 22 deletions
diff --git a/secblock.h b/secblock.h
index 580aab7..d763bc5 100644
--- a/secblock.h
+++ b/secblock.h
@@ -114,23 +114,26 @@ public:
{
assert(false);
}
+
+ size_type max_size() const {return 0;}
};
-// this allocator can't be used with standard collections
-template <class T, unsigned int S, class A = NullAllocator<T> >
+// This allocator can't be used with standard collections because
+// 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>
{
public:
CRYPTOPP_INHERIT_ALLOCATOR_TYPES
+ FixedSizeAllocatorWithCleanup() : m_allocated(false) {}
+
pointer allocate(size_type n)
{
- if (n <= S)
+ if (n <= S && !m_allocated)
{
- assert(!m_allocated);
-#ifndef NDEBUG
m_allocated = true;
-#endif
return m_array;
}
else
@@ -139,12 +142,9 @@ public:
pointer allocate(size_type n, const void *hint)
{
- if (n <= S)
+ if (n <= S && !m_allocated)
{
- assert(!m_allocated);
-#ifndef NDEBUG
m_allocated = true;
-#endif
return m_array;
}
else
@@ -153,13 +153,11 @@ public:
void deallocate(void *p, size_type n)
{
- if (n <= S)
+ if (p == m_array)
{
+ assert(n <= S);
assert(m_allocated);
- assert(p == m_array);
-#ifndef NDEBUG
m_allocated = false;
-#endif
memset(p, 0, n*sizeof(T));
}
else
@@ -168,23 +166,23 @@ public:
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 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:
- A m_fallbackAllocator;
T m_array[S];
-
-#ifndef NDEBUG
-public:
- FixedSizeAllocatorWithCleanup() : m_allocated(false) {}
+ A m_fallbackAllocator;
bool m_allocated;
-#endif
};
//! a block of memory allocated using A