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