Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / gprpp / memory.h
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 #include <grpc/support/log.h>
26
27 #include <limits>
28 #include <memory>
29 #include <utility>
30
31 // Add this to a class that want to use Delete(), but has a private or
32 // protected destructor.
33 // Should not be used in new code.
34 // TODO(juanlishen): Remove this macro, and instead comment that the public dtor
35 // should not be used directly.
36 #define GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE         \
37   template <typename _Delete_T, bool _Delete_can_be_null> \
38   friend void ::grpc_core::Delete(_Delete_T*);            \
39   template <typename _Delete_T>                           \
40   friend void ::grpc_core::Delete(_Delete_T*);
41
42 // Add this to a class that want to use New(), but has a private or
43 // protected constructor.
44 // Should not be used in new code.
45 // TODO(juanlishen): Remove this macro, and instead comment that the public dtor
46 // should not be used directly.
47 #define GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW      \
48   template <typename _New_T, typename... _New_Args> \
49   friend _New_T* grpc_core::New(_New_Args&&...);
50
51 namespace grpc_core {
52
53 // Alternative to new, since we cannot use it (for fear of libstdc++)
54 template <typename T, typename... Args>
55 inline T* New(Args&&... args) {
56   void* p = gpr_malloc(sizeof(T));
57   return new (p) T(std::forward<Args>(args)...);
58 }
59
60 // Alternative to delete, since we cannot use it (for fear of libstdc++)
61 // We cannot add a default value for can_be_null, because they are used as
62 // as friend template methods where we cannot define a default value.
63 // Instead we simply define two variants, one with and one without the boolean
64 // argument.
65 template <typename T, bool can_be_null>
66 inline void Delete(T* p) {
67   GPR_DEBUG_ASSERT(can_be_null || p != nullptr);
68   if (can_be_null && p == nullptr) return;
69   p->~T();
70   gpr_free(p);
71 }
72 template <typename T>
73 inline void Delete(T* p) {
74   Delete<T, /*can_be_null=*/true>(p);
75 }
76
77 template <typename T>
78 class DefaultDelete {
79  public:
80   void operator()(T* p) {
81     // std::unique_ptr is gauranteed not to call the deleter
82     // if the pointer is nullptr.
83     Delete<T, /*can_be_null=*/false>(p);
84   }
85 };
86
87 template <typename T, typename Deleter = DefaultDelete<T>>
88 using UniquePtr = std::unique_ptr<T, Deleter>;
89
90 template <typename T, typename... Args>
91 inline UniquePtr<T> MakeUnique(Args&&... args) {
92   return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
93 }
94
95 // an allocator that uses gpr_malloc/gpr_free
96 template <class T>
97 class Allocator {
98  public:
99   typedef T value_type;
100   typedef T* pointer;
101   typedef const T* const_pointer;
102   typedef T& reference;
103   typedef const T& const_reference;
104   typedef std::size_t size_type;
105   typedef std::ptrdiff_t difference_type;
106   typedef std::false_type propagate_on_container_move_assignment;
107   template <class U>
108   struct rebind {
109     typedef Allocator<U> other;
110   };
111   typedef std::true_type is_always_equal;
112
113   pointer address(reference x) const { return &x; }
114   const_pointer address(const_reference x) const { return &x; }
115   pointer allocate(std::size_t n,
116                    std::allocator<void>::const_pointer hint = nullptr) {
117     return static_cast<pointer>(gpr_malloc(n * sizeof(T)));
118   }
119   void deallocate(T* p, std::size_t n) { gpr_free(p); }
120   size_t max_size() const {
121     return std::numeric_limits<size_type>::max() / sizeof(value_type);
122   }
123   void construct(pointer p, const_reference val) { new ((void*)p) T(val); }
124   template <class U, class... Args>
125   void construct(U* p, Args&&... args) {
126     ::new ((void*)p) U(std::forward<Args>(args)...);
127   }
128   void destroy(pointer p) { p->~T(); }
129   template <class U>
130   void destroy(U* p) {
131     p->~U();
132   }
133 };
134
135 }  // namespace grpc_core
136
137 #endif /* GRPC_CORE_LIB_GPRPP_MEMORY_H */