00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Paul Breslin 00008 * Side Effects Software Inc. 00009 * 20 Maud St. 00010 * Toronto, Ontario, M5V 2M5 00011 * Canada 00012 * 416-366-4607 00013 * 00014 * NAME: UT_SharedMem.h (C++) 00015 * 00016 * COMMENTS: 00017 * Typical use is as follows: 00018 * 00019 * UT_SharedMem ipc(IPC_PATHNAME); 00020 * void *mem; 00021 * 00022 * if( !(mem = ipc.getMemory()) ) 00023 * { 00024 * ipc.setSize(SHARED_MEM_SIZE); 00025 * mem = ipc.getMemory(); 00026 * if( !mem ) abortOrSomething(); 00027 * } 00028 * 00029 * 00030 * 00031 */ 00032 #ifndef __UT_SharedMem__ 00033 #define __UT_SharedMem__ 00034 00035 #include "UT_API.h" 00036 #include <sys/types.h> 00037 #include "UT_String.h" 00038 00039 #ifdef WIN32 00040 typedef void * key_t; 00041 #endif 00042 00043 class UT_API UT_SharedMem 00044 { 00045 public: 00046 // 00047 // This constructor is for using non-private shared memory segments 00048 // and will create a shared memory key using the path and the id value. 00049 // The named file should exist and is used only to create a unique 00050 // shared memory key. "size" is the size of the shared memory segment 00051 // in bytes. If size is non-zero this object will try to create the 00052 // memory, otherwise it is assumed to already exist. If no arguments 00053 // are given then the user must later call setKey and setSize for public 00054 // memory or just setSize for private. 00055 // 00056 explicit UT_SharedMem(const char *keypath=0, int size=0, int keyid=0); 00057 00058 // 00059 // This constructor is for using non-private shared memory segments 00060 // with a given key. If size is non-zero this object will try to 00061 // create the memory, otherwise it is assumed to already exist. 00062 // 00063 UT_SharedMem(key_t key, int size); 00064 00065 // 00066 // This constructor will create a private shared memory segment. 00067 // 00068 explicit UT_SharedMem(int size); 00069 00070 // 00071 // The destructor will detach the shared memory segment and if we 00072 // were the creators it will also delete the segment. 00073 // 00074 virtual ~UT_SharedMem(); 00075 00076 // 00077 // setKey will return 1 upon success and 0 upon failure. 00078 // It will only fail if the memory is already attached. 00079 // 00080 int setKey(const char *path, int id=0); 00081 int setKey(key_t key); 00082 00083 // 00084 // setSize will return 1 upon success and 0 upon failure. 00085 // It will only fail if the memory is already attached. 00086 // 00087 int setSize(int size); 00088 00089 // 00090 // detach will disassociate the shared memory segment from this process. 00091 // It returns 1 upon success and 0 if there was no memory to detach. 00092 // 00093 virtual int detach(); 00094 00095 // 00096 // destroy will detach from the memory and then attempt to remove the 00097 // shared memory segment from the system. 00098 // 00099 virtual int destroy(); 00100 00101 // 00102 // getMemory will return a pointer to the shared memory segment. 00103 // If this method returns zero then the global "errno" should indicate 00104 // the reason for the failure. 00105 // 00106 virtual void *getMemory( int reuse = 1 ); 00107 00108 00109 protected: 00110 UT_String ourKeyPath; 00111 int ourKeyId; 00112 00113 private: 00114 int mySize; 00115 key_t myShmKey; 00116 key_t myShmId; 00117 void *myMemory; 00118 }; 00119 00120 #endif /* __UT_SharedMem__ */
1.5.9