HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_SharedMem.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_SharedMem.h (C++)
7  *
8  * COMMENTS:
9  * Typical use is as follows:
10  *
11  * UT_SharedMem ipc(IPC_PATHNAME);
12  * void *mem;
13  *
14  * if( !(mem = ipc.getMemory()) )
15  * {
16  * ipc.setSize(SHARED_MEM_SIZE);
17  * mem = ipc.getMemory();
18  * if( !mem ) abortOrSomething();
19  * }
20  *
21  *
22  *
23  */
24 #ifndef __UT_SharedMem__
25 #define __UT_SharedMem__
26 
27 #include "UT_API.h"
28 #include <sys/types.h>
29 #include "UT_String.h"
30 
31 #ifdef WIN32
32  typedef void * key_t;
33 #endif
34 
36 {
37 public:
38 //
39 // This constructor is for using non-private shared memory segments
40 // and will create a shared memory key using the path and the id value.
41 // The named file should exist and is used only to create a unique
42 // shared memory key. "size" is the size of the shared memory segment
43 // in bytes. If size is non-zero this object will try to create the
44 // memory, otherwise it is assumed to already exist. If no arguments
45 // are given then the user must later call setKey and setSize for public
46 // memory or just setSize for private.
47 //
48  explicit UT_SharedMem(const char *keypath=0, int size=0, int keyid=0);
49 
50 //
51 // This constructor is for using non-private shared memory segments
52 // with a given key. If size is non-zero this object will try to
53 // create the memory, otherwise it is assumed to already exist.
54 //
55  UT_SharedMem(key_t key, int size);
56 
57 //
58 // This constructor will create a private shared memory segment.
59 //
60  explicit UT_SharedMem(int size);
61 
62 //
63 // The destructor will detach the shared memory segment and if we
64 // were the creators it will also delete the segment.
65 //
66  virtual ~UT_SharedMem();
67 
68 //
69 // setKey will return 1 upon success and 0 upon failure.
70 // It will only fail if the memory is already attached.
71 //
72  int setKey(const char *path, int id=0);
73  int setKey(key_t key);
74 #ifdef WIN32
75  int setKey(int key);
76 #endif
77 
78 //
79 // setSize will return 1 upon success and 0 upon failure.
80 // It will only fail if the memory is already attached.
81 //
82  int setSize(int size);
83 
84 //
85 // detach will disassociate the shared memory segment from this process.
86 // It returns 1 upon success and 0 if there was no memory to detach.
87 //
88  virtual int detach();
89 
90 //
91 // destroy will detach from the memory and then attempt to remove the
92 // shared memory segment from the system.
93 //
94  virtual int destroy();
95 
96 //
97 // getMemory will return a pointer to the shared memory segment.
98 // If this method returns zero then the global "errno" should indicate
99 // the reason for the failure.
100 //
101  virtual void *getMemory( int reuse = 1 );
102 
103 
104 protected:
106  int ourKeyId;
107 
108 private:
109  int mySize;
110  key_t myShmKey;
111  key_t myShmId;
112  void *myMemory;
113 };
114 
115 #endif /* __UT_SharedMem__ */
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
#define UT_API
Definition: UT_API.h:12
GLsizeiptr size
Definition: glcorearb.h:663
UT_String ourKeyPath
Definition: UT_SharedMem.h:105