Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

ShMemory.hpp

00001 // Sh: A GPU metaprogramming language. 00002 // 00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory 00004 // Project administrator: Michael D. McCool 00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa, 00006 // Michael D. McCool 00007 // 00008 // This software is provided 'as-is', without any express or implied 00009 // warranty. In no event will the authors be held liable for any damages 00010 // arising from the use of this software. 00011 // 00012 // Permission is granted to anyone to use this software for any purpose, 00013 // including commercial applications, and to alter it and redistribute it 00014 // freely, subject to the following restrictions: 00015 // 00016 // 1. The origin of this software must not be misrepresented; you must 00017 // not claim that you wrote the original software. If you use this 00018 // software in a product, an acknowledgment in the product documentation 00019 // would be appreciated but is not required. 00020 // 00021 // 2. Altered source versions must be plainly marked as such, and must 00022 // not be misrepresented as being the original software. 00023 // 00024 // 3. This notice may not be removed or altered from any source 00025 // distribution. 00027 #ifndef SHMEMORY_HPP 00028 #define SHMEMORY_HPP 00029 00030 #include <list> 00031 #include <map> 00032 #include <utility> 00033 #include <string> 00034 #include "ShDllExport.hpp" 00035 #include "ShRefCount.hpp" 00036 00037 namespace SH { 00038 00043 class ShStorage; 00044 00053 class 00054 SH_DLLEXPORT ShMemory : public ShRefCountable { 00055 public: 00056 virtual ~ShMemory(); 00057 00059 int timestamp() const; 00060 00062 ShPointer<ShStorage> findStorage(const std::string& id); 00063 00066 template<typename Functor> 00067 ShPointer<ShStorage> findStorage(const std::string& id, const Functor& f); 00068 00070 void removeStorage(const ShPointer<ShStorage>& storage); 00071 00072 protected: 00073 ShMemory(); 00074 00075 private: 00076 void updateTimestamp(int timestamp); 00077 00078 void addStorage(const ShPointer<ShStorage>& storage); 00079 00080 typedef std::list< ShPointer<ShStorage> > StorageList; 00081 StorageList m_storages; 00082 int m_timestamp; 00083 00084 friend class ShStorage; 00085 00087 ShMemory& operator=(const ShMemory& other); 00089 ShMemory(const ShMemory& other); 00090 }; 00091 00092 typedef ShPointer<ShMemory> ShMemoryPtr; 00093 typedef ShPointer<const ShMemory> ShMemoryCPtr; 00094 00106 class 00107 SH_DLLEXPORT ShTransfer { 00108 public: 00110 virtual bool transfer(const ShStorage* from, ShStorage* to) = 0; 00111 00115 virtual int cost() = 0; 00116 00117 protected: 00118 ShTransfer(const std::string& from, const std::string& to); 00119 00120 private: 00121 00123 ShTransfer(const ShTransfer& other); 00125 ShTransfer& operator=(const ShTransfer& other); 00126 }; 00127 00136 class 00137 SH_DLLEXPORT ShStorage : public ShRefCountable { 00138 public: 00139 ShStorage(); 00140 virtual ~ShStorage(); 00141 00143 int timestamp() const; 00145 void setTimestamp(int timestamp); 00147 const ShMemory* memory() const; 00149 ShMemory* memory(); 00150 00155 void sync(); 00156 00159 void dirty(); 00160 00164 virtual std::string id() const = 0; 00165 00168 static int cost(ShStorage* from, ShStorage* to); 00169 00172 static bool transfer(ShStorage* from, ShStorage* to); 00173 00175 static void addTransfer(const std::string& from, 00176 const std::string& to, 00177 ShTransfer* transfer); 00178 00180 void orphan(); 00181 00182 protected: 00183 ShStorage(ShMemory* memory); 00184 00185 private: 00186 ShMemory* m_memory; 00187 int m_timestamp; 00188 00189 typedef std::map<std::pair<std::string, std::string>, ShTransfer*> TransferMap; 00190 static TransferMap* m_transfers; 00191 00193 ShStorage(const ShStorage& other); 00195 ShStorage& operator=(const ShStorage& other); 00196 }; 00197 00198 typedef ShPointer<ShStorage> ShStoragePtr; 00199 typedef ShPointer<const ShStorage> ShStorageCPtr; 00200 00207 class 00208 SH_DLLEXPORT ShHostStorage : public ShStorage { 00209 public: 00210 ShHostStorage(ShMemory* memory, int length); 00211 ShHostStorage(ShMemory* memory, int length, void* data); 00212 00215 ~ShHostStorage(); 00216 00217 std::string id() const; 00218 00220 int length() const; 00221 00223 const void* data() const; 00225 void* data(); 00226 00227 private: 00228 int m_length; 00229 void* m_data; 00230 00231 bool m_managed; 00232 00233 // NOT IMPLEMENTED 00234 ShHostStorage& operator=(const ShHostStorage& other); 00235 ShHostStorage(const ShHostStorage& other); 00236 }; 00237 00238 typedef ShPointer<ShHostStorage> ShHostStoragePtr; 00239 typedef ShPointer<const ShHostStorage> ShHostStorageCPtr; 00240 00245 class 00246 SH_DLLEXPORT ShHostMemory : public ShMemory { 00247 public: 00248 ShHostMemory(int length); 00249 ShHostMemory(int length, void* data); 00250 00251 ~ShHostMemory(); 00252 00253 ShHostStoragePtr hostStorage(); 00254 ShPointer<const ShHostStorage> hostStorage() const; 00255 00256 private: 00257 ShHostStoragePtr m_hostStorage; 00258 // NOT IMPLEMENTED 00259 ShHostMemory& operator=(const ShHostMemory& other); 00260 ShHostMemory(const ShHostMemory& other); 00261 }; 00262 00263 typedef ShPointer<ShHostMemory> ShHostMemoryPtr; 00264 typedef ShPointer<const ShHostMemory> ShHostMemoryCPtr; 00265 00266 template<typename Functor> 00267 ShPointer<ShStorage> ShMemory::findStorage(const std::string& id, const Functor& f) 00268 { 00269 for (StorageList::iterator I = m_storages.begin(); I != m_storages.end(); ++I) { 00270 if ((*I)->id() == id && f(*I)) return *I; 00271 } 00272 return 0; 00273 } 00274 00277 } 00278 00279 #endif

Generated on Mon Oct 18 14:17:39 2004 for Sh by doxygen 1.3.7