sbMemoryUtils.h
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 #ifndef __SBMEMORYUTILS_H__
26 #define __SBMEMORYUTILS_H__
27 
28 #include <nsMemory.h>
29 #include <stdlib.h>
30 
31 template<class T>
33 {
34 public:
35  sbAutoFreeXPCOMPointerArray(PRUint32 aCount, T** aArray) :
36  mCount(aCount),
37  mArray(aArray)
38  {}
39 
41  {
42  NS_FREE_XPCOM_ISUPPORTS_POINTER_ARRAY(mCount, mArray);
43  }
44 
45 private:
46  PRUint32 mCount;
47  T** mArray;
48 };
49 
54 template<class T>
56 {
57 public:
58  sbAutoFreeXPCOMArrayByRef(PRUint32 & aCount, T & aArray)
59  : mCount(aCount),
60  mArray(aArray)
61  {
62  if (aCount) {
63  NS_ASSERTION(aArray, "Null pointer!");
64  }
65  }
66 
68  {
69  NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mCount, mArray);
70  }
71 
72 private:
73  PRUint32 & mCount;
74  T & mArray;
75 };
76 
77 template <class T>
79 {
80 public:
81  sbAutoFreeXPCOMArray(PRUint32 aCount, T aArray)
82  : mCount(aCount),
83  mArray(aArray)
84  {
85  if (aCount) {
86  NS_ASSERTION(aArray, "Null pointer!");
87  }
88  }
89 
91  {
92  NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(mCount, mArray);
93  }
94 
95 private:
96  PRUint32 mCount;
97  T mArray;
98 };
99 
100 
135 #define SB_AUTO_CLASS2(aName, aType, aType2, aIsValid, aDispose, aInvalidate) \
136 class aName \
137 { \
138 public: \
139  aName() { Invalidate(); } \
140  \
141  aName(aType aValue) : mValue(aValue) {} \
142  \
143  aName(aType aValue, aType2 aValue2) : mValue(aValue), mValue2(aValue2) {} \
144  \
145  virtual ~aName() \
146  { \
147  if (aIsValid) { \
148  aDispose; \
149  } \
150  } \
151  \
152  void Set(aType aValue) { mValue = aValue; } \
153  \
154  void Set(aType aValue, aType2 aValue2) \
155  { mValue = aValue; mValue2 = aValue2; } \
156  \
157  aType forget() \
158  { \
159  aType value = mValue; \
160  Invalidate(); \
161  return value; \
162  } \
163  \
164  aType get() const \
165  { \
166  return mValue; \
167  } \
168  \
169  operator aType() const \
170  { \
171  return get(); \
172  } \
173  \
174  aType& operator=(aType aValue) \
175  { \
176  Clear(); \
177  Set(aValue); \
178  return mValue; \
179  } \
180  \
181  aType operator->() const \
182  { \
183  NS_PRECONDITION \
184  (aIsValid, \
185  "Cannot dereference an invalid auto class with operator->()."); \
186  return get(); \
187  } \
188  \
189  operator bool() \
190  { \
191  if (!(aIsValid)) \
192  return PR_FALSE; \
193  return PR_TRUE; \
194  } \
195  \
196  void Clear() \
197  { \
198  if (aIsValid) { \
199  aDispose; \
200  aInvalidate; \
201  } \
202  } \
203  \
204  aType* StartAssignment() \
205  { \
206  Clear(); \
207  return reinterpret_cast<aType*>(&mValue); \
208  } \
209  \
210 private: \
211  aType mValue; \
212  aType2 mValue2; \
213  \
214  void Invalidate() { aInvalidate; } \
215  void operator=(const aName aValue) {} \
216 }
217 
218 #define SB_AUTO_CLASS(aName, aType, aIsValid, aDispose, aInvalidate) \
219  SB_AUTO_CLASS2(aName, aType, char, aIsValid, aDispose, aInvalidate)
220 
221 
231 #define SB_AUTO_NULL_CLASS(aName, aType, aDispose) \
232  SB_AUTO_CLASS(aName, aType, mValue != NULL, aDispose, mValue = NULL)
233 
234 
235 //
236 // Auto-disposal class wrappers.
237 //
238 // sbAutoNSMemPtr Wrapper to auto-dispose memory blocks allocated
239 // with NS_Alloc.
240 // sbAutoMemPtr Typed wrapper to auto-dispose memory blocks
241 // allocated with malloc.
242 // sbAutoNSTypePtr Typed version of sbAutoNSMemPtr
243 // sbAutoNSArray Wrapper to auto-dispose a simple array and all
244 // of its members allocated with NS_Alloc.
245 // Example: sbAutoNSArray<char*>
246 // autoArray(array, arrayLength);
247 //
248 
250  void*,
251  !!mValue,
252  NS_Free(mValue),
253  mValue = nsnull);
254 
255 template<typename T>
256 SB_AUTO_NULL_CLASS(sbAutoMemPtr, T*, free(mValue));
257 
258 template<typename T>
259 SB_AUTO_CLASS(sbAutoNSTypePtr, T*, !!mValue, NS_Free(mValue), mValue = nsnull);
260 
261 template<typename T>
263  T*,
264  PRUint32,
265  !!mValue,
266  {
267  for (PRUint32 i = 0; i < mValue2; ++i) {
268  if (mValue[i])
269  NS_Free(mValue[i]);
270  }
271  NS_Free(mValue);
272  },
273  mValue = nsnull);
274 
275 
276 template <class COMPtr, class ReturnType>
277 inline
278 nsresult sbReturnCOMPtr(COMPtr & aPtr, ReturnType ** aReturn)
279 {
280  if (!aReturn) {
281  return NS_ERROR_INVALID_POINTER;
282  }
283 
284  *aReturn = aPtr.get();
285  NS_IF_ADDREF(*aReturn);
286 
287  return NS_OK;
288 }
289 
293 inline void *
294 SB_CloneMemory(const void* ptr, PRSize size) {
295  void* newPtr = NS_Alloc(size);
296  if (newPtr)
297  memcpy(newPtr, ptr, size);
298  return newPtr;
299 }
300 
301 #endif /* __SBMEMORYUTILS_H__ */
302 
sbAutoFreeXPCOMPointerArray(PRUint32 aCount, T **aArray)
Definition: sbMemoryUtils.h:35
void * SB_CloneMemory(const void *ptr, PRSize size)
Clone a block of contiguous memory.
return NS_OK
#define SB_AUTO_NULL_CLASS(aName, aType, aDispose)
nsresult sbReturnCOMPtr(COMPtr &aPtr, ReturnType **aReturn)
sbAutoFreeXPCOMArrayByRef(PRUint32 &aCount, T &aArray)
Definition: sbMemoryUtils.h:58
sbAutoFreeXPCOMArray(PRUint32 aCount, T aArray)
Definition: sbMemoryUtils.h:81
#define SB_AUTO_CLASS(aName, aType, aIsValid, aDispose, aInvalidate)
#define SB_AUTO_CLASS2(aName, aType, aType2, aIsValid, aDispose, aInvalidate)
restoreHistoryPrecursor aCount
_getSelectedPageStyle s i