michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsMemory_h__ michael@0: #define nsMemory_h__ michael@0: michael@0: #include "nsXPCOM.h" michael@0: michael@0: class nsIMemory; michael@0: michael@0: #define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1" michael@0: #define NS_MEMORY_CID \ michael@0: { /* 30a04e40-38e7-11d4-8cf5-0060b0fc14a3 */ \ michael@0: 0x30a04e40, \ michael@0: 0x38e7, \ michael@0: 0x11d4, \ michael@0: {0x8c, 0xf5, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \ michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Static helper routines to manage memory. These routines allow easy access michael@0: * to xpcom's built-in (global) nsIMemory implementation, without needing michael@0: * to go through the service manager to get it. However this requires clients michael@0: * to link with the xpcom DLL. michael@0: * michael@0: * This class is not threadsafe and is intented for use only on the main michael@0: * thread. michael@0: */ michael@0: class nsMemory michael@0: { michael@0: public: michael@0: static NS_HIDDEN_(void*) Alloc(size_t size) michael@0: { return NS_Alloc(size); } michael@0: michael@0: static NS_HIDDEN_(void*) Realloc(void* ptr, size_t size) michael@0: { return NS_Realloc(ptr, size); } michael@0: michael@0: static NS_HIDDEN_(void) Free(void* ptr) michael@0: { NS_Free(ptr); } michael@0: michael@0: static NS_COM_GLUE nsresult HeapMinimize(bool aImmediate); michael@0: static NS_COM_GLUE void* Clone(const void* ptr, size_t size); michael@0: static NS_COM_GLUE nsIMemory* GetGlobalMemoryService(); // AddRefs michael@0: }; michael@0: michael@0: /** michael@0: * Macro to free all elements of an XPCOM array of a given size using michael@0: * freeFunc, then frees the array itself using nsMemory::Free(). michael@0: * michael@0: * Note that this macro (and its wrappers) can be used to deallocate a michael@0: * partially- or completely-built array while unwinding an error michael@0: * condition inside the XPCOM routine that was going to return the michael@0: * array. For this to work on a partially-built array, your code michael@0: * needs to be building the array from index 0 upwards, and simply michael@0: * pass the number of elements that have already been built (and thus michael@0: * need to be freed) as |size|. michael@0: * michael@0: * Thanks to for suggesting this form, which michael@0: * allows the macro to be used with NS_RELEASE / NS_RELEASE_IF in michael@0: * addition to nsMemory::Free. michael@0: * michael@0: * @param size Number of elements in the array. If not a constant, this michael@0: * should be a int32_t. Note that this means this macro michael@0: * will not work if size >= 2^31. michael@0: * @param array The array to be freed. michael@0: * @param freeFunc The function or macro to be used to free it. michael@0: * For arrays of nsISupports (or any class derived michael@0: * from it), NS_IF_RELEASE (or NS_RELEASE) should be michael@0: * passed as freeFunc. For most (all?) other pointer michael@0: * types (including XPCOM strings and wstrings), michael@0: * nsMemory::Free should be used, since the michael@0: * shared-allocator (nsMemory) is what will have been michael@0: * used to allocate the memory. michael@0: */ michael@0: #define NS_FREE_XPCOM_POINTER_ARRAY(size, array, freeFunc) \ michael@0: PR_BEGIN_MACRO \ michael@0: int32_t iter_ = int32_t(size); \ michael@0: while (--iter_ >= 0) \ michael@0: freeFunc((array)[iter_]); \ michael@0: NS_Free((array)); \ michael@0: PR_END_MACRO michael@0: michael@0: // convenience macros for commonly used calls. mmmmm. syntactic sugar. michael@0: michael@0: /** michael@0: * Macro to free arrays of non-refcounted objects allocated by the michael@0: * shared allocator (nsMemory) such as strings and wstrings. A michael@0: * convenience wrapper around NS_FREE_XPCOM_POINTER_ARRAY. michael@0: * michael@0: * @param size Number of elements in the array. If not a constant, this michael@0: * should be a int32_t. Note that this means this macro michael@0: * will not work if size >= 2^31. michael@0: * @param array The array to be freed. michael@0: */ michael@0: #define NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(size, array) \ michael@0: NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_Free) michael@0: michael@0: /** michael@0: * Macro to free an array of pointers to nsISupports (or classes michael@0: * derived from it). A convenience wrapper around michael@0: * NS_FREE_XPCOM_POINTER_ARRAY. michael@0: * michael@0: * Note that if you know that none of your nsISupports pointers are michael@0: * going to be 0, you can gain a bit of speed by calling michael@0: * NS_FREE_XPCOM_POINTER_ARRAY directly and using NS_RELEASE as your michael@0: * free function. michael@0: * michael@0: * @param size Number of elements in the array. If not a constant, this michael@0: * should be a int32_t. Note that this means this macro michael@0: * will not work if size >= 2^31. michael@0: * @param array The array to be freed. michael@0: */ michael@0: #define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array) \ michael@0: NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE) michael@0: michael@0: /** michael@0: * A macro, NS_ALIGNMENT_OF(t_) that determines the alignment michael@0: * requirements of a type. michael@0: */ michael@0: namespace mozilla { michael@0: template michael@0: struct AlignmentTestStruct michael@0: { michael@0: char c; michael@0: T t; michael@0: }; michael@0: } michael@0: michael@0: #define NS_ALIGNMENT_OF(t_) \ michael@0: (sizeof(mozilla::AlignmentTestStruct) - sizeof(t_)) michael@0: michael@0: /** michael@0: * An enumeration type used to represent a method of assignment. michael@0: */ michael@0: enum nsAssignmentType { michael@0: NS_ASSIGNMENT_COPY, // copy by value michael@0: NS_ASSIGNMENT_DEPEND, // copy by reference michael@0: NS_ASSIGNMENT_ADOPT // copy by reference (take ownership of resource) michael@0: }; michael@0: michael@0: #endif // nsMemory_h__ michael@0: