00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024 #ifndef SHMEMORY_HPP
00025 #define SHMEMORY_HPP
00026
00027 #include <list>
00028 #include <map>
00029 #include <utility>
00030 #include <string>
00031 #include "ShDllExport.hpp"
00032 #include "ShRefCount.hpp"
00033 #include "ShMemoryDep.hpp"
00034
00035 namespace SH {
00036
00041 class ShStorage;
00042
00051 class
00052 SH_DLLEXPORT ShMemory : public ShRefCountable {
00053 public:
00054 virtual ~ShMemory();
00055
00057 int timestamp() const;
00058
00060 ShPointer<ShStorage> findStorage(const std::string& id);
00061
00064 template<typename Functor>
00065 ShPointer<ShStorage> findStorage(const std::string& id, const Functor& f);
00066
00068 void removeStorage(const ShPointer<ShStorage>& storage);
00069
00071 void add_dep(ShMemoryDep* dep);
00072
00074 void flush();
00075
00076 protected:
00077 ShMemory();
00078
00079 private:
00080 void updateTimestamp(int timestamp);
00081
00082 void addStorage(const ShPointer<ShStorage>& storage);
00083
00084 typedef std::list< ShPointer<ShStorage> > StorageList;
00085 StorageList m_storages;
00086 int m_timestamp;
00087
00089 std::list<ShMemoryDep*> dependencies;
00090
00091 friend class ShStorage;
00092
00094 ShMemory& operator=(const ShMemory& other);
00096 ShMemory(const ShMemory& other);
00097 };
00098
00099 typedef ShPointer<ShMemory> ShMemoryPtr;
00100 typedef ShPointer<const ShMemory> ShMemoryCPtr;
00101
00113 class
00114 SH_DLLEXPORT ShTransfer {
00115 public:
00116 virtual ~ShTransfer() {}
00117
00119 virtual bool transfer(const ShStorage* from, ShStorage* to) = 0;
00120
00124 virtual int cost() = 0;
00125
00126 protected:
00127 ShTransfer(const std::string& from, const std::string& to);
00128
00129 private:
00130
00132 ShTransfer(const ShTransfer& other);
00134 ShTransfer& operator=(const ShTransfer& other);
00135 };
00136
00145 class
00146 SH_DLLEXPORT ShStorage : public ShRefCountable {
00147 public:
00148 ShStorage();
00149 virtual ~ShStorage();
00150
00152 int timestamp() const;
00154 void setTimestamp(int timestamp);
00156 const ShMemory* memory() const;
00158 ShMemory* memory();
00159
00164 void sync();
00165
00168 void dirty();
00169
00170
00172 void dirtyall();
00173
00177 virtual std::string id() const = 0;
00178
00181 static int cost(ShStorage* from, ShStorage* to);
00182
00185 static bool transfer(ShStorage* from, ShStorage* to);
00186
00188 static void addTransfer(const std::string& from,
00189 const std::string& to,
00190 ShTransfer* transfer);
00191
00193 void orphan();
00194
00195 protected:
00196 ShStorage(ShMemory* memory);
00197
00198 private:
00199 ShMemory* m_memory;
00200 int m_timestamp;
00201
00202 typedef std::map<std::pair<std::string, std::string>, ShTransfer*> TransferMap;
00203 static TransferMap* m_transfers;
00204
00206 ShStorage(const ShStorage& other);
00208 ShStorage& operator=(const ShStorage& other);
00209 };
00210
00211 typedef ShPointer<ShStorage> ShStoragePtr;
00212 typedef ShPointer<const ShStorage> ShStorageCPtr;
00213
00220 class
00221 SH_DLLEXPORT ShHostStorage : public ShStorage {
00222 public:
00223 ShHostStorage(ShMemory* memory, int length);
00224 ShHostStorage(ShMemory* memory, int length, void* data);
00225
00228 ~ShHostStorage();
00229
00230 std::string id() const;
00231
00233 int length() const;
00234
00236 const void* data() const;
00238 void* data();
00239
00240 private:
00241 int m_length;
00242 void* m_data;
00243
00244 bool m_managed;
00245
00246
00247 ShHostStorage& operator=(const ShHostStorage& other);
00248 ShHostStorage(const ShHostStorage& other);
00249 };
00250
00251 typedef ShPointer<ShHostStorage> ShHostStoragePtr;
00252 typedef ShPointer<const ShHostStorage> ShHostStorageCPtr;
00253
00258 class
00259 SH_DLLEXPORT ShHostMemory : public ShMemory {
00260 public:
00261 ShHostMemory(int length);
00262 ShHostMemory(int length, void* data);
00263
00264 ~ShHostMemory();
00265
00266 ShHostStoragePtr hostStorage();
00267 ShPointer<const ShHostStorage> hostStorage() const;
00268
00269 private:
00270 ShHostStoragePtr m_hostStorage;
00271
00272 ShHostMemory& operator=(const ShHostMemory& other);
00273 ShHostMemory(const ShHostMemory& other);
00274 };
00275
00276 typedef ShPointer<ShHostMemory> ShHostMemoryPtr;
00277 typedef ShPointer<const ShHostMemory> ShHostMemoryCPtr;
00278
00279 template<typename Functor>
00280 ShPointer<ShStorage> ShMemory::findStorage(const std::string& id, const Functor& f)
00281 {
00282 for (StorageList::iterator I = m_storages.begin(); I != m_storages.end(); ++I) {
00283 if ((*I)->id() == id && f(*I)) return *I;
00284 }
00285 return 0;
00286 }
00287
00290 }
00291
00292 #endif