diff --git a/hal/lib/athena/handles/IndexedHandleResource.h b/hal/lib/athena/handles/IndexedHandleResource.h index 504b2a488a..76baa14e2c 100644 --- a/hal/lib/athena/handles/IndexedHandleResource.h +++ b/hal/lib/athena/handles/IndexedHandleResource.h @@ -12,15 +12,13 @@ #include #include +#include "HAL/Errors.h" #include "HAL/Handles.h" #include "HAL/cpp/priority_mutex.h" #include "HandlesInternal.h" namespace hal { -constexpr int32_t IndexedResourceIndexOutOfRange = -1; -constexpr int32_t IndexedResourceNotAllocated = -2; - /** * The IndexedHandleResource class is a way to track handles. This version * allows a limited number of handles that are allocated by index. @@ -41,57 +39,47 @@ class IndexedHandleResource { public: IndexedHandleResource(const IndexedHandleResource&) = delete; IndexedHandleResource operator=(const IndexedHandleResource&) = delete; - IndexedHandleResource(); - THandle Allocate(int16_t index, const TStruct& toSet); - TStruct Get(THandle handle, int32_t* status); + IndexedHandleResource() = default; + THandle Allocate(int16_t index, int32_t* status); + std::shared_ptr Get(THandle handle); void Free(THandle handle); private: - TStruct m_structures[size]; - bool m_allocated[size]; + std::shared_ptr m_structures[size]; priority_mutex m_handleMutexes[size]; }; -template -IndexedHandleResource::IndexedHandleResource() { - // ensure all allocations are initially false. - for (int i = 0; i < size; i++) { - m_allocated[i] = false; - } -} - template THandle IndexedHandleResource::Allocate( - int16_t index, const TStruct& toSet) { + int16_t index, int32_t* status) { // don't aquire the lock if we can fail early. - if (index < 0 || index >= size) return HAL_INVALID_HANDLE; + if (index < 0 || index >= size) { + *status = PARAMETER_OUT_OF_RANGE; + return HAL_INVALID_HANDLE; + } std::lock_guard sync(m_handleMutexes[index]); // check for allocation, otherwise allocate and return a valid handle - if (m_allocated[index]) return HAL_INVALID_HANDLE; - m_allocated[index] = true; - m_structures[index] = toSet; + if (m_structures[index] != nullptr) { + *status = RESOURCE_IS_ALLOCATED; + return HAL_INVALID_HANDLE; + } + m_structures[index] = std::make_shared(); return (THandle)hal::createHandle(index, enumValue); } template -TStruct IndexedHandleResource::Get( - THandle handle, int32_t* status) { +std::shared_ptr +IndexedHandleResource::Get(THandle handle) { // get handle index, and fail early if index out of range or wrong handle int16_t index = getHandleTypedIndex(handle, enumValue); if (index < 0 || index >= size) { - *status = IndexedResourceIndexOutOfRange; - return TStruct(); + return nullptr; } std::lock_guard sync(m_handleMutexes[index]); - // check for already deallocated handle, then return structure - if (!m_allocated[index]) { - *status = IndexedResourceNotAllocated; - return TStruct(); - } + // return structure. Null will propogate correctly, so no need to manually + // check. return m_structures[index]; } @@ -104,6 +92,6 @@ void IndexedHandleResource::Free( if (index < 0 || index >= size) return; // lock and deallocated handle std::lock_guard sync(m_handleMutexes[index]); - m_allocated[index] = false; + m_structures[index].reset(); } } diff --git a/hal/lib/athena/handles/LimitedHandleResource.h b/hal/lib/athena/handles/LimitedHandleResource.h index 4f3d5d42a2..573c31a5dd 100644 --- a/hal/lib/athena/handles/LimitedHandleResource.h +++ b/hal/lib/athena/handles/LimitedHandleResource.h @@ -18,9 +18,6 @@ namespace hal { -constexpr int32_t LimitedResourceIndexOutOfRange = -1; -constexpr int32_t LimitedResourceNotAllocated = -2; - /** * The LimitedHandleResource class is a way to track handles. This version * allows a limited number of handles that are allocated sequentially. @@ -39,42 +36,29 @@ class LimitedHandleResource { public: LimitedHandleResource(const LimitedHandleResource&) = delete; LimitedHandleResource operator=(const LimitedHandleResource&) = delete; - LimitedHandleResource(); - THandle Allocate(const TStruct& toSet); - TStruct Get(THandle handle, int32_t* status); + LimitedHandleResource() = default; + THandle Allocate(); + std::shared_ptr Get(THandle handle); void Free(THandle handle); private: - TStruct m_structures[size]; - bool m_allocated[size]; + std::shared_ptr m_structures[size]; priority_mutex m_handleMutexes[size]; priority_mutex m_allocateMutex; }; template -LimitedHandleResource::LimitedHandleResource() { - // ensure all allocations are initially false. - for (int i = 0; i < size; i++) { - m_allocated[i] = false; - } -} - -template -THandle LimitedHandleResource::Allocate( - const TStruct& toSet) { +THandle LimitedHandleResource::Allocate() { // globally lock to loop through indices std::lock_guard sync(m_allocateMutex); int16_t i; for (i = 0; i < size; i++) { - if (m_allocated[i] == false) { + if (m_structures[i] == nullptr) { // if a false index is found, grab its specific mutex // and allocate it. std::lock_guard sync(m_handleMutexes[i]); - m_allocated[i] = true; - m_structures[i] = toSet; + m_structures[i] = std::make_shared(); return (THandle)createHandle(i, enumValue); } } @@ -83,21 +67,16 @@ THandle LimitedHandleResource::Allocate( template -TStruct LimitedHandleResource::Get( - THandle handle, int32_t* status) { - *status = 0; +std::shared_ptr +LimitedHandleResource::Get(THandle handle) { // get handle index, and fail early if index out of range or wrong handle int16_t index = getHandleTypedIndex(handle, enumValue); if (index < 0 || index >= size) { - *status = LimitedResourceIndexOutOfRange; - return TStruct(); + return nullptr; } std::lock_guard sync(m_handleMutexes[index]); - // check for already deallocated handle, then return structure - if (!m_allocated[index]) { - *status = LimitedResourceNotAllocated; - return TStruct(); - } + // return structure. Null will propogate correctly, so no need to manually + // check. return m_structures[index]; } @@ -111,6 +90,6 @@ void LimitedHandleResource::Free( // lock and deallocated handle std::lock_guard sync(m_allocateMutex); std::lock_guard lock(m_handleMutexes[index]); - m_allocated[index] = false; + m_structures[index].reset(); } }