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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (show annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (9 years, 4 months ago) by william
File MIME type: text/plain
File size: 6738 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug 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 // 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
137 enum PageProtectionMode
138 {
139 Protect_NoAccess = 0,
140 Protect_ReadOnly,
141 Protect_ReadWrite
142 };
143
144 // --------------------------------------------------------------------------------------
145 // HostSys
146 // --------------------------------------------------------------------------------------
147 // (this namespace name sucks, and is a throw-back to an older attempt to make things cross
148 // platform prior to wxWidgets .. it should prolly be removed -- air)
149 namespace HostSys
150 {
151 // Maps a block of memory for use as a recompiled code buffer.
152 // The allocated block has code execution privileges.
153 // Returns NULL on allocation failure.
154 extern void *Mmap(uptr base, u32 size);
155
156 // Unmaps a block allocated by SysMmap
157 extern void Munmap(uptr base, u32 size);
158
159 extern void MemProtect( void* baseaddr, size_t size, PageProtectionMode mode, bool allowExecution=false );
160
161 extern void Munmap( void* base, u32 size );
162
163 template< uint size >
164 void MemProtectStatic( u8 (&arr)[size], PageProtectionMode mode, bool allowExecution=false )
165 {
166 MemProtect( arr, size, mode, allowExecution );
167 }
168 }
169
170
171 extern void InitCPUTicks();
172 extern u64 GetTickFrequency();
173 extern u64 GetCPUTicks();
174
175 extern wxString GetOSVersionString();
176
177 extern void* __fastcall pcsx2_aligned_malloc(size_t size, size_t align);
178 extern void* __fastcall pcsx2_aligned_realloc(void* handle, size_t size, size_t align);
179 extern void pcsx2_aligned_free(void* pmem);
180
181 // aligned_malloc: Implement/declare linux equivalents here!
182 #if !defined(_MSC_VER) && !defined(HAVE_ALIGNED_MALLOC)
183 # define _aligned_malloc pcsx2_aligned_malloc
184 # define _aligned_free pcsx2_aligned_free
185 # define _aligned_realloc pcsx2_aligned_realloc
186 #endif

  ViewVC Help
Powered by ViewVC 1.1.22