-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResource.cpp
105 lines (94 loc) · 2.47 KB
/
Resource.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include <VxWorks.h>
#include "Resource.h"
#include "Utility.h"
#include "WPIStatus.h"
Resource *Resource::m_resourceList = NULL;
/**
* Allocate storage for a new instance of Resource.
* Allocate a bool array of values that will get initialized to indicate that no resources
* have been allocated yet. The indicies of the resources are 0..size-1.
*/
Resource::Resource(UINT32 elements)
{
m_size = elements;
m_isAllocated = new bool[m_size];
for (UINT32 i=0; i < m_size; i++)
m_isAllocated[i] = false;
m_nextResource = m_resourceList;
m_resourceList = this;
}
/*static*/ void Resource::CreateResourceObject(Resource **r, UINT32 elements)
{
if (*r == NULL)
*r = new Resource(elements);
}
/**
* Delete the allocated array or resources.
* This happens when the module is unloaded (provided it was statically allocated).
*/
Resource::~Resource()
{
delete[] m_isAllocated;
}
/**
* Allocate a resource.
* When a resource is requested, mark it allocated. In this case, a free resource value
* within the range is located and returned after it is marked allocated.
*/
UINT32 Resource::Allocate()
{
for (UINT32 i=0; i < m_size; i++)
{
if ( ! m_isAllocated[i])
{
m_isAllocated[i] = true;
return i;
}
}
wpi_fatal(NoAvailablePorts);
return 0;
}
/**
* Allocate a specific resource value.
* The user requests a specific resource value, i.e. channel number and it is verified
* unallocated, then returned.
*/
UINT32 Resource::Allocate(UINT32 index)
{
if (index >= m_size)
{
wpi_fatal(IndexOutOfRange);
return 0;
}
if ( m_isAllocated[index] )
{
wpi_fatal(ResourceAlreadyAllocated);
return 0;
}
m_isAllocated[index] = true;
return index;
}
/**
* Free an allocated resource.
* After a resource is no longer needed, for example a destructor is called for a channel assignment
* class, Free will release the resource value so it can be reused somewhere else in the program.
*/
void Resource::Free(UINT32 index)
{
if (index >= m_size)
{
wpi_fatal(IndexOutOfRange);
return;
}
if ( ! m_isAllocated[index] )
{
wpi_fatal(NotAllocated);
return;
}
m_isAllocated[index] = false;
}