2020-02-07 12:58:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <mesosphere/kern_common.hpp>
|
|
|
|
#include <mesosphere/kern_k_slab_heap.hpp>
|
|
|
|
#include <mesosphere/kern_k_page_group.hpp>
|
|
|
|
#include <mesosphere/kern_k_memory_block.hpp>
|
2020-04-19 00:10:26 +00:00
|
|
|
#include <mesosphere/kern_k_dynamic_page_manager.hpp>
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class KDynamicSlabHeap {
|
|
|
|
NON_COPYABLE(KDynamicSlabHeap);
|
|
|
|
NON_MOVEABLE(KDynamicSlabHeap);
|
|
|
|
private:
|
|
|
|
using Impl = impl::KSlabHeapImpl;
|
2020-04-19 00:10:26 +00:00
|
|
|
using PageBuffer = KDynamicPageManager::PageBuffer;
|
2020-02-07 12:58:35 +00:00
|
|
|
private:
|
2020-12-18 01:18:47 +00:00
|
|
|
Impl m_impl;
|
|
|
|
KDynamicPageManager *m_page_allocator;
|
|
|
|
std::atomic<size_t> m_used;
|
|
|
|
std::atomic<size_t> m_peak;
|
|
|
|
std::atomic<size_t> m_count;
|
|
|
|
KVirtualAddress m_address;
|
|
|
|
size_t m_size;
|
2020-02-07 12:58:35 +00:00
|
|
|
private:
|
|
|
|
ALWAYS_INLINE Impl *GetImpl() {
|
2020-12-18 01:18:47 +00:00
|
|
|
return std::addressof(m_impl);
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
ALWAYS_INLINE const Impl *GetImpl() const {
|
2020-12-18 01:18:47 +00:00
|
|
|
return std::addressof(m_impl);
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
public:
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KDynamicSlabHeap() : m_impl(), m_page_allocator(), m_used(), m_peak(), m_count(), m_address(), m_size() { /* ... */ }
|
2020-02-07 12:58:35 +00:00
|
|
|
|
2020-12-18 01:18:47 +00:00
|
|
|
constexpr KVirtualAddress GetAddress() const { return m_address; }
|
|
|
|
constexpr size_t GetSize() const { return m_size; }
|
|
|
|
constexpr size_t GetUsed() const { return m_used; }
|
|
|
|
constexpr size_t GetPeak() const { return m_peak; }
|
|
|
|
constexpr size_t GetCount() const { return m_count; }
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
constexpr bool IsInRange(KVirtualAddress addr) const {
|
|
|
|
return this->GetAddress() <= addr && addr <= this->GetAddress() + this->GetSize() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Initialize(KVirtualAddress memory, size_t sz) {
|
|
|
|
/* Set tracking fields. */
|
2020-12-18 01:18:47 +00:00
|
|
|
m_address = memory;
|
|
|
|
m_count = sz / sizeof(T);
|
|
|
|
m_size = m_count * sizeof(T);
|
2020-02-07 12:58:35 +00:00
|
|
|
|
|
|
|
/* Free blocks to memory. */
|
2020-12-18 01:18:47 +00:00
|
|
|
u8 *cur = GetPointer<u8>(m_address + m_size);
|
|
|
|
for (size_t i = 0; i < m_count; i++) {
|
2020-02-07 12:58:35 +00:00
|
|
|
cur -= sizeof(T);
|
|
|
|
this->GetImpl()->Free(cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 00:10:26 +00:00
|
|
|
void Initialize(KDynamicPageManager *page_allocator) {
|
2020-12-18 01:18:47 +00:00
|
|
|
m_page_allocator = page_allocator;
|
|
|
|
m_address = m_page_allocator->GetAddress();
|
|
|
|
m_size = m_page_allocator->GetSize();
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 07:35:05 +00:00
|
|
|
void Initialize(KDynamicPageManager *page_allocator, size_t num_objects) {
|
|
|
|
MESOSPHERE_ASSERT(page_allocator != nullptr);
|
|
|
|
|
|
|
|
/* Initialize members. */
|
|
|
|
this->Initialize(page_allocator);
|
|
|
|
|
|
|
|
/* Allocate until we have the correct number of objects. */
|
2020-12-18 01:18:47 +00:00
|
|
|
while (m_count < num_objects) {
|
|
|
|
auto *allocated = reinterpret_cast<T *>(m_page_allocator->Allocate());
|
2020-04-19 07:35:05 +00:00
|
|
|
MESOSPHERE_ABORT_UNLESS(allocated != nullptr);
|
|
|
|
for (size_t i = 0; i < sizeof(PageBuffer) / sizeof(T); i++) {
|
|
|
|
this->GetImpl()->Free(allocated + i);
|
|
|
|
}
|
2020-12-18 01:18:47 +00:00
|
|
|
m_count += sizeof(PageBuffer) / sizeof(T);
|
2020-04-19 07:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 12:58:35 +00:00
|
|
|
T *Allocate() {
|
|
|
|
T *allocated = reinterpret_cast<T *>(this->GetImpl()->Allocate());
|
|
|
|
|
|
|
|
/* If we fail to allocate, try to get a new page from our next allocator. */
|
|
|
|
if (AMS_UNLIKELY(allocated == nullptr)) {
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_page_allocator != nullptr) {
|
|
|
|
allocated = reinterpret_cast<T *>(m_page_allocator->Allocate());
|
2020-02-07 12:58:35 +00:00
|
|
|
if (allocated != nullptr) {
|
|
|
|
/* If we succeeded in getting a page, free the rest to our slab. */
|
|
|
|
for (size_t i = 1; i < sizeof(PageBuffer) / sizeof(T); i++) {
|
|
|
|
this->GetImpl()->Free(allocated + i);
|
|
|
|
}
|
2020-12-18 01:18:47 +00:00
|
|
|
m_count += sizeof(PageBuffer) / sizeof(T);
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AMS_LIKELY(allocated != nullptr)) {
|
2020-02-14 01:38:56 +00:00
|
|
|
/* Construct the object. */
|
|
|
|
new (allocated) T();
|
|
|
|
|
|
|
|
/* Update our tracking. */
|
2020-12-18 01:18:47 +00:00
|
|
|
size_t used = ++m_used;
|
|
|
|
size_t peak = m_peak;
|
2020-02-07 12:58:35 +00:00
|
|
|
while (peak < used) {
|
2020-12-18 01:18:47 +00:00
|
|
|
if (m_peak.compare_exchange_weak(peak, used, std::memory_order_relaxed)) {
|
2020-02-07 12:58:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Free(T *t) {
|
|
|
|
this->GetImpl()->Free(t);
|
2020-12-18 01:18:47 +00:00
|
|
|
--m_used;
|
2020-02-07 12:58:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class KBlockInfoManager : public KDynamicSlabHeap<KBlockInfo>{};
|
|
|
|
class KMemoryBlockSlabManager : public KDynamicSlabHeap<KMemoryBlock>{};
|
|
|
|
|
|
|
|
}
|