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
19 #ifndef GRPC_CORE_LIB_GPRPP_MEMORY_H
20 #define GRPC_CORE_LIB_GPRPP_MEMORY_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include <grpc/support/alloc.h>
25
26 #include <limits>
27 #include <memory>
28 #include <utility>
29
30 // Add this to a class that want to use Delete(), but has a private or
31 // protected destructor.
32 #define GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE \
33 template <typename T> \
34 friend void grpc_core::Delete(T*);
35 // Add this to a class that want to use New(), but has a private or
36 // protected constructor.
37 #define GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW \
38 template <typename T, typename... Args> \
39 friend T* grpc_core::New(Args&&...);
40
41 namespace grpc_core {
42
43 // The alignment of memory returned by gpr_malloc().
44 constexpr size_t kAlignmentForDefaultAllocationInBytes = 8;
45
46 // Alternative to new, since we cannot use it (for fear of libstdc++)
47 template <typename T, typename... Args>
New(Args &&...args)48 inline T* New(Args&&... args) {
49 void* p = alignof(T) > kAlignmentForDefaultAllocationInBytes
50 ? gpr_malloc_aligned(sizeof(T), alignof(T))
51 : gpr_malloc(sizeof(T));
52 return new (p) T(std::forward<Args>(args)...);
53 }
54
55 // Alternative to delete, since we cannot use it (for fear of libstdc++)
56 template <typename T>
Delete(T * p)57 inline void Delete(T* p) {
58 if (p == nullptr) return;
59 p->~T();
60 if (alignof(T) > kAlignmentForDefaultAllocationInBytes) {
61 gpr_free_aligned(p);
62 } else {
63 gpr_free(p);
64 }
65 }
66
67 template <typename T>
68 class DefaultDelete {
69 public:
operator()70 void operator()(T* p) { Delete(p); }
71 };
72
73 template <typename T, typename Deleter = DefaultDelete<T>>
74 using UniquePtr = std::unique_ptr<T, Deleter>;
75
76 template <typename T, typename... Args>
MakeUnique(Args &&...args)77 inline UniquePtr<T> MakeUnique(Args&&... args) {
78 return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
79 }
80
81 // an allocator that uses gpr_malloc/gpr_free
82 template <class T>
83 class Allocator {
84 public:
85 typedef T value_type;
86 typedef T* pointer;
87 typedef const T* const_pointer;
88 typedef T& reference;
89 typedef const T& const_reference;
90 typedef std::size_t size_type;
91 typedef std::ptrdiff_t difference_type;
92 typedef std::false_type propagate_on_container_move_assignment;
93 template <class U>
94 struct rebind {
95 typedef Allocator<U> other;
96 };
97 typedef std::true_type is_always_equal;
98
address(reference x)99 pointer address(reference x) const { return &x; }
address(const_reference x)100 const_pointer address(const_reference x) const { return &x; }
101 pointer allocate(std::size_t n,
102 std::allocator<void>::const_pointer hint = nullptr) {
103 return static_cast<pointer>(gpr_malloc(n * sizeof(T)));
104 }
deallocate(T * p,std::size_t n)105 void deallocate(T* p, std::size_t n) { gpr_free(p); }
max_size()106 size_t max_size() const {
107 return std::numeric_limits<size_type>::max() / sizeof(value_type);
108 }
construct(pointer p,const_reference val)109 void construct(pointer p, const_reference val) { new ((void*)p) T(val); }
110 template <class U, class... Args>
construct(U * p,Args &&...args)111 void construct(U* p, Args&&... args) {
112 ::new ((void*)p) U(std::forward<Args>(args)...);
113 }
destroy(pointer p)114 void destroy(pointer p) { p->~T(); }
115 template <class U>
destroy(U * p)116 void destroy(U* p) {
117 p->~U();
118 }
119 };
120
121 } // namespace grpc_core
122
123 #endif /* GRPC_CORE_LIB_GPRPP_MEMORY_H */
124