Medial Code Documentation
Loading...
Searching...
No Matches
memory_io.h
Go to the documentation of this file.
1
6#ifndef DMLC_MEMORY_IO_H_
7#define DMLC_MEMORY_IO_H_
8
9#include <cstring>
10#include <string>
11#include <algorithm>
12#include "./base.h"
13#include "./io.h"
14#include "./logging.h"
15
16namespace dmlc {
22 public:
28 MemoryFixedSizeStream(void *p_buffer, size_t buffer_size)
29 : p_buffer_(reinterpret_cast<char*>(p_buffer)),
30 buffer_size_(buffer_size) {
31 curr_ptr_ = 0;
32 }
33 virtual size_t Read(void *ptr, size_t size) {
34 CHECK(curr_ptr_ + size <= buffer_size_);
35 size_t nread = std::min(buffer_size_ - curr_ptr_, size);
36 if (nread != 0) std::memcpy(ptr, p_buffer_ + curr_ptr_, nread);
37 curr_ptr_ += nread;
38 return nread;
39 }
40 virtual void Write(const void *ptr, size_t size) {
41 if (size == 0) return;
42 CHECK(curr_ptr_ + size <= buffer_size_);
43 std::memcpy(p_buffer_ + curr_ptr_, ptr, size);
44 curr_ptr_ += size;
45 }
46 virtual void Seek(size_t pos) {
47 curr_ptr_ = static_cast<size_t>(pos);
48 }
49 virtual size_t Tell(void) {
50 return curr_ptr_;
51 }
52
53 private:
55 char *p_buffer_;
57 size_t buffer_size_;
59 size_t curr_ptr_;
60}; // class MemoryFixedSizeStream
61
67 public:
72 explicit MemoryStringStream(std::string *p_buffer)
73 : p_buffer_(p_buffer) {
74 curr_ptr_ = 0;
75 }
76 virtual size_t Read(void *ptr, size_t size) {
77 CHECK(curr_ptr_ <= p_buffer_->length());
78 size_t nread = std::min(p_buffer_->length() - curr_ptr_, size);
79 if (nread != 0) std::memcpy(ptr, &(*p_buffer_)[0] + curr_ptr_, nread);
80 curr_ptr_ += nread;
81 return nread;
82 }
83 virtual void Write(const void *ptr, size_t size) {
84 if (size == 0) return;
85 if (curr_ptr_ + size > p_buffer_->length()) {
86 p_buffer_->resize(curr_ptr_+size);
87 }
88 std::memcpy(&(*p_buffer_)[0] + curr_ptr_, ptr, size);
89 curr_ptr_ += size;
90 }
91 virtual void Seek(size_t pos) {
92 curr_ptr_ = static_cast<size_t>(pos);
93 }
94 virtual size_t Tell(void) {
95 return curr_ptr_;
96 }
97
98 private:
100 std::string *p_buffer_;
102 size_t curr_ptr_;
103}; // class MemoryStringStream
104} // namespace dmlc
105#endif // DMLC_MEMORY_IO_H_
interface of i/o stream that support seek
Definition io.h:109
defines console logging options for xgboost. Use to enforce unified print behavior.
namespace for dmlc
Definition array_view.h:12
Macros common to all headers.
Copyright 2014-2023, XGBoost Contributors.
A Stream that operates on fixed region of memory This class allows us to read/write from/to a fixed m...
Definition memory_io.h:21
virtual size_t Read(void *ptr, size_t size)
reads data from a stream
Definition memory_io.h:33
virtual void Seek(size_t pos)
seek to certain position of the file
Definition memory_io.h:46
MemoryFixedSizeStream(void *p_buffer, size_t buffer_size)
constructor
Definition memory_io.h:28
virtual void Write(const void *ptr, size_t size)
writes data to a stream
Definition memory_io.h:40
virtual size_t Tell(void)
tell the position of the stream
Definition memory_io.h:49
A in memory stream that is backed by std::string. This class allows us to read/write from/to a std::s...
Definition memory_io.h:66
virtual void Seek(size_t pos)
seek to certain position of the file
Definition memory_io.h:91
virtual size_t Read(void *ptr, size_t size)
reads data from a stream
Definition memory_io.h:76
virtual void Write(const void *ptr, size_t size)
writes data to a stream
Definition memory_io.h:83
virtual size_t Tell(void)
tell the position of the stream
Definition memory_io.h:94
MemoryStringStream(std::string *p_buffer)
constructor
Definition memory_io.h:72