blob: 3e7e86d17119473faec341eae18ae337e06aaa45 [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
7#include "HAL/cpp/Resource.hpp"
8#include "HAL/Errors.hpp"
9#include <cstddef>
10#include "HAL/cpp/priority_mutex.h"
11
12namespace hal {
13
14priority_recursive_mutex Resource::m_createLock;
15
16/**
17 * Allocate storage for a new instance of Resource.
18 * Allocate a bool array of values that will get initialized to indicate that no resources
19 * have been allocated yet. The indicies of the resources are [0 .. elements - 1].
20 */
21Resource::Resource(uint32_t elements) {
22 std::lock_guard<priority_recursive_mutex> sync(m_createLock);
23 m_isAllocated = std::vector<bool>(elements, false);
24}
25
26/**
27 * Factory method to create a Resource allocation-tracker *if* needed.
28 *
29 * @param r -- address of the caller's Resource pointer. If *r == NULL, this
30 * will construct a Resource and make *r point to it. If *r != NULL, i.e.
31 * the caller already has a Resource instance, this won't do anything.
32 * @param elements -- the number of elements for this Resource allocator to
33 * track, that is, it will allocate resource numbers in the range
34 * [0 .. elements - 1].
35 */
36/*static*/ void Resource::CreateResourceObject(Resource **r, uint32_t elements)
37{
38 std::lock_guard<priority_recursive_mutex> sync(m_createLock);
39 if (*r == NULL)
40 {
41 *r = new Resource(elements);
42 }
43}
44
45/**
46 * Allocate a resource.
47 * When a resource is requested, mark it allocated. In this case, a free resource value
48 * within the range is located and returned after it is marked allocated.
49 */
50uint32_t Resource::Allocate(const char *resourceDesc)
51{
52 std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
53 for (uint32_t i=0; i < m_isAllocated.size(); i++)
54 {
55 if (!m_isAllocated[i])
56 {
57 m_isAllocated[i] = true;
58 return i;
59 }
60 }
61 // TODO: wpi_setWPIErrorWithContext(NoAvailableResources, resourceDesc);
62 return ~0ul;
63}
64
65/**
66 * Allocate a specific resource value.
67 * The user requests a specific resource value, i.e. channel number and it is verified
68 * unallocated, then returned.
69 */
70uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc)
71{
72 std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
73 if (index >= m_isAllocated.size())
74 {
75 // TODO: wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc);
76 return ~0ul;
77 }
78 if ( m_isAllocated[index] )
79 {
80 // TODO: wpi_setWPIErrorWithContext(ResourceAlreadyAllocated, resourceDesc);
81 return ~0ul;
82 }
83 m_isAllocated[index] = true;
84 return index;
85}
86
87
88/**
89 * Free an allocated resource.
90 * After a resource is no longer needed, for example a destructor is called for a channel assignment
91 * class, Free will release the resource value so it can be reused somewhere else in the program.
92 */
93void Resource::Free(uint32_t index)
94{
95 std::lock_guard<priority_recursive_mutex> sync(m_allocateLock);
96 if (index == ~0ul) return;
97 if (index >= m_isAllocated.size())
98 {
99 // TODO: wpi_setWPIError(NotAllocated);
100 return;
101 }
102 if (!m_isAllocated[index])
103 {
104 // TODO: wpi_setWPIError(NotAllocated);
105 return;
106 }
107 m_isAllocated[index] = false;
108}
109
110} // namespace hal