mirror of
https://github.com/matrix-construct/construct
synced 2024-06-02 10:08:56 +02:00
67 lines
1.4 KiB
C++
67 lines
1.4 KiB
C++
// Matrix Construct
|
|
//
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
void
|
|
ircd::allocator::state::deallocate(const uint &pos,
|
|
const size_type &n)
|
|
{
|
|
for(size_t i(0); i < n; ++i)
|
|
{
|
|
assert(test(pos + i));
|
|
btc(pos + i);
|
|
}
|
|
|
|
last = pos;
|
|
}
|
|
|
|
uint
|
|
ircd::allocator::state::allocate(const size_type &n,
|
|
const uint &hint)
|
|
{
|
|
const auto next(this->next(n));
|
|
if(unlikely(next >= size)) // No block of n was found anywhere (next is past-the-end)
|
|
throw std::bad_alloc();
|
|
|
|
for(size_t i(0); i < n; ++i)
|
|
{
|
|
assert(!test(next + i));
|
|
bts(next + i);
|
|
}
|
|
|
|
last = next + n;
|
|
return next;
|
|
}
|
|
|
|
uint
|
|
ircd::allocator::state::next(const size_t &n)
|
|
const
|
|
{
|
|
uint ret(last), rem(n);
|
|
for(; ret < size && rem; ++ret)
|
|
if(test(ret))
|
|
rem = n;
|
|
else
|
|
--rem;
|
|
|
|
if(likely(!rem))
|
|
return ret - n;
|
|
|
|
for(ret = 0, rem = n; ret < last && rem; ++ret)
|
|
if(test(ret))
|
|
rem = n;
|
|
else
|
|
--rem;
|
|
|
|
if(unlikely(rem)) // The allocator should throw std::bad_alloc if !rem
|
|
return size;
|
|
|
|
return ret - n;
|
|
}
|