remove \r
[extl.git] / extl / win / memory / heap.h
blob0041ee39ffa2ca086360603bc397175f405efa7b
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: heap.h
4 * Created: 08.05.22
5 * Updated: 08.05.22
7 * Brief: heap class
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_PLATFORM_WIN_MEMORY_HEAP_H
13 #define EXTL_PLATFORM_WIN_MEMORY_HEAP_H
15 /*!\file heap.h
16 * \brief heap class
18 #ifndef __cplusplus
19 # error heap.h need be supported by c++.
20 #endif
22 /* ///////////////////////////////////////////////////////////////////////
23 * Includes
25 #include "../win.h"
26 #include "../../memory/prefix.h"
27 #include "../../utility/uncopyable.h"
29 /* ///////////////////////////////////////////////////////////////////////
30 * ::extl::platform::win namespace
32 EXTL_WIN_BEGIN_WHOLE_NAMESPACE
34 /*!\brief heap class
35 * \ingroup extl_group_memory
37 class heap : private uncopyable<heap>
39 /// \name Types
40 /// @{
41 public:
42 typedef heap class_type;
43 typedef HANDLE handle_type;
44 typedef DWORD size_type;
45 /// @}
47 private:
48 /// The handle of the heap
49 handle_type m_hheap;
50 /// Indicates whether is the owner of the heap
51 e_bool_t m_is_owner;
53 private:
54 enum { en_default_page_size = 8 * 1024 };
56 /// \name Constructors
57 /// @{
58 public:
59 /// Creates a private heap
60 heap( size_type initial_size = en_default_page_size
61 , size_type max_size = 0
62 , size_type option = HEAP_GENERATE_EXCEPTIONS)
63 : m_hheap(NULL), m_is_owner(e_true_v)
65 create(initial_size, max_size, option);
67 /// Attach a heap handle
68 heap(handle_type hheap)
69 : m_hheap(hheap), m_is_owner(e_false_v)
72 ~heap()
74 destroy();
76 /// @}
78 public:
79 /// Creates a private heap
80 e_bool_t create ( size_type initial_size = en_default_page_size
81 , size_type max_size = 0
82 , size_type option = HEAP_GENERATE_EXCEPTIONS)
84 m_hheap = ::HeapCreate(option, initial_size, max_size);
85 if (NULL == m_hheap) return e_false_v;
86 return e_true_v;
88 /// Allocates a memory block from the heap
89 void* allocate(size_type n, size_type flag = HEAP_ZERO_MEMORY)
91 return (NULL != m_hheap)? ::HeapAlloc(m_hheap, flag, n) : NULL;
93 /// Reallocates a memory block from the heap
94 void* reallocate(void* p, size_type n, size_type flag = HEAP_ZERO_MEMORY)
96 EXTL_ASSERT(NULL != p);
97 if (NULL == p) return NULL;
98 return (NULL != m_hheap)? ::HeapReAlloc(m_hheap, flag, p, n) : p;
100 /// Deallocates a memory block to the heap
101 void deallocate(void* p, size_type flag = 0)
103 EXTL_ASSERT(NULL != p);
104 if (NULL == p) return ;
105 if (NULL != m_hheap) ::HeapFree(m_hheap, flag, p);
107 /// Return the size of the given memory block
108 size_type get_size(void* p, size_type flag = 0)
110 EXTL_ASSERT(NULL == p || NULL != m_hheap);
111 return ::HeapSize(m_hheap, flag, p);
113 /// Destroys the heap
114 void destroy()
116 if (NULL != m_hheap && m_is_owner)
118 ::HeapDestroy(m_hheap);
119 m_hheap = NULL;
120 m_is_owner = e_false_v;
123 /// Returns the heap handle
124 handle_type handle() const
126 return m_hheap;
132 /* ///////////////////////////////////////////////////////////////////////
133 * ::extl::platform::win namespace
135 EXTL_WIN_END_WHOLE_NAMESPACE
137 /* //////////////////////////////////////////////////////////////////// */
138 #endif /* EXTL_PLATFORM_WIN_MEMORY_HEAP_H */
139 /* //////////////////////////////////////////////////////////////////// */