1 /* ///////////////////////////////////////////////////////////////////////
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
19 # error heap.h need be supported by c++.
22 /* ///////////////////////////////////////////////////////////////////////
26 #include "../../memory/prefix.h"
27 #include "../../utility/uncopyable.h"
29 /* ///////////////////////////////////////////////////////////////////////
30 * ::extl::platform::win namespace
32 EXTL_WIN_BEGIN_WHOLE_NAMESPACE
35 * \ingroup extl_group_memory
37 class heap
: private uncopyable
<heap
>
42 typedef heap class_type
;
43 typedef HANDLE handle_type
;
44 typedef DWORD size_type
;
48 /// The handle of the heap
50 /// Indicates whether is the owner of the heap
54 enum { en_default_page_size
= 8 * 1024 };
56 /// \name Constructors
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
)
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
;
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
116 if (NULL
!= m_hheap
&& m_is_owner
)
118 ::HeapDestroy(m_hheap
);
120 m_is_owner
= e_false_v
;
123 /// Returns the heap handle
124 handle_type
handle() const
132 /* ///////////////////////////////////////////////////////////////////////
133 * ::extl::platform::win namespace
135 EXTL_WIN_END_WHOLE_NAMESPACE
137 /* //////////////////////////////////////////////////////////////////// */
138 #endif /* EXTL_PLATFORM_WIN_MEMORY_HEAP_H */
139 /* //////////////////////////////////////////////////////////////////// */