/* * Copyright (C) 2024 Emeric Poupon * * This file is part of LMS. * * LMS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * LMS is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with LMS. If not, see . */ #pragma once #include "TLSMonotonicMemoryResource.hpp" namespace lms::api::subsonic { // Stateless allocator that uses a shared MemoryResource template class Allocator { public: using value_type = T; using pointer = T*; using const_pointer = const T*; using reference = T&; using const_reference = const T&; using size_type = std::size_t; using difference_type = std::ptrdiff_t; constexpr Allocator() noexcept = default; template constexpr Allocator(const Allocator&) noexcept { } template struct rebind { using other = Allocator; }; [[nodiscard]] pointer allocate(size_type n) { return reinterpret_cast(MemoryResource::getInstance().allocate(n * sizeof(T), alignof(T))); } // Deallocate memory pointed to by p void deallocate(pointer p, std::size_t) noexcept { MemoryResource::getInstance().deallocate(reinterpret_cast(p)); } }; template bool operator==(const Allocator&, const Allocator&) { return true; } template bool operator!=(const Allocator&, const Allocator&) { return false; } template using ResponseAllocator = Allocator; } // namespace lms::api::subsonic