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