1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsMemory.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,145 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsMemory_h__ 1.10 +#define nsMemory_h__ 1.11 + 1.12 +#include "nsXPCOM.h" 1.13 + 1.14 +class nsIMemory; 1.15 + 1.16 +#define NS_MEMORY_CONTRACTID "@mozilla.org/xpcom/memory-service;1" 1.17 +#define NS_MEMORY_CID \ 1.18 +{ /* 30a04e40-38e7-11d4-8cf5-0060b0fc14a3 */ \ 1.19 + 0x30a04e40, \ 1.20 + 0x38e7, \ 1.21 + 0x11d4, \ 1.22 + {0x8c, 0xf5, 0x00, 0x60, 0xb0, 0xfc, 0x14, 0xa3} \ 1.23 +} 1.24 + 1.25 + 1.26 +/** 1.27 + * Static helper routines to manage memory. These routines allow easy access 1.28 + * to xpcom's built-in (global) nsIMemory implementation, without needing 1.29 + * to go through the service manager to get it. However this requires clients 1.30 + * to link with the xpcom DLL. 1.31 + * 1.32 + * This class is not threadsafe and is intented for use only on the main 1.33 + * thread. 1.34 + */ 1.35 +class nsMemory 1.36 +{ 1.37 +public: 1.38 + static NS_HIDDEN_(void*) Alloc(size_t size) 1.39 + { return NS_Alloc(size); } 1.40 + 1.41 + static NS_HIDDEN_(void*) Realloc(void* ptr, size_t size) 1.42 + { return NS_Realloc(ptr, size); } 1.43 + 1.44 + static NS_HIDDEN_(void) Free(void* ptr) 1.45 + { NS_Free(ptr); } 1.46 + 1.47 + static NS_COM_GLUE nsresult HeapMinimize(bool aImmediate); 1.48 + static NS_COM_GLUE void* Clone(const void* ptr, size_t size); 1.49 + static NS_COM_GLUE nsIMemory* GetGlobalMemoryService(); // AddRefs 1.50 +}; 1.51 + 1.52 +/** 1.53 + * Macro to free all elements of an XPCOM array of a given size using 1.54 + * freeFunc, then frees the array itself using nsMemory::Free(). 1.55 + * 1.56 + * Note that this macro (and its wrappers) can be used to deallocate a 1.57 + * partially- or completely-built array while unwinding an error 1.58 + * condition inside the XPCOM routine that was going to return the 1.59 + * array. For this to work on a partially-built array, your code 1.60 + * needs to be building the array from index 0 upwards, and simply 1.61 + * pass the number of elements that have already been built (and thus 1.62 + * need to be freed) as |size|. 1.63 + * 1.64 + * Thanks to <alecf@netscape.com> for suggesting this form, which 1.65 + * allows the macro to be used with NS_RELEASE / NS_RELEASE_IF in 1.66 + * addition to nsMemory::Free. 1.67 + * 1.68 + * @param size Number of elements in the array. If not a constant, this 1.69 + * should be a int32_t. Note that this means this macro 1.70 + * will not work if size >= 2^31. 1.71 + * @param array The array to be freed. 1.72 + * @param freeFunc The function or macro to be used to free it. 1.73 + * For arrays of nsISupports (or any class derived 1.74 + * from it), NS_IF_RELEASE (or NS_RELEASE) should be 1.75 + * passed as freeFunc. For most (all?) other pointer 1.76 + * types (including XPCOM strings and wstrings), 1.77 + * nsMemory::Free should be used, since the 1.78 + * shared-allocator (nsMemory) is what will have been 1.79 + * used to allocate the memory. 1.80 + */ 1.81 +#define NS_FREE_XPCOM_POINTER_ARRAY(size, array, freeFunc) \ 1.82 + PR_BEGIN_MACRO \ 1.83 + int32_t iter_ = int32_t(size); \ 1.84 + while (--iter_ >= 0) \ 1.85 + freeFunc((array)[iter_]); \ 1.86 + NS_Free((array)); \ 1.87 + PR_END_MACRO 1.88 + 1.89 +// convenience macros for commonly used calls. mmmmm. syntactic sugar. 1.90 + 1.91 +/** 1.92 + * Macro to free arrays of non-refcounted objects allocated by the 1.93 + * shared allocator (nsMemory) such as strings and wstrings. A 1.94 + * convenience wrapper around NS_FREE_XPCOM_POINTER_ARRAY. 1.95 + * 1.96 + * @param size Number of elements in the array. If not a constant, this 1.97 + * should be a int32_t. Note that this means this macro 1.98 + * will not work if size >= 2^31. 1.99 + * @param array The array to be freed. 1.100 + */ 1.101 +#define NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(size, array) \ 1.102 + NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_Free) 1.103 + 1.104 +/** 1.105 + * Macro to free an array of pointers to nsISupports (or classes 1.106 + * derived from it). A convenience wrapper around 1.107 + * NS_FREE_XPCOM_POINTER_ARRAY. 1.108 + * 1.109 + * Note that if you know that none of your nsISupports pointers are 1.110 + * going to be 0, you can gain a bit of speed by calling 1.111 + * NS_FREE_XPCOM_POINTER_ARRAY directly and using NS_RELEASE as your 1.112 + * free function. 1.113 + * 1.114 + * @param size Number of elements in the array. If not a constant, this 1.115 + * should be a int32_t. Note that this means this macro 1.116 + * will not work if size >= 2^31. 1.117 + * @param array The array to be freed. 1.118 + */ 1.119 +#define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array) \ 1.120 + NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE) 1.121 + 1.122 +/** 1.123 + * A macro, NS_ALIGNMENT_OF(t_) that determines the alignment 1.124 + * requirements of a type. 1.125 + */ 1.126 +namespace mozilla { 1.127 + template <class T> 1.128 + struct AlignmentTestStruct 1.129 + { 1.130 + char c; 1.131 + T t; 1.132 + }; 1.133 +} 1.134 + 1.135 +#define NS_ALIGNMENT_OF(t_) \ 1.136 + (sizeof(mozilla::AlignmentTestStruct<t_>) - sizeof(t_)) 1.137 + 1.138 +/** 1.139 + * An enumeration type used to represent a method of assignment. 1.140 + */ 1.141 +enum nsAssignmentType { 1.142 + NS_ASSIGNMENT_COPY, // copy by value 1.143 + NS_ASSIGNMENT_DEPEND, // copy by reference 1.144 + NS_ASSIGNMENT_ADOPT // copy by reference (take ownership of resource) 1.145 +}; 1.146 + 1.147 +#endif // nsMemory_h__ 1.148 +