blob: ae3199669271d51d12c62c211727a2c45b239cac [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6#pragma once
7
8#include "../Errors.hpp"
9#include "HAL/cpp/priority_mutex.h"
10#include <stdint.h>
11
12#include <vector>
13
14// TODO: Replace this with something appropriate to avoid conflicts with
15// wpilibC++ Resource class (which performs an essentially identical function).
16namespace hal {
17
18/**
19 * The Resource class is a convenient way to track allocated resources.
20 * It tracks them as indicies in the range [0 .. elements - 1].
21 * E.g. the library uses this to track hardware channel allocation.
22 *
23 * The Resource class does not allocate the hardware channels or other
24 * resources; it just tracks which indices were marked in use by
25 * Allocate and not yet freed by Free.
26 */
27class Resource
28{
29public:
30 Resource(const Resource&) = delete;
31 Resource& operator=(const Resource&) = delete;
32 explicit Resource(uint32_t size);
33 virtual ~Resource() = default;
34 static void CreateResourceObject(Resource **r, uint32_t elements);
35 uint32_t Allocate(const char *resourceDesc);
36 uint32_t Allocate(uint32_t index, const char *resourceDesc);
37 void Free(uint32_t index);
38
39private:
40 std::vector<bool> m_isAllocated;
41 priority_recursive_mutex m_allocateLock;
42
43 static priority_recursive_mutex m_createLock;
44};
45
46} // namespace hal