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