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