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

Annotation of /trunk/common/include/Utilities/General.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (hide annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 6 months ago) by william
File MIME type: text/plain
File size: 8123 byte(s)
re-commit (had local access denied errors when committing)
1 william 31 /* 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     // This macro is actually useful for about any and every possible application of C++
19     // equality operators.
20     #define OpEqu( field ) (field == right.field)
21    
22     // Macro used for removing some of the redtape involved in defining bitfield/union helpers.
23     //
24     #define BITFIELD32() \
25     union { \
26     u32 bitset; \
27     struct {
28    
29     #define BITFIELD_END }; };
30    
31    
32     // ----------------------------------------------------------------------------------------
33     // RecursionGuard - Basic protection against function recursion
34     // ----------------------------------------------------------------------------------------
35     // Thread safety note: If used in a threaded environment, you shoud use a handle to a __threadlocal
36     // storage variable (protects aaginst race conditions and, in *most* cases, is more desirable
37     // behavior as well.
38     //
39     // Rationale: wxWidgets has its own wxRecursionGuard, but it has a sloppy implementation with
40     // entirely unnecessary assertion checks.
41     //
42     class RecursionGuard
43     {
44     public:
45     int& Counter;
46    
47     RecursionGuard( int& counter ) : Counter( counter )
48     { ++Counter; }
49    
50     virtual ~RecursionGuard() throw()
51     { --Counter; }
52    
53     bool IsReentrant() const { return Counter > 1; }
54     };
55    
56     // --------------------------------------------------------------------------------------
57     // ICloneable / IActionInvocation / IDeletableObject
58     // --------------------------------------------------------------------------------------
59     class IActionInvocation
60     {
61     public:
62     virtual ~IActionInvocation() throw() {}
63     virtual void InvokeAction()=0;
64     };
65    
66     class ICloneable
67     {
68     public:
69     virtual ICloneable* Clone() const=0;
70     };
71    
72     class IDeletableObject
73     {
74     public:
75     virtual ~IDeletableObject() throw() {}
76    
77     virtual void DeleteSelf()=0;
78     virtual bool IsBeingDeleted()=0;
79    
80     protected:
81     // This function is GUI implementation dependent! It's implemented by PCSX2's AppHost,
82     // but if the SysCore is being linked to another front end, you'll need to implement this
83     // yourself. Most GUIs have built in message pumps. If a platform lacks one then you'll
84     // need to implement one yourself (yay?).
85     virtual void DoDeletion()=0;
86     };
87    
88     // --------------------------------------------------------------------------------------
89     // BaseDeletableObject
90     // --------------------------------------------------------------------------------------
91     // Oh the fruits and joys of multithreaded C++ coding conundrums! This class provides a way
92     // to be deleted from arbitraty threads, or to delete themselves (which is considered unsafe
93     // in C++, though it does typically work). It also gives objects a second recourse for
94     // doing fully virtualized cleanup, something C++ also makes impossible because of how it
95     // implements it's destructor hierarchy.
96     //
97     // To utilize virtual destruction, override DoDeletion() and be sure to invoke the base class
98     // implementation of DoDeletion().
99     //
100     // Assertions:
101     // This class generates an assertion of the destructor is called from anything other than
102     // the main/gui thread.
103     //
104     // Rationale:
105     // wxWidgets provides a pending deletion feature, but it's specific to wxCore (not wxBase)
106     // which means it requires wxApp and all that, which is bad for plugins and the possibility
107     // of linking PCSX2 core against a non-WX gui in the future. It's also not thread safe
108     // (sigh). And, finally, it requires quite a bit of red tape to implement wxObjects because
109     // of the wx-custom runtime type information. So I made my own.
110     //
111     class BaseDeletableObject : public virtual IDeletableObject
112     {
113     protected:
114     volatile long m_IsBeingDeleted;
115    
116     public:
117     BaseDeletableObject();
118     virtual ~BaseDeletableObject() throw();
119    
120     void DeleteSelf();
121     bool IsBeingDeleted() { return !!m_IsBeingDeleted; }
122    
123     // Returns FALSE if the object is already marked for deletion, or TRUE if the app
124     // should schedule the object for deletion. Only schedule if TRUE is returned, otherwise
125     // the object could get deleted twice if two threads try to schedule it at the same time.
126     bool MarkForDeletion();
127    
128     protected:
129     // This function is GUI implementation dependent! It's implemented by PCSX2's AppHost,
130     // but if the SysCore is being linked to another front end, you'll need to implement this
131     // yourself. Most GUIs have built in message pumps. If a platform lacks one then you'll
132     // need to implement one yourself (yay?).
133     virtual void DoDeletion();
134     };
135    
136 william 280 // --------------------------------------------------------------------------------------
137     // PageProtectionMode
138     // --------------------------------------------------------------------------------------
139     class PageProtectionMode
140     {
141     protected:
142     bool m_read;
143     bool m_write;
144     bool m_exec;
145 william 31
146 william 280 public:
147     PageProtectionMode()
148     {
149     All( false );
150     }
151    
152     PageProtectionMode& Read( bool allow=true )
153     {
154     m_read = allow;
155     return *this;
156     }
157    
158     PageProtectionMode& Write( bool allow=true )
159     {
160     m_write = allow;
161     return *this;
162     }
163    
164     PageProtectionMode& Execute( bool allow=true )
165     {
166     m_exec = allow;
167     return *this;
168     }
169    
170     PageProtectionMode& All( bool allow=true )
171     {
172     m_read = m_write = m_exec = allow;
173     return *this;
174     }
175    
176     bool CanRead() const { return m_read; }
177     bool CanWrite() const { return m_write; }
178     bool CanExecute() const { return m_exec && m_read; }
179     bool IsNone() const { return !m_read && !m_write; }
180    
181     wxString ToString() const;
182 william 31 };
183    
184 william 280 static __fi PageProtectionMode PageAccess_None()
185     {
186     return PageProtectionMode();
187     }
188    
189     static __fi PageProtectionMode PageAccess_ReadOnly()
190     {
191     return PageProtectionMode().Read();
192     }
193    
194     static __fi PageProtectionMode PageAccess_WriteOnly()
195     {
196     return PageProtectionMode().Write();
197     }
198    
199     static __fi PageProtectionMode PageAccess_ReadWrite()
200     {
201     return PageAccess_ReadOnly().Write();
202     }
203    
204     static __fi PageProtectionMode PageAccess_ExecOnly()
205     {
206     return PageAccess_ReadOnly().Execute();
207     }
208    
209     static __fi PageProtectionMode PageAccess_Any()
210     {
211     return PageProtectionMode().All();
212     }
213    
214 william 31 // --------------------------------------------------------------------------------------
215     // HostSys
216     // --------------------------------------------------------------------------------------
217     // (this namespace name sucks, and is a throw-back to an older attempt to make things cross
218     // platform prior to wxWidgets .. it should prolly be removed -- air)
219     namespace HostSys
220     {
221 william 280 void* MmapReserve(uptr base, size_t size);
222     bool MmapCommit(uptr base, size_t size, const PageProtectionMode& mode);
223     void MmapReset(uptr base, size_t size);
224    
225     void* MmapReservePtr(void* base, size_t size);
226     bool MmapCommitPtr(void* base, size_t size, const PageProtectionMode& mode);
227     void MmapResetPtr(void* base, size_t size);
228    
229 william 31 // Maps a block of memory for use as a recompiled code buffer.
230     // Returns NULL on allocation failure.
231 william 280 extern void* Mmap(uptr base, size_t size);
232 william 31
233     // Unmaps a block allocated by SysMmap
234 william 280 extern void Munmap(uptr base, size_t size);
235 william 31
236 william 280 extern void MemProtect( void* baseaddr, size_t size, const PageProtectionMode& mode );
237 william 31
238 william 280 extern void Munmap( void* base, size_t size );
239 william 31
240     template< uint size >
241 william 280 void MemProtectStatic( u8 (&arr)[size], const PageProtectionMode& mode )
242 william 31 {
243 william 280 MemProtect( arr, size, mode );
244 william 31 }
245     }
246    
247 william 273 // Safe version of Munmap -- NULLs the pointer variable immediately after free'ing it.
248     #define SafeSysMunmap( ptr, size ) \
249     ((void) ( HostSys::Munmap( (uptr)(ptr), size ), (ptr) = NULL ))
250 william 31
251     extern void InitCPUTicks();
252     extern u64 GetTickFrequency();
253     extern u64 GetCPUTicks();
254 william 280 extern u64 GetPhysicalMemory();
255 william 31
256     extern wxString GetOSVersionString();

  ViewVC Help
Powered by ViewVC 1.1.22