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:
46 using Stream::Read;
47 using Stream::Write;
48
49 BytesInStream(const void* data, size_t size)
50 : data_(reinterpret_cast<const char*>(data)), size_(size), pos_(0) {}
51
52 explicit BytesInStream(const ffi::Bytes& bytes)
53 : data_(bytes.data()), size_(bytes.size()), pos_(0) {}
54
55 explicit BytesInStream(const std::string& str) : data_(str.data()), size_(str.size()), pos_(0) {}
56
57 size_t Read(void* ptr, size_t size) override {
58 size_t nread = std::min(size_ - pos_, size);
59 if (nread != 0) std::memcpy(ptr, data_ + pos_, nread);
60 pos_ += nread;
61 return nread;
62 }
63
64 size_t Write(const void*, size_t) override {
65 TVM_FFI_THROW(InternalError) << "write to input stream";
66 return 0;
67 }
68
69 private:
70 const char* data_;
71 size_t size_;
72 size_t pos_;
73};
74
78class BytesOutStream : public Stream {
79 public:
80 using Stream::Read;
81 using Stream::Write;
82
83 explicit BytesOutStream(std::string* buffer) : buffer_(buffer) {}
84
85 size_t Write(const void* ptr, size_t size) override {
86 if (size == 0) return 0;
87 buffer_->append(reinterpret_cast<const char*>(ptr), size);
88 return size;
89 }
90
91 size_t Read(void*, size_t) override {
92 TVM_FFI_THROW(InternalError) << "read from output stream";
93 return 0;
94 }
95
96 private:
97 std::string* buffer_;
98};
99
100} // namespace support
101} // namespace tvm
102
103#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:52
size_t Read(void *ptr, size_t size) override
Read raw bytes from the stream.
Definition bytes_io.h:57
BytesInStream(const void *data, size_t size)
Definition bytes_io.h:49
BytesInStream(const std::string &str)
Definition bytes_io.h:55
size_t Write(const void *, size_t) override
Write raw bytes to the stream.
Definition bytes_io.h:64
Write-only stream that appends to a caller-provided std::string.
Definition bytes_io.h:78
size_t Write(const void *ptr, size_t size) override
Write raw bytes to the stream.
Definition bytes_io.h:85
BytesOutStream(std::string *buffer)
Definition bytes_io.h:83
size_t Read(void *, size_t) override
Read raw bytes from the stream.
Definition bytes_io.h:91
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 attr.h:23