Program Listing for File first_fit_allocator.hpp
↰ Return to documentation for file (include/holoscan/core/resources/gxf/first_fit_allocator.hpp
)
/*
* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HOLOSCAN_CORE_RESOURCES_GXF_FIRST_FIT_ALLOCATOR_HPP
#define HOLOSCAN_CORE_RESOURCES_GXF_FIRST_FIT_ALLOCATOR_HPP
#include <cstdint>
#include <memory>
#include <new>
#include <utility>
#include "holoscan/core/expected.hpp"
#include "holoscan/core/resources/gxf/first_fit_allocator_base.hpp"
namespace holoscan {
template<class T>
class FirstFitAllocator {
public:
template <typename E>
using expected_t = expected<E, FirstFitAllocatorBase::Error>;
using unexpected_t = unexpected<FirstFitAllocatorBase::Error>;
FirstFitAllocator() = default;
expected_t<int32_t> allocate(const int32_t size, const int chunk_size = 1) {
if (buffer_.get() != nullptr) {
return unexpected_t(FirstFitAllocatorBase::Error::kAlreadyInUse);
}
if (size < 0 || chunk_size <= 0) {
return unexpected_t(FirstFitAllocatorBase::Error::kInvalidSize);
}
chunk_size_ = chunk_size;
number_of_chunks_ = get_number_of_chunks(size);
// Allocate memory
buffer_.reset(new(std::nothrow) T[number_of_chunks_ * chunk_size_]);
if (buffer_.get() == nullptr) {
return unexpected_t(FirstFitAllocatorBase::Error::kOutOfMemory);
}
// Prepare the memory management.
auto res = memory_management_.allocate(number_of_chunks_);
if (!res) {
return unexpected_t(res.error());
}
return size;
}
expected_t<std::pair<T*, int32_t>> acquire(const int32_t size) {
const int32_t number_of_chunks = get_number_of_chunks(size);
auto res = memory_management_.acquire(number_of_chunks);
if (!res) {
return unexpected_t(res.error());
}
return std::make_pair(&buffer_.get()[res.value() * chunk_size_],
number_of_chunks * chunk_size_);
}
expected_t<void> release(const T* ptr) {
const T* begin = buffer_.get();
const T* end = begin + (number_of_chunks_ * chunk_size_);
if (ptr < begin || ptr >= end) {
return unexpected_t(FirstFitAllocatorBase::Error::kBlockNotAllocated);
}
const int32_t index = ptr - begin;
if (index % chunk_size_ != 0) {
return unexpected_t(FirstFitAllocatorBase::Error::kBlockNotAllocated);
}
return memory_management_.release(index / chunk_size_);
}
private:
int32_t get_number_of_chunks(const int32_t size) const {
return (size + chunk_size_ - 1) / chunk_size_;
}
FirstFitAllocatorBase memory_management_;
int32_t chunk_size_{};
int32_t number_of_chunks_{};
std::unique_ptr<T[]> buffer_{nullptr};
};
} // namespace holoscan
#endif// HOLOSCAN_CORE_RESOURCES_GXF_FIRST_FIT_ALLOCATOR_HPP