/src/cryptopp/allocate.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
30 | 0 | std::new_handler newHandler = std::set_new_handler(NULLPTR); |
31 | 0 | if (newHandler) |
32 | 0 | std::set_new_handler(newHandler); |
33 | |
|
34 | 0 | if (newHandler) |
35 | 0 | newHandler(); |
36 | 0 | else |
37 | 0 | throw std::bad_alloc(); |
38 | 0 | } |
39 | | |
40 | | void * AlignedAllocate(size_t size) |
41 | 94.4M | { |
42 | 94.4M | 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 | 94.4M | 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 |
54 | 0 | CallNewHandler(); |
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. |
65 | 94.4M | CRYPTOPP_ASSERT(IsAlignedOn(p, 16)); |
66 | 94.4M | return p; |
67 | 94.4M | } |
68 | | |
69 | | void AlignedDeallocate(void *p) |
70 | 94.4M | { |
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 | 94.4M | CRYPTOPP_ASSERT(p); |
75 | | |
76 | 94.4M | if (p != NULLPTR) |
77 | 94.4M | { |
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 | 94.4M | free(p); |
85 | 94.4M | #endif |
86 | 94.4M | } |
87 | 94.4M | } |
88 | | |
89 | | void * UnalignedAllocate(size_t size) |
90 | 302k | { |
91 | 302k | void *p; |
92 | 302k | while ((p = malloc(size)) == NULLPTR) |
93 | 0 | CallNewHandler(); |
94 | 302k | return p; |
95 | 302k | } |
96 | | |
97 | | void UnalignedDeallocate(void *p) |
98 | 302k | { |
99 | 302k | free(p); |
100 | 302k | } |
101 | | |
102 | | NAMESPACE_END |
103 | | |
104 | | #endif // CRYPTOPP_IMPORTS |