Bug 771489: Use standard threadsafe refcounting in host resolver. r=sworkman
authorJosh Aas <joshmoz@gmail.com>
Tue, 10 Jul 2012 10:14:01 -0400
changeset 98810 7578fa8c76f72133c9fd1fb394e9fe4776c3a941
parent 98809 e8dc86da1363480c2720bb27dea22df7b9bd697c
child 98811 6e6bfdabd6631f3f21bb984c99d670028a76e9fe
push id11663
push userjosh@mozilla.com
push dateTue, 10 Jul 2012 14:14:23 +0000
treeherdermozilla-inbound@7578fa8c76f7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssworkman
bugs771489
milestone16.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 771489: Use standard threadsafe refcounting in host resolver. r=sworkman
netwerk/dns/nsHostResolver.cpp
netwerk/dns/nsHostResolver.h
--- a/netwerk/dns/nsHostResolver.cpp
+++ b/netwerk/dns/nsHostResolver.cpp
@@ -144,48 +144,47 @@ private:
 //----------------------------------------------------------------------------
 
 // this macro filters out any flags that are not used when constructing the
 // host key.  the significant flags are those that would affect the resulting
 // host record (i.e., the flags that are passed down to PR_GetAddrInfoByName).
 #define RES_KEY_FLAGS(_f) ((_f) & nsHostResolver::RES_CANON_NAME)
 
 nsHostRecord::nsHostRecord(const nsHostKey *key)
-    : _refc(1)
-    , addr_info_lock("nsHostRecord.addr_info_lock")
+    : addr_info_lock("nsHostRecord.addr_info_lock")
     , addr_info_gencnt(0)
     , addr_info(nsnull)
     , addr(nsnull)
     , negative(false)
     , resolving(false)
     , onQueue(false)
     , usingAnyThread(false)
 {
     host = ((char *) this) + sizeof(nsHostRecord);
     memcpy((char *) host, key->host, strlen(key->host) + 1);
     flags = key->flags;
     af = key->af;
 
-    NS_LOG_ADDREF(this, 1, "nsHostRecord", sizeof(nsHostRecord));
     expiration = NowInMinutes();
 
     PR_INIT_CLIST(this);
     PR_INIT_CLIST(&callbacks);
 }
 
 nsresult
 nsHostRecord::Create(const nsHostKey *key, nsHostRecord **result)
 {
     size_t hostLen = strlen(key->host) + 1;
     size_t size = hostLen + sizeof(nsHostRecord);
 
     // Use placement new to create the object with room for the hostname
     // allocated after it.
     void *place = ::operator new(size);
     *result = new(place) nsHostRecord(key);
+    NS_ADDREF(*result);
     return NS_OK;
 }
 
 nsHostRecord::~nsHostRecord()
 {
     if (addr)
         free(addr);
 }
--- a/netwerk/dns/nsHostResolver.h
+++ b/netwerk/dns/nsHostResolver.h
@@ -17,34 +17,16 @@
 #include "nsIDNSListener.h"
 #include "nsString.h"
 #include "nsTArray.h"
 
 class nsHostResolver;
 class nsHostRecord;
 class nsResolveHostCallback;
 
-/* XXX move this someplace more generic */
-#define NS_DECL_REFCOUNTED_THREADSAFE(classname)                             \
-  private:                                                                   \
-    nsAutoRefCnt _refc;                                                      \
-  public:                                                                    \
-    PRInt32 AddRef() {                                                       \
-        PRInt32 n = NS_AtomicIncrementRefcnt(_refc);                         \
-        NS_LOG_ADDREF(this, n, #classname, sizeof(classname));               \
-        return n;                                                            \
-    }                                                                        \
-    PRInt32 Release() {                                                      \
-        PRInt32 n = NS_AtomicDecrementRefcnt(_refc);                         \
-        NS_LOG_RELEASE(this, n, #classname);                                 \
-        if (n == 0)                                                          \
-            delete this;                                                     \
-        return n;                                                            \
-    }
-
 #define MAX_RESOLVER_THREADS_FOR_ANY_PRIORITY  3
 #define MAX_RESOLVER_THREADS_FOR_HIGH_PRIORITY 5
 #define MAX_NON_PRIORITY_REQUESTS 150
 
 #define MAX_RESOLVER_THREADS (MAX_RESOLVER_THREADS_FOR_ANY_PRIORITY + \
                               MAX_RESOLVER_THREADS_FOR_HIGH_PRIORITY)
 
 struct nsHostKey
@@ -57,17 +39,17 @@ struct nsHostKey
 /**
  * nsHostRecord - ref counted object type stored in host resolver cache.
  */
 class nsHostRecord : public PRCList, public nsHostKey
 {
     typedef mozilla::Mutex Mutex;
 
 public:
-    NS_DECL_REFCOUNTED_THREADSAFE(nsHostRecord)
+    NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nsHostRecord)
 
     /* instantiates a new host record */
     static nsresult Create(const nsHostKey *key, nsHostRecord **record);
 
     /* a fully resolved host record has either a non-null |addr_info| or |addr|
      * field.  if |addr_info| is null, it implies that the |host| is an IP
      * address literal.  in which case, |addr| contains the parsed address.
      * otherwise, if |addr_info| is non-null, then it contains one or many
@@ -173,17 +155,17 @@ class nsHostResolver
 {
     typedef mozilla::CondVar CondVar;
     typedef mozilla::Mutex Mutex;
 
 public:
     /**
      * host resolver instances are reference counted.
      */
-    NS_DECL_REFCOUNTED_THREADSAFE(nsHostResolver)
+    NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nsHostResolver)
 
     /**
      * creates an addref'd instance of a nsHostResolver object.
      */
     static nsresult Create(PRUint32         maxCacheEntries,  // zero disables cache
                            PRUint32         maxCacheLifetime, // minutes
                            PRUint32         lifetimeGracePeriod, // minutes
                            nsHostResolver **resolver);