aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
authorGravatar Mark D. Roth <roth@google.com>2017-11-27 14:53:26 -0800
committerGravatar Mark D. Roth <roth@google.com>2017-11-27 14:53:26 -0800
commit70db663ae8773dc45464c904603a3e73045b45c5 (patch)
treedc7b4b650ed6e898f8cf255237dc3f6f4b04eaeb /src/core
parentde93112a3f70afa39d3e9aa87da165f9f737fdef (diff)
Add ReferenceCounted base class.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/lib/support/debug_location.h48
-rw-r--r--src/core/lib/support/reference_counted.cc58
-rw-r--r--src/core/lib/support/reference_counted.h55
3 files changed, 161 insertions, 0 deletions
diff --git a/src/core/lib/support/debug_location.h b/src/core/lib/support/debug_location.h
new file mode 100644
index 0000000000..9260752d5e
--- /dev/null
+++ b/src/core/lib/support/debug_location.h
@@ -0,0 +1,48 @@
+/*
+ *
+ * Copyright 2017 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H
+#define GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H
+
+namespace grpc_core {
+
+#ifndef NDEBUG
+class DebugLocation {
+ public:
+ DebugLocation(const char* file, int line) : file_(file), line_(line) {}
+ bool Log() const { return true; }
+ const char* file() const { return file_; }
+ int line() const { return line_; }
+ private:
+ const char* file_;
+ const int line_;
+};
+#define DEBUG_LOCATION DebugLocation(__FILE__, __LINE__)
+#else
+class DebugLocation {
+ public:
+ bool Log() const { return false; }
+ const char* file() const { return nullptr; }
+ int line() const { return -1; }
+};
+#define DEBUG_LOCATION DebugLocation()
+#endif
+
+} // namespace grpc_core
+
+#endif // GRPC_CORE_LIB_SUPPORT_DEBUG_LOCATION_H
diff --git a/src/core/lib/support/reference_counted.cc b/src/core/lib/support/reference_counted.cc
new file mode 100644
index 0000000000..853a29da2f
--- /dev/null
+++ b/src/core/lib/support/reference_counted.cc
@@ -0,0 +1,58 @@
+/*
+ *
+ * Copyright 2017 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "src/core/lib/support/reference_counted.h"
+
+#include <grpc/support/log.h>
+
+namespace grpc_core {
+
+void ReferenceCounted::Ref(const DebugLocation& location, const char* reason) {
+ if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) {
+ gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count);
+ gpr_log(GPR_DEBUG, "%s:%p %s:%d ref %" PRIdPTR " -> %" PRIdPTR " %s",
+ trace_flag_->name(), this, location.file(), location.line(),
+ old_refs, old_refs + 1, reason);
+ }
+ Ref();
+}
+
+void ReferenceCounted::Ref() {
+ gpr_ref(&refs_);
+}
+
+bool ReferenceCounted::Unref(const DebugLocation& location,
+ const char* reason) {
+ if (location.Log() && trace_flag_ != nullptr && trace_flag_->enabled()) {
+ gpr_atm old_refs = gpr_atm_no_barrier_load(&refs_.count);
+ gpr_log(GPR_DEBUG, "%s:%p %s:%d unref %" PRIdPTR " -> %" PRIdPTR " %s",
+ trace_flag_->name(), this, location.file(), location.line(),
+ old_refs, old_refs - 1, reason);
+ }
+ return Unref();
+}
+
+bool ReferenceCounted::Unref() {
+ if (gpr_unref(&refs_)) {
+ delete this;
+ return true;
+ }
+ return false;
+}
+
+} // namespace grpc_core
diff --git a/src/core/lib/support/reference_counted.h b/src/core/lib/support/reference_counted.h
new file mode 100644
index 0000000000..ebd620c553
--- /dev/null
+++ b/src/core/lib/support/reference_counted.h
@@ -0,0 +1,55 @@
+/*
+ *
+ * Copyright 2017 gRPC authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H
+#define GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H
+
+#include <grpc/support/sync.h>
+
+#include "src/core/lib/debug/trace.h"
+#include "src/core/lib/support/debug_location.h"
+
+namespace grpc_core {
+
+class ReferenceCounted {
+ public:
+ void Ref();
+ void Ref(const DebugLocation& location, const char* reason);
+
+ bool Unref();
+ bool Unref(const DebugLocation& location, const char* reason);
+
+ // Not copyable nor movable.
+ ReferenceCounted(const ReferenceCounted&) = delete;
+ ReferenceCounted& operator=(const ReferenceCounted&) = delete;
+
+ protected:
+ explicit ReferenceCounted(TraceFlag* trace_flag) : trace_flag_(trace_flag) {
+ gpr_ref_init(&refs_, 1);
+ }
+
+ virtual ~ReferenceCounted() {}
+
+ private:
+ TraceFlag* trace_flag_;
+ gpr_refcount refs_;
+};
+
+} // namespace grpc_core
+
+#endif // GRPC_CORE_LIB_SUPPORT_REFERENCE_COUNTED_H