memory.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPC_CORE_LIB_GPRPP_MEMORY_H
  19. #define GRPC_CORE_LIB_GPRPP_MEMORY_H
  20. #include <grpc/support/alloc.h>
  21. #include <limits>
  22. #include <memory>
  23. #include <utility>
  24. namespace grpc_core {
  25. // The alignment of memory returned by gpr_malloc().
  26. constexpr size_t kAllignmentForDefaultAllocationInBytes = 8;
  27. // Alternative to new, since we cannot use it (for fear of libstdc++)
  28. template <typename T, typename... Args>
  29. inline T* New(Args&&... args) {
  30. void* p = alignof(T) > kAllignmentForDefaultAllocationInBytes
  31. ? gpr_malloc_aligned(sizeof(T), alignof(T))
  32. : gpr_malloc(sizeof(T));
  33. return new (p) T(std::forward<Args>(args)...);
  34. }
  35. // Alternative to delete, since we cannot use it (for fear of libstdc++)
  36. template <typename T>
  37. inline void Delete(T* p) {
  38. p->~T();
  39. if (alignof(T) > kAllignmentForDefaultAllocationInBytes) {
  40. gpr_free_aligned(p);
  41. } else {
  42. gpr_free(p);
  43. }
  44. }
  45. template <typename T>
  46. class DefaultDelete {
  47. public:
  48. void operator()(T* p) { Delete(p); }
  49. };
  50. template <typename T, typename Deleter = DefaultDelete<T>>
  51. using UniquePtr = std::unique_ptr<T, Deleter>;
  52. template <typename T, typename... Args>
  53. inline UniquePtr<T> MakeUnique(Args&&... args) {
  54. return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
  55. }
  56. // an allocator that uses gpr_malloc/gpr_free
  57. template <class T>
  58. class Allocator {
  59. public:
  60. typedef T value_type;
  61. typedef T* pointer;
  62. typedef const T* const_pointer;
  63. typedef T& reference;
  64. typedef const T& const_reference;
  65. typedef std::size_t size_type;
  66. typedef std::ptrdiff_t difference_type;
  67. typedef std::false_type propagate_on_container_move_assignment;
  68. template <class U>
  69. struct rebind {
  70. typedef Allocator<U> other;
  71. };
  72. typedef std::true_type is_always_equal;
  73. pointer address(reference x) const { return &x; }
  74. const_pointer address(const_reference x) const { return &x; }
  75. pointer allocate(std::size_t n,
  76. std::allocator<void>::const_pointer hint = nullptr) {
  77. return static_cast<pointer>(gpr_malloc(n * sizeof(T)));
  78. }
  79. void deallocate(T* p, std::size_t n) { gpr_free(p); }
  80. size_t max_size() const {
  81. return std::numeric_limits<size_type>::max() / sizeof(value_type);
  82. }
  83. void construct(pointer p, const_reference val) { new ((void*)p) T(val); }
  84. template <class U, class... Args>
  85. void construct(U* p, Args&&... args) {
  86. ::new ((void*)p) U(std::forward<Args>(args)...);
  87. }
  88. void destroy(pointer p) { p->~T(); }
  89. template <class U>
  90. void destroy(U* p) {
  91. p->~U();
  92. }
  93. };
  94. } // namespace grpc_core
  95. #endif /* GRPC_CORE_LIB_GPRPP_MEMORY_H */