nsTraceRefcnt.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is Mozilla Communicator client code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * L. David Baron <[email protected]>
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either of the GNU General Public License Version 2 or later (the "GPL"),
  27. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. #ifndef nsTraceRefcnt_h___
  39. #define nsTraceRefcnt_h___
  40. #include "nscore.h"
  41. class nsISupports;
  42. // By default refcnt logging is not part of the build.
  43. #undef NS_BUILD_REFCNT_LOGGING
  44. #if (defined(DEBUG) || defined(FORCE_BUILD_REFCNT_LOGGING))
  45. // Make refcnt logging part of the build. This doesn't mean that
  46. // actual logging will occur (that requires a separate enable; see
  47. // nsTraceRefcnt.h for more information).
  48. #define NS_BUILD_REFCNT_LOGGING 1
  49. #endif
  50. // If NO_BUILD_REFCNT_LOGGING is defined then disable refcnt logging
  51. // in the build. This overrides FORCE_BUILD_REFCNT_LOGGING.
  52. #if defined(NO_BUILD_REFCNT_LOGGING)
  53. #undef NS_BUILD_REFCNT_LOGGING
  54. #endif
  55. #ifdef NS_BUILD_REFCNT_LOGGING
  56. #define NS_LOG_ADDREF(_p, _rc, _type, _size) \
  57. nsTraceRefcnt::LogAddRef((_p), (_rc), (_type), (PRUint32) (_size))
  58. #define NS_LOG_RELEASE(_p, _rc, _type) \
  59. nsTraceRefcnt::LogRelease((_p), (_rc), (_type))
  60. #define MOZ_DECL_CTOR_COUNTER(_type)
  61. #define MOZ_COUNT_CTOR(_type) \
  62. PR_BEGIN_MACRO \
  63. nsTraceRefcnt::LogCtor((void*)this, #_type, sizeof(*this)); \
  64. PR_END_MACRO
  65. #define MOZ_COUNT_DTOR(_type) \
  66. PR_BEGIN_MACRO \
  67. nsTraceRefcnt::LogDtor((void*)this, #_type, sizeof(*this)); \
  68. PR_END_MACRO
  69. #ifdef HAVE_CPP_DYNAMIC_CAST_TO_VOID_PTR // from autoconf (XXX needs to be
  70. // set for non-autoconf platforms)
  71. // nsCOMPtr.h allows these macros to be defined by clients
  72. // These logging functions require dynamic_cast<void *>, so we don't
  73. // define these macros if we don't have dynamic_cast.
  74. #define NSCAP_LOG_ASSIGNMENT(_c, _p) \
  75. if (_p) \
  76. nsTraceRefcnt::LogAddCOMPtr((_c),NS_STATIC_CAST(nsISupports*,_p))
  77. #define NSCAP_LOG_RELEASE(_c, _p) \
  78. if (_p) \
  79. nsTraceRefcnt::LogReleaseCOMPtr((_c), NS_STATIC_CAST(nsISupports*,_p))
  80. #endif /* HAVE_CPP_DYNAMIC_CAST_TO_VOID_PTR */
  81. #else /* !NS_BUILD_REFCNT_LOGGING */
  82. #define NS_LOG_ADDREF(_p, _rc, _type, _size)
  83. #define NS_LOG_RELEASE(_p, _rc, _type)
  84. #define MOZ_DECL_CTOR_COUNTER(_type)
  85. #define MOZ_COUNT_CTOR(_type)
  86. #define MOZ_COUNT_DTOR(_type)
  87. #endif /* NS_BUILD_REFCNT_LOGGING */
  88. //----------------------------------------------------------------------
  89. /**
  90. * Note: The implementations for these methods are no-ops in a build
  91. * where NS_BUILD_REFCNT_LOGGING is disabled.
  92. */
  93. class nsTraceRefcnt {
  94. public:
  95. static NS_COM_GLUE void LogAddRef(void* aPtr,
  96. nsrefcnt aNewRefCnt,
  97. const char* aTypeName,
  98. PRUint32 aInstanceSize);
  99. static NS_COM_GLUE void LogRelease(void* aPtr,
  100. nsrefcnt aNewRefCnt,
  101. const char* aTypeName);
  102. static NS_COM_GLUE void LogCtor(void* aPtr, const char* aTypeName,
  103. PRUint32 aInstanceSize);
  104. static NS_COM_GLUE void LogDtor(void* aPtr, const char* aTypeName,
  105. PRUint32 aInstanceSize);
  106. static NS_COM_GLUE void LogAddCOMPtr(void *aCOMPtr, nsISupports *aObject);
  107. static NS_COM_GLUE void LogReleaseCOMPtr(void *aCOMPtr, nsISupports *aObject);
  108. };
  109. #endif /* nsTraceRefcnt_h___ */