GRPC C++  1.26.0
memory.h
Go to the documentation of this file.
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 
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 namespace grpc_core {
32 
34  public:
35  void operator()(char* p) {
36  if (p == nullptr) return;
37  gpr_free(p);
38  }
39 };
40 
41 // UniquePtr<T> is only allowed for char and UniquePtr<char> is deprecated
42 // in favor of std::string. UniquePtr<char> is equivalent std::unique_ptr
43 // except that it uses gpr_free for deleter.
44 template <typename T>
45 using UniquePtr = std::unique_ptr<T, DefaultDeleteChar>;
46 
47 // TODO(veblush): Replace this with absl::make_unique once abseil is added.
48 template <typename T, typename... Args>
49 inline std::unique_ptr<T> MakeUnique(Args&&... args) {
50  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
51 }
52 
53 } // namespace grpc_core
54 
55 #endif /* GRPC_CORE_LIB_GPRPP_MEMORY_H */
GPRAPI void gpr_free(void *ptr)
free
Definition: memory.h:33
Internal thread interface.
Definition: backoff.h:26
void operator()(char *p)
Definition: memory.h:35
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: memory.h:45
std::unique_ptr< T > MakeUnique(Args &&... args)
Definition: memory.h:49