Now you can download a copy of these docs so you can use them offline! Download now
Resource.cpp
1 /*----------------------------------------------------------------------------*/
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 "Resource.h"
8 #include "WPIErrors.h"
9 #include "ErrorBase.h"
10 
11 ReentrantSemaphore Resource::m_createLock;
12 
18 Resource::Resource(uint32_t elements)
19 {
20  Synchronized sync(m_createLock);
21  m_size = elements;
22  m_isAllocated = new bool[m_size];
23  for (uint32_t i=0; i < m_size; i++)
24  {
25  m_isAllocated[i] = false;
26  }
27 }
28 
39 /*static*/ void Resource::CreateResourceObject(Resource **r, uint32_t elements)
40 {
41  Synchronized sync(m_createLock);
42  if (*r == NULL)
43  {
44  *r = new Resource(elements);
45  }
46 }
47 
53 {
54  delete[] m_isAllocated;
55 }
56 
62 uint32_t Resource::Allocate(const char *resourceDesc)
63 {
64  Synchronized sync(m_allocateLock);
65  for (uint32_t i=0; i < m_size; i++)
66  {
67  if (!m_isAllocated[i])
68  {
69  m_isAllocated[i] = true;
70  return i;
71  }
72  }
73  wpi_setWPIErrorWithContext(NoAvailableResources, resourceDesc);
74  return ~0ul;
75 }
76 
82 uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc)
83 {
84  Synchronized sync(m_allocateLock);
85  if (index >= m_size)
86  {
87  wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc);
88  return ~0ul;
89  }
90  if ( m_isAllocated[index] )
91  {
92  wpi_setWPIErrorWithContext(ResourceAlreadyAllocated, resourceDesc);
93  return ~0ul;
94  }
95  m_isAllocated[index] = true;
96  return index;
97 }
98 
99 
105 void Resource::Free(uint32_t index)
106 {
107  Synchronized sync(m_allocateLock);
108  if (index == ~0ul) return;
109  if (index >= m_size)
110  {
111  wpi_setWPIError(NotAllocated);
112  return;
113  }
114  if (!m_isAllocated[index])
115  {
116  wpi_setWPIError(NotAllocated);
117  return;
118  }
119  m_isAllocated[index] = false;
120 }
void Free(uint32_t index)
Definition: Resource.cpp:105
uint32_t Allocate(const char *resourceDesc)
Definition: Resource.cpp:62
virtual ~Resource()
Definition: Resource.cpp:52
static void CreateResourceObject(Resource **r, uint32_t elements)
Definition: Resource.cpp:39

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6