Crypto++  8.2
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 
29 {
30  using std::new_handler;
31  using std::set_new_handler;
32 
33  new_handler newHandler = set_new_handler(NULLPTR);
34  if (newHandler)
35  set_new_handler(newHandler);
36 
37  if (newHandler)
38  newHandler();
39  else
40  throw std::bad_alloc();
41 }
42 
43 void * AlignedAllocate(size_t size)
44 {
45  byte *p;
46 #if defined(CRYPTOPP_MM_MALLOC_AVAILABLE)
47  while ((p = (byte *)_mm_malloc(size, 16)) == NULLPTR)
48 #elif defined(CRYPTOPP_MEMALIGN_AVAILABLE)
49  while ((p = (byte *)memalign(16, size)) == NULLPTR)
50 #elif defined(CRYPTOPP_MALLOC_ALIGNMENT_IS_16)
51  while ((p = (byte *)malloc(size)) == NULLPTR)
52 #elif defined(CRYPTOPP_POSIX_MEMALIGN_AVAILABLE)
53  while (posix_memalign(reinterpret_cast<void**>(&p), 16, size) != 0)
54 #else
55  while ((p = (byte *)malloc(size + 16)) == NULLPTR)
56 #endif
58 
59 #ifdef CRYPTOPP_NO_ALIGNED_ALLOC
60  size_t adjustment = 16-((size_t)p%16);
61  CRYPTOPP_ASSERT(adjustment > 0);
62  p += adjustment;
63  p[-1] = (byte)adjustment;
64 #endif
65 
66  // If this assert fires then there are problems that need
67  // to be fixed. Please open a bug report.
69  return p;
70 }
71 
72 void AlignedDeallocate(void *p)
73 {
74  // Guard pointer due to crash on AIX when CRYPTOPP_NO_ALIGNED_ALLOC
75  // is in effect. The guard was previously in place in SecBlock,
76  // but it was removed at f4d68353ca7c as part of GH #875.
77  CRYPTOPP_ASSERT(p);
78 
79  if (p != NULLPTR)
80  {
81 #ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
82  _mm_free(p);
83 #elif defined(CRYPTOPP_NO_ALIGNED_ALLOC)
84  p = (byte *)p - ((byte *)p)[-1];
85  free(p);
86 #else
87  free(p);
88 #endif
89  }
90 }
91 
92 void * UnalignedAllocate(size_t size)
93 {
94  void *p;
95  while ((p = malloc(size)) == NULLPTR)
97  return p;
98 }
99 
100 void UnalignedDeallocate(void *p)
101 {
102  free(p);
103 }
104 
105 NAMESPACE_END
106 
107 #endif // CRYPTOPP_IMPORTS
CRYPTOPP_DLL void CRYPTOPP_API UnalignedDeallocate(void *ptr)
Frees a buffer allocated with UnalignedAllocate.
Definition: allocate.cpp:100
Utility functions for the Crypto++ library.
CRYPTOPP_DLL void CRYPTOPP_API CallNewHandler()
Attempts to reclaim unused memory.
Definition: allocate.cpp:28
CRYPTOPP_DLL void *CRYPTOPP_API AlignedAllocate(size_t size)
Allocates a buffer on 16-byte boundary.
Definition: allocate.cpp:43
Library configuration file.
Common C++ header files.
Functions for allocating aligned buffers.
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
Definition: misc.h:1207
Precompiled header file.
CRYPTOPP_DLL void *CRYPTOPP_API UnalignedAllocate(size_t size)
Allocates a buffer.
Definition: allocate.cpp:92
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:69
CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *ptr)
Frees a buffer allocated with AlignedAllocate.
Definition: allocate.cpp:72
Debugging and diagnostic assertions.
Crypto++ library namespace.