HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SYS_SharedMemory.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: SYS_SharedMemory.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __SYS_SharedMemory__
13 #define __SYS_SharedMemory__
14 
15 #include "SYS_API.h"
16 #include "SYS_Types.h"
17 
18 #include <iostream>
19 
20 class SYS_SharedMemoryImpl;
21 class SYS_SharedMemoryViewImpl;
22 class SYS_SharedMemoryStreamBuffer;
23 
24 /// Creates a read-only shared memory object, that accesses an existing shared
25 /// memory buffer, using a unique identifier.
27 {
28 public:
29  /// Create a new shared memory block of a given size.
31 
32  /// Open an existing shared memory block through the given identifier.
33  SYS_SharedMemory(const char *id, bool read_only=true);
34 
36 
37  /// On some operating systems, shared memory is handled using a file system
38  /// interface. On these systems, we need to know where the temporary files
39  /// should be installed. This is typically in HOUDINI_TEMP_DIR.
40  ///
41  /// Note: This is not thread safe, and should only be done once
42  static void setTempDir(const char *path);
43 
44  /// Resets the shared memory to the given size. This has the effect of
45  /// removing the previous shared memory segment (unless it's already open by
46  /// someone else, in which case it gets deleted when that user closes it).
47  /// This operation will also result in a new id() being generated to avoid
48  /// a potential conflict with the potentially still open shared memory
49  /// section.
50  /// The shared memory cannot be reset if it was opened from an existing
51  /// identifier.
52  bool reset(exint size);
53 
54  /// Returns the identifier of the shared memory segment, such that it can
55  /// be opened from another process. The pointer is valid as long as this
56  /// object is.
57  const char *id() const;
58 
59  /// Returns the size of the shared memory, in bytes.
60  exint size() const;
61 
62  /// Returns true if this shared memory is read-only.
63  bool readOnly() const;
64 
65  /// Returns an approximation of the memory used by these structures,
66  /// NOT the shared memory itself.
67  int64 getMemoryUsage(bool inclusive) const;
68 
69 private:
70  friend class SYS_SharedMemoryView;
71  SYS_SharedMemoryImpl *myImpl;
72 };
73 
75 {
76 public:
77  // Initialize a new shared memory view from a given shared memory. The
78  // offset and size will be truncated to fit inside the size of the
79  // shared memory.
82 
84 
85  // Sets a new view on the shared memory data.
86  void setView(exint offset = 0, exint size = SYS_EXINT_MAX);
87 
88  // Returns the offset into the shared memory that this view represents.
89  exint offset() const;
90 
91  // Returns the size of this shared memory view. If the size is 0, then
92  // the view is not valid.
93  exint size() const;
94 
95  // Returns true if the shared memory this view applies to is read only.
96  bool readOnly() const;
97 
98  // Returns a pointer to the data represented by this view. If the view is
99  // not valid, a NULL pointer is returned.
100  // Note: If the view is marked read-only, a write to the data pointed to
101  // will very likely cause a crash.
102  void *data() const;
103 
104  // This can be called to synchronize the data with the underlying
105  // representation while keeping the view open.
106  void sync();
107 
108  /// Returns an approximation of the memory used by these structures,
109  /// NOT the shared memory itself.
110  int64 getMemoryUsage(bool inclusive) const;
111 
112 private:
113  SYS_SharedMemoryViewImpl *myImpl;
114 };
115 
116 // An adapter for std::istream that can be used to read from a shared memory
117 // buffer in an iostream context.
118 class SYS_API SYS_SharedMemoryInputStream : public std::istream
119 {
120 public:
121  SYS_SharedMemoryInputStream(SYS_SharedMemory &shm, exint block_size = 0);
123 
124  exint blockSize() const;
125 private:
126  SYS_SharedMemoryStreamBuffer *myBuf;
127 };
128 
129 // An adapter fro std::ostream that can be used to write to a shared memory
130 // buffer in an iostream context. It has two modes of operation, one which can
131 // be initialized with a NULL buffer to simply count how many bytes would have
132 // been written to the buffer and a second mode where a shared memory, of the
133 // appropriate size, is supplied to write to.
134 class SYS_API SYS_SharedMemoryOutputStream : public std::ostream
135 {
136 public:
139 
140  exint size() const;
141  exint blockSize() const;
142 private:
143  SYS_SharedMemoryStreamBuffer *myBuf;
144 };
145 
146 #endif // __SYS_SharedMemory__
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
#define SYS_EXINT_MAX
Definition: SYS_Types.h:171
GLsizeiptr size
Definition: glcorearb.h:663
GLuint id
Definition: glcorearb.h:654
long long int64
Definition: SYS_Types.h:106
int64 exint
Definition: SYS_Types.h:115
GLintptr offset
Definition: glcorearb.h:664
GLboolean * data
Definition: glcorearb.h:130
#define SYS_API
Definition: SYS_API.h:11