Add refcount assertion, writable data matrix, read-only data matrix components, with tests, and new memory allocators for XPCOM native arrays.
1 #ifndef __nsMemoryArray_h__
2 #define __nsMemoryArray_h__
4 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is nsMemoryRefArray.
19 * The Initial Developer of the Original Code is
20 * Alexander J. Vincent <ajvincent@gmail.com>.
21 * Portions created by the Initial Developer are Copyright (C) 2008
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
43 /* The nsMemoryArray template is for building a native XPCOM array for returning
44 to a caller, and managing the memory needed. Should the function defining
45 a nsMemoryArray object return without calling Finalize(), the memory we
46 allocated will automatically be freed.
54 * Build the memory array.
56 * @param T The entry type (PRUint32, nsISupports*, ...)
57 * @param count The number of entries we will store in the array.
58 * @param rv The return value from this function.
60 nsMemoryArray<T>(PRUint32 count, nsresult &rv) {
63 memPtr = (T*) nsMemory::Alloc(count * sizeof(T));
64 rv = (memPtr) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
66 for (PRUint32 i = 0; i < count; i++)
71 if (memPtr && !mSuccess)
72 nsMemory::Free(memPtr);
76 * Set an item at a given index. You should call this exactly once per item.
78 void setIndex(PRUint32 index, T elem)
83 void Finalize(PRUint32* count, T** retval)
96 // Reference-counting version of nsMemoryArray.
98 class nsMemoryRefArray
102 * Build the memory array.
104 * @param T The entry type (PRUint32, nsISupports*, ...)
105 * @param count The number of entries we will store in the array.
106 * @param rv The return value from this function.
108 nsMemoryRefArray<T>(PRUint32 count, nsresult &rv) {
111 memPtr = (T*) nsMemory::Alloc(count * sizeof(T));
112 rv = (memPtr) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
114 for (PRUint32 i = 0; i < count; i++)
118 ~nsMemoryRefArray<T>() {
119 if (memPtr && !mSuccess)
121 for (PRUint32 i = 0; i < mCount; i++)
126 nsMemory::Free(memPtr);
131 * Set an item at a given index. You should call this exactly once per item.
133 void setIndex(PRUint32 index, T elem)
135 NS_IF_RELEASE(memPtr[index]);
136 memPtr[index] = elem;
140 void Finalize(PRUint32* count, T** retval)
153 #endif // __nsMemoryArray_h__