8
8
9
9
namespace ov {
10
10
11
- // / \brief SharedBuffer class to store pointer to pre-allocated buffer.
11
+ // / \brief SharedBuffer class to store pointer to pre-allocated buffer. Own the shared object.
12
12
template <typename T>
13
13
class SharedBuffer : public ov ::AlignedBuffer {
14
14
public:
@@ -28,4 +28,60 @@ class SharedBuffer : public ov::AlignedBuffer {
28
28
T _shared_object;
29
29
};
30
30
31
+ // / \brief SharedStreamBuffer class to store pointer to pre-acclocated buffer and provide streambuf interface.
32
+ // / Can return ptr to shared memory and its size
33
+ class SharedStreamBuffer : public std ::streambuf {
34
+ public:
35
+ SharedStreamBuffer (char * data, size_t size) : m_data(data), m_size(size), m_offset(0 ) {}
36
+
37
+ protected:
38
+ // override std::streambuf methods
39
+ std::streamsize xsgetn (char * s, std::streamsize count) override {
40
+ auto real_count = std::min<std::streamsize>(m_size - m_offset, count);
41
+ std::memcpy (s, m_data + m_offset, real_count);
42
+ m_offset += real_count;
43
+ return real_count;
44
+ }
45
+
46
+ int_type underflow () override {
47
+ return (m_size == m_offset) ? traits_type::eof () : traits_type::to_int_type (*(m_data + m_offset));
48
+ }
49
+
50
+ int_type uflow () override {
51
+ return (m_size == m_offset) ? traits_type::eof () : traits_type::to_int_type (*(m_data + m_offset++));
52
+ }
53
+
54
+ std::streamsize showmanyc () override {
55
+ return m_size - m_offset;
56
+ }
57
+
58
+ pos_type seekoff (off_type off,
59
+ std::ios_base::seekdir dir,
60
+ std::ios_base::openmode which = std::ios_base::in) override {
61
+ if (dir != std::ios_base::cur || which != std::ios_base::in) {
62
+ return pos_type (off_type (-1 ));
63
+ }
64
+ m_offset += off;
65
+ return pos_type (m_offset);
66
+ }
67
+
68
+ char * m_data;
69
+ size_t m_size;
70
+ size_t m_offset;
71
+ };
72
+
73
+ // / \brief OwningSharedStreamBuffer is a SharedStreamBuffer which owns its shared object.
74
+ class OwningSharedStreamBuffer : public SharedStreamBuffer {
75
+ public:
76
+ OwningSharedStreamBuffer (std::shared_ptr<ov::AlignedBuffer> buffer)
77
+ : SharedStreamBuffer(static_cast <char *>(buffer->get_ptr ()), buffer->size()),
78
+ m_shared_obj(buffer) {}
79
+
80
+ std::shared_ptr<ov::AlignedBuffer> get_buffer () {
81
+ return m_shared_obj;
82
+ }
83
+
84
+ protected:
85
+ std::shared_ptr<ov::AlignedBuffer> m_shared_obj;
86
+ };
31
87
} // namespace ov
0 commit comments