2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* 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 "Resource.h"
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include "ErrorBase.h"
|
|
|
|
|
|
|
|
|
|
ReentrantSemaphore Resource::m_createLock;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allocate storage for a new instance of Resource.
|
2015-06-25 15:07:55 -04:00
|
|
|
* 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 .. elements -
|
|
|
|
|
* 1].
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Resource::Resource(uint32_t elements) {
|
|
|
|
|
Synchronized sync(m_createLock);
|
|
|
|
|
m_size = elements;
|
|
|
|
|
m_isAllocated = new bool[m_size];
|
|
|
|
|
for (uint32_t i = 0; i < m_size; i++) {
|
|
|
|
|
m_isAllocated[i] = false;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Factory method to create a Resource allocation-tracker *if* needed.
|
|
|
|
|
*
|
|
|
|
|
* @param r -- address of the caller's Resource pointer. If *r == NULL, this
|
|
|
|
|
* will construct a Resource and make *r point to it. If *r != NULL, i.e.
|
|
|
|
|
* the caller already has a Resource instance, this won't do anything.
|
|
|
|
|
* @param elements -- the number of elements for this Resource allocator to
|
|
|
|
|
* track, that is, it will allocate resource numbers in the range
|
|
|
|
|
* [0 .. elements - 1].
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/*static*/ void Resource::CreateResourceObject(Resource **r,
|
|
|
|
|
uint32_t elements) {
|
|
|
|
|
Synchronized sync(m_createLock);
|
|
|
|
|
if (*r == NULL) {
|
|
|
|
|
*r = new Resource(elements);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete the allocated array or resources.
|
2015-06-25 15:07:55 -04:00
|
|
|
* This happens when the module is unloaded (provided it was statically
|
|
|
|
|
* allocated).
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Resource::~Resource() { delete[] m_isAllocated; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allocate a resource.
|
2015-06-25 15:07:55 -04:00
|
|
|
* When a resource is requested, mark it allocated. In this case, a free
|
|
|
|
|
* resource value
|
2013-12-15 18:30:16 -05:00
|
|
|
* within the range is located and returned after it is marked allocated.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
uint32_t Resource::Allocate(const char *resourceDesc) {
|
|
|
|
|
Synchronized sync(m_allocateLock);
|
|
|
|
|
for (uint32_t i = 0; i < m_size; i++) {
|
|
|
|
|
if (!m_isAllocated[i]) {
|
|
|
|
|
m_isAllocated[i] = true;
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
wpi_setWPIErrorWithContext(NoAvailableResources, resourceDesc);
|
|
|
|
|
return ~0ul;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allocate a specific resource value.
|
2015-06-25 15:07:55 -04:00
|
|
|
* The user requests a specific resource value, i.e. channel number and it is
|
|
|
|
|
* verified
|
2013-12-15 18:30:16 -05:00
|
|
|
* unallocated, then returned.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc) {
|
|
|
|
|
Synchronized sync(m_allocateLock);
|
|
|
|
|
if (index >= m_size) {
|
|
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc);
|
|
|
|
|
return ~0ul;
|
|
|
|
|
}
|
|
|
|
|
if (m_isAllocated[index]) {
|
|
|
|
|
wpi_setWPIErrorWithContext(ResourceAlreadyAllocated, resourceDesc);
|
|
|
|
|
return ~0ul;
|
|
|
|
|
}
|
|
|
|
|
m_isAllocated[index] = true;
|
|
|
|
|
return index;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Free an allocated resource.
|
2015-06-25 15:07:55 -04:00
|
|
|
* 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.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Resource::Free(uint32_t index) {
|
|
|
|
|
Synchronized sync(m_allocateLock);
|
|
|
|
|
if (index == ~0ul) return;
|
|
|
|
|
if (index >= m_size) {
|
|
|
|
|
wpi_setWPIError(NotAllocated);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!m_isAllocated[index]) {
|
|
|
|
|
wpi_setWPIError(NotAllocated);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_isAllocated[index] = false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|