Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions include/kf/Allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once
#include "stl/new"

namespace kf
{
//////////////////////////////////////////////////////////////////////////
// Allocator

template <class T, POOL_TYPE PoolType>
class Allocator
{
public:
static_assert(!std::is_const_v<T>, "The C++ Standard forbids containers of const elements because allocator<const T> is ill-formed.");

using value_type = T;
using size_type = size_t;
using difference_type = ptrdiff_t;
using propagate_on_container_swap = std::true_type;

constexpr Allocator() noexcept = default;

Allocator(const Allocator&) noexcept = default;

template <typename Other>
constexpr Allocator(const Allocator<Other, PoolType>&) noexcept {}

constexpr void deallocate(T* const p, size_t) noexcept
{
operator delete(p);
}

[[nodiscard]] constexpr T* allocate(const size_t count) noexcept
{
return static_cast<value_type*>(operator new(count * sizeof(T), PoolType));
}

template <typename Other>
struct rebind
{
using other = Allocator<Other, PoolType>;
};
};
}
144 changes: 1 addition & 143 deletions include/kf/ScopedBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,147 +3,5 @@
namespace kf
{
template<typename T, POOL_TYPE _Pool = PagedPool>
class scoped_buffer
{
public:
scoped_buffer()
: m_size(0)
{
m_buf = nullptr;
}

~scoped_buffer()
{
clear();
}

scoped_buffer(__in const scoped_buffer&) = delete;
scoped_buffer& operator=(__in const scoped_buffer&) = delete;

void operator==(__in const scoped_buffer&) const = delete;
void operator!=(__in const scoped_buffer&) const = delete;

scoped_buffer(__inout scoped_buffer&& other)
: m_buf(other.m_buf)
, m_size(other.m_size)
{
other.m_buf = nullptr;
other.m_size = 0;
}

scoped_buffer& operator=(__inout scoped_buffer&& other)
{
if (this == &other)
{
return *this;
}
clear();
m_buf = other.m_buf;
m_size = other.m_size;
other.m_buf = nullptr;
other.m_size = 0;
return *this;
}

NTSTATUS resize(__in ULONG size)
{
T* buf = new(_Pool) T[size];

if (!buf)
{
return STATUS_NO_MEMORY;
}

if (m_buf)
{
const size_t minsize = min(m_size, size);
RtlCopyMemory(buf, m_buf, minsize * sizeof(T));
clear();
}

m_buf = buf;
m_size = size;

return STATUS_SUCCESS;
}

ULONG size() const
{
return m_size;
}

void clear()
{
if (m_buf)
{
delete[] m_buf;
m_buf = nullptr;
m_size = 0;
}
ASSERT(m_size == 0);
}

T* release()
{
auto ret = m_buf;

m_buf = nullptr;
m_size = 0;

return ret;
}

T* get()
{
ASSERT(m_buf);
return m_buf;
}

const T* get() const
{
ASSERT(m_buf);
return m_buf;
}

T& operator*() const
{
return *get();
}

T* operator->() const
{
return get();
}

operator bool() const
{
return m_size;
}

void swap(__inout scoped_buffer& other)
{
T* tmp_buf = m_buf;
ULONG tmp_size = m_size;

m_buf = other.m_buf;
m_size = other.m_size;

other.m_buf = tmp_buf;
other.m_size = tmp_size;
}

T* begin()
{
return get();
}

T* end()
{
return get() + m_size;
}

private:
T* m_buf;
ULONG m_size;
};
class [[deprecated("Use kf::vector instead")]] scoped_buffer;
}
1 change: 1 addition & 0 deletions include/kf/stl/new
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma warning(push)
#pragma warning(disable : 4595) // non-member operator new or delete functions may not be declared inline
#pragma warning(disable : 4996) // TODO: ExAllocatePoolWithTag is deprecated, use ExAllocatePool2

inline void* __cdecl operator new(size_t size, POOL_TYPE poolType)
{
Expand Down
Loading