xpcom/glue/nsMemory.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial