blob: ba97dbd94eaeac0955ac157d38142021a58473fc [file] [log] [blame]
Georgios Pinitasbaf174e2017-09-08 19:47:30 +01001/*
Georgios Pinitasdf310362018-11-14 13:16:56 +00002 * Copyright (c) 2017-2018 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#ifndef __ARM_COMPUTE_BLOBMEMORYPOOL_H__
25#define __ARM_COMPUTE_BLOBMEMORYPOOL_H__
26
27#include "arm_compute/runtime/IMemoryPool.h"
28
Georgios Pinitasdf310362018-11-14 13:16:56 +000029#include "arm_compute/runtime/IMemoryRegion.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010030#include "arm_compute/runtime/Types.h"
31
32#include <cstddef>
Georgios Pinitasd9e5b192017-09-19 15:03:20 +010033#include <memory>
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010034#include <vector>
35
36namespace arm_compute
37{
Georgios Pinitas555f1c22018-12-14 17:11:20 +000038// Forward declaration
Georgios Pinitasd9e5b192017-09-19 15:03:20 +010039class IAllocator;
40
Georgios Pinitas555f1c22018-12-14 17:11:20 +000041/** Meta-data information for each blob */
42struct BlobInfo
43{
44 BlobInfo(size_t size_ = 0, size_t alignment_ = 0)
45 : size(size_), alignment(alignment_)
46 {
47 }
48 size_t size; /**< Blob size */
49 size_t alignment; /**< Blob alignment */
50};
51
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010052/** Blob memory pool */
53class BlobMemoryPool : public IMemoryPool
54{
55public:
Georgios Pinitasd9e5b192017-09-19 15:03:20 +010056 /** Default Constructor
57 *
58 * @note allocator should outlive the memory pool
59 *
Georgios Pinitas555f1c22018-12-14 17:11:20 +000060 * @param[in] allocator Backing memory allocator
61 * @param[in] blob_info Configuration information of the blobs to be allocated
Georgios Pinitasd9e5b192017-09-19 15:03:20 +010062 */
Georgios Pinitas555f1c22018-12-14 17:11:20 +000063 BlobMemoryPool(IAllocator *allocator, std::vector<BlobInfo> blob_info);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010064 /** Default Destructor */
65 ~BlobMemoryPool();
66 /** Prevent instances of this class to be copy constructed */
67 BlobMemoryPool(const BlobMemoryPool &) = delete;
68 /** Prevent instances of this class to be copy assigned */
69 BlobMemoryPool &operator=(const BlobMemoryPool &) = delete;
70 /** Allow instances of this class to be move constructed */
71 BlobMemoryPool(BlobMemoryPool &&) = default;
72 /** Allow instances of this class to be move assigned */
73 BlobMemoryPool &operator=(BlobMemoryPool &&) = default;
74
75 // Inherited methods overridden:
76 void acquire(MemoryMappings &handles) override;
77 void release(MemoryMappings &handles) override;
78 MappingType mapping_type() const override;
79 std::unique_ptr<IMemoryPool> duplicate() override;
80
81private:
82 /** Allocates internal blobs
83 *
Georgios Pinitas555f1c22018-12-14 17:11:20 +000084 * @param blob_info Size of each blob
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010085 */
Georgios Pinitas555f1c22018-12-14 17:11:20 +000086 void allocate_blobs(const std::vector<BlobInfo> &blob_info);
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010087 /** Frees blobs **/
88 void free_blobs();
89
90private:
Georgios Pinitas555f1c22018-12-14 17:11:20 +000091 IAllocator *_allocator; /**< Allocator to use for internal allocation */
92 std::vector<std::unique_ptr<IMemoryRegion>> _blobs; /**< Vector holding all the memory blobs */
93 std::vector<BlobInfo> _blob_info; /**< Information of each blob */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010094};
Georgios Pinitas511347a2017-09-18 18:33:07 +010095} // namespace arm_compute
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010096#endif /* __ARM_COMPUTE_BLOBMEMORYPOOL_H__ */