forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_ptr_data_loader.h
More file actions
67 lines (58 loc) · 1.88 KB
/
shared_ptr_data_loader.h
File metadata and controls
67 lines (58 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <executorch/runtime/core/data_loader.h>
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/result.h>
#include <executorch/runtime/platform/log.h>
#include <memory>
namespace executorch {
namespace extension {
/**
* A DataLoader that wraps a pre-allocated buffer and shares ownership to it.
* The FreeableBuffers that it returns do not actually free any data.
*
* This can be used to wrap data that was allocated elsewhere.
*/
class SharedPtrDataLoader final : public executorch::runtime::DataLoader {
public:
SharedPtrDataLoader(std::shared_ptr<void> data, size_t size)
: data_(data), size_(size) {}
ET_NODISCARD
executorch::runtime::Result<executorch::runtime::FreeableBuffer> load(
size_t offset,
size_t size,
ET_UNUSED const DataLoader::SegmentInfo& segment_info) const override {
ET_CHECK_OR_RETURN_ERROR(
offset + size <= size_,
InvalidArgument,
"offset %zu + size %zu > size_ %zu",
offset,
size,
size_);
return executorch::runtime::FreeableBuffer(
static_cast<uint8_t*>(data_.get()) + offset, size, /*free_fn=*/nullptr);
}
ET_NODISCARD executorch::runtime::Result<size_t> size() const override {
return size_;
}
private:
const std::shared_ptr<void> data_;
const size_t size_;
};
} // namespace extension
} // namespace executorch
namespace torch {
namespace executor {
namespace util {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::extension::SharedPtrDataLoader;
} // namespace util
} // namespace executor
} // namespace torch