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