2021-01-11 14:39:19 +11:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* Copyright 2021, Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "chunked.hpp"
|
|
|
|
|
2021-04-12 15:53:13 +10:00
|
|
|
#include "../debug/warn.hpp"
|
2021-01-12 15:58:55 +11:00
|
|
|
#include "../log.hpp"
|
|
|
|
#include "../maths.hpp"
|
|
|
|
#include "../pointer.hpp"
|
|
|
|
|
|
|
|
|
2021-01-11 14:39:19 +11:00
|
|
|
using cruft::alloc::chunked;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2021-01-12 15:58:55 +11:00
|
|
|
chunked::chunked (std::size_t initial_size)
|
|
|
|
: chunked (initial_size, DEFAULT_CHUNK_SIZE)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
chunked::chunked (std::size_t initial_size, std::size_t _chunk_size)
|
|
|
|
: m_chunk (_chunk_size)
|
|
|
|
{
|
2021-04-09 15:40:36 +10:00
|
|
|
m_chunk = cruft::max (m_chunk, sizeof (allocation_t));
|
2021-01-12 15:58:55 +11:00
|
|
|
|
|
|
|
auto const initial_arenas = cruft::divup (initial_size, m_chunk);
|
|
|
|
m_data.resize (initial_arenas);
|
|
|
|
|
|
|
|
for (auto &i: m_data) {
|
2021-04-09 15:40:36 +10:00
|
|
|
i.head = i.remain.data = new std::byte[m_chunk];
|
|
|
|
*i.remain.node = {
|
2021-01-12 15:58:55 +11:00
|
|
|
.size = m_chunk,
|
|
|
|
.next = nullptr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-09 15:40:36 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
chunked::~chunked () noexcept
|
|
|
|
{
|
|
|
|
for (auto &i: m_data)
|
|
|
|
delete [] i.head;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-12 15:58:55 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void*
|
|
|
|
chunked::do_allocate (std::size_t size, std::size_t alignment)
|
|
|
|
{
|
|
|
|
// Find a chunk that can service this allocation.
|
|
|
|
for (auto &i: m_data) {
|
2021-04-09 15:40:36 +10:00
|
|
|
if (i.remain.node->size < size + alignment)
|
2021-01-12 15:58:55 +11:00
|
|
|
continue;
|
|
|
|
|
2021-04-09 15:40:36 +10:00
|
|
|
auto base = cruft::align::up (i.remain.data, alignment);
|
2021-01-12 15:58:55 +11:00
|
|
|
std::byte* newbase = base + size;
|
2021-04-09 15:40:36 +10:00
|
|
|
if (newbase > i.remain.data + i.remain.node->size)
|
2021-01-12 15:58:55 +11:00
|
|
|
continue;
|
|
|
|
|
2021-04-09 15:40:36 +10:00
|
|
|
std::size_t newsize = i.remain.data + i.remain.node->size - newbase;
|
2021-01-12 15:58:55 +11:00
|
|
|
|
2021-04-09 15:40:36 +10:00
|
|
|
i.remain.data = newbase;
|
|
|
|
i.remain.node->size = newsize;
|
2021-01-12 15:58:55 +11:00
|
|
|
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No chunk was found. We need to allocate a new chunk of at least enough size.
|
2021-01-14 08:47:39 +11:00
|
|
|
std::size_t allocation_size = cruft::max (size + alignment, m_chunk);
|
|
|
|
std::unique_ptr<std::byte[]> ptr (new std::byte[allocation_size]);
|
2021-04-09 15:40:36 +10:00
|
|
|
m_data.push_back (
|
|
|
|
::cruft::alloc::chunked::arena {
|
|
|
|
.head = ptr.get (),
|
|
|
|
.remain = { .data = ptr.get (), },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
m_data.back ().remain.node->size = allocation_size;
|
|
|
|
m_data.back ().remain.node->next = m_data.back ().remain.node;
|
2021-01-14 08:47:39 +11:00
|
|
|
ptr.release ();
|
|
|
|
return do_allocate (size, alignment);
|
2021-01-12 15:58:55 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
chunked::do_deallocate (void *p, std::size_t bytes, std::size_t alignment)
|
|
|
|
{
|
|
|
|
(void)p;
|
|
|
|
(void)bytes;
|
|
|
|
(void)alignment;
|
|
|
|
|
|
|
|
LOG_WARNING ("chunked::do_deallocate is unimplemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
chunked::do_is_equal (memory_resource const &) const noexcept
|
|
|
|
{
|
|
|
|
warn ("chunked::do_is_equal is unimplemented");
|
|
|
|
return false;
|
|
|
|
}
|