allocate.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // allocate.h - written and placed in the public domain by Jeffrey Walton
  2. // The functions in allocate.h and allocate.cpp were originally in misc.h
  3. // and misc.cpp. They were extracted in September 2019 to sidestep a circular
  4. // dependency with misc.h and secblock.h.
  5. /// \file allocate.h
  6. /// \brief Functions for allocating aligned buffers
  7. #ifndef CRYPTOPP_ALLOCATE_H
  8. #define CRYPTOPP_ALLOCATE_H
  9. #include "config.h"
  10. #include "cryptlib.h"
  11. NAMESPACE_BEGIN(CryptoPP)
  12. /// \brief Attempts to reclaim unused memory
  13. /// \throw bad_alloc
  14. /// \details In the normal course of running a program, a request for memory
  15. /// normally succeeds. If a call to AlignedAllocate or UnalignedAllocate fails,
  16. /// then CallNewHandler is called in n effort to recover. Internally,
  17. /// CallNewHandler calls set_new_handler(nullptr) in an effort to free memory.
  18. /// There is no guarantee CallNewHandler will be able to obtain more memory so
  19. /// an allocation succeeds. If the call to set_new_handler fails, then CallNewHandler
  20. /// throws a bad_alloc exception.
  21. /// \throw bad_alloc on failure
  22. /// \since Crypto++ 5.0
  23. /// \sa AlignedAllocate, AlignedDeallocate, UnalignedAllocate, UnalignedDeallocate
  24. CRYPTOPP_DLL void CRYPTOPP_API CallNewHandler();
  25. /// \brief Allocates a buffer on 16-byte boundary
  26. /// \param size the size of the buffer
  27. /// \details AlignedAllocate is primarily used when the data will be
  28. /// processed by SSE, NEON, ARMv8 or PowerPC instructions. The assembly
  29. /// language routines rely on the alignment. If the alignment is not
  30. /// respected, then a SIGBUS could be generated on Unix and Linux, and an
  31. /// EXCEPTION_DATATYPE_MISALIGNMENT could be generated on Windows.
  32. /// \details Formerly, AlignedAllocate and AlignedDeallocate were only
  33. /// available on certain platforms when CRYTPOPP_DISABLE_ASM was not in
  34. /// effect. However, Android and iOS debug simulator builds got into a
  35. /// state where the aligned allocator was not available and caused link
  36. /// failures.
  37. /// \since AlignedAllocate for SIMD since Crypto++ 1.0, AlignedAllocate
  38. /// for all builds since Crypto++ 8.1
  39. /// \sa AlignedDeallocate, UnalignedAllocate, UnalignedDeallocate, CallNewHandler,
  40. /// <A HREF="http://github.com/weidai11/cryptopp/issues/779">Issue 779</A>
  41. CRYPTOPP_DLL void* CRYPTOPP_API AlignedAllocate(size_t size);
  42. /// \brief Frees a buffer allocated with AlignedAllocate
  43. /// \param ptr the buffer to free
  44. /// \since AlignedDeallocate for SIMD since Crypto++ 1.0, AlignedAllocate
  45. /// for all builds since Crypto++ 8.1
  46. /// \sa AlignedAllocate, UnalignedAllocate, UnalignedDeallocate, CallNewHandler,
  47. /// <A HREF="http://github.com/weidai11/cryptopp/issues/779">Issue 779</A>
  48. CRYPTOPP_DLL void CRYPTOPP_API AlignedDeallocate(void *ptr);
  49. /// \brief Allocates a buffer
  50. /// \param size the size of the buffer
  51. /// \since Crypto++ 1.0
  52. /// \sa AlignedAllocate, AlignedDeallocate, UnalignedDeallocate, CallNewHandler,
  53. /// <A HREF="http://github.com/weidai11/cryptopp/issues/779">Issue 779</A>
  54. CRYPTOPP_DLL void * CRYPTOPP_API UnalignedAllocate(size_t size);
  55. /// \brief Frees a buffer allocated with UnalignedAllocate
  56. /// \param ptr the buffer to free
  57. /// \since Crypto++ 1.0
  58. /// \sa AlignedAllocate, AlignedDeallocate, UnalignedAllocate, CallNewHandler,
  59. /// <A HREF="http://github.com/weidai11/cryptopp/issues/779">Issue 779</A>
  60. CRYPTOPP_DLL void CRYPTOPP_API UnalignedDeallocate(void *ptr);
  61. NAMESPACE_END
  62. #endif // CRYPTOPP_ALLOCATE_H