1 /*++ 2 3 Copyright (c) Microsoft Corporation 4 5 Module Name: 6 7 FxCommonBuffer.hpp 8 9 Abstract: 10 11 WDF CommonBuffer Object support 12 13 Environment: 14 15 Kernel mode only. 16 17 Notes: 18 19 20 Revision History: 21 22 --*/ 23 24 #ifndef _FXCOMMONBUFFER_H_ 25 #define _FXCOMMONBUFFER_H_ 26 27 // 28 // Calculate an "aligned" address (Logical or Virtual) per 29 // a specific alignment value. 30 // 31 FORCEINLINE 32 PVOID FX_ALIGN_VIRTUAL_ADDRESS(__in PVOID VA,__in size_t AlignTo)33FX_ALIGN_VIRTUAL_ADDRESS( 34 __in PVOID VA, 35 __in size_t AlignTo 36 ) 37 { 38 return (PVOID)(((ULONG_PTR)VA + AlignTo) & ~AlignTo); 39 } 40 41 FORCEINLINE 42 ULONGLONG FX_ALIGN_LOGICAL_ADDRESS(__in PHYSICAL_ADDRESS LA,__in size_t AlignTo)43FX_ALIGN_LOGICAL_ADDRESS( 44 __in PHYSICAL_ADDRESS LA, 45 __in size_t AlignTo 46 ) 47 { 48 return (LA.QuadPart + AlignTo) & ~((ULONGLONG)AlignTo); 49 } 50 51 // 52 // Declare the FxCommonBuffer class 53 // 54 class FxCommonBuffer : public FxNonPagedObject { 55 56 public: 57 58 FxCommonBuffer( 59 __in PFX_DRIVER_GLOBALS FxDriverGlobals, 60 __in FxDmaEnabler * pDmaEnabler 61 ); 62 63 virtual 64 BOOLEAN 65 Dispose( 66 VOID 67 ); 68 69 _Must_inspect_result_ 70 NTSTATUS 71 AllocateCommonBuffer( 72 __in size_t Length 73 ); 74 75 VOID 76 FreeCommonBuffer( 77 VOID 78 ); 79 80 __forceinline 81 PHYSICAL_ADDRESS GetAlignedLogicalAddress(VOID)82 GetAlignedLogicalAddress( 83 VOID 84 ) 85 { 86 return m_BufferAlignedLA; 87 } 88 89 __forceinline 90 PVOID GetAlignedVirtualAddress(VOID)91 GetAlignedVirtualAddress( 92 VOID 93 ) 94 { 95 return m_BufferAlignedVA; 96 } 97 98 __forceinline 99 size_t GetLength(VOID)100 GetLength( 101 VOID 102 ) 103 { 104 return m_Length; 105 } 106 107 __forceinline 108 VOID SetAlignment(__in ULONG Alignment)109 SetAlignment( 110 __in ULONG Alignment 111 ) 112 { 113 m_Alignment = Alignment; 114 } 115 116 protected: 117 118 // 119 // Unaligned virtual address 120 // 121 PVOID m_BufferRawVA; 122 123 // 124 // Aligned virtual address 125 // 126 PVOID m_BufferAlignedVA; 127 128 // 129 // Aligned logical address 130 // 131 PHYSICAL_ADDRESS m_BufferAlignedLA; 132 133 // 134 // Unaligned logical address 135 // 136 PHYSICAL_ADDRESS m_BufferRawLA; 137 138 // 139 // Pointer to the DMA enabler 140 // 141 FxDmaEnabler * m_DmaEnabler; 142 143 // 144 // Length specified by the caller 145 // 146 size_t m_Length; 147 148 // 149 // Actual length used to allocate buffer after adding the alignement 150 // value. 151 // 152 size_t m_RawLength; 153 154 // 155 // Alignment of the allocated buffer. 156 // 157 size_t m_Alignment; 158 159 }; 160 161 #endif // _FXCOMMONBUFFER_H_ 162