tvm
Loading...
Searching...
No Matches
bytes_io.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
27#ifndef TVM_SUPPORT_BYTES_IO_H_
28#define TVM_SUPPORT_BYTES_IO_H_
29
30#include <tvm/ffi/error.h>
31#include <tvm/ffi/string.h>
32#include <tvm/support/io.h>
33
34#include <algorithm>
35#include <cstring>
36#include <string>
37
38namespace tvm {
39namespace support {
40
44class BytesInStream : public Stream {
45 public:
47 using Stream::Read;
48 using Stream::Write;
50
51 BytesInStream(const void* data, size_t size)
52 : data_(reinterpret_cast<const char*>(data)), size_(size), pos_(0) {}
53
54 explicit BytesInStream(const ffi::Bytes& bytes)
55 : data_(bytes.data()), size_(bytes.size()), pos_(0) {}
56
57 explicit BytesInStream(const std::string& str) : data_(str.data()), size_(str.size()), pos_(0) {}
58
59 size_t Read(void* ptr, size_t size) override {
60 size_t nread = std::min(size_ - pos_, size);
61 if (nread != 0) std::memcpy(ptr, data_ + pos_, nread);
62 pos_ += nread;
63 return nread;
64 }
65
66 size_t Write(const void*, size_t) override {
67 TVM_FFI_THROW(InternalError) << "write to input stream";
68 return 0;
69 }
70
71 private:
72 const char* data_;
73 size_t size_;
74 size_t pos_;
75};
76
80class BytesOutStream : public Stream {
81 public:
83 using Stream::Read;
84 using Stream::Write;
86
87 explicit BytesOutStream(std::string* buffer) : buffer_(buffer) {}
88
89 size_t Write(const void* ptr, size_t size) override {
90 if (size == 0) return 0;
91 buffer_->append(reinterpret_cast<const char*>(ptr), size);
92 return size;
93 }
94
95 size_t Read(void*, size_t) override {
96 TVM_FFI_THROW(InternalError) << "read from output stream";
97 return 0;
98 }
99
100 private:
101 std::string* buffer_;
102};
103
104} // namespace support
105} // namespace tvm
106
107#endif // TVM_SUPPORT_BYTES_IO_H_
Read-only stream over a fixed memory region.
Definition bytes_io.h:44
BytesInStream(const ffi::Bytes &bytes)
Definition bytes_io.h:54
size_t Read(void *ptr, size_t size) override
Read raw bytes from the stream.
Definition bytes_io.h:59
BytesInStream(const void *data, size_t size)
Definition bytes_io.h:51
BytesInStream(const std::string &str)
Definition bytes_io.h:57
size_t Write(const void *, size_t) override
Write raw bytes to the stream.
Definition bytes_io.h:66
Write-only stream that appends to a caller-provided std::string.
Definition bytes_io.h:80
size_t Write(const void *ptr, size_t size) override
Write raw bytes to the stream.
Definition bytes_io.h:89
BytesOutStream(std::string *buffer)
Definition bytes_io.h:87
size_t Read(void *, size_t) override
Read raw bytes from the stream.
Definition bytes_io.h:95
Abstract binary stream for serialization.
Definition io.h:57
virtual size_t Read(void *ptr, size_t size)=0
Read raw bytes from the stream.
virtual size_t Write(const void *ptr, size_t size)=0
Write raw bytes to the stream.
Binary stream I/O interface.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41