3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_LIB_GPRPP_MEMORY_H
20 #define GRPC_CORE_LIB_GPRPP_MEMORY_H
22 #include <grpc/support/port_platform.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
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*);
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&&...);
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)...);
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
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;
73 inline void Delete(T* p) {
74 Delete<T, /*can_be_null=*/true>(p);
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);
87 template <typename T, typename Deleter = DefaultDelete<T>>
88 using UniquePtr = std::unique_ptr<T, Deleter>;
90 template <typename T, typename... Args>
91 inline UniquePtr<T> MakeUnique(Args&&... args) {
92 return UniquePtr<T>(New<T>(std::forward<Args>(args)...));
95 // an allocator that uses gpr_malloc/gpr_free
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;
109 typedef Allocator<U> other;
111 typedef std::true_type is_always_equal;
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)));
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);
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)...);
128 void destroy(pointer p) { p->~T(); }
135 } // namespace grpc_core
137 #endif /* GRPC_CORE_LIB_GPRPP_MEMORY_H */