Mozdev
mozilla/verbosio/datatypes/nsMemoryArray.h
author Alex Vincent@SKYFIREDEMO1
Thu Sep 18 19:33:09 2008 -0700 (16 months ago)
changeset 32 0fef113e1411
permissions -rw-r--r--
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__
     3 
     4 /* ***** BEGIN LICENSE BLOCK *****
     5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
     6  *
     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/
    11  *
    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
    15  * License.
    16  *
    17  * The Original Code is nsMemoryRefArray.
    18  *
    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.
    23  *
    24  * Contributor(s):
    25  *
    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.
    37  *
    38  * ***** END LICENSE BLOCK ***** */
    39 
    40 #include "nsMemory.h"
    41 #include "nscore.h"
    42 
    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.
    47 */
    48 
    49 template<class T>
    50 class nsMemoryArray
    51 {
    52   public:
    53     /**
    54      * Build the memory array.
    55      *
    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.
    59      */
    60     nsMemoryArray<T>(PRUint32 count, nsresult &rv) {
    61       mCount = count;
    62       mSuccess = PR_FALSE;
    63       memPtr = (T*) nsMemory::Alloc(count * sizeof(T));
    64       rv = (memPtr) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
    65       if (memPtr)
    66         for (PRUint32 i = 0; i < count; i++)
    67           memPtr[i] = nsnull;
    68     };
    69 
    70     ~nsMemoryArray<T>() {
    71       if (memPtr && !mSuccess)
    72         nsMemory::Free(memPtr);
    73     };
    74 
    75     /**
    76      * Set an item at a given index.  You should call this exactly once per item.
    77      */
    78     void setIndex(PRUint32 index, T elem)
    79     {
    80       memPtr[index] = elem;
    81     };
    82 
    83     void Finalize(PRUint32* count, T** retval)
    84     {
    85       *count = mCount;
    86       *retval = memPtr;
    87       mSuccess = PR_TRUE;
    88     };
    89 
    90   private:
    91     PRBool mSuccess;
    92     PRUint32 mCount;
    93     T* memPtr;
    94 };
    95 
    96 // Reference-counting version of nsMemoryArray.
    97 template<class T>
    98 class nsMemoryRefArray
    99 {
   100   public:
   101     /**
   102      * Build the memory array.
   103      *
   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.
   107      */
   108     nsMemoryRefArray<T>(PRUint32 count, nsresult &rv) {
   109       mCount = count;
   110       mSuccess = PR_FALSE;
   111       memPtr = (T*) nsMemory::Alloc(count * sizeof(T));
   112       rv = (memPtr) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
   113       if (memPtr)
   114         for (PRUint32 i = 0; i < count; i++)
   115           memPtr[i] = nsnull;
   116     };
   117 
   118     ~nsMemoryRefArray<T>() {
   119       if (memPtr && !mSuccess)
   120       {
   121         for (PRUint32 i = 0; i < mCount; i++)
   122         {
   123           T elem = memPtr[i];
   124           NS_IF_RELEASE(elem);
   125         }
   126         nsMemory::Free(memPtr);
   127       }
   128     };
   129 
   130     /**
   131      * Set an item at a given index.  You should call this exactly once per item.
   132      */
   133     void setIndex(PRUint32 index, T elem)
   134     {
   135       NS_IF_RELEASE(memPtr[index]);
   136       memPtr[index] = elem;
   137       NS_IF_ADDREF(elem);
   138     };
   139 
   140     void Finalize(PRUint32* count, T** retval)
   141     {
   142       *count = mCount;
   143       *retval = memPtr;
   144       mSuccess = PR_TRUE;
   145     };
   146 
   147   private:
   148     PRBool mSuccess;
   149     PRUint32 mCount;
   150     T* memPtr;
   151 };
   152 
   153 #endif // __nsMemoryArray_h__