ReactOS  0.4.13-dev-257-gfabbd7c
cache.c File Reference
#include <freeldr.h>
#include <debug.h>
Include dependency graph for cache.c:

Go to the source code of this file.

Functions

 DBG_DEFAULT_CHANNEL (CACHE)
 
BOOLEAN CacheInitializeDrive (UCHAR DriveNumber)
 
VOID CacheInvalidateCacheData (VOID)
 
BOOLEAN CacheReadDiskSectors (UCHAR DiskNumber, ULONGLONG StartSector, ULONG SectorCount, PVOID Buffer)
 
BOOLEAN CacheReleaseMemory (ULONG MinimumAmountToRelease)
 

Variables

CACHE_DRIVE CacheManagerDrive
 
BOOLEAN CacheManagerInitialized = FALSE
 
BOOLEAN CacheManagerDataInvalid = FALSE
 
ULONG CacheBlockCount = 0
 
SIZE_T CacheSizeLimit = 0
 
SIZE_T CacheSizeCurrent = 0
 

Function Documentation

◆ CacheInitializeDrive()

BOOLEAN CacheInitializeDrive ( UCHAR  DriveNumber)

Definition at line 37 of file cache.c.

38 {
39  PCACHE_BLOCK NextCacheBlock;
40  GEOMETRY DriveGeometry;
41 
42  // If we already have a cache for this drive then
43  // by all means lets keep it, unless it is a removable
44  // drive, in which case we'll invalidate the cache
46  (DriveNumber == CacheManagerDrive.DriveNumber) &&
47  (DriveNumber >= 0x80) &&
49  {
50  return TRUE;
51  }
52 
54 
55  //
56  // If we have already been initialized then free
57  // the old data
58  //
60  {
62 
63  TRACE("CacheBlockCount: %d\n", CacheBlockCount);
64  TRACE("CacheSizeLimit: %d\n", CacheSizeLimit);
65  TRACE("CacheSizeCurrent: %d\n", CacheSizeCurrent);
66  //
67  // Loop through and free the cache blocks
68  //
70  {
73  ListEntry);
74 
75  FrLdrTempFree(NextCacheBlock->BlockData, TAG_CACHE_DATA);
76  FrLdrTempFree(NextCacheBlock, TAG_CACHE_BLOCK);
77  }
78  }
79 
80  // Initialize the structure
83  CacheManagerDrive.DriveNumber = DriveNumber;
84  if (!MachDiskGetDriveGeometry(DriveNumber, &DriveGeometry))
85  {
86  return FALSE;
87  }
89 
90  // Get the number of sectors in each cache block
92 
93  CacheBlockCount = 0;
94  CacheSizeLimit = TotalPagesInLookupTable / 8 * MM_PAGE_SIZE;
95  CacheSizeCurrent = 0;
96  if (CacheSizeLimit > TEMP_HEAP_SIZE - (128 * 1024))
97  {
98  CacheSizeLimit = TEMP_HEAP_SIZE - (128 * 1024);
99  }
100 
102 
103  TRACE("Initializing BIOS drive 0x%x.\n", DriveNumber);
104  TRACE("BytesPerSector: %d.\n", CacheManagerDrive.BytesPerSector);
105  TRACE("BlockSize: %d.\n", CacheManagerDrive.BlockSize);
106  TRACE("CacheSizeLimit: %d.\n", CacheSizeLimit);
107 
108  return TRUE;
109 }
#define TAG_CACHE_BLOCK
Definition: cache.h:24
#define TRUE
Definition: types.h:120
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
SIZE_T CacheSizeCurrent
Definition: cache.c:35
#define MachDiskGetCacheableBlockCount(Drive)
Definition: machine.h:124
LIST_ENTRY CacheBlockHead
Definition: cache.h:60
#define TEMP_HEAP_SIZE
Definition: mm.h:123
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
PFN_NUMBER TotalPagesInLookupTable
Definition: meminit.c:27
PVOID BlockData
Definition: cache.h:43
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
#define MachDiskGetDriveGeometry(Drive, Geom)
Definition: machine.h:122
#define TRACE(s)
Definition: solgame.cpp:4
ULONG BlockSize
Definition: cache.h:59
CACHE_DRIVE CacheManagerDrive
Definition: cache.c:30
SIZE_T CacheSizeLimit
Definition: cache.c:34
BOOLEAN CacheManagerInitialized
Definition: cache.c:31
UCHAR DriveNumber
Definition: cache.h:56
ULONG BytesPerSector
Definition: disk.h:29
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
Definition: disk.h:24
#define TAG_CACHE_DATA
Definition: cache.h:23
BOOLEAN CacheManagerDataInvalid
Definition: cache.c:32
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
ULONG CacheBlockCount
Definition: cache.c:33
ULONG BytesPerSector
Definition: cache.h:57
FORCEINLINE VOID FrLdrTempFree(PVOID Allocation, ULONG Tag)
Definition: mm.h:186

Referenced by Ext2OpenVolume().

◆ CacheInvalidateCacheData()

VOID CacheInvalidateCacheData ( VOID  )

Definition at line 111 of file cache.c.

112 {
114 }
#define TRUE
Definition: types.h:120
BOOLEAN CacheManagerDataInvalid
Definition: cache.c:32

Referenced by DriveMapInstallInt13Handler(), and DriveMapRemoveInt13Handler().

◆ CacheReadDiskSectors()

BOOLEAN CacheReadDiskSectors ( UCHAR  DiskNumber,
ULONGLONG  StartSector,
ULONG  SectorCount,
PVOID  Buffer 
)

Definition at line 116 of file cache.c.

117 {
118  PCACHE_BLOCK CacheBlock;
119  ULONG StartBlock;
120  ULONG SectorOffsetInStartBlock;
121  ULONG CopyLengthInStartBlock;
122  ULONG EndBlock;
123  ULONG SectorOffsetInEndBlock;
124  ULONG BlockCount;
125  ULONG Idx;
126 
127  TRACE("CacheReadDiskSectors() DiskNumber: 0x%x StartSector: %I64d SectorCount: %d Buffer: 0x%x\n", DiskNumber, StartSector, SectorCount, Buffer);
128 
129  // If we aren't initialized yet then they can't do this
131  {
132  return FALSE;
133  }
134 
135  //
136  // Calculate which blocks we must cache
137  //
138  StartBlock = (ULONG)(StartSector / CacheManagerDrive.BlockSize);
139  SectorOffsetInStartBlock = (ULONG)(StartSector % CacheManagerDrive.BlockSize);
140  CopyLengthInStartBlock = (ULONG)((SectorCount > (CacheManagerDrive.BlockSize - SectorOffsetInStartBlock)) ? (CacheManagerDrive.BlockSize - SectorOffsetInStartBlock) : SectorCount);
141  EndBlock = (ULONG)((StartSector + (SectorCount - 1)) / CacheManagerDrive.BlockSize);
142  SectorOffsetInEndBlock = (ULONG)(1 + (StartSector + (SectorCount - 1)) % CacheManagerDrive.BlockSize);
143  BlockCount = (EndBlock - StartBlock) + 1;
144  TRACE("StartBlock: %d SectorOffsetInStartBlock: %d CopyLengthInStartBlock: %d EndBlock: %d SectorOffsetInEndBlock: %d BlockCount: %d\n", StartBlock, SectorOffsetInStartBlock, CopyLengthInStartBlock, EndBlock, SectorOffsetInEndBlock, BlockCount);
145 
146  //
147  // Read the first block into the buffer
148  //
149  if (BlockCount > 0)
150  {
151  //
152  // Get cache block pointer (this forces the disk sectors into the cache memory)
153  //
154  CacheBlock = CacheInternalGetBlockPointer(&CacheManagerDrive, StartBlock);
155  if (CacheBlock == NULL)
156  {
157  return FALSE;
158  }
159 
160  //
161  // Copy the portion requested into the buffer
162  //
164  (PVOID)((ULONG_PTR)CacheBlock->BlockData + (SectorOffsetInStartBlock * CacheManagerDrive.BytesPerSector)),
165  (CopyLengthInStartBlock * CacheManagerDrive.BytesPerSector));
166  TRACE("1 - RtlCopyMemory(0x%x, 0x%x, %d)\n", Buffer, ((ULONG_PTR)CacheBlock->BlockData + (SectorOffsetInStartBlock * CacheManagerDrive.BytesPerSector)), (CopyLengthInStartBlock * CacheManagerDrive.BytesPerSector));
167 
168  //
169  // Update the buffer address
170  //
171  Buffer = (PVOID)((ULONG_PTR)Buffer + (CopyLengthInStartBlock * CacheManagerDrive.BytesPerSector));
172 
173  //
174  // Update the block count
175  //
176  BlockCount--;
177  }
178 
179  //
180  // Loop through the middle blocks and read them into the buffer
181  //
182  for (Idx=StartBlock+1; BlockCount>1; Idx++)
183  {
184  //
185  // Get cache block pointer (this forces the disk sectors into the cache memory)
186  //
188  if (CacheBlock == NULL)
189  {
190  return FALSE;
191  }
192 
193  //
194  // Copy the portion requested into the buffer
195  //
197  CacheBlock->BlockData,
199  TRACE("2 - RtlCopyMemory(0x%x, 0x%x, %d)\n", Buffer, CacheBlock->BlockData, CacheManagerDrive.BlockSize * CacheManagerDrive.BytesPerSector);
200 
201  //
202  // Update the buffer address
203  //
205 
206  //
207  // Update the block count
208  //
209  BlockCount--;
210  }
211 
212  //
213  // Read the last block into the buffer
214  //
215  if (BlockCount > 0)
216  {
217  //
218  // Get cache block pointer (this forces the disk sectors into the cache memory)
219  //
220  CacheBlock = CacheInternalGetBlockPointer(&CacheManagerDrive, EndBlock);
221  if (CacheBlock == NULL)
222  {
223  return FALSE;
224  }
225 
226  //
227  // Copy the portion requested into the buffer
228  //
230  CacheBlock->BlockData,
231  SectorOffsetInEndBlock * CacheManagerDrive.BytesPerSector);
232  TRACE("3 - RtlCopyMemory(0x%x, 0x%x, %d)\n", Buffer, CacheBlock->BlockData, SectorOffsetInEndBlock * CacheManagerDrive.BytesPerSector);
233 
234  //
235  // Update the buffer address
236  //
237  Buffer = (PVOID)((ULONG_PTR)Buffer + (SectorOffsetInEndBlock * CacheManagerDrive.BytesPerSector));
238 
239  //
240  // Update the block count
241  //
242  BlockCount--;
243  }
244 
245  return TRUE;
246 }
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
uint32_t ULONG_PTR
Definition: typedefs.h:63
PCACHE_BLOCK CacheInternalGetBlockPointer(PCACHE_DRIVE CacheDrive, ULONG BlockNumber)
Definition: blocklist.c:28
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
void * PVOID
Definition: retypes.h:9
PVOID BlockData
Definition: cache.h:43
#define TRACE(s)
Definition: solgame.cpp:4
ULONG BlockSize
Definition: cache.h:59
CACHE_DRIVE CacheManagerDrive
Definition: cache.c:30
BOOLEAN CacheManagerInitialized
Definition: cache.c:31
ULONG SectorCount
Definition: part_xbox.c:32
unsigned int ULONG
Definition: retypes.h:1
ULONG BytesPerSector
Definition: cache.h:57

Referenced by Ext2ReadVolumeSectors().

◆ CacheReleaseMemory()

BOOLEAN CacheReleaseMemory ( ULONG  MinimumAmountToRelease)

Definition at line 296 of file cache.c.

297 {
298  ULONG AmountReleased;
299 
300  TRACE("CacheReleaseMemory() MinimumAmountToRelease = %d\n", MinimumAmountToRelease);
301 
302  // If we aren't initialized yet then they can't do this
304  {
305  return FALSE;
306  }
307 
308  // Loop through and try to free the requested amount of memory
309  for (AmountReleased=0; AmountReleased<MinimumAmountToRelease; )
310  {
311  // Try to free a block
312  // If this fails then break out of the loop
314  {
315  break;
316  }
317 
318  // It succeeded so increment the amount of memory we have freed
320  }
321 
322  // Return status
323  return (AmountReleased >= MinimumAmountToRelease);
324 }
BOOLEAN CacheInternalFreeBlock(PCACHE_DRIVE CacheDrive)
Definition: blocklist.c:142
#define TRACE(s)
Definition: solgame.cpp:4
ULONG BlockSize
Definition: cache.h:59
CACHE_DRIVE CacheManagerDrive
Definition: cache.c:30
BOOLEAN CacheManagerInitialized
Definition: cache.c:31
unsigned int ULONG
Definition: retypes.h:1
ULONG BytesPerSector
Definition: cache.h:57

◆ DBG_DEFAULT_CHANNEL()

DBG_DEFAULT_CHANNEL ( CACHE  )

Variable Documentation

◆ CacheBlockCount

◆ CacheManagerDataInvalid

BOOLEAN CacheManagerDataInvalid = FALSE

Definition at line 32 of file cache.c.

Referenced by CacheInitializeDrive(), and CacheInvalidateCacheData().

◆ CacheManagerDrive

CACHE_DRIVE CacheManagerDrive

Definition at line 30 of file cache.c.

Referenced by CacheInitializeDrive(), CacheReadDiskSectors(), and CacheReleaseMemory().

◆ CacheManagerInitialized

BOOLEAN CacheManagerInitialized = FALSE

Definition at line 31 of file cache.c.

Referenced by CacheInitializeDrive(), CacheReadDiskSectors(), and CacheReleaseMemory().

◆ CacheSizeCurrent

◆ CacheSizeLimit

SIZE_T CacheSizeLimit = 0