File tree Expand file tree Collapse file tree 6 files changed +630
-143
lines changed
Expand file tree Collapse file tree 6 files changed +630
-143
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 33namespace 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}
Original file line number Diff line number Diff line change 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
67inline void* __cdecl operator new(size_t size, POOL_TYPE poolType)
78{
You can’t perform that action at this time.
0 commit comments