| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * \copyright | ||
| 3 | * Copyright 2009-2016, by the California Institute of Technology. | ||
| 4 | * ALL RIGHTS RESERVED. United States Government Sponsorship | ||
| 5 | * acknowledged. | ||
| 6 | */ | ||
| 7 | #include <Fw/Types/Assert.hpp> | ||
| 8 | #include <Fw/Types/MemAllocator.hpp> | ||
| 9 | #include <config/MemoryAllocation.hpp> | ||
| 10 | #include <type_traits> | ||
| 11 | namespace Fw { | ||
| 12 | |||
| 13 | 4 | MemAllocator::MemAllocator() {} | |
| 14 | |||
| 15 | 8 | MemAllocator::~MemAllocator() {} | |
| 16 | |||
| 17 | 112 | void* MemAllocator::allocate(const FwEnumStoreType identifier, FwSizeType& size, FwSizeType alignment) { | |
| 18 | 112 | bool unused = false; | |
| 19 |
1/1✓ Branch 1 taken 112 times.
|
224 | return this->allocate(identifier, size, unused, alignment); |
| 20 | } | ||
| 21 | |||
| 22 | 1 | void* MemAllocator ::checkedAllocate(const FwEnumStoreType identifier, | |
| 23 | FwSizeType& size, | ||
| 24 | bool& recoverable, | ||
| 25 | FwSizeType alignment) { | ||
| 26 | 1 | FwSizeType requestedSize = size; | |
| 27 | 1 | void* memory = this->allocate(identifier, size, recoverable, alignment); | |
| 28 | 1 | FW_ASSERT(memory != nullptr && size >= requestedSize, static_cast<FwAssertArgType>(identifier), | |
| 29 | static_cast<FwAssertArgType>(requestedSize), static_cast<FwAssertArgType>(size)); | ||
| 30 | 1 | return memory; | |
| 31 | } | ||
| 32 | |||
| 33 | ✗ | void* MemAllocator ::checkedAllocate(const FwEnumStoreType identifier, FwSizeType& size, FwSizeType alignment) { | |
| 34 | ✗ | bool unused = false; | |
| 35 | ✗ | return this->checkedAllocate(identifier, size, unused, alignment); | |
| 36 | } | ||
| 37 | |||
| 38 | 1 | MemAllocatorRegistry::MemAllocatorRegistry() : m_defaultAllocator(MemAllocatorRegistry::getDefaultAllocator()) { | |
| 39 |
1/1✓ Branch 1 taken 1 times.
|
1 | this->registerAllocator(MemoryAllocation::MemoryAllocatorType::SYSTEM, m_defaultAllocator); |
| 40 | 1 | } | |
| 41 | |||
| 42 | 1 | void MemAllocatorRegistry::registerAllocator(const MemoryAllocation::MemoryAllocatorType type, | |
| 43 | MemAllocator& allocator) { | ||
| 44 | 1 | this->m_allocators[type] = &allocator; | |
| 45 | 1 | } | |
| 46 | |||
| 47 | 56 | MemAllocatorRegistry& MemAllocatorRegistry::getInstance() { | |
| 48 |
4/7✓ Branch 0 taken 1 times.
✓ Branch 1 taken 55 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
56 | static MemAllocatorRegistry registry; |
| 49 | 56 | return registry; | |
| 50 | } | ||
| 51 | |||
| 52 | 56 | MemAllocator& MemAllocatorRegistry::getAllocator(const MemoryAllocation::MemoryAllocatorType type) { | |
| 53 | 56 | FW_ASSERT(this->m_allocators[type] != nullptr, static_cast<FwAssertArgType>(type)); | |
| 54 | 56 | return *this->m_allocators[type]; | |
| 55 | } | ||
| 56 | |||
| 57 | 56 | MemAllocator& MemAllocatorRegistry::getAnAllocator(const MemoryAllocation::MemoryAllocatorType type) { | |
| 58 | // If the allocator is not registered, return the SYSTEM allocator | ||
| 59 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | if (this->m_allocators[type] == nullptr) { |
| 60 |
2/2✓ Branch 1 taken 56 times.
✓ Branch 4 taken 56 times.
|
56 | return this->getAllocator(MemoryAllocation::MemoryAllocatorType::SYSTEM); |
| 61 | } | ||
| 62 | ✗ | return *this->m_allocators[type]; | |
| 63 | } | ||
| 64 | |||
| 65 | 1 | MemAllocator& MemAllocatorRegistry::getDefaultAllocator() { | |
| 66 | static_assert(std::is_constructible<MemoryAllocation::DefaultMemoryAllocatorType>::value, | ||
| 67 | "DefaultMemoryAllocatorType must be constructible without arguments"); | ||
| 68 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | static MemoryAllocation::DefaultMemoryAllocatorType defaultAllocator; |
| 69 | 1 | return defaultAllocator; | |
| 70 | } | ||
| 71 | |||
| 72 | } /* namespace Fw */ | ||
| 73 |