BWAPI
|
00001 #pragma once 00002 /*********************************************************************** 00003 * SharedMemory is a memory buffer which can be shared with other 00004 * processes 00005 * 00006 * 00007 * SharedMemory::Pointer<T> is a T* pointer to inside a SharedMemory 00008 * - SharedMemory instance dependent 00009 * - process independent 00010 * 00011 * use SharedMemory::unpack() 00012 * 00013 * 00014 * SharedMemory::Export is used to export this SharedMemory instance 00015 * to another processes, transmit it with any IPC and import at the 00016 * other side 00017 ***********/ 00018 00019 namespace Util 00020 { 00021 class SharedMemory; 00022 } 00023 00024 #include "MemoryFrame.h" 00025 #include "Exceptions.h" 00026 #include "RemoteProcess.h" 00027 #define WIN32_LEAN_AND_MEAN 00028 #include "windows.h" 00029 00030 namespace Util 00031 { 00032 class SharedMemory 00033 { 00034 public: 00035 //----------------------- TYPES ------------------------------------ 00036 struct Export 00037 { 00038 friend class SharedMemory; // structure is part of SharedMemory 00039 00040 Export(); 00041 bool isValid() const; 00042 00043 private: 00044 HANDLE targetProcessMappingObjectHandle; 00045 unsigned int bufferSize; 00046 bool readOnly; 00047 }; 00048 template <typename T> struct Pointer 00049 { 00050 friend class SharedMemory; // structure is part of SharedMemory 00051 private: 00052 int offset; 00053 }; 00054 //----------------------- FUNCTIONS -------------------------------- 00055 SharedMemory(); 00056 ~SharedMemory(); 00057 00058 void create(int size); // allocates memory 00059 bool create(int size, const char* systemName); // allocates memory, returns false if opened an existing named object 00060 void import(Export source); // connects to shared memory 00061 void release(); // releases memory 00062 Export exportToProcess(RemoteProcess &target, bool readOnly) const; 00063 00064 template<typename T> 00065 T *unpack(Pointer<T> p) const // to process address space 00066 { 00067 if(!(int)this->bufferBase) 00068 return NULL; 00069 return (T*)(this->bufferBase + p.offset); 00070 } 00071 00072 template<typename T> 00073 Pointer<T> pack(T *pt) const // to shared address space 00074 { 00075 Pointer<T> retval; 00076 retval.offset = (int)pt - (int)this->bufferBase; 00077 } 00078 00079 MemoryFrame getMemory() const; 00080 00081 static int getPageSize(); // allocation size snapping 00082 00083 private: 00084 //----------------------- ------------------------------------------ 00085 SharedMemory(SharedMemory&); // no copying 00086 HANDLE mappingObjectHandle; 00087 void *bufferBase; 00088 unsigned int bufferSize; 00089 //----------------------- ------------------------------------------ 00090 }; 00091 }