VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/glue/nsMemory.h

最後變更 在這個檔案是 101852,由 vboxsync 提交於 13 月 前

libs/xpcom: Get rid of nsMemoryImpl.{cpp,h} and nsIMemory and repalce it with direct calls to RTMem* APIs, bugref:10545

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.0 KB
 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#ifndef nsMemory_h__
39#define nsMemory_h__
40
41#include "nsISupports.h"
42
43
44/**
45 * Static helper routines to manage memory. These routines allow easy access
46 * to xpcom's built-in (global) nsIMemory implementation, without needing
47 * to go through the service manager to get it. However this requires clients
48 * to link with the xpcom DLL.
49 *
50 * This class is not threadsafe and is intented for use only on the main
51 * thread.
52 */
53class nsMemory
54{
55public:
56 static NS_COM void* Alloc(size_t size);
57 static NS_COM void* Realloc(void* ptr, size_t size);
58 static NS_COM void Free(void* ptr);
59 static NS_COM void* Clone(const void* ptr, size_t size);
60};
61
62/**
63 * Macro to free all elements of an XPCOM array of a given size using
64 * freeFunc, then frees the array itself using nsMemory::Free().
65 *
66 * Note that this macro (and its wrappers) can be used to deallocate a
67 * partially- or completely-built array while unwinding an error
68 * condition inside the XPCOM routine that was going to return the
69 * array. For this to work on a partially-built array, your code
70 * needs to be building the array from index 0 upwards, and simply
71 * pass the number of elements that have already been built (and thus
72 * need to be freed) as |size|.
73 *
74 * Thanks to <[email protected]> for suggesting this form, which
75 * allows the macro to be used with NS_RELEASE / NS_RELEASE_IF in
76 * addition to nsMemory::Free.
77 *
78 * @param size Number of elements in the array. If not a constant, this
79 * should be a PRInt32. Note that this means this macro
80 * will not work if size >= 2^31.
81 * @param array The array to be freed.
82 * @param freeFunc The function or macro to be used to free it.
83 * For arrays of nsISupports (or any class derived
84 * from it), NS_IF_RELEASE (or NS_RELEASE) should be
85 * passed as freeFunc. For most (all?) other pointer
86 * types (including XPCOM strings and wstrings),
87 * nsMemory::Free should be used, since the
88 * shared-allocator (nsMemory) is what will have been
89 * used to allocate the memory.
90 */
91#define NS_FREE_XPCOM_POINTER_ARRAY(size, array, freeFunc) \
92 PR_BEGIN_MACRO \
93 PRInt32 iter_ = PRInt32(size); \
94 while (--iter_ >= 0) \
95 freeFunc((array)[iter_]); \
96 nsMemory::Free((array)); \
97 PR_END_MACRO
98
99// convenience macros for commonly used calls. mmmmm. syntactic sugar.
100
101/**
102 * Macro to free arrays of non-refcounted objects allocated by the
103 * shared allocator (nsMemory) such as strings and wstrings. A
104 * convenience wrapper around NS_FREE_XPCOM_POINTER_ARRAY.
105 *
106 * @param size Number of elements in the array. If not a constant, this
107 * should be a PRInt32. Note that this means this macro
108 * will not work if size >= 2^31.
109 * @param array The array to be freed.
110 */
111#define NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(size, array) \
112 NS_FREE_XPCOM_POINTER_ARRAY((size), (array), nsMemory::Free)
113
114/**
115 * Macro to free an array of pointers to nsISupports (or classes
116 * derived from it). A convenience wrapper around
117 * NS_FREE_XPCOM_POINTER_ARRAY.
118 *
119 * Note that if you know that none of your nsISupports pointers are
120 * going to be 0, you can gain a bit of speed by calling
121 * NS_FREE_XPCOM_POINTER_ARRAY directly and using NS_RELEASE as your
122 * free function.
123 *
124 * @param size Number of elements in the array. If not a constant, this
125 * should be a PRInt32. Note that this means this macro
126 * will not work if size >= 2^31.
127 * @param array The array to be freed.
128 */
129#define NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(size, array) \
130 NS_FREE_XPCOM_POINTER_ARRAY((size), (array), NS_IF_RELEASE)
131
132/**
133 * Helpful array length function for calculating the length of a
134 * statically declared array.
135 */
136
137#define NS_ARRAY_LENGTH(array_) \
138 (sizeof(array_)/sizeof(array_[0]))
139
140
141#endif // nsMemory_h__
142
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette