// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include #include "core/framework/allocator.h" namespace onnxruntime { // An STL wrapper for ORT allocators. This enables overriding the // std::allocator used in STL containers for better memory performance. template class OrtStlAllocator { template friend class OrtStlAllocator; AllocatorPtr allocator_; public: typedef T value_type; using propagate_on_container_copy_assignment = std::true_type; using propagate_on_container_move_assignment = std::true_type; using propagate_on_container_swap = std::true_type; using is_always_equal = std::true_type; OrtStlAllocator(const AllocatorPtr& a) noexcept { allocator_ = a; } OrtStlAllocator(const OrtStlAllocator& other) noexcept { allocator_ = other.allocator_; } template OrtStlAllocator(const OrtStlAllocator& other) noexcept { allocator_ = other.allocator_; } T* allocate(size_t n, const void* hint = 0) { ORT_UNUSED_PARAMETER(hint); return reinterpret_cast(allocator_->Alloc(n * sizeof(T))); } void deallocate(T* p, size_t n) { ORT_UNUSED_PARAMETER(n); allocator_->Free(p); } }; template bool operator==(const OrtStlAllocator& lhs, const OrtStlAllocator& rhs) noexcept { return lhs.allocator_ == rhs.allocator_; } template bool operator!=(const OrtStlAllocator& lhs, const OrtStlAllocator& rhs) noexcept { return lhs.allocator_ != rhs.allocator_; } } // namespace onnxruntime