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/. */
13
#define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1"
14
#define NS_MEMORY_CID \
15
{ /* 30a04e40-38e7-11d4-8cf5-0060b0fc14a3 */ \
19
{0x8c, 0xf5, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \
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.
29
* This class is not threadsafe and is intented for use only on the main
35
static NS_HIDDEN_(void*) Alloc(size_t size)
36
{ return NS_Alloc(size); }
38
static NS_HIDDEN_(void*) Realloc(void* ptr, size_t size)
39
{ return NS_Realloc(ptr, size); }
41
static NS_HIDDEN_(void) Free(void* ptr)
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
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().
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|.
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.
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.
78
#define NS_FREE_XPCOM_POINTER_ARRAY(size, array, freeFunc) \
80
int32_t iter_ = int32_t(size); \
81
while (--iter_ >= 0) \
82
freeFunc((array)[iter_]); \
86
// convenience macros for commonly used calls. mmmmm. syntactic sugar.
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.
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.
98
#define NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(size, array) \
99
NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_Free)
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.
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
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.
116
#define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array) \
117
NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE)
120
* Helpful array length function for calculating the length of a
121
* statically declared array.
124
#define NS_ARRAY_LENGTH(array_) \
125
(sizeof(array_)/sizeof(array_[0]))
128
* A macro, NS_ALIGNMENT_OF(t_) that determines the alignment
129
* requirements of a type.
133
struct AlignmentTestStruct
140
#define NS_ALIGNMENT_OF(t_) \
141
(sizeof(mozilla::AlignmentTestStruct<t_>) - sizeof(t_))
144
* An enumeration type used to represent a method of assignment.
146
enum nsAssignmentType {
147
NS_ASSIGNMENT_COPY, // copy by value
148
NS_ASSIGNMENT_DEPEND, // copy by reference
149
NS_ASSIGNMENT_ADOPT // copy by reference (take ownership of resource)
152
#endif // nsMemory_h__