Skip to content

Commit efb3297

Browse files
Replace scoped buffer with vector (#22)
* Replace scoped_buffer with vector (#5) * Replace scoped_buffer with vector * Fix optional reference usage * Fix ternary operator usage * Fix using iterator aliases definition, add comments regarding StdSupport * Move to stl/vector, add tests (#5) * Add ScopedBuffer with deprecation notice * Fix build on VS2022 --------- Co-authored-by: fyurchyshen <144681274+fyurchyshen@users.noreply.github.com>
1 parent 812335a commit efb3297

File tree

6 files changed

+630
-143
lines changed

6 files changed

+630
-143
lines changed

include/kf/Allocator.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
#include "stl/new"
3+
4+
namespace kf
5+
{
6+
//////////////////////////////////////////////////////////////////////////
7+
// Allocator
8+
9+
template <class T, POOL_TYPE PoolType>
10+
class Allocator
11+
{
12+
public:
13+
static_assert(!std::is_const_v<T>, "The C++ Standard forbids containers of const elements because allocator<const T> is ill-formed.");
14+
15+
using value_type = T;
16+
using size_type = size_t;
17+
using difference_type = ptrdiff_t;
18+
using propagate_on_container_swap = std::true_type;
19+
20+
constexpr Allocator() noexcept = default;
21+
22+
Allocator(const Allocator&) noexcept = default;
23+
24+
template <typename Other>
25+
constexpr Allocator(const Allocator<Other, PoolType>&) noexcept {}
26+
27+
constexpr void deallocate(T* const p, size_t) noexcept
28+
{
29+
operator delete(p);
30+
}
31+
32+
[[nodiscard]] constexpr T* allocate(const size_t count) noexcept
33+
{
34+
return static_cast<value_type*>(operator new(count * sizeof(T), PoolType));
35+
}
36+
37+
template <typename Other>
38+
struct rebind
39+
{
40+
using other = Allocator<Other, PoolType>;
41+
};
42+
};
43+
}

include/kf/ScopedBuffer.h

Lines changed: 1 addition & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -3,147 +3,5 @@
33
namespace kf
44
{
55
template<typename T, POOL_TYPE _Pool = PagedPool>
6-
class scoped_buffer
7-
{
8-
public:
9-
scoped_buffer()
10-
: m_size(0)
11-
{
12-
m_buf = nullptr;
13-
}
14-
15-
~scoped_buffer()
16-
{
17-
clear();
18-
}
19-
20-
scoped_buffer(__in const scoped_buffer&) = delete;
21-
scoped_buffer& operator=(__in const scoped_buffer&) = delete;
22-
23-
void operator==(__in const scoped_buffer&) const = delete;
24-
void operator!=(__in const scoped_buffer&) const = delete;
25-
26-
scoped_buffer(__inout scoped_buffer&& other)
27-
: m_buf(other.m_buf)
28-
, m_size(other.m_size)
29-
{
30-
other.m_buf = nullptr;
31-
other.m_size = 0;
32-
}
33-
34-
scoped_buffer& operator=(__inout scoped_buffer&& other)
35-
{
36-
if (this == &other)
37-
{
38-
return *this;
39-
}
40-
clear();
41-
m_buf = other.m_buf;
42-
m_size = other.m_size;
43-
other.m_buf = nullptr;
44-
other.m_size = 0;
45-
return *this;
46-
}
47-
48-
NTSTATUS resize(__in ULONG size)
49-
{
50-
T* buf = new(_Pool) T[size];
51-
52-
if (!buf)
53-
{
54-
return STATUS_NO_MEMORY;
55-
}
56-
57-
if (m_buf)
58-
{
59-
const size_t minsize = min(m_size, size);
60-
RtlCopyMemory(buf, m_buf, minsize * sizeof(T));
61-
clear();
62-
}
63-
64-
m_buf = buf;
65-
m_size = size;
66-
67-
return STATUS_SUCCESS;
68-
}
69-
70-
ULONG size() const
71-
{
72-
return m_size;
73-
}
74-
75-
void clear()
76-
{
77-
if (m_buf)
78-
{
79-
delete[] m_buf;
80-
m_buf = nullptr;
81-
m_size = 0;
82-
}
83-
ASSERT(m_size == 0);
84-
}
85-
86-
T* release()
87-
{
88-
auto ret = m_buf;
89-
90-
m_buf = nullptr;
91-
m_size = 0;
92-
93-
return ret;
94-
}
95-
96-
T* get()
97-
{
98-
ASSERT(m_buf);
99-
return m_buf;
100-
}
101-
102-
const T* get() const
103-
{
104-
ASSERT(m_buf);
105-
return m_buf;
106-
}
107-
108-
T& operator*() const
109-
{
110-
return *get();
111-
}
112-
113-
T* operator->() const
114-
{
115-
return get();
116-
}
117-
118-
operator bool() const
119-
{
120-
return m_size;
121-
}
122-
123-
void swap(__inout scoped_buffer& other)
124-
{
125-
T* tmp_buf = m_buf;
126-
ULONG tmp_size = m_size;
127-
128-
m_buf = other.m_buf;
129-
m_size = other.m_size;
130-
131-
other.m_buf = tmp_buf;
132-
other.m_size = tmp_size;
133-
}
134-
135-
T* begin()
136-
{
137-
return get();
138-
}
139-
140-
T* end()
141-
{
142-
return get() + m_size;
143-
}
144-
145-
private:
146-
T* m_buf;
147-
ULONG m_size;
148-
};
6+
class [[deprecated("Use kf::vector instead")]] scoped_buffer;
1497
}

include/kf/stl/new

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

67
inline void* __cdecl operator new(size_t size, POOL_TYPE poolType)
78
{

0 commit comments

Comments
 (0)