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.
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  SYS_SharedMemory(const SYS_SharedMemory &) = delete;
38  SYS_SharedMemory &operator=(const SYS_SharedMemory &) = delete;
39 
40  /// On some operating systems, shared memory is handled using a file system
41  /// interface. On these systems, we need to know where the temporary files
42  /// should be installed. This is typically in HOUDINI_TEMP_DIR.
43  ///
44  /// Note: This is not thread safe, and should only be done once
45  static void setTempDir(const char *path);
46 
47  /// Resets the shared memory to the given size. This has the effect of
48  /// removing the previous shared memory segment (unless it's already open by
49  /// someone else, in which case it gets deleted when that user closes it).
50  /// This operation will also result in a new id() being generated to avoid
51  /// a potential conflict with the potentially still open shared memory
52  /// section.
53  /// The shared memory cannot be reset if it was opened from an existing
54  /// identifier.
55  bool reset(exint size);
56 
57  /// Returns the identifier of the shared memory segment, such that it can
58  /// be opened from another process. The pointer is valid as long as this
59  /// object is.
60  const char *id() const;
61 
62  /// Returns the size of the shared memory, in bytes.
63  exint size() const;
64 
65  /// Returns true if this shared memory is read-only.
66  bool readOnly() const;
67 
68  /// Returns an approximation of the memory used by these structures,
69  /// NOT the shared memory itself.
70  int64 getMemoryUsage(bool inclusive) const;
71 
72 private:
73  friend class SYS_SharedMemoryView;
74  SYS_SharedMemoryImpl *myImpl;
75 };
76 
78 {
79 public:
80  // Initialize a new shared memory view from a given shared memory. The
81  // offset and size will be truncated to fit inside the size of the
82  // shared memory.
85 
87 
89  SYS_SharedMemoryView &operator=(const SYS_SharedMemoryView &) = delete;
90 
91  // Sets a new view on the shared memory data.
92  void setView(exint offset = 0, exint size = SYS_EXINT_MAX);
93 
94  // Returns the offset into the shared memory that this view represents.
95  exint offset() const;
96 
97  // Returns the size of this shared memory view. If the size is 0, then
98  // the view is not valid.
99  exint size() const;
100 
101  // Returns true if the shared memory this view applies to is read only.
102  bool readOnly() const;
103 
104  // Returns a pointer to the data represented by this view. If the view is
105  // not valid, a NULL pointer is returned.
106  // Note: If the view is marked read-only, a write to the data pointed to
107  // will very likely cause a crash.
108  void *data() const;
109 
110  // This can be called to synchronize the data with the underlying
111  // representation while keeping the view open.
112  void sync();
113 
114  /// Returns an approximation of the memory used by these structures,
115  /// NOT the shared memory itself.
116  int64 getMemoryUsage(bool inclusive) const;
117 
118 private:
119  SYS_SharedMemoryViewImpl *myImpl;
120 };
121 
122 // An adapter for std::istream that can be used to read from a shared memory
123 // buffer in an iostream context.
124 class SYS_API SYS_SharedMemoryInputStream : public std::istream
125 {
126 public:
127  SYS_SharedMemoryInputStream(SYS_SharedMemory &shm, exint block_size = 0);
128  ~SYS_SharedMemoryInputStream() override;
129 
130  exint blockSize() const;
131 private:
132  SYS_SharedMemoryStreamBuffer *myBuf;
133 };
134 
135 // An adapter fro std::ostream that can be used to write to a shared memory
136 // buffer in an iostream context. It has two modes of operation, one which can
137 // be initialized with a NULL buffer to simply count how many bytes would have
138 // been written to the buffer and a second mode where a shared memory, of the
139 // appropriate size, is supplied to write to.
140 class SYS_API SYS_SharedMemoryOutputStream : public std::ostream
141 {
142 public:
144  ~SYS_SharedMemoryOutputStream() override;
145 
146  exint size() const;
147  exint blockSize() const;
148 private:
149  SYS_SharedMemoryStreamBuffer *myBuf;
150 };
151 
152 #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
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR_14 uint8_t blockSize(VULKAN_HPP_NAMESPACE::Format format)
#define SYS_API
Definition: SYS_API.h:11