move memory allocation/deallocation for SecBlock into DLL
parent
c81fc05b99
commit
57de1d522b
53
misc.cpp
53
misc.cpp
|
|
@ -125,6 +125,59 @@ void CallNewHandler()
|
||||||
throw std::bad_alloc();
|
throw std::bad_alloc();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if CRYPTOPP_BOOL_ALIGN16_ENABLED
|
||||||
|
|
||||||
|
void * AlignedAllocate(size_t size)
|
||||||
|
{
|
||||||
|
byte *p;
|
||||||
|
#ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
|
||||||
|
while (!(p = (byte *)_mm_malloc(size, 16)))
|
||||||
|
#elif defined(CRYPTOPP_MEMALIGN_AVAILABLE)
|
||||||
|
while (!(p = (byte *)memalign(16, size)))
|
||||||
|
#elif defined(CRYPTOPP_MALLOC_ALIGNMENT_IS_16)
|
||||||
|
while (!(p = (byte *)malloc(size)))
|
||||||
|
#else
|
||||||
|
while (!(p = (byte *)malloc(size + 16)))
|
||||||
|
#endif
|
||||||
|
CallNewHandler();
|
||||||
|
|
||||||
|
#ifdef CRYPTOPP_NO_ALIGNED_ALLOC
|
||||||
|
size_t adjustment = 16-((size_t)p%16);
|
||||||
|
p += adjustment;
|
||||||
|
p[-1] = (byte)adjustment;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
assert(IsAlignedOn(p, 16));
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AlignedDeallocate(void *p)
|
||||||
|
{
|
||||||
|
#ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
|
||||||
|
_mm_free(p);
|
||||||
|
#elif defined(CRYPTOPP_NO_ALIGNED_ALLOC)
|
||||||
|
p = (byte *)p - ((byte *)p)[-1];
|
||||||
|
free(p);
|
||||||
|
#else
|
||||||
|
free(p);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void * UnalignedAllocate(size_t size)
|
||||||
|
{
|
||||||
|
void *p;
|
||||||
|
while (!(p = malloc(size)))
|
||||||
|
CallNewHandler();
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UnalignedDeallocate(void *p)
|
||||||
|
{
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
|
||||||
NAMESPACE_END
|
NAMESPACE_END
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
8
misc.h
8
misc.h
|
|
@ -567,6 +567,14 @@ static std::string StringNarrow(const wchar_t *str, bool throwOnError = true)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if CRYPTOPP_BOOL_ALIGN16_ENABLED
|
||||||
|
CRYPTOPP_DLL void * CRYPTOPP_API AlignedAllocate(size_t size);
|
||||||
|
CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *p);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CRYPTOPP_DLL void * CRYPTOPP_API UnalignedAllocate(size_t size);
|
||||||
|
CRYPTOPP_DLL void CRYPTOPP_API UnalignedDeallocate(void *p);
|
||||||
|
|
||||||
// ************** rotate functions ***************
|
// ************** rotate functions ***************
|
||||||
|
|
||||||
template <class T> inline T rotlFixed(T x, unsigned int y)
|
template <class T> inline T rotlFixed(T x, unsigned int y)
|
||||||
|
|
|
||||||
46
secblock.h
46
secblock.h
|
|
@ -96,54 +96,24 @@ public:
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (CRYPTOPP_BOOL_ALIGN16_ENABLED && T_Align16 && n*sizeof(T) >= 16)
|
#if CRYPTOPP_BOOL_ALIGN16_ENABLED
|
||||||
{
|
if (T_Align16 && n*sizeof(T) >= 16)
|
||||||
byte *p;
|
return (pointer)AlignedAllocate(n*sizeof(T));
|
||||||
#ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
|
|
||||||
while (!(p = (byte *)_mm_malloc(sizeof(T)*n, 16)))
|
|
||||||
#elif defined(CRYPTOPP_MEMALIGN_AVAILABLE)
|
|
||||||
while (!(p = (byte *)memalign(16, sizeof(T)*n)))
|
|
||||||
#elif defined(CRYPTOPP_MALLOC_ALIGNMENT_IS_16)
|
|
||||||
while (!(p = (byte *)malloc(sizeof(T)*n)))
|
|
||||||
#else
|
|
||||||
while (!(p = (byte *)malloc(sizeof(T)*n + 16)))
|
|
||||||
#endif
|
|
||||||
CallNewHandler();
|
|
||||||
|
|
||||||
#ifdef CRYPTOPP_NO_ALIGNED_ALLOC
|
|
||||||
size_t adjustment = 16-((size_t)p%16);
|
|
||||||
p += adjustment;
|
|
||||||
p[-1] = (byte)adjustment;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
assert(IsAlignedOn(p, 16));
|
return (pointer)UnalignedAllocate(n*sizeof(T));
|
||||||
return (pointer)p;
|
|
||||||
}
|
|
||||||
|
|
||||||
pointer p;
|
|
||||||
while (!(p = (pointer)malloc(sizeof(T)*n)))
|
|
||||||
CallNewHandler();
|
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void deallocate(void *p, size_type n)
|
void deallocate(void *p, size_type n)
|
||||||
{
|
{
|
||||||
SecureWipeArray((pointer)p, n);
|
SecureWipeArray((pointer)p, n);
|
||||||
|
|
||||||
if (CRYPTOPP_BOOL_ALIGN16_ENABLED && T_Align16 && n*sizeof(T) >= 16)
|
#if CRYPTOPP_BOOL_ALIGN16_ENABLED
|
||||||
{
|
if (T_Align16 && n*sizeof(T) >= 16)
|
||||||
#ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
|
return AlignedDeallocate(p);
|
||||||
_mm_free(p);
|
|
||||||
#elif defined(CRYPTOPP_NO_ALIGNED_ALLOC)
|
|
||||||
p = (byte *)p - ((byte *)p)[-1];
|
|
||||||
free(p);
|
|
||||||
#else
|
|
||||||
free(p);
|
|
||||||
#endif
|
#endif
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(p);
|
UnalignedDeallocate(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
pointer reallocate(T *p, size_type oldSize, size_type newSize, bool preserve)
|
pointer reallocate(T *p, size_type oldSize, size_type newSize, bool preserve)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue