00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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 #include "ShMemoryDep.hpp"
00037
00038 namespace SH {
00039
00044 class ShStorage;
00045
00054 class
00055 SH_DLLEXPORT ShMemory : public ShRefCountable {
00056 public:
00057 virtual ~ShMemory();
00058
00060 int timestamp() const;
00061
00063 ShPointer<ShStorage> findStorage(const std::string& id);
00064
00067 template<typename Functor>
00068 ShPointer<ShStorage> findStorage(const std::string& id, const Functor& f);
00069
00071 void removeStorage(const ShPointer<ShStorage>& storage);
00072
00074 void add_dep(ShMemoryDep* dep);
00075
00077 void flush();
00078
00079 protected:
00080 ShMemory();
00081
00082 private:
00083 void updateTimestamp(int timestamp);
00084
00085 void addStorage(const ShPointer<ShStorage>& storage);
00086
00087 typedef std::list< ShPointer<ShStorage> > StorageList;
00088 StorageList m_storages;
00089 int m_timestamp;
00090
00092 std::list<ShMemoryDep*> dependencies;
00093
00094 friend class ShStorage;
00095
00097 ShMemory& operator=(const ShMemory& other);
00099 ShMemory(const ShMemory& other);
00100 };
00101
00102 typedef ShPointer<ShMemory> ShMemoryPtr;
00103 typedef ShPointer<const ShMemory> ShMemoryCPtr;
00104
00116 class
00117 SH_DLLEXPORT ShTransfer {
00118 public:
00120 virtual bool transfer(const ShStorage* from, ShStorage* to) = 0;
00121
00125 virtual int cost() = 0;
00126
00127 protected:
00128 ShTransfer(const std::string& from, const std::string& to);
00129
00130 private:
00131
00133 ShTransfer(const ShTransfer& other);
00135 ShTransfer& operator=(const ShTransfer& other);
00136 };
00137
00146 class
00147 SH_DLLEXPORT ShStorage : public ShRefCountable {
00148 public:
00149 ShStorage();
00150 virtual ~ShStorage();
00151
00153 int timestamp() const;
00155 void setTimestamp(int timestamp);
00157 const ShMemory* memory() const;
00159 ShMemory* memory();
00160
00165 void sync();
00166
00169 void dirty();
00170
00171
00173 void dirtyall();
00174
00178 virtual std::string id() const = 0;
00179
00182 static int cost(ShStorage* from, ShStorage* to);
00183
00186 static bool transfer(ShStorage* from, ShStorage* to);
00187
00189 static void addTransfer(const std::string& from,
00190 const std::string& to,
00191 ShTransfer* transfer);
00192
00194 void orphan();
00195
00196 protected:
00197 ShStorage(ShMemory* memory);
00198
00199 private:
00200 ShMemory* m_memory;
00201 int m_timestamp;
00202
00203 typedef std::map<std::pair<std::string, std::string>, ShTransfer*> TransferMap;
00204 static TransferMap* m_transfers;
00205
00207 ShStorage(const ShStorage& other);
00209 ShStorage& operator=(const ShStorage& other);
00210 };
00211
00212 typedef ShPointer<ShStorage> ShStoragePtr;
00213 typedef ShPointer<const ShStorage> ShStorageCPtr;
00214
00221 class
00222 SH_DLLEXPORT ShHostStorage : public ShStorage {
00223 public:
00224 ShHostStorage(ShMemory* memory, int length);
00225 ShHostStorage(ShMemory* memory, int length, void* data);
00226
00229 ~ShHostStorage();
00230
00231 std::string id() const;
00232
00234 int length() const;
00235
00237 const void* data() const;
00239 void* data();
00240
00241 private:
00242 int m_length;
00243 void* m_data;
00244
00245 bool m_managed;
00246
00247
00248 ShHostStorage& operator=(const ShHostStorage& other);
00249 ShHostStorage(const ShHostStorage& other);
00250 };
00251
00252 typedef ShPointer<ShHostStorage> ShHostStoragePtr;
00253 typedef ShPointer<const ShHostStorage> ShHostStorageCPtr;
00254
00259 class
00260 SH_DLLEXPORT ShHostMemory : public ShMemory {
00261 public:
00262 ShHostMemory(int length);
00263 ShHostMemory(int length, void* data);
00264
00265 ~ShHostMemory();
00266
00267 ShHostStoragePtr hostStorage();
00268 ShPointer<const ShHostStorage> hostStorage() const;
00269
00270 private:
00271 ShHostStoragePtr m_hostStorage;
00272
00273 ShHostMemory& operator=(const ShHostMemory& other);
00274 ShHostMemory(const ShHostMemory& other);
00275 };
00276
00277 typedef ShPointer<ShHostMemory> ShHostMemoryPtr;
00278 typedef ShPointer<const ShHostMemory> ShHostMemoryCPtr;
00279
00280 template<typename Functor>
00281 ShPointer<ShStorage> ShMemory::findStorage(const std::string& id, const Functor& f)
00282 {
00283 for (StorageList::iterator I = m_storages.begin(); I != m_storages.end(); ++I) {
00284 if ((*I)->id() == id && f(*I)) return *I;
00285 }
00286 return 0;
00287 }
00288
00291 }
00292
00293 #endif