HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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.
30  /// This constructor can fail, so you must use isValid to check for
31  /// validity.
33 
34  /// Create a new shared memory block with the given name and size.
35  /// This constructor can fail, so you must use isValid to check for
36  /// validity.
37  SYS_SharedMemory(exint size, const char *id);
38 
39  /// Open an existing shared memory block through the given identifier.
40  /// This constructor can fail, so you must use isValid to check for
41  /// validity.
42  SYS_SharedMemory(const char *id, bool read_only=true);
43 
45 
46  SYS_SharedMemory(const SYS_SharedMemory &) = delete;
47  SYS_SharedMemory &operator=(const SYS_SharedMemory &) = delete;
48 
49  /// On some operating systems, shared memory is handled using a file system
50  /// interface. On these systems, we need to know where the temporary files
51  /// should be installed. This is typically in HOUDINI_TEMP_DIR.
52  ///
53  /// Note: This is not thread safe, and should only be done once
54  static void setTempDir(const char *path);
55 
56  /// Resets the shared memory to the given size and name, if specified. This
57  /// has the effect of removing the previous shared memory segment (unless
58  /// it's already open by someone else, in which case it gets deleted when
59  /// that user closes it).
60  //
61  /// If an id is not specified in the arguments, a new id() will be generated
62  /// in order to avoid a potential conflict with potentially still opened
63  /// previous shared memory section.
64  ///
65  /// The shared memory cannot be reset if it was opened from an existing
66  /// identifier.
67  bool reset(exint size, const char *id = nullptr);
68 
69  /// Returns the identifier of the shared memory segment, such that it can
70  /// be opened from another process. The pointer is valid as long as this
71  /// object is.
72  const char *id() const;
73 
74  /// Returns the size of the shared memory, in bytes.
75  exint size() const;
76 
77  /// Returns true if this shared memory is read-only.
78  bool readOnly() const;
79 
80  /// Returns an approximation of the memory used by these structures,
81  /// NOT the shared memory itself.
82  int64 getMemoryUsage(bool inclusive) const;
83 
84  /// Returns whether the shared memory is in a valid state for
85  /// reading/writing. If the constructor failed to initialize the shared
86  /// memory, an invalid shared memory size was supplied (such as 0), or the
87  /// shared memory was closed, then this method will return false.
88  bool isValid() const;
89 
90 private:
91  friend class SYS_SharedMemoryView;
92  SYS_SharedMemoryImpl *myImpl;
93 };
94 
96 {
97 public:
98  // Initialize a new shared memory view from a given shared memory. The
99  // offset and size will be truncated to fit inside the size of the
100  // shared memory.
103 
105 
106  SYS_SharedMemoryView(const SYS_SharedMemoryView &) = delete;
108 
109  // Sets a new view on the shared memory data.
110  void setView(exint offset = 0, exint size = SYS_EXINT_MAX);
111 
112  // Returns the offset into the shared memory that this view represents.
113  exint offset() const;
114 
115  // Returns the size of this shared memory view. If the size is 0, then
116  // the view is not valid.
117  exint size() const;
118 
119  // Returns true if the shared memory this view applies to is read only.
120  bool readOnly() const;
121 
122  // Returns a pointer to the data represented by this view. If the view is
123  // not valid, a NULL pointer is returned.
124  // Note: If the view is marked read-only, a write to the data pointed to
125  // will very likely cause a crash.
126  void *data() const;
127 
128  // This can be called to synchronize the data with the underlying
129  // representation while keeping the view open.
130  void sync();
131 
132  /// Returns an approximation of the memory used by these structures,
133  /// NOT the shared memory itself.
134  int64 getMemoryUsage(bool inclusive) const;
135 
136 private:
137  SYS_SharedMemoryViewImpl *myImpl;
138 };
139 
140 // An adapter for std::istream that can be used to read from a shared memory
141 // buffer in an iostream context.
142 class SYS_API SYS_SharedMemoryInputStream : public std::istream
143 {
144 public:
145  SYS_SharedMemoryInputStream(SYS_SharedMemory &shm, exint block_size = 0);
146  ~SYS_SharedMemoryInputStream() override;
147 
148  exint blockSize() const;
149 private:
150  SYS_SharedMemoryStreamBuffer *myBuf;
151 };
152 
153 // An adapter fro std::ostream that can be used to write to a shared memory
154 // buffer in an iostream context. It has two modes of operation, one which can
155 // be initialized with a NULL buffer to simply count how many bytes would have
156 // been written to the buffer and a second mode where a shared memory, of the
157 // appropriate size, is supplied to write to.
158 class SYS_API SYS_SharedMemoryOutputStream : public std::ostream
159 {
160 public:
162  ~SYS_SharedMemoryOutputStream() override;
163 
164  exint size() const;
165  exint blockSize() const;
166 private:
167  SYS_SharedMemoryStreamBuffer *myBuf;
168 };
169 
170 #endif // __SYS_SharedMemory__
GLboolean * data
Definition: glcorearb.h:131
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
int64 exint
Definition: SYS_Types.h:125
#define SYS_EXINT_MAX
Definition: SYS_Types.h:181
GLintptr offset
Definition: glcorearb.h:665
GLboolean reset
Definition: glad.h:5138
long long int64
Definition: SYS_Types.h:116
GLuint id
Definition: glcorearb.h:655
GLsizeiptr size
Definition: glcorearb.h:664
LeafData & operator=(const LeafData &)=delete
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR_14 uint8_t blockSize(VULKAN_HPP_NAMESPACE::Format format)
#define SYS_API
Definition: SYS_API.h:11