xpcom/glue/nsMemory.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:6cd7f7604fab
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #ifndef nsMemory_h__
7 #define nsMemory_h__
8
9 #include "nsXPCOM.h"
10
11 class nsIMemory;
12
13 #define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
14 #define NS_MEMORY_CID \
15 { /* 30a04e40-38e7-11d4-8cf5-0060b0fc14a3 */ \
16 0x30a04e40, \
17 0x38e7, \
18 0x11d4, \
19 {0x8c, 0xf5, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \
20 }
21
22
23 /**
24 * Static helper routines to manage memory. These routines allow easy access
25 * to xpcom's built-in (global) nsIMemory implementation, without needing
26 * to go through the service manager to get it. However this requires clients
27 * to link with the xpcom DLL.
28 *
29 * This class is not threadsafe and is intented for use only on the main
30 * thread.
31 */
32 class nsMemory
33 {
34 public:
35 static NS_HIDDEN_(void*) Alloc(size_t size)
36 { return NS_Alloc(size); }
37
38 static NS_HIDDEN_(void*) Realloc(void* ptr, size_t size)
39 { return NS_Realloc(ptr, size); }
40
41 static NS_HIDDEN_(void) Free(void* ptr)
42 { NS_Free(ptr); }
43
44 static NS_COM_GLUE nsresult HeapMinimize(bool aImmediate);
45 static NS_COM_GLUE void* Clone(const void* ptr, size_t size);
46 static NS_COM_GLUE nsIMemory* GetGlobalMemoryService(); // AddRefs
47 };
48
49 /**
50 * Macro to free all elements of an XPCOM array of a given size using
51 * freeFunc, then frees the array itself using nsMemory::Free().
52 *
53 * Note that this macro (and its wrappers) can be used to deallocate a
54 * partially- or completely-built array while unwinding an error
55 * condition inside the XPCOM routine that was going to return the
56 * array. For this to work on a partially-built array, your code
57 * needs to be building the array from index 0 upwards, and simply
58 * pass the number of elements that have already been built (and thus
59 * need to be freed) as |size|.
60 *
61 * Thanks to <alecf@netscape.com> for suggesting this form, which
62 * allows the macro to be used with NS_RELEASE / NS_RELEASE_IF in
63 * addition to nsMemory::Free.
64 *
65 * @param size Number of elements in the array. If not a constant, this
66 * should be a int32_t. Note that this means this macro
67 * will not work if size >= 2^31.
68 * @param array The array to be freed.
69 * @param freeFunc The function or macro to be used to free it.
70 * For arrays of nsISupports (or any class derived
71 * from it), NS_IF_RELEASE (or NS_RELEASE) should be
72 * passed as freeFunc. For most (all?) other pointer
73 * types (including XPCOM strings and wstrings),
74 * nsMemory::Free should be used, since the
75 * shared-allocator (nsMemory) is what will have been
76 * used to allocate the memory.
77 */
78 #define NS_FREE_XPCOM_POINTER_ARRAY(size, array, freeFunc) \
79 PR_BEGIN_MACRO \
80 int32_t iter_ = int32_t(size); \
81 while (--iter_ >= 0) \
82 freeFunc((array)[iter_]); \
83 NS_Free((array)); \
84 PR_END_MACRO
85
86 // convenience macros for commonly used calls. mmmmm. syntactic sugar.
87
88 /**
89 * Macro to free arrays of non-refcounted objects allocated by the
90 * shared allocator (nsMemory) such as strings and wstrings. A
91 * convenience wrapper around NS_FREE_XPCOM_POINTER_ARRAY.
92 *
93 * @param size Number of elements in the array. If not a constant, this
94 * should be a int32_t. Note that this means this macro
95 * will not work if size >= 2^31.
96 * @param array The array to be freed.
97 */
98 #define NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(size, array) \
99 NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_Free)
100
101 /**
102 * Macro to free an array of pointers to nsISupports (or classes
103 * derived from it). A convenience wrapper around
104 * NS_FREE_XPCOM_POINTER_ARRAY.
105 *
106 * Note that if you know that none of your nsISupports pointers are
107 * going to be 0, you can gain a bit of speed by calling
108 * NS_FREE_XPCOM_POINTER_ARRAY directly and using NS_RELEASE as your
109 * free function.
110 *
111 * @param size Number of elements in the array. If not a constant, this
112 * should be a int32_t. Note that this means this macro
113 * will not work if size >= 2^31.
114 * @param array The array to be freed.
115 */
116 #define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array) \
117 NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE)
118
119 /**
120 * A macro, NS_ALIGNMENT_OF(t_) that determines the alignment
121 * requirements of a type.
122 */
123 namespace mozilla {
124 template <class T>
125 struct AlignmentTestStruct
126 {
127 char c;
128 T t;
129 };
130 }
131
132 #define NS_ALIGNMENT_OF(t_) \
133 (sizeof(mozilla::AlignmentTestStruct<t_>) - sizeof(t_))
134
135 /**
136 * An enumeration type used to represent a method of assignment.
137 */
138 enum nsAssignmentType {
139 NS_ASSIGNMENT_COPY, // copy by value
140 NS_ASSIGNMENT_DEPEND, // copy by reference
141 NS_ASSIGNMENT_ADOPT // copy by reference (take ownership of resource)
142 };
143
144 #endif // nsMemory_h__
145

mercurial