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/. */
15
#define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
16
#define NS_MEMORY_CID \
17
{ /* 30a04e40-38e7-11d4-8cf5-0060b0fc14a3 */ \
21
{0x8c, 0xf5, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \
26
* Static helper routines to manage memory. These routines allow easy access
27
* to xpcom's built-in (global) nsIMemory implementation, without needing
28
* to go through the service manager to get it. However this requires clients
29
* to link with the xpcom DLL.
31
* This class is not threadsafe and is intented for use only on the main
38
static NS_HIDDEN_(void*) Alloc(size_t size)
39
{ return NS_Alloc(size); }
41
static NS_HIDDEN_(void*) Realloc(void* ptr, size_t size)
42
{ return NS_Realloc(ptr, size); }
44
static NS_HIDDEN_(void) Free(void* ptr)
47
static NS_COM_GLUE nsresult HeapMinimize(bool aImmediate);
48
static NS_COM_GLUE void* Clone(const void* ptr, size_t size);
49
static NS_COM_GLUE nsIMemory* GetGlobalMemoryService(); // AddRefs
54
* Macro to free all elements of an XPCOM array of a given size using
55
* freeFunc, then frees the array itself using nsMemory::Free().
57
* Note that this macro (and its wrappers) can be used to deallocate a
58
* partially- or completely-built array while unwinding an error
59
* condition inside the XPCOM routine that was going to return the
60
* array. For this to work on a partially-built array, your code
61
* needs to be building the array from index 0 upwards, and simply
62
* pass the number of elements that have already been built (and thus
63
* need to be freed) as |size|.
65
* Thanks to <alecf@netscape.com> for suggesting this form, which
66
* allows the macro to be used with NS_RELEASE / NS_RELEASE_IF in
67
* addition to nsMemory::Free.
69
* @param size Number of elements in the array. If not a constant, this
70
* should be a int32_t. Note that this means this macro
71
* will not work if size >= 2^31.
72
* @param array The array to be freed.
73
* @param freeFunc The function or macro to be used to free it.
74
* For arrays of nsISupports (or any class derived
75
* from it), NS_IF_RELEASE (or NS_RELEASE) should be
76
* passed as freeFunc. For most (all?) other pointer
77
* types (including XPCOM strings and wstrings),
78
* nsMemory::Free should be used, since the
79
* shared-allocator (nsMemory) is what will have been
80
* used to allocate the memory.
82
#define NS_FREE_XPCOM_POINTER_ARRAY(size, array, freeFunc) \
84
int32_t iter_ = int32_t(size); \
85
while (--iter_ >= 0) \
86
freeFunc((array)[iter_]); \
90
// convenience macros for commonly used calls. mmmmm. syntactic sugar.
93
* Macro to free arrays of non-refcounted objects allocated by the
94
* shared allocator (nsMemory) such as strings and wstrings. A
95
* convenience wrapper around NS_FREE_XPCOM_POINTER_ARRAY.
97
* @param size Number of elements in the array. If not a constant, this
98
* should be a int32_t. Note that this means this macro
99
* will not work if size >= 2^31.
100
* @param array The array to be freed.
102
#define NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(size, array) \
103
NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_Free)
106
* Macro to free an array of pointers to nsISupports (or classes
107
* derived from it). A convenience wrapper around
108
* NS_FREE_XPCOM_POINTER_ARRAY.
110
* Note that if you know that none of your nsISupports pointers are
111
* going to be 0, you can gain a bit of speed by calling
112
* NS_FREE_XPCOM_POINTER_ARRAY directly and using NS_RELEASE as your
115
* @param size Number of elements in the array. If not a constant, this
116
* should be a int32_t. Note that this means this macro
117
* will not work if size >= 2^31.
118
* @param array The array to be freed.
120
#define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array) \
121
NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE)
124
* Helpful array length function for calculating the length of a
125
* statically declared array.
128
#define NS_ARRAY_LENGTH(array_) \
129
(sizeof(array_)/sizeof(array_[0]))
132
* A macro, NS_ALIGNMENT_OF(t_) that determines the alignment
133
* requirements of a type.
137
struct AlignmentTestStruct
144
#define NS_ALIGNMENT_OF(t_) \
145
(sizeof(mozilla::AlignmentTestStruct<t_>) - sizeof(t_))
148
* An enumeration type used to represent a method of assignment.
150
enum nsAssignmentType {
151
NS_ASSIGNMENT_COPY, // copy by value
152
NS_ASSIGNMENT_DEPEND, // copy by reference
153
NS_ASSIGNMENT_ADOPT // copy by reference (take ownership of resource)
156
#endif // nsMemory_h__