MemAlloc.h 3.4 KB

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