3 Copyright (C) Microsoft Corporation. All rights reserved.
11 Functions/macros for using the scratch buffer and scratch request/SRB
12 in the serial I/O queue context
31 _IRQL_requires_max_(APC_LEVEL
)
33 ScratchBuffer_Deallocate(
34 _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
37 _IRQL_requires_max_(APC_LEVEL
)
39 ScratchBuffer_Allocate(
40 _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
44 ScratchBuffer_ResetItems(
45 _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
,
46 _In_ BOOLEAN ResetRequestHistory
49 _IRQL_requires_max_(APC_LEVEL
)
51 ScratchBuffer_SetupSrb(
52 _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
,
53 _In_opt_ WDFREQUEST OriginalRequest
,
54 _In_ ULONG MaximumTransferLength
,
55 _In_ BOOLEAN GetDataFromDevice
59 ScratchBuffer_SetupReadWriteSrb(
60 _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
,
61 _In_ WDFREQUEST OriginalRequest
,
62 _In_ LARGE_INTEGER StartingOffset
,
63 _In_ ULONG RequiredLength
,
64 _Inout_updates_bytes_(RequiredLength
) UCHAR
* DataBuffer
,
65 _In_ BOOLEAN IsReadRequest
,
66 _In_ BOOLEAN UsePartialMdl
70 ScratchBuffer_SendSrb(
71 _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
,
72 _In_ BOOLEAN SynchronousSrb
,
73 _When_(SynchronousSrb
, _Pre_null_
)
74 _When_(!SynchronousSrb
, _In_opt_
)
75 PSRB_HISTORY_ITEM
*SrbHistoryItem
79 ScratchBuffer_PerformNextReadWrite(
80 _In_ PCDROM_DEVICE_EXTENSION DeviceExtension
,
85 #define ScratchBuffer_BeginUse(context) ScratchBuffer_BeginUseX((context), __FILE__, __LINE__)
87 #define ScratchBuffer_BeginUse(context) ScratchBuffer_BeginUseX((context), NULL, (ULONG)-1)
90 FORCEINLINE VOID
ScratchBuffer_BeginUseX(_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
, _In_opt_ LPCSTR File
, ULONG Line
)
92 // NOTE: these are not "real" locks. They are simply to help
93 // avoid multiple uses of the scratch buffer. Thus, it
94 // is not critical to have atomic operations here.
95 PVOID tmp
= InterlockedCompareExchangePointer((PVOID
)&(DeviceExtension
->ScratchContext
.ScratchInUse
), (PVOID
)-1, NULL
);
96 NT_ASSERT(tmp
== NULL
);
97 UNREFERENCED_PARAMETER(tmp
); //defensive coding, avoid PREFAST warning.
98 DeviceExtension
->ScratchContext
.ScratchInUseFileName
= File
;
99 DeviceExtension
->ScratchContext
.ScratchInUseLineNumber
= Line
;
100 ScratchBuffer_ResetItems(DeviceExtension
, TRUE
);
101 RequestClearSendTime(DeviceExtension
->ScratchContext
.ScratchRequest
);
104 FORCEINLINE VOID
ScratchBuffer_EndUse(_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
)
106 // NOTE: these are not "real" locks. They are simply to help
107 // avoid multiple uses of the scratch buffer. Thus, it
108 // is not critical to have atomic operations here.
110 // On lock release, we erase ScratchInUseFileName and ScratchInUseLineNumber _before_ releasing ScratchInUse,
111 // because otherwise we may erase these after the lock has been acquired again by another thread. We store the
112 // old values of ScratchInUseFileName and ScratchInUseLineNumber in local variables to facilitate debugging,
113 // if the ASSERT at the end of the function is hit.
114 PCSTR scratchInUseFileName
;
115 ULONG scratchInUseLineNumber
;
118 scratchInUseFileName
= DeviceExtension
->ScratchContext
.ScratchInUseFileName
;
119 scratchInUseLineNumber
= DeviceExtension
->ScratchContext
.ScratchInUseLineNumber
;
120 UNREFERENCED_PARAMETER(scratchInUseFileName
);
121 UNREFERENCED_PARAMETER(scratchInUseLineNumber
);
122 DeviceExtension
->ScratchContext
.ScratchInUseFileName
= NULL
;
123 DeviceExtension
->ScratchContext
.ScratchInUseLineNumber
= 0;
126 // If we have used the PartialMdl in the scratch context we should notify MM that we will be reusing it
127 // otherwise it may leak System VA if some one below us has mapped the same.
130 if (DeviceExtension
->ScratchContext
.PartialMdlIsBuilt
!= FALSE
)
132 MmPrepareMdlForReuse(DeviceExtension
->ScratchContext
.PartialMdl
);
133 DeviceExtension
->ScratchContext
.PartialMdlIsBuilt
= FALSE
;
136 tmp
= InterlockedCompareExchangePointer((PVOID
)&(DeviceExtension
->ScratchContext
.ScratchInUse
), NULL
, (PVOID
)-1);
137 NT_ASSERT(tmp
== ((PVOID
)-1));
138 UNREFERENCED_PARAMETER(tmp
); //defensive coding, avoid PREFAST warning.
143 CompressSrbHistoryData(
144 _Inout_ PSRB_HISTORY RequestHistory
148 ValidateSrbHistoryDataPresumptions(
149 _In_ SRB_HISTORY
const* RequestHistory
152 _IRQL_requires_max_(APC_LEVEL
)
154 ScratchBuffer_ExecuteCdbEx(
155 _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
,
156 _In_opt_ WDFREQUEST OriginalRequest
,
157 _In_ ULONG TransferSize
,
158 _In_ BOOLEAN GetDataFromDevice
,
160 _In_ UCHAR OprationLength
,
161 _In_ ULONG TimeoutValue
164 _IRQL_requires_max_(APC_LEVEL
)
165 FORCEINLINE
// __REACTOS__
167 ScratchBuffer_ExecuteCdb(
168 _Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
,
169 _In_opt_ WDFREQUEST OriginalRequest
,
170 _In_ ULONG TransferSize
,
171 _In_ BOOLEAN GetDataFromDevice
,
173 _In_ UCHAR OprationLength
176 return ScratchBuffer_ExecuteCdbEx(DeviceExtension
,
185 KDEFERRED_ROUTINE ScratchBuffer_ReadWriteTimerRoutine
;
187 #endif //__SCRATCH_H__