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 #include "ShStorageType.hpp"
00035
00036 namespace SH {
00037
00042 class ShStorage;
00043
00052 class
00053 SH_DLLEXPORT ShMemory : public ShRefCountable {
00054 public:
00055 virtual ~ShMemory();
00056
00058 int timestamp() const;
00059
00061 ShPointer<ShStorage> findStorage(const std::string& id);
00062
00065 template<typename Functor>
00066 ShPointer<ShStorage> findStorage(const std::string& id, const Functor& f);
00067
00069 void removeStorage(const ShPointer<ShStorage>& storage);
00070
00072 void add_dep(ShMemoryDep* dep);
00073
00075 void flush();
00076
00077 protected:
00078 ShMemory();
00079
00080 private:
00081 void updateTimestamp(int timestamp);
00082
00083 void addStorage(const ShPointer<ShStorage>& storage);
00084
00085 typedef std::list< ShPointer<ShStorage> > StorageList;
00086 StorageList m_storages;
00087 int m_timestamp;
00088
00090 std::list<ShMemoryDep*> dependencies;
00091
00092 friend class ShStorage;
00093
00095 ShMemory& operator=(const ShMemory& other);
00097 ShMemory(const ShMemory& other);
00098 };
00099
00100 typedef ShPointer<ShMemory> ShMemoryPtr;
00101 typedef ShPointer<const ShMemory> ShMemoryCPtr;
00102
00114 class
00115 SH_DLLEXPORT ShTransfer {
00116 public:
00117 virtual ~ShTransfer() {}
00118
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
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
00197 ShValueType value_type() const { return m_value_type; }
00198
00200 void value_type(ShValueType value_type) { m_value_type = value_type; }
00201
00203 int value_size() const { return m_value_size; }
00204
00205 protected:
00206 ShStorage(ShMemory* memory, ShValueType value_type);
00207
00208 ShValueType m_value_type;
00209 int m_value_size;
00210
00211 private:
00212 ShMemory* m_memory;
00213 int m_timestamp;
00214
00215 typedef std::map<std::pair<std::string, std::string>, ShTransfer*> TransferMap;
00216 static TransferMap* m_transfers;
00217
00219 ShStorage(const ShStorage& other);
00221 ShStorage& operator=(const ShStorage& other);
00222 };
00223
00224 typedef ShPointer<ShStorage> ShStoragePtr;
00225 typedef ShPointer<const ShStorage> ShStorageCPtr;
00226
00233 class
00234 SH_DLLEXPORT ShHostStorage : public ShStorage {
00235 public:
00236 ShHostStorage(ShMemory* memory, int length, ShValueType storage_type);
00237 ShHostStorage(ShMemory* memory, int length, void* data, ShValueType storage_type);
00238
00241 ~ShHostStorage();
00242
00243 std::string id() const;
00244
00246 int length() const;
00247
00249 const void* data() const;
00251 void* data();
00252
00253 private:
00254 int m_length;
00255 void* m_data;
00256
00257 bool m_managed;
00258
00259
00260 ShHostStorage& operator=(const ShHostStorage& other);
00261 ShHostStorage(const ShHostStorage& other);
00262 };
00263
00264 typedef ShPointer<ShHostStorage> ShHostStoragePtr;
00265 typedef ShPointer<const ShHostStorage> ShHostStorageCPtr;
00266
00271 class
00272 SH_DLLEXPORT ShHostMemory : public ShMemory {
00273 public:
00274 ShHostMemory(int length, ShValueType value_type);
00275 ShHostMemory(int length, void* data, ShValueType value_type);
00276
00277 ~ShHostMemory();
00278
00279 ShHostStoragePtr hostStorage();
00280 ShPointer<const ShHostStorage> hostStorage() const;
00281
00282 private:
00283 ShHostStoragePtr m_hostStorage;
00284
00285 ShHostMemory& operator=(const ShHostMemory& other);
00286 ShHostMemory(const ShHostMemory& other);
00287 };
00288
00289 typedef ShPointer<ShHostMemory> ShHostMemoryPtr;
00290 typedef ShPointer<const ShHostMemory> ShHostMemoryCPtr;
00291
00292 template<typename Functor>
00293 ShPointer<ShStorage> ShMemory::findStorage(const std::string& id, const Functor& f)
00294 {
00295 for (StorageList::iterator I = m_storages.begin(); I != m_storages.end(); ++I) {
00296 if ((*I)->id() == id && f(*I)) return *I;
00297 }
00298 return 0;
00299 }
00300
00303 }
00304
00305 #endif