Skip to content

Commit

Permalink
Update sahred buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
olpipi committed Sep 20, 2024
1 parent 57a4efa commit 6074ec3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
53 changes: 45 additions & 8 deletions src/core/dev_api/openvino/runtime/shared_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace ov {

/// \brief SharedBuffer class to store pointer to pre-acclocated buffer.
/// \brief SharedBuffer class to store pointer to pre-acclocated buffer. Own the shared object.
template <typename T>
class SharedBuffer : public ov::AlignedBuffer {
public:
Expand All @@ -30,17 +30,54 @@ class SharedBuffer : public ov::AlignedBuffer {


/// \brief SharedStreamBuffer class to store pointer to pre-acclocated buffer and provide streambuf interface.
template <typename T>
class SharedStreamBuffer final : public std::stringbuf {
class SharedStreamBuffer : public std::streambuf {
public:
SharedStreamBuffer(char* data, size_t size, const T& shared_object) : _shared_object(shared_object) {
basic_streambuf::pubsetbuf(data, size);
SharedStreamBuffer(char* data, size_t size) :
m_data(data),
m_size(size),
m_offset(0) {
}

SharedStreamBuffer() = delete;
protected:
std::streamsize xsgetn(char* s, std::streamsize count) override{
auto real_count = std::min<std::streamsize>(m_size - m_offset, + count);
std::memcpy(s, m_data + m_offset, real_count);
m_offset += real_count;
return real_count;
}

private:
T _shared_object;
int_type underflow() override {
return (m_size == m_offset) ? traits_type::eof() : traits_type::to_int_type(*(m_data + m_offset));
}

int_type uflow() override {
return (m_size == m_offset) ? traits_type::eof() : traits_type::to_int_type(*(m_data + m_offset++));
}

std::streamsize showmanyc() override {
return m_size - m_offset;
}

size_t m_size;
char* m_data;
size_t m_offset;
};

/// \brief OwningSharedStreamBuffer is a SharedStreamBuffer which owns its shared object. Can return AlignedBuffer to shared memory
class OwningSharedStreamBuffer : public SharedStreamBuffer {
public:
template<typename T>
OwningSharedStreamBuffer(char* data, size_t size, const T& shared_object)
: SharedStreamBuffer(data, size),
m_alligned_buffer(std::make_shared<SharedBuffer<T>>(data, size, shared_object)) {}

std::shared_ptr<AlignedBuffer> get_aligned_buffer() {
return m_alligned_buffer;
}

protected:
std::shared_ptr<AlignedBuffer> m_alligned_buffer;
};


} // namespace ov
2 changes: 1 addition & 1 deletion src/inference/src/cache_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class FileStorageCacheManager final : public ICacheManager {
if (ov::util::file_exists(blobFileName)) {
if (enable_mmap) {
auto mmap = ov::load_mmap_object(blobFileName);
SharedStreamBuffer<std::shared_ptr<MappedMemory>> buf(mmap->data(), mmap->size(), mmap);
OwningSharedStreamBuffer buf(mmap->data(), mmap->size(), mmap);
std::istream stream(&buf);
reader(stream);
} else {
Expand Down

0 comments on commit 6074ec3

Please sign in to comment.