CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
unmanaged_memory_stream.h
1
2#ifndef _csharptest_UnmanagedMemoryStream_h_
3#define _csharptest_UnmanagedMemoryStream_h_
4// Licensed to the .NET Foundation under one or more agreements.
5// The .NET Foundation licenses this file to you under the MIT license.
6// See the LICENSE file in the project root for more information.
7/*============================================================
8**
9**
10**
11**
12** Purpose: Create a stream over unmanaged memory, mostly
13** useful for memory-mapped files.
14**
15**
16===========================================================*/
17
18#include <system/shared_ptr.h>
19#include <system/object.h>
20#include <system/io/stream.h>
21#include <system/io/seekorigin.h>
22#include <system/io/file_access.h>
23#include <system/enum_helpers.h>
24#include <system/array.h>
25
26namespace System {
27
28namespace IO {
29
30/*
31 * This class is used to access a contiguous block of memory, likely outside
32 * the GC heap (or pinned in place in the GC heap, but a MemoryStream may
33 * make more sense in those cases). It's great if you have a pointer and
34 * a length for a section of memory mapped in by someone else and you don't
35 * want to copy this into the GC heap. UnmanagedMemoryStream assumes these
36 * two things:
37 *
38 * 1) All the memory in the specified block is readable or writable,
39 * depending on the values you pass to the constructor.
40 * 2) The lifetime of the block of memory is at least as long as the lifetime
41 * of the UnmanagedMemoryStream.
42 * 3) You clean up the memory when appropriate. The UnmanagedMemoryStream
43 * currently will do NOTHING to free this memory.
44 * 4) All calls to Write and WriteByte may not be threadsafe currently.
45 *
46 * It may become necessary to add in some sort of
47 * DeallocationMode enum, specifying whether we unmap a section of memory,
48 * call free, run a user-provided delegate to free the memory, etc etc.
49 * We'll suggest user write a subclass of UnmanagedMemoryStream that uses
50 * a SafeHandle subclass to hold onto the memory.
51 * Check for problems when using this in the negative parts of a
52 * process's address space. We may need to use unsigned longs internally
53 * and change the overflow detection logic.
54 *
55 * -----SECURITY MODEL AND SILVERLIGHT-----
56 * A few key notes about exposing UMS in silverlight:
57 * 1. No ctors are exposed to transparent code. This version of UMS only
58 * supports byte* (not SafeBuffer). Therefore, framework code can create
59 * a UMS and hand it to transparent code. Transparent code can use most
60 * operations on a UMS, but not operations that directly expose a
61 * pointer.
62 *
63 * 2. Scope of "unsafe" and non-CLS compliant operations reduced: The
64 * Whidbey version of this class has CLSCompliant(false) at the class
65 * level and unsafe modifiers at the method level. These were reduced to
66 * only where the unsafe operation is performed -- i.e. immediately
67 * around the pointer manipulation. Note that this brings UMS in line
68 * with recent changes in pu/clr to support SafeBuffer.
69 *
70 * 3. Currently, the only caller that creates a UMS is ResourceManager,
71 * which creates read-only UMSs, and therefore operations that can
72 * change the length will throw because write isn't supported. A
73 * conservative option would be to formalize the concept that _only_
74 * read-only UMSs can be creates, and enforce this by making WriteX and
75 * SetLength SecurityCritical. However, this is a violation of
76 * security inheritance rules, so we must keep these safe. The
77 * following notes make this acceptable for future use.
78 * a. a race condition in WriteX that could have allowed a thread to
79 * read from unzeroed memory was fixed
80 * b. memory region cannot be expanded beyond _capacity; in other
81 * words, a UMS creator is saying a writeable UMS is safe to
82 * write to anywhere in the memory range up to _capacity, specified
83 * in the ctor. Even if the caller doesn't specify a capacity, then
84 * length is used as the capacity.
85 */
86
91class ASPOSECPP_SHARED_CLASS UnmanagedMemoryStream : public Stream
92{
96 typedef Stream BaseType;
97
98 ASPOSECPP_SHARED_RTTI_INFO_DECL();
99
100public:
103 virtual ASPOSECPP_SHARED_API bool get_CanRead() const override;
106 virtual ASPOSECPP_SHARED_API bool get_CanSeek() const override;
109 virtual ASPOSECPP_SHARED_API bool get_CanWrite() const override;
111 virtual ASPOSECPP_SHARED_API int64_t get_Length() const override;
113 virtual ASPOSECPP_SHARED_API int64_t get_Capacity() const;
115 virtual ASPOSECPP_SHARED_API int64_t get_Position() const override;
118 virtual ASPOSECPP_SHARED_API void set_Position(int64_t value) override;
120 ASPOSECPP_SHARED_API uint8_t* get_PositionPointer();
122 ASPOSECPP_SHARED_API void set_PositionPointer(uint8_t* value);
123
127 ASPOSECPP_SHARED_API UnmanagedMemoryStream(uint8_t* pointer, int64_t length);
133 ASPOSECPP_SHARED_API UnmanagedMemoryStream(uint8_t* pointer, int64_t length, int64_t capacity, FileAccess access);
134
136 virtual ASPOSECPP_SHARED_API void Flush() override;
142 virtual ASPOSECPP_SHARED_API int32_t Read(const ArrayPtr<uint8_t>& buffer, int32_t offset, int32_t count) override;
147 virtual ASPOSECPP_SHARED_API int64_t Seek(int64_t offset, SeekOrigin loc) override;
150 virtual ASPOSECPP_SHARED_API void SetLength(int64_t value) override;
153 virtual ASPOSECPP_SHARED_API void Write(const ArrayPtr<uint8_t>& buffer, int32_t offset, int32_t count) override;
159 virtual ASPOSECPP_SHARED_API int32_t Read(const System::Details::ArrayView<uint8_t>& buffer, int32_t offset, int32_t count) override;
162 virtual ASPOSECPP_SHARED_API void Write(const System::Details::ArrayView<uint8_t>& buffer, int32_t offset, int32_t count) override;
163
164protected:
168 ASPOSECPP_SHARED_API uint8_t* get_Pointer();
169
171 ASPOSECPP_SHARED_API UnmanagedMemoryStream();
172
179 ASPOSECPP_SHARED_API UnmanagedMemoryStream(uint8_t* pointer, int64_t length, int64_t capacity, FileAccess access, bool skipSecurityCheck);
180
186 ASPOSECPP_SHARED_API void Initialize(uint8_t* pointer, int64_t length, int64_t capacity, FileAccess access);
193 ASPOSECPP_SHARED_API void Initialize(uint8_t* pointer, int64_t length, int64_t capacity, FileAccess access, bool skipSecurityCheck);
196 virtual ASPOSECPP_SHARED_API void Dispose(bool disposing) override;
203 static ASPOSECPP_SHARED_API void Memcpy(const System::Details::ArrayView<uint8_t>& dest, int destIndex, uint8_t* src, int srcIndex, int len);
204
205private:
207 static const int64_t UnmanagedMemStreamMaxLength;
208 //System::SharedPtr<Runtime::InteropServices::SafeBuffer> _buffer;
210 uint8_t * _mem = nullptr;
212 int64_t _length;
214 int64_t _capacity;
216 int64_t _position;
218 int64_t _offset;
220 FileAccess _access;
221 //System::SharedPtr<Threading::Tasks::Task<int>> _lastReadTask;
222
223};
224
225} // namespace IO
226} // namespace System
227
228#endif // _csharptest_UnmanagedMemoryStream_h_
229
A base class for a variety of stream implementations. Objects of this class should only be allocated ...
Definition: stream.h:24
Provides access to unmanaged memory. Objects of this class should only be allocated using System::Mak...
Definition: unmanaged_memory_stream.h:92
uint8_t * get_Pointer()
NOT IMPLEMENTED.
UnmanagedMemoryStream(uint8_t *pointer, int64_t length)
Constructs a new instance of UnmanagedMemoryStream.
virtual bool get_CanRead() const override
Determines if the stream is readable.
virtual void Write(const ArrayPtr< uint8_t > &buffer, int32_t offset, int32_t count) override
NOT IMPLEMENTED.
uint8_t * get_PositionPointer()
NOT IMPLEMENTED.
virtual bool get_CanSeek() const override
Determines if the stream supports seeking.
virtual int64_t get_Position() const override
Returns the current position of the stream.
virtual int32_t Read(const System::Details::ArrayView< uint8_t > &buffer, int32_t offset, int32_t count) override
Reads the specified number of bytes from the stream and writes them to the specified byte array.
static void Memcpy(const System::Details::ArrayView< uint8_t > &dest, int destIndex, uint8_t *src, int srcIndex, int len)
Copies bytes from the specified source buffer starting to the specified byte array.
void Initialize(uint8_t *pointer, int64_t length, int64_t capacity, FileAccess access, bool skipSecurityCheck)
Initializes a newly created instance of UnmanagedMemoryStream class.
void set_PositionPointer(uint8_t *value)
NOT IMPLEMENTED.
virtual int64_t get_Length() const override
Returns the length of the stream in bytes.
virtual void SetLength(int64_t value) override
NOT IMPLEMENTED.
virtual int64_t Seek(int64_t offset, SeekOrigin loc) override
Sets the position of the stream represented by the current object.
bool _isOpen
Indicates if the stream is in open state.
Definition: unmanaged_memory_stream.h:166
virtual void Flush() override
Does nothing.
virtual void Dispose(bool disposing) override
Releases all resources used by the current object and closes the stream.
virtual int64_t get_Capacity() const
Returns the current capacity of the underlying memory buffer.
virtual bool get_CanWrite() const override
Determines if the stream is writable.
UnmanagedMemoryStream(uint8_t *pointer, int64_t length, int64_t capacity, FileAccess access, bool skipSecurityCheck)
Constructs a new instance of UnmanagedMemoryStream.
virtual int32_t Read(const ArrayPtr< uint8_t > &buffer, int32_t offset, int32_t count) override
Reads the specified number of bytes from the stream and writes them to the specified byte array.
UnmanagedMemoryStream(uint8_t *pointer, int64_t length, int64_t capacity, FileAccess access)
Constructs a new instance of UnmanagedMemoryStream.
virtual void set_Position(int64_t value) override
Sets the stream's position.
virtual void Write(const System::Details::ArrayView< uint8_t > &buffer, int32_t offset, int32_t count) override
NOT IMPLEMENTED.
UnmanagedMemoryStream()
Constructs a new instnace of UnmanagedMemoryStream class.
void Initialize(uint8_t *pointer, int64_t length, int64_t capacity, FileAccess access)
Initializes a newly created instance of UnmanagedMemoryStream class.
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
FileAccess
Specifies the type of access when opening the file.
Definition: file_access.h:11
SeekOrigin
Specifies the reference position in the stream relative to which the position to seek to is specified...
Definition: seekorigin.h:11
Definition: db_command.h:9