/[pcsx2_0.9.7]/trunk/common/include/Utilities/SafeArray.h
ViewVC logotype

Contents of /trunk/common/include/Utilities/SafeArray.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 283 - (show annotations) (download)
Thu Dec 23 12:39:52 2010 UTC (9 years, 7 months ago) by william
File MIME type: text/plain
File size: 7148 byte(s)
Auto Commited Import of: pcsx2-0.9.7-DEBUG (upstream: v0.9.7.4132 local: v0.9.7.282-latest) in ./trunk
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #pragma once
17
18 // pxUSE_SECURE_MALLOC - enables bounds checking on scoped malloc allocations.
19
20 #ifndef pxUSE_SECURE_MALLOC
21 #define pxUSE_SECURE_MALLOC 0
22 #endif
23
24
25 // Microsoft Windows only macro, useful for freeing out COM objects:
26 #define safe_release( ptr ) \
27 ((void) (( ( (ptr) != NULL ) && ((ptr)->Release(), !!0) ), (ptr) = NULL))
28
29 // --------------------------------------------------------------------------------------
30 // SafeArray
31 // --------------------------------------------------------------------------------------
32 // Handy little class for allocating a resizable memory block, complete with exception
33 // error handling and automatic cleanup. A lightweight alternative to std::vector.
34 //
35 template< typename T >
36 class SafeArray
37 {
38 DeclareNoncopyableObject(SafeArray);
39
40 public:
41 static const int DefaultChunkSize = 0x1000 * sizeof(T);
42
43 public:
44 wxString Name; // user-assigned block name
45 int ChunkSize;
46
47 protected:
48 T* m_ptr;
49 int m_size; // size of the allocation of memory
50
51 protected:
52 SafeArray( const wxChar* name, T* allocated_mem, int initSize );
53 virtual T* _virtual_realloc( int newsize );
54
55 // A safe array index fetcher. Asserts if the index is out of bounds (dev and debug
56 // builds only -- no bounds checking is done in release builds).
57 T* _getPtr( uint i ) const;
58
59 public:
60 virtual ~SafeArray() throw();
61
62 explicit SafeArray( const wxChar* name=L"Unnamed" );
63 explicit SafeArray( int initialSize, const wxChar* name=L"Unnamed" );
64
65 void Dispose();
66 void ExactAlloc( int newsize );
67 void MakeRoomFor( int newsize )
68 {
69 if( newsize > m_size )
70 ExactAlloc( newsize );
71 }
72
73 bool IsDisposed() const { return (m_ptr==NULL); }
74
75 // Returns the size of the memory allocation, as according to the array type.
76 int GetLength() const { return m_size; }
77 // Returns the size of the memory allocation in bytes.
78 int GetSizeInBytes() const { return m_size * sizeof(T); }
79
80 // Extends the containment area of the array. Extensions are performed
81 // in chunks.
82 void GrowBy( int items )
83 {
84 MakeRoomFor( m_size + ChunkSize + items + 1 );
85 }
86
87 // Gets a pointer to the requested allocation index.
88 // DevBuilds : Generates assertion if the index is invalid.
89 T* GetPtr( uint idx=0 ) { return _getPtr( idx ); }
90 const T* GetPtr( uint idx=0 ) const { return _getPtr( idx ); }
91
92 // Gets a pointer to the element directly after the last element in the array.
93 // This is equivalent to doing GetPtr(GetLength()), except that this call *avoids*
94 // the out-of-bounds assertion check that typically occurs when you do that. :)
95 T* GetPtrEnd( uint idx=0 ) { return &m_ptr[m_size]; }
96 const T* GetPtrEnd( uint idx=0 ) const { return &m_ptr[m_size]; }
97
98 // Gets an element of this memory allocation much as if it were an array.
99 // DevBuilds : Generates assertion if the index is invalid.
100 T& operator[]( int idx ) { return *_getPtr( (uint)idx ); }
101 const T& operator[]( int idx ) const { return *_getPtr( (uint)idx ); }
102
103 virtual SafeArray<T>* Clone() const;
104 };
105
106 //////////////////////////////////////////////////////////////////////////////////////////
107 // SafeList - Simple growable container without all the mess or hassle of std containers.
108 //
109 // This container is intended for reasonably simple class types only. Things which this
110 // container does not handle with desired robustness:
111 //
112 // * Classes with non-trivial constructors (such that construction creates much overhead)
113 // * Classes with copy constructors (copying is done using performance memcpy)
114 // * Classes with destructors (they're not called, sorry!)
115 //
116 template< typename T >
117 class SafeList
118 {
119 DeclareNoncopyableObject(SafeList);
120
121 public:
122 static const int DefaultChunkSize = 0x80 * sizeof(T);
123
124 public:
125 wxString Name; // user-assigned block name
126 int ChunkSize; // assigned DefaultChunkSize on init, reconfigurable at any time.
127
128 protected:
129 T* m_ptr;
130 int m_allocsize; // size of the allocation of memory
131 uint m_length; // length of the array (active items, not buffer allocation)
132
133 protected:
134 virtual T* _virtual_realloc( int newsize );
135 void _MakeRoomFor_threshold( int newsize );
136
137 T* _getPtr( uint i ) const;
138
139 public:
140 virtual ~SafeList() throw();
141 explicit SafeList( const wxChar* name=L"Unnamed" );
142 explicit SafeList( int initialSize, const wxChar* name=L"Unnamed" );
143 virtual SafeList<T>* Clone() const;
144
145 void Remove( int index );
146 void MakeRoomFor( int blockSize );
147
148 T& New();
149 int Add( const T& src );
150 T& AddNew( const T& src );
151
152 // Returns the size of the list, as according to the array type. This includes
153 // mapped items only. The actual size of the allocation may differ.
154 int GetLength() const { return m_length; }
155
156 // Returns the size of the list, in bytes. This includes mapped items only.
157 // The actual size of the allocation may differ.
158 int GetSizeInBytes() const { return m_length * sizeof(T); }
159
160 void MatchLengthToAllocatedSize()
161 {
162 m_length = m_allocsize;
163 }
164
165 void GrowBy( int items )
166 {
167 MakeRoomFor( m_length + ChunkSize + items + 1 );
168 }
169
170 // Sets the item length to zero. Does not free memory allocations.
171 void Clear()
172 {
173 m_length = 0;
174 }
175
176 // Gets an element of this memory allocation much as if it were an array.
177 // DevBuilds : Generates assertion if the index is invalid.
178 T& operator[]( int idx ) { return *_getPtr( (uint)idx ); }
179 const T& operator[]( int idx ) const { return *_getPtr( (uint)idx ); }
180
181 T* GetPtr() { return m_ptr; }
182 const T* GetPtr() const { return m_ptr; }
183
184 T& GetLast() { return m_ptr[m_length-1]; }
185 const T& GetLast() const{ return m_ptr[m_length-1]; }
186 };
187
188 // --------------------------------------------------------------------------------------
189 // SafeAlignedArray<T>
190 // --------------------------------------------------------------------------------------
191 // Handy little class for allocating a resizable memory block, complete with
192 // exception-based error handling and automatic cleanup.
193 // This one supports aligned data allocations too!
194
195 template< typename T, uint Alignment >
196 class SafeAlignedArray : public SafeArray<T>
197 {
198 typedef SafeArray<T> _parent;
199
200 protected:
201 T* _virtual_realloc( int newsize );
202
203 public:
204 using _parent::operator[];
205
206 virtual ~SafeAlignedArray() throw();
207
208 explicit SafeAlignedArray( const wxChar* name=L"Unnamed" ) :
209 SafeArray<T>::SafeArray( name )
210 {
211 }
212
213 explicit SafeAlignedArray( int initialSize, const wxChar* name=L"Unnamed" );
214 virtual SafeAlignedArray<T,Alignment>* Clone() const;
215 };
216

  ViewVC Help
Powered by ViewVC 1.1.22