nsMemory.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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.org 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. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either of the GNU General Public License Version 2 or later (the "GPL"),
  26. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #ifndef nsMemory_h__
  38. #define nsMemory_h__
  39. #include "nsXPCOM.h"
  40. #include "nsIMemory.h"
  41. #define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
  42. #define NS_MEMORY_CLASSNAME "Global Memory Service"
  43. #define NS_MEMORY_CID \
  44. { /* 30a04e40-38e7-11d4-8cf5-0060b0fc14a3 */ \
  45. 0x30a04e40, \
  46. 0x38e7, \
  47. 0x11d4, \
  48. {0x8c, 0xf5, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \
  49. }
  50. /**
  51. * Static helper routines to manage memory. These routines allow easy access
  52. * to xpcom's built-in (global) nsIMemory implementation, without needing
  53. * to go through the service manager to get it. However this requires clients
  54. * to link with the xpcom DLL.
  55. *
  56. * This class is not threadsafe and is intented for use only on the main
  57. * thread.
  58. */
  59. class nsMemory
  60. {
  61. public:
  62. static NS_HIDDEN_(void*) Alloc(size_t size)
  63. { return NS_Alloc(size); }
  64. static NS_HIDDEN_(void*) Realloc(void* ptr, PRSize size)
  65. { return NS_Realloc(ptr, size); }
  66. static NS_HIDDEN_(void) Free(void* ptr)
  67. { NS_Free(ptr); }
  68. static NS_COM_GLUE nsresult HeapMinimize(PRBool aImmediate);
  69. static NS_COM_GLUE void* Clone(const void* ptr, PRSize size);
  70. static NS_COM_GLUE nsIMemory* GetGlobalMemoryService(); // AddRefs
  71. };
  72. /**
  73. * Macro to free all elements of an XPCOM array of a given size using
  74. * freeFunc, then frees the array itself using nsMemory::Free().
  75. *
  76. * Note that this macro (and its wrappers) can be used to deallocate a
  77. * partially- or completely-built array while unwinding an error
  78. * condition inside the XPCOM routine that was going to return the
  79. * array. For this to work on a partially-built array, your code
  80. * needs to be building the array from index 0 upwards, and simply
  81. * pass the number of elements that have already been built (and thus
  82. * need to be freed) as |size|.
  83. *
  84. * Thanks to <[email protected]> for suggesting this form, which
  85. * allows the macro to be used with NS_RELEASE / NS_RELEASE_IF in
  86. * addition to nsMemory::Free.
  87. *
  88. * @param size Number of elements in the array. If not a constant, this
  89. * should be a PRInt32. Note that this means this macro
  90. * will not work if size >= 2^31.
  91. * @param array The array to be freed.
  92. * @param freeFunc The function or macro to be used to free it.
  93. * For arrays of nsISupports (or any class derived
  94. * from it), NS_IF_RELEASE (or NS_RELEASE) should be
  95. * passed as freeFunc. For most (all?) other pointer
  96. * types (including XPCOM strings and wstrings),
  97. * nsMemory::Free should be used, since the
  98. * shared-allocator (nsMemory) is what will have been
  99. * used to allocate the memory.
  100. */
  101. #define NS_FREE_XPCOM_POINTER_ARRAY(size, array, freeFunc) \
  102. PR_BEGIN_MACRO \
  103. PRInt32 iter_ = PRInt32(size); \
  104. while (--iter_ >= 0) \
  105. freeFunc((array)[iter_]); \
  106. NS_Free((array)); \
  107. PR_END_MACRO
  108. // convenience macros for commonly used calls. mmmmm. syntactic sugar.
  109. /**
  110. * Macro to free arrays of non-refcounted objects allocated by the
  111. * shared allocator (nsMemory) such as strings and wstrings. A
  112. * convenience wrapper around NS_FREE_XPCOM_POINTER_ARRAY.
  113. *
  114. * @param size Number of elements in the array. If not a constant, this
  115. * should be a PRInt32. Note that this means this macro
  116. * will not work if size >= 2^31.
  117. * @param array The array to be freed.
  118. */
  119. #define NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(size, array) \
  120. NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_Free)
  121. /**
  122. * Macro to free an array of pointers to nsISupports (or classes
  123. * derived from it). A convenience wrapper around
  124. * NS_FREE_XPCOM_POINTER_ARRAY.
  125. *
  126. * Note that if you know that none of your nsISupports pointers are
  127. * going to be 0, you can gain a bit of speed by calling
  128. * NS_FREE_XPCOM_POINTER_ARRAY directly and using NS_RELEASE as your
  129. * free function.
  130. *
  131. * @param size Number of elements in the array. If not a constant, this
  132. * should be a PRInt32. Note that this means this macro
  133. * will not work if size >= 2^31.
  134. * @param array The array to be freed.
  135. */
  136. #define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array) \
  137. NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE)
  138. /**
  139. * Helpful array length function for calculating the length of a
  140. * statically declared array.
  141. */
  142. #define NS_ARRAY_LENGTH(array_) \
  143. (sizeof(array_)/sizeof(array_[0]))
  144. #endif // nsMemory_h__