1 //
2 //    Copyright (C) Microsoft.  All rights reserved.
3 //
4 /*++
5 
6 Module Name:
7 
8     IFxMemory.hpp
9 
10 Abstract:
11 
12     Abstract base class for a memory object.  It is necessary to split the
13     memory interface away from an FxObject derived base class so that we can
14     hand out WDFMEMORY handles that are embedded within other FxObject derived
15     classes without having to embed another FxObject derived class within the
16     parent.
17 
18 Author:
19 
20 
21 
22 Environment:
23 
24     kernel mode only
25 
26 Revision History:
27 
28 --*/
29 
30 #ifndef __IFX_MEMORY_HPP__
31 #define __IFX_MEMORY_HPP__
32 
33 // begin_wpp enum
34 enum IFxMemoryFlags {
35     IFxMemoryFlagReadOnly = 0x0001,
36 };
37 // end_wpp
38 
39 
40 class IFxMemory {
41 public:
42     virtual
43     PVOID
44     GetBuffer(
45         VOID
46         ) =0;
47 
48     virtual
49     size_t
50     GetBufferSize(
51         VOID
52         ) =0;
53 
54     virtual
55     PMDL
56     GetMdl(
57         VOID
58         ) =0;
59 
60     virtual
61     WDFMEMORY
62     GetHandle(
63         VOID
64         ) =0;
65 
66     //
67     // Value returned is a bit field from the enum IFxMemoryFlags
68     //
69     virtual
70     USHORT
71     GetFlags(
72         VOID
73         ) =0;
74 
75     virtual
76     PFX_DRIVER_GLOBALS
77     GetDriverGlobals(
78         VOID
79         ) =0;
80 
81     virtual
82     ULONG
83     AddRef(
84         __in PVOID Tag,
85         __in LONG Line,
86         __in_opt PSTR File
87         ) =0;
88 
89     virtual
90     ULONG
91     Release(
92         __in PVOID Tag,
93         __in LONG Line,
94         __in_opt PSTR File
95         ) =0;
96 
97     virtual
98     VOID
99     Delete(
100         VOID
101         ) =0;
102 
103     _Must_inspect_result_
104     NTSTATUS
ValidateMemoryOffsets(__in_opt PWDFMEMORY_OFFSET Offsets)105     ValidateMemoryOffsets(
106         __in_opt PWDFMEMORY_OFFSET Offsets
107         )
108     {
109         NTSTATUS status;
110         size_t total;
111 
112         if (Offsets == NULL) {
113             return STATUS_SUCCESS;
114         }
115 
116         status = RtlSizeTAdd(Offsets->BufferLength, Offsets->BufferOffset, &total);
117 
118         if (!NT_SUCCESS(status)) {
119             return status;
120         }
121 
122         if (total > GetBufferSize()) {
123             return STATUS_INTEGER_OVERFLOW;
124         }
125 
126         return STATUS_SUCCESS;
127     }
128 
129     _Must_inspect_result_
130     NTSTATUS
131     CopyFromPtr(
132         __in_opt PWDFMEMORY_OFFSET DestinationOffsets,
133         __in_bcount(SourceBufferLength) PVOID SourceBuffer,
134         __in size_t SourceBufferLength,
135         __in_opt PWDFMEMORY_OFFSET SourceOffsets
136         );
137 
138     _Must_inspect_result_
139     NTSTATUS
140     CopyToPtr(
141         __in_opt PWDFMEMORY_OFFSET SourceOffsets,
142         __out_bcount(DestinationBufferLength) PVOID DestinationBuffer,
143         __in size_t DestinationBufferLength,
144         __in_opt PWDFMEMORY_OFFSET DestinationOffsets
145         );
146 
147 protected:
148     static
149     _Must_inspect_result_
150     NTSTATUS
151     _CopyPtrToPtr(
152         __in_bcount(SourceBufferLength)  PVOID SourceBuffer,
153         __in size_t SourceBufferLength,
154         __in_opt PWDFMEMORY_OFFSET SourceOffsets,
155         __out_bcount(DestinationBufferLength) PVOID DestinationBuffer,
156         __in size_t DestinationBufferLength,
157         __in_opt PWDFMEMORY_OFFSET DestinationOffsets
158         );
159 };
160 
161 #endif // __IFX_MEMORY_HPP__
162