diff options
-rw-r--r-- | misc.cpp | 53 | ||||
-rw-r--r-- | misc.h | 8 | ||||
-rw-r--r-- | secblock.h | 50 |
3 files changed, 71 insertions, 40 deletions
@@ -125,6 +125,59 @@ void CallNewHandler() 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 #endif @@ -567,6 +567,14 @@ static std::string StringNarrow(const wchar_t *str, bool throwOnError = true) #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 *************** template <class T> inline T rotlFixed(T x, unsigned int y) @@ -96,54 +96,24 @@ public: if (n == 0) return NULL; - if (CRYPTOPP_BOOL_ALIGN16_ENABLED && T_Align16 && n*sizeof(T) >= 16) - { - byte *p; - #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 - - assert(IsAlignedOn(p, 16)); - return (pointer)p; - } +#if CRYPTOPP_BOOL_ALIGN16_ENABLED + if (T_Align16 && n*sizeof(T) >= 16) + return (pointer)AlignedAllocate(n*sizeof(T)); +#endif - pointer p; - while (!(p = (pointer)malloc(sizeof(T)*n))) - CallNewHandler(); - return p; + return (pointer)UnalignedAllocate(n*sizeof(T)); } void deallocate(void *p, size_type n) { SecureWipeArray((pointer)p, n); - if (CRYPTOPP_BOOL_ALIGN16_ENABLED && T_Align16 && n*sizeof(T) >= 16) - { - #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 - return; - } +#if CRYPTOPP_BOOL_ALIGN16_ENABLED + if (T_Align16 && n*sizeof(T) >= 16) + return AlignedDeallocate(p); +#endif - free(p); + UnalignedDeallocate(p); } pointer reallocate(T *p, size_type oldSize, size_type newSize, bool preserve) |