blob: ebd620c5536170438f7fbf395a57d6eec20bdb3e [file] [log] [blame]
Mark D. Roth70db6632017-11-27 14:53:26 -08001/*
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_SUPPORT_REFERENCE_COUNTED_H
20#define GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H
21
22#include <grpc/support/sync.h>
23
24#include "src/core/lib/debug/trace.h"
25#include "src/core/lib/support/debug_location.h"
26
27namespace grpc_core {
28
29class ReferenceCounted {
30 public:
31 void Ref();
32 void Ref(const DebugLocation& location, const char* reason);
33
34 bool Unref();
35 bool Unref(const DebugLocation& location, const char* reason);
36
37 // Not copyable nor movable.
38 ReferenceCounted(const ReferenceCounted&) = delete;
39 ReferenceCounted& operator=(const ReferenceCounted&) = delete;
40
41 protected:
42 explicit ReferenceCounted(TraceFlag* trace_flag) : trace_flag_(trace_flag) {
43 gpr_ref_init(&refs_, 1);
44 }
45
46 virtual ~ReferenceCounted() {}
47
48 private:
49 TraceFlag* trace_flag_;
50 gpr_refcount refs_;
51};
52
53} // namespace grpc_core
54
55#endif // GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H