Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <memory> |
| 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "ErrorBase.h" |
| 17 | #include "HAL/cpp/priority_mutex.h" |
| 18 | |
| 19 | namespace frc { |
| 20 | |
| 21 | /** |
| 22 | * The Resource class is a convenient way to track allocated resources. |
| 23 | * It tracks them as indicies in the range [0 .. elements - 1]. |
| 24 | * E.g. the library uses this to track hardware channel allocation. |
| 25 | * |
| 26 | * The Resource class does not allocate the hardware channels or other |
| 27 | * resources; it just tracks which indices were marked in use by |
| 28 | * Allocate and not yet freed by Free. |
| 29 | */ |
| 30 | class Resource : public ErrorBase { |
| 31 | public: |
| 32 | virtual ~Resource() = default; |
| 33 | |
| 34 | Resource(const Resource&) = delete; |
| 35 | Resource& operator=(const Resource&) = delete; |
| 36 | |
| 37 | static void CreateResourceObject(std::unique_ptr<Resource>& r, |
| 38 | uint32_t elements); |
| 39 | explicit Resource(uint32_t size); |
| 40 | uint32_t Allocate(const std::string& resourceDesc); |
| 41 | uint32_t Allocate(uint32_t index, const std::string& resourceDesc); |
| 42 | void Free(uint32_t index); |
| 43 | |
| 44 | private: |
| 45 | std::vector<bool> m_isAllocated; |
| 46 | priority_recursive_mutex m_allocateLock; |
| 47 | |
| 48 | static priority_recursive_mutex m_createLock; |
| 49 | }; |
| 50 | |
| 51 | } // namespace frc |