580db0315946a9be782c332b66e843acc54d1eec
[reactos.git] / reactos / lib / atl / atlmem.h
1 #ifndef __ATLMEM_H__
2 #define __ATLMEM_H__
3
4 #pragma once
5 #include "atlcore.h"
6
7 // HACK HACK! This must be placed in another global ATL header!!
8 // Placement new operator
9 void *operator new (size_t, void *buf)
10 {
11 return buf;
12 }
13
14 namespace ATL
15 {
16
17 //__interface __declspec(uuid("654F7EF5-CFDF-4df9-A450-6C6A13C622C0"))
18 class IAtlMemMgr
19 {
20 public:
21 virtual ~IAtlMemMgr() {};
22
23 virtual _Ret_maybenull_ _Post_writable_byte_size_(SizeBytes) void* Allocate(
24 _In_ size_t SizeBytes
25 ) = 0;
26
27 virtual void Free(
28 _Inout_opt_ void* Buffer
29 ) = 0;
30
31 virtual _Ret_maybenull_ _Post_writable_byte_size_(SizeBytes) void* Reallocate(
32 _Inout_updates_bytes_opt_(SizeBytes) void* Buffer,
33 _In_ size_t SizeBytes
34 ) = 0;
35
36 virtual size_t GetSize(
37 _In_ void* Buffer
38 ) = 0;
39 };
40
41 class CWin32Heap : public IAtlMemMgr
42 {
43 public:
44 HANDLE m_hHeap;
45
46 public:
47 CWin32Heap() :
48 m_hHeap(NULL)
49 {
50 }
51
52 CWin32Heap(_In_ HANDLE hHeap) :
53 m_hHeap(hHeap)
54 {
55 ATLASSERT(hHeap != NULL);
56 }
57
58 virtual ~CWin32Heap()
59 {
60 }
61
62
63 // IAtlMemMgr
64 _Ret_maybenull_ _Post_writable_byte_size_(SizeBytes) virtual void* Allocate(
65 _In_ size_t SizeBytes
66 )
67 {
68 return ::HeapAlloc(m_hHeap, 0, SizeBytes);
69 }
70
71 virtual void Free(
72 _In_opt_ void* Buffer
73 )
74 {
75 if (Buffer)
76 {
77 if (!::HeapFree(m_hHeap, 0, Buffer))
78 ATLASSERT(FALSE);
79 }
80 }
81
82 _Ret_maybenull_ _Post_writable_byte_size_(SizeBytes) virtual void* Reallocate(
83 _In_opt_ void* Buffer,
84 _In_ size_t SizeBytes
85 )
86 {
87 if (SizeBytes == 0)
88 {
89 Free(Buffer);
90 return NULL;
91 }
92
93 if (Buffer == NULL)
94 {
95 return Allocate(SizeBytes);
96 }
97
98 return ::HeapReAlloc(m_hHeap, 0, Buffer, SizeBytes);
99 }
100
101 virtual size_t GetSize(
102 _Inout_ void* Buffer
103 )
104 {
105 return ::HeapSize(m_hHeap, 0, Buffer);
106 }
107 };
108
109 }
110
111 #endif