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
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
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
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