blob: a2f63ef52b936e81453e3d9fa7b35ecd72003e1e [file] [log] [blame]
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01001/*
Georgios Pinitas6061a892021-02-23 00:21:04 +00002 * Copyright (c) 2017-2021 Arm Limited.
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01003 *
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#include "arm_compute/runtime/BlobMemoryPool.h"
25
26#include "arm_compute/core/Error.h"
Georgios Pinitasd9e5b192017-09-19 15:03:20 +010027#include "arm_compute/runtime/IAllocator.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010028#include "arm_compute/runtime/IMemoryPool.h"
29#include "arm_compute/runtime/Types.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010030
31#include <vector>
32
33using namespace arm_compute;
34
Georgios Pinitas555f1c22018-12-14 17:11:20 +000035BlobMemoryPool::BlobMemoryPool(IAllocator *allocator, std::vector<BlobInfo> blob_info)
36 : _allocator(allocator), _blobs(), _blob_info(std::move(blob_info))
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010037{
38 ARM_COMPUTE_ERROR_ON(!allocator);
Georgios Pinitas555f1c22018-12-14 17:11:20 +000039 allocate_blobs(_blob_info);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010040}
41
42BlobMemoryPool::~BlobMemoryPool()
43{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010044 free_blobs();
45}
46
47void BlobMemoryPool::acquire(MemoryMappings &handles)
48{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010049 // Set memory to handlers
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010050 for (auto &handle : handles)
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010051 {
52 ARM_COMPUTE_ERROR_ON(handle.first == nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +000053 handle.first->set_region(_blobs[handle.second].get());
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010054 }
55}
56
57void BlobMemoryPool::release(MemoryMappings &handles)
58{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 for (auto &handle : handles)
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010060 {
61 ARM_COMPUTE_ERROR_ON(handle.first == nullptr);
Georgios Pinitasdf310362018-11-14 13:16:56 +000062 handle.first->set_region(nullptr);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010063 }
64}
65
66MappingType BlobMemoryPool::mapping_type() const
67{
68 return MappingType::BLOBS;
69}
70
71std::unique_ptr<IMemoryPool> BlobMemoryPool::duplicate()
72{
73 ARM_COMPUTE_ERROR_ON(!_allocator);
Georgios Pinitas40f51a62020-11-21 03:04:18 +000074 return std::make_unique<BlobMemoryPool>(_allocator, _blob_info);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010075}
76
Georgios Pinitas555f1c22018-12-14 17:11:20 +000077void BlobMemoryPool::allocate_blobs(const std::vector<BlobInfo> &blob_info)
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010078{
79 ARM_COMPUTE_ERROR_ON(!_allocator);
80
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 for (const auto &bi : blob_info)
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010082 {
Georgios Pinitas555f1c22018-12-14 17:11:20 +000083 _blobs.push_back(_allocator->make_region(bi.size, bi.alignment));
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010084 }
85}
86
87void BlobMemoryPool::free_blobs()
88{
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010089 _blobs.clear();
Matthew Bentham758b5ba2020-03-05 23:37:48 +000090}