HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  UT_SharedMem(const UT_SharedMem &) = delete;
69  UT_SharedMem &operator=(const UT_SharedMem &) = delete;
70 
71 //
72 // setKey will return 1 upon success and 0 upon failure.
73 // It will only fail if the memory is already attached.
74 //
75  int setKey(const char *path, int id=0);
76  int setKey(key_t key);
77 #ifdef WIN32
78  int setKey(int key);
79 #endif
80 
81 //
82 // setSize will return 1 upon success and 0 upon failure.
83 // It will only fail if the memory is already attached.
84 //
85  int setSize(int size);
86 
87 //
88 // detach will disassociate the shared memory segment from this process.
89 // It returns 1 upon success and 0 if there was no memory to detach.
90 //
91  int detach();
92 
93 //
94 // destroy will detach from the memory and then attempt to remove the
95 // shared memory segment from the system.
96 //
97  int destroy();
98 
99 //
100 // getMemory will return a pointer to the shared memory segment.
101 // If this method returns zero then the global "errno" should indicate
102 // the reason for the failure.
103 //
104  virtual void *getMemory( int reuse = 1 );
105 
106 
107 protected:
109  int ourKeyId;
110 
111 private:
112  int mySize;
113  key_t myShmKey;
114  key_t myShmId;
115  void *myMemory;
116 };
117 
118 #endif /* __UT_SharedMem__ */
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
#define UT_API
Definition: UT_API.h:14
GLsizeiptr size
Definition: glcorearb.h:664
UT_String ourKeyPath
Definition: UT_SharedMem.h:108
LeafData & operator=(const LeafData &)=delete