blob: ec43f47fe625d8297f13ab08f623c8f2c92d745d [file] [log] [blame]
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_BLOBLIFETIMEMANAGER_H__
25#define __ARM_COMPUTE_BLOBLIFETIMEMANAGER_H__
26
27#include "arm_compute/runtime/ILifetimeManager.h"
28
29#include "arm_compute/runtime/IMemoryGroup.h"
30#include "arm_compute/runtime/Types.h"
31
32#include <cstddef>
33#include <map>
34#include <vector>
35
36namespace arm_compute
37{
38class IMemoryGroup;
39
40/** Class that tracks the lifetime of registered tensors and calculates the systems memory requirements in terms of blobs */
41class BlobLifetimeManager : public ILifetimeManager
42{
43public:
44 /** Constructor */
45 BlobLifetimeManager();
46 /** Prevent instances of this class to be copy constructed */
47 BlobLifetimeManager(const BlobLifetimeManager &) = delete;
48 /** Prevent instances of this class to be copied */
49 BlobLifetimeManager &operator=(const BlobLifetimeManager &) = delete;
50 /** Allow instances of this class to be move constructed */
51 BlobLifetimeManager(BlobLifetimeManager &&) = default;
52 /** Allow instances of this class to be moved */
53 BlobLifetimeManager &operator=(BlobLifetimeManager &&) = default;
54
55 // Inherited methods overridden:
56 void register_group(IMemoryGroup *group) override;
57 void start_lifetime(void *obj) override;
58 void end_lifetime(void *obj, void **handle, size_t size) override;
59 std::unique_ptr<IMemoryPool> create_pool(IAllocator *allocator) override;
60 bool are_all_finalized() const override;
61 MappingType mapping_type() const override;
62
63private:
64 /** Update blobs and mappings */
65 void update_blobs_and_mappings();
66
67private:
68 /** Element struct */
69 struct Element
70 {
71 Element(void *id_ = nullptr, void **handle_ = nullptr, size_t size_ = 0, bool status_ = false)
72 : id(id_), handle(handle_), size(size_), status(status_)
73 {
74 }
75 void *id; /**< Element id */
76 void **handle; /**< Element's memory handle */
77 size_t size; /**< Element's size */
78 bool status; /**< Lifetime status */
79 };
80
81 IMemoryGroup *_active_group; /**< Active group */
82 std::vector<Element> _active_elements; /**< A map that contains the active elements */
83 std::map<IMemoryGroup *, std::vector<Element>> _finalized_groups; /**< A map that contains the finalized groups */
84 std::vector<size_t> _blobs;
85};
86} // arm_compute
87#endif /* __ARM_COMPUTE_BLOBLIFETIMEMANAGER_H__ */