This is an automated email from the ASF dual-hosted git repository.

wkaras pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 0d8e42524b Add RefCountObjInHeap class. (#11199)
0d8e42524b is described below

commit 0d8e42524b691eaa461795e8e45013f7ee908e7a
Author: Walt Karas <[email protected]>
AuthorDate: Tue Apr 9 10:25:11 2024 -0400

    Add RefCountObjInHeap class. (#11199)
    
    And some related cleanup.
---
 include/iocore/eventsystem/ConfigProcessor.h |  2 +-
 include/proxy/http/remap/PluginDso.h         | 10 ++++++----
 include/proxy/http/remap/UrlRewrite.h        |  2 +-
 include/proxy/logging/LogFieldAliasMap.h     |  2 +-
 include/proxy/logging/LogFile.h              |  2 +-
 include/proxy/logging/LogFilter.h            |  2 +-
 include/proxy/logging/LogFormat.h            |  2 +-
 include/proxy/logging/LogObject.h            |  2 +-
 include/tscore/Ptr.h                         | 23 ++++++++++++++++++-----
 src/iocore/net/P_NetAccept.h                 |  2 +-
 src/proxy/hdrs/unit_tests/test_Hdrs.cc       | 11 ++++++++++-
 src/proxy/http/remap/PluginDso.cc            | 20 ++++++++++----------
 src/proxy/logging/LogFormat.cc               |  3 +--
 src/tscore/unit_tests/test_Ptr.cc            |  2 +-
 14 files changed, 54 insertions(+), 31 deletions(-)

diff --git a/include/iocore/eventsystem/ConfigProcessor.h 
b/include/iocore/eventsystem/ConfigProcessor.h
index 6af0da2020..d31b4f5aae 100644
--- a/include/iocore/eventsystem/ConfigProcessor.h
+++ b/include/iocore/eventsystem/ConfigProcessor.h
@@ -31,7 +31,7 @@ class ProxyMutex;
 
 #define MAX_CONFIGS 100
 
-using ConfigInfo = RefCountObj;
+using ConfigInfo = RefCountObjInHeap;
 
 class ConfigProcessor
 {
diff --git a/include/proxy/http/remap/PluginDso.h 
b/include/proxy/http/remap/PluginDso.h
index 41f18b943d..e93954f615 100644
--- a/include/proxy/http/remap/PluginDso.h
+++ b/include/proxy/http/remap/PluginDso.h
@@ -36,6 +36,7 @@
 #include <vector>
 #include <forward_list>
 #include <ctime>
+#include <atomic>
 
 #include "swoc/IntrusiveDList.h"
 
@@ -49,7 +50,7 @@ namespace fs = swoc::file;
 
 #include "proxy/Plugin.h"
 
-class PluginThreadContext : public RefCountObj
+class PluginThreadContext : public RefCountObjInHeap
 {
 public:
   virtual void acquire()                  = 0;
@@ -100,10 +101,10 @@ public:
 
   void incInstanceCount();
   void decInstanceCount();
-  int instanceCount();
+  int instanceCount() const;
   bool isDynamicReloadEnabled() const;
 
-  class LoadedPlugins : public RefCountObj
+  class LoadedPlugins : public RefCountObjInHeap
   {
   public:
     LoadedPlugins() : _mutex(new_ProxyMutex()) {}
@@ -173,7 +174,8 @@ protected:
 
   static Ptr<LoadedPlugins>
     _plugins; /** @brief a global list of plugins, usually maintained by a 
plugin factory or plugin instance itself */
-  RefCountObj _instanceCount; /** @brief used for properly calling "done" and 
"indicate config reload" methods by the factory */
+  std::atomic<int> _instanceCount{
+    0}; /** @brief used for properly calling "done" and "indicate config 
reload" methods by the factory */
 };
 
 inline const DbgCtl &
diff --git a/include/proxy/http/remap/UrlRewrite.h 
b/include/proxy/http/remap/UrlRewrite.h
index 797ad94c47..d87b6b0dcb 100644
--- a/include/proxy/http/remap/UrlRewrite.h
+++ b/include/proxy/http/remap/UrlRewrite.h
@@ -56,7 +56,7 @@ enum mapping_type {
 /**
  *
  **/
-class UrlRewrite : public RefCountObj
+class UrlRewrite : public RefCountObjInHeap
 {
 public:
   using URLTable = std::unordered_map<std::string, UrlMappingPathIndex *>;
diff --git a/include/proxy/logging/LogFieldAliasMap.h 
b/include/proxy/logging/LogFieldAliasMap.h
index 23bc039941..21d56e883a 100644
--- a/include/proxy/logging/LogFieldAliasMap.h
+++ b/include/proxy/logging/LogFieldAliasMap.h
@@ -69,7 +69,7 @@ any memory the map may have allocated.
 
  *****************************************************************************/
 
-class LogFieldAliasMap : public RefCountObj
+class LogFieldAliasMap : public RefCountObjInHeap
 {
 public:
   // the logging system assumes log entries of type sINT are
diff --git a/include/proxy/logging/LogFile.h b/include/proxy/logging/LogFile.h
index 0326d3498a..f374c1ee68 100644
--- a/include/proxy/logging/LogFile.h
+++ b/include/proxy/logging/LogFile.h
@@ -39,7 +39,7 @@ class BaseMetaInfo;
   LogFile
   -------------------------------------------------------------------------*/
 
-class LogFile : public LogBufferSink, public RefCountObj
+class LogFile : public LogBufferSink, public RefCountObjInHeap
 {
 public:
   LogFile(const char *name, const char *header, LogFileFormat format, uint64_t 
signature, size_t ascii_buffer_size = 4 * 9216,
diff --git a/include/proxy/logging/LogFilter.h 
b/include/proxy/logging/LogFilter.h
index 2a1c0cc692..2493360b7a 100644
--- a/include/proxy/logging/LogFilter.h
+++ b/include/proxy/logging/LogFilter.h
@@ -39,7 +39,7 @@
   function which, given a LogAccess object, returns true if
   the log entry is to be tossed out.
   -------------------------------------------------------------------------*/
-class LogFilter : public RefCountObj
+class LogFilter : public RefCountObjInHeap
 {
 public:
   enum Type {
diff --git a/include/proxy/logging/LogFormat.h 
b/include/proxy/logging/LogFormat.h
index 860a30bbc7..6e14bd11b0 100644
--- a/include/proxy/logging/LogFormat.h
+++ b/include/proxy/logging/LogFormat.h
@@ -49,7 +49,7 @@ enum LogFileFormat {
   which is defined as a set of fields.
   -------------------------------------------------------------------------*/
 
-class LogFormat : public RefCountObj
+class LogFormat : public RefCountObjInHeap
 {
 public:
   LogFormat(const char *name, const char *format_str, unsigned interval_sec = 
0, LogEscapeType escape_type = LOG_ESCAPE_NONE);
diff --git a/include/proxy/logging/LogObject.h 
b/include/proxy/logging/LogObject.h
index 9250693b0f..1dcced90e1 100644
--- a/include/proxy/logging/LogObject.h
+++ b/include/proxy/logging/LogObject.h
@@ -81,7 +81,7 @@ public:
 
 // LogObject is atomically reference counted, and the reference count is 
always owned by
 // one or more LogObjectManagers.
-class LogObject : public RefCountObj
+class LogObject : public RefCountObjInHeap
 {
 public:
   enum LogObjectFlags {
diff --git a/include/tscore/Ptr.h b/include/tscore/Ptr.h
index 43ae72199d..4b8bc38087 100644
--- a/include/tscore/Ptr.h
+++ b/include/tscore/Ptr.h
@@ -40,6 +40,7 @@ struct ForceVFPTToTop {
 //
 // class RefCountObj
 // prototypical class for reference counting
+// not necessarily allocated with new
 //
 ////////////////////////////////////////////////////////////////////////
 class RefCountObj : public ForceVFPTToTop
@@ -71,16 +72,28 @@ public:
     return m_refcount;
   }
 
-  virtual void
-  free()
-  {
-    delete this;
-  }
+  virtual void free() = 0;
 
 private:
   std::atomic<int> m_refcount = 0;
 };
 
+////////////////////////////////////////////////////////////////////////
+//
+// class RefCountObjInHeap
+// reference counted object allocated with new
+//
+////////////////////////////////////////////////////////////////////////
+class RefCountObjInHeap : public RefCountObj
+{
+public:
+  void
+  free() override
+  {
+    delete this;
+  }
+};
+
 ////////////////////////////////////////////////////////////////////////
 //
 // class Ptr
diff --git a/src/iocore/net/P_NetAccept.h b/src/iocore/net/P_NetAccept.h
index efaff3709a..3272466cbb 100644
--- a/src/iocore/net/P_NetAccept.h
+++ b/src/iocore/net/P_NetAccept.h
@@ -60,7 +60,7 @@ AcceptFunction net_accept;
 class UnixNetVConnection;
 
 // TODO fix race between cancel accept and call back
-struct NetAcceptAction : public Action, public RefCountObj {
+struct NetAcceptAction : public Action, public RefCountObjInHeap {
   Server *server;
 
   void
diff --git a/src/proxy/hdrs/unit_tests/test_Hdrs.cc 
b/src/proxy/hdrs/unit_tests/test_Hdrs.cc
index e4cac5e3e2..4469b83651 100644
--- a/src/proxy/hdrs/unit_tests/test_Hdrs.cc
+++ b/src/proxy/hdrs/unit_tests/test_Hdrs.cc
@@ -360,6 +360,15 @@ test_http_hdr_ctl_char(int testnum, const char *request, 
const char * /*request_
   return 1;
 }
 
+struct TestRefCountObj : public RefCountObj {
+  void
+  free() override
+  {
+    std::printf("FAILED: TestRefCountObj object should not be freed\n");
+    std::exit(1);
+  }
+};
+
 int
 test_http_hdr_print_and_copy_aux(int testnum, const char *request, const char 
*request_tgt, const char *response,
                                  const char *response_tgt)
@@ -404,7 +413,7 @@ test_http_hdr_print_and_copy_aux(int testnum, const char 
*request, const char *r
 
   /*** (2) copy the request header ***/
   HTTPHdr new_hdr, marshal_hdr;
-  RefCountObj ref;
+  TestRefCountObj ref;
 
   // Pretend to pin this object with a refcount.
   ref.refcount_inc();
diff --git a/src/proxy/http/remap/PluginDso.cc 
b/src/proxy/http/remap/PluginDso.cc
index 9da092a790..1fbc235998 100644
--- a/src/proxy/http/remap/PluginDso.cc
+++ b/src/proxy/http/remap/PluginDso.cc
@@ -266,15 +266,15 @@ PluginDso::clean(std::string &error)
 void
 PluginDso::acquire()
 {
-  this->refcount_inc();
-  PluginDbg(_dbg_ctl(), "plugin DSO acquire (ref-count:%d, dso-addr:%p)", 
this->refcount(), this);
+  ++this->_instanceCount;
+  PluginDbg(_dbg_ctl(), "plugin DSO acquire (ref-count:%d, dso-addr:%p)", 
this->_instanceCount.load(), this);
 }
 
 void
 PluginDso::release()
 {
-  PluginDbg(_dbg_ctl(), "plugin DSO release (ref-count:%d, dso-addr:%p)", 
this->refcount() - 1, this);
-  if (0 == this->refcount_dec()) {
+  PluginDbg(_dbg_ctl(), "plugin DSO release (ref-count:%d, dso-addr:%p)", 
this->_instanceCount.load() - 1, this);
+  if (0 == --this->_instanceCount) {
     PluginDbg(_dbg_ctl(), "unloading plugin DSO '%s' (dso-addr:%p)", 
_configPath.c_str(), this);
     _plugins->remove(this);
   }
@@ -283,21 +283,21 @@ PluginDso::release()
 void
 PluginDso::incInstanceCount()
 {
-  _instanceCount.refcount_inc();
-  PluginDbg(_dbg_ctl(), "instance count (inst-count:%d, dso-addr:%p)", 
_instanceCount.refcount(), this);
+  ++_instanceCount;
+  PluginDbg(_dbg_ctl(), "instance count (inst-count:%d, dso-addr:%p)", 
_instanceCount.load(), this);
 }
 
 void
 PluginDso::decInstanceCount()
 {
-  _instanceCount.refcount_dec();
-  PluginDbg(_dbg_ctl(), "instance count (inst-count:%d, dso-addr:%p)", 
_instanceCount.refcount(), this);
+  --_instanceCount;
+  PluginDbg(_dbg_ctl(), "instance count (inst-count:%d, dso-addr:%p)", 
_instanceCount.load(), this);
 }
 
 int
-PluginDso::instanceCount()
+PluginDso::instanceCount() const
 {
-  return _instanceCount.refcount();
+  return _instanceCount;
 }
 
 bool
diff --git a/src/proxy/logging/LogFormat.cc b/src/proxy/logging/LogFormat.cc
index 626f79f011..1fceba2888 100644
--- a/src/proxy/logging/LogFormat.cc
+++ b/src/proxy/logging/LogFormat.cc
@@ -206,8 +206,7 @@ LogFormat::LogFormat(const char *name, const char 
*format_str, unsigned interval
   -------------------------------------------------------------------------*/
 
 LogFormat::LogFormat(const LogFormat &rhs)
-  : RefCountObj(),
-    m_interval_sec(0),
+  : m_interval_sec(0),
     m_interval_next(0),
     m_agg_marshal_space(nullptr),
     m_valid(rhs.m_valid),
diff --git a/src/tscore/unit_tests/test_Ptr.cc 
b/src/tscore/unit_tests/test_Ptr.cc
index bcdbd57b30..63f369dd8f 100644
--- a/src/tscore/unit_tests/test_Ptr.cc
+++ b/src/tscore/unit_tests/test_Ptr.cc
@@ -22,7 +22,7 @@
 #include "tscore/Ptr.h"
 #include "catch.hpp"
 
-struct PtrObject : RefCountObj {
+struct PtrObject : RefCountObjInHeap {
   PtrObject(unsigned *_c) : count(_c) { ++(*count); }
   ~PtrObject() override { --(*count); }
   unsigned *count;

Reply via email to