Crypto++  8.8
Free C++ class library of cryptographic schemes
allocate.cpp
1 // allocate.cpp - written and placed in the public domain by Jeffrey Walton
2 
3 // The functions in allocate.h and allocate.cpp were originally in misc.h
4 // and misc.cpp. They were extracted in September 2019 to sidestep a circular
5 // dependency with misc.h and secblock.h.
6 
7 #include "pch.h"
8 #include "config.h"
9 
10 #ifndef CRYPTOPP_IMPORTS
11 
12 #include "allocate.h"
13 #include "stdcpp.h"
14 #include "misc.h"
15 #include "trap.h"
16 
17 // for memalign
18 #if defined(CRYPTOPP_MEMALIGN_AVAILABLE) || defined(CRYPTOPP_MM_MALLOC_AVAILABLE) || defined(QNX)
19 # include <malloc.h>
20 #endif
21 // for posix_memalign
22 #if defined(CRYPTOPP_POSIX_MEMALIGN_AVAILABLE)
23 # include <stdlib.h>
24 #endif
25 
26 NAMESPACE_BEGIN(CryptoPP)
27 
28 void CallNewHandler()
29 {
30  std::new_handler newHandler = std::set_new_handler(NULLPTR);
31  if (newHandler)
32  std::set_new_handler(newHandler);
33 
34  if (newHandler)
35  newHandler();
36  else
37  throw std::bad_alloc();
38 }
39 
40 void * AlignedAllocate(size_t size)
41 {
42  byte *p;
43 #if defined(CRYPTOPP_MM_MALLOC_AVAILABLE)
44  while ((p = (byte *)_mm_malloc(size, 16)) == NULLPTR)
45 #elif defined(CRYPTOPP_MEMALIGN_AVAILABLE)
46  while ((p = (byte *)memalign(16, size)) == NULLPTR)
47 #elif defined(CRYPTOPP_MALLOC_ALIGNMENT_IS_16)
48  while ((p = (byte *)malloc(size)) == NULLPTR)
49 #elif defined(CRYPTOPP_POSIX_MEMALIGN_AVAILABLE)
50  while (posix_memalign(reinterpret_cast<void**>(&p), 16, size) != 0)
51 #else
52  while ((p = (byte *)malloc(size + 16)) == NULLPTR)
53 #endif
55 
56 #ifdef CRYPTOPP_NO_ALIGNED_ALLOC
57  size_t adjustment = 16-((size_t)p%16);
58  CRYPTOPP_ASSERT(adjustment > 0);
59  p += adjustment;
60  p[-1] = (byte)adjustment;
61 #endif
62 
63  // If this assert fires then there are problems that need
64  // to be fixed. Please open a bug report.
66  return p;
67 }
68 
69 void AlignedDeallocate(void *p)
70 {
71  // Guard pointer due to crash on AIX when CRYPTOPP_NO_ALIGNED_ALLOC
72  // is in effect. The guard was previously in place in SecBlock,
73  // but it was removed at f4d68353ca7c as part of GH #875.
74  CRYPTOPP_ASSERT(p);
75 
76  if (p != NULLPTR)
77  {
78 #ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
79  _mm_free(p);
80 #elif defined(CRYPTOPP_NO_ALIGNED_ALLOC)
81  p = (byte *)p - ((byte *)p)[-1];
82  free(p);
83 #else
84  free(p);
85 #endif
86  }
87 }
88 
89 void * UnalignedAllocate(size_t size)
90 {
91  void *p;
92  while ((p = malloc(size)) == NULLPTR)
94  return p;
95 }
96 
97 void UnalignedDeallocate(void *p)
98 {
99  free(p);
100 }
101 
102 NAMESPACE_END
103 
104 #endif // CRYPTOPP_IMPORTS
Functions for allocating aligned buffers.
CRYPTOPP_DLL void AlignedDeallocate(void *ptr)
Frees a buffer allocated with AlignedAllocate.
CRYPTOPP_DLL void CallNewHandler()
Attempts to reclaim unused memory.
CRYPTOPP_DLL void * UnalignedAllocate(size_t size)
Allocates a buffer.
CRYPTOPP_DLL void UnalignedDeallocate(void *ptr)
Frees a buffer allocated with UnalignedAllocate.
CRYPTOPP_DLL void * AlignedAllocate(size_t size)
Allocates a buffer on 16-byte boundary.
Library configuration file.
unsigned char byte
8-bit unsigned datatype
Definition: config_int.h:66
Utility functions for the Crypto++ library.
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
Definition: misc.h:1436
Crypto++ library namespace.
Precompiled header file.
Common C++ header files.
Debugging and diagnostic assertions.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68