1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
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 auto buffer template.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Conrad Carlen <ccarlen@mac.com>.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 2004
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * Jungshik Shin <jshin@mailaps.org>
|
---|
24 | *
|
---|
25 | * Alternatively, the contents of this file may be used under the terms of
|
---|
26 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
29 | * of those above. If you wish to allow use of your version of this file only
|
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
31 | * use your version of this file under the terms of the MPL, indicate your
|
---|
32 | * decision by deleting the provisions above and replace them with the notice
|
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
34 | * the provisions above, a recipient may use your version of this file under
|
---|
35 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
36 | *
|
---|
37 | * ***** END LICENSE BLOCK ***** */
|
---|
38 |
|
---|
39 | #ifndef nsAutoBuffer_h__
|
---|
40 | #define nsAutoBuffer_h__
|
---|
41 |
|
---|
42 | #ifndef nsMemory_h__
|
---|
43 | #include "nsMemory.h"
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * A buffer which will use stack space if the requested size will
|
---|
48 | * fit in the stack buffer and allocate from the heap if not.
|
---|
49 | *
|
---|
50 | * Below is a usage example :
|
---|
51 | *
|
---|
52 | * typedef nsAutoBuffer<PRUnichar, 256> nsAutoUnicharBuffer;
|
---|
53 | *
|
---|
54 | * nsAutoUnicharBuffer buffer;
|
---|
55 | *
|
---|
56 | * if (!buffer.EnsureElemCapacity(initialLength))
|
---|
57 | * return NS_ERROR_OUT_OF_MEMORY;
|
---|
58 | *
|
---|
59 | * PRUnichar *unicharPtr = buffer.get();
|
---|
60 | *
|
---|
61 | * // add PRUnichar's to the buffer pointed to by |unicharPtr| as long as
|
---|
62 | * // the number of PRUnichar's is less than |intialLength|
|
---|
63 | *
|
---|
64 | * // increase the capacity
|
---|
65 | * if (!buffer.AddElemCapacity(extraLength))
|
---|
66 | * return NS_ERROR_OUT_OF_MEMORY
|
---|
67 | *
|
---|
68 | * unicharPtr = buffer.get() + initialLength;
|
---|
69 | *
|
---|
70 | * //continue to add PRUnichar's....
|
---|
71 | */
|
---|
72 |
|
---|
73 | template <class T, PRInt32 sz>
|
---|
74 | class nsAutoBuffer
|
---|
75 | {
|
---|
76 | public:
|
---|
77 | nsAutoBuffer() :
|
---|
78 | mBufferPtr(mStackBuffer),
|
---|
79 | mCurElemCapacity(sz)
|
---|
80 | {
|
---|
81 | }
|
---|
82 |
|
---|
83 | ~nsAutoBuffer()
|
---|
84 | {
|
---|
85 | if (mBufferPtr != mStackBuffer)
|
---|
86 | nsMemory::Free(mBufferPtr);
|
---|
87 | }
|
---|
88 |
|
---|
89 | PRBool EnsureElemCapacity(PRInt32 inElemCapacity)
|
---|
90 | {
|
---|
91 | if (inElemCapacity <= mCurElemCapacity)
|
---|
92 | return PR_TRUE;
|
---|
93 |
|
---|
94 | T* newBuffer;
|
---|
95 |
|
---|
96 | if (mBufferPtr != mStackBuffer)
|
---|
97 | newBuffer = (T*)nsMemory::Realloc((void *)mBufferPtr, inElemCapacity * sizeof(T));
|
---|
98 | else
|
---|
99 | newBuffer = (T*)nsMemory::Alloc(inElemCapacity * sizeof(T));
|
---|
100 |
|
---|
101 | if (!newBuffer)
|
---|
102 | return PR_FALSE;
|
---|
103 |
|
---|
104 | if (mBufferPtr != mStackBuffer)
|
---|
105 | nsMemory::Free(mBufferPtr);
|
---|
106 |
|
---|
107 | mBufferPtr = newBuffer;
|
---|
108 | mCurElemCapacity = inElemCapacity;
|
---|
109 | return PR_TRUE;
|
---|
110 | }
|
---|
111 |
|
---|
112 | PRBool AddElemCapacity(PRInt32 inElemCapacity)
|
---|
113 | {
|
---|
114 | return EnsureElemCapacity(mCurElemCapacity + inElemCapacity);
|
---|
115 | }
|
---|
116 |
|
---|
117 | T* get() const { return mBufferPtr; }
|
---|
118 | PRInt32 GetElemCapacity() const { return mCurElemCapacity; }
|
---|
119 |
|
---|
120 | protected:
|
---|
121 |
|
---|
122 | T *mBufferPtr;
|
---|
123 | T mStackBuffer[sz];
|
---|
124 | PRInt32 mCurElemCapacity;
|
---|
125 | };
|
---|
126 |
|
---|
127 | #endif // nsAutoBuffer_h__
|
---|