ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Data.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __ONERT_IR_DATA_H__
18#define __ONERT_IR_DATA_H__
19
20#include <algorithm>
21#include <sys/mman.h>
22
23namespace onert
24{
25namespace ir
26{
27
28struct Data
29{
30 virtual ~Data() = default;
31
32 virtual size_t size(void) const = 0;
33 virtual const uint8_t *base(void) const = 0;
34};
35
36class CachedData final : public Data
37{
38public:
39 CachedData(const uint8_t *base, size_t size) : _base{new uint8_t[size]}, _size{size}
40 {
41 std::copy(base, base + size, _base);
42 }
43
44public:
45 ~CachedData() { delete[] _base; }
46
47public:
48 size_t size(void) const override { return _size; }
49 const uint8_t *base(void) const override { return _base; }
50
51private:
52 uint8_t *_base;
53 size_t _size;
54};
55
56class ExternalData : public Data
57{
58public:
59 ExternalData(const uint8_t *base, size_t size) : _base{base}, _size{size}
60 {
61 // DO NOTHING
62 }
63
64public:
65 size_t size(void) const override { return _size; }
66 const uint8_t *base(void) const override { return _base; }
67
68private:
69 const uint8_t *_base;
70 const size_t _size;
71};
72
73class MMapedData final : public ExternalData
74{
75public:
76 MMapedData(int fd, const std::ptrdiff_t mmap_offset, const size_t mmap_size,
77 const std::ptrdiff_t data_offset, const size_t data_size)
78 : ExternalData(nullptr, data_size),
79 _mmap_base(
80 static_cast<uint8_t *>(mmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, mmap_offset))),
81 _mmap_size(mmap_size), _offset(data_offset - mmap_offset)
82 {
83 // DO NOTHING
84 }
85
86public:
88 {
89 if (_mmap_size > 0)
90 {
91 munmap(const_cast<uint8_t *>(_mmap_base), _mmap_size);
92 }
93 }
94
95public:
96 const uint8_t *base(void) const override { return _mmap_base + _offset; }
97
98private:
99 const uint8_t *_mmap_base;
100 size_t _mmap_size;
101 std::ptrdiff_t _offset;
102};
103
104} // namespace ir
105} // namespace onert
106
107#endif // __ONERT_IR_DATA_H__
CachedData(const uint8_t *base, size_t size)
Definition Data.h:39
size_t size(void) const override
Definition Data.h:48
const uint8_t * base(void) const override
Definition Data.h:49
const uint8_t * base(void) const override
Definition Data.h:66
size_t size(void) const override
Definition Data.h:65
ExternalData(const uint8_t *base, size_t size)
Definition Data.h:59
MMapedData(int fd, const std::ptrdiff_t mmap_offset, const size_t mmap_size, const std::ptrdiff_t data_offset, const size_t data_size)
Definition Data.h:76
const uint8_t * base(void) const override
Definition Data.h:96
virtual size_t size(void) const =0
virtual const uint8_t * base(void) const =0
virtual ~Data()=default