MemAlloc.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===- MemAlloc.h - Memory allocation functions -----------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. ///
  10. /// This file defines counterparts of C library allocation functions defined in
  11. /// the namespace 'std'. The new allocation functions crash on allocation
  12. /// failure instead of returning null pointer.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_SUPPORT_MEMALLOC_H
  16. #define LLVM_SUPPORT_MEMALLOC_H
  17. #include "llvm/Support/Compiler.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include <cstdlib>
  20. namespace llvm {
  21. LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
  22. void *Result = std::malloc(Sz);
  23. if (Result == nullptr) {
  24. // It is implementation-defined whether allocation occurs if the space
  25. // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
  26. // non-zero, if the space requested was zero.
  27. if (Sz == 0)
  28. return safe_malloc(1);
  29. report_bad_alloc_error("Allocation failed");
  30. }
  31. return Result;
  32. }
  33. LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
  34. size_t Sz) {
  35. void *Result = std::calloc(Count, Sz);
  36. if (Result == nullptr) {
  37. // It is implementation-defined whether allocation occurs if the space
  38. // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
  39. // non-zero, if the space requested was zero.
  40. if (Count == 0 || Sz == 0)
  41. return safe_malloc(1);
  42. report_bad_alloc_error("Allocation failed");
  43. }
  44. return Result;
  45. }
  46. LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
  47. void *Result = std::realloc(Ptr, Sz);
  48. if (Result == nullptr) {
  49. // It is implementation-defined whether allocation occurs if the space
  50. // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
  51. // non-zero, if the space requested was zero.
  52. if (Sz == 0)
  53. return safe_malloc(1);
  54. report_bad_alloc_error("Allocation failed");
  55. }
  56. return Result;
  57. }
  58. /// Allocate a buffer of memory with the given size and alignment.
  59. ///
  60. /// When the compiler supports aligned operator new, this will use it to to
  61. /// handle even over-aligned allocations.
  62. ///
  63. /// However, this doesn't make any attempt to leverage the fancier techniques
  64. /// like posix_memalign due to portability. It is mostly intended to allow
  65. /// compatibility with platforms that, after aligned allocation was added, use
  66. /// reduced default alignment.
  67. LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void *
  68. allocate_buffer(size_t Size, size_t Alignment);
  69. /// Deallocate a buffer of memory with the given size and alignment.
  70. ///
  71. /// If supported, this will used the sized delete operator. Also if supported,
  72. /// this will pass the alignment to the delete operator.
  73. ///
  74. /// The pointer must have been allocated with the corresponding new operator,
  75. /// most likely using the above helper.
  76. void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment);
  77. } // namespace llvm
  78. #endif