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
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/
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
15 * The Original Code is mozilla.org code.
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.
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.
36 * ***** END LICENSE BLOCK ***** */
42 #include "nsIMemory.h"
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 */ \
51 {0x8c, 0xf5, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \
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.
61 * This class is not threadsafe and is intented for use only on the main
67 static NS_HIDDEN_(void*) Alloc(size_t size
)
68 { return NS_Alloc(size
); }
70 static NS_HIDDEN_(void*) Realloc(void* ptr
, PRSize size
)
71 { return NS_Realloc(ptr
, size
); }
73 static NS_HIDDEN_(void) Free(void* ptr
)
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
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().
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|.
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.
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.
110 #define NS_FREE_XPCOM_POINTER_ARRAY(size, array, freeFunc) \
112 PRInt32 iter_ = PRInt32(size); \
113 while (--iter_ >= 0) \
114 freeFunc((array)[iter_]); \
118 // convenience macros for commonly used calls. mmmmm. syntactic sugar.
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.
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.
130 #define NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(size, array) \
131 NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_Free)
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.
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
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.
148 #define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array) \
149 NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE)
152 * Helpful array length function for calculating the length of a
153 * statically declared array.
156 #define NS_ARRAY_LENGTH(array_) \
157 (sizeof(array_)/sizeof(array_[0]))
160 * An enumeration type used to represent a method of assignment.
162 enum nsAssignmentType
{
163 NS_ASSIGNMENT_COPY
, // copy by value
164 NS_ASSIGNMENT_DEPEND
, // copy by reference
165 NS_ASSIGNMENT_ADOPT
// copy by reference (take ownership of resource)
168 #endif // nsMemory_h__