[FREELDR] In PXE, do not cache the current file

We can already restart reading the whole file if seeking backward,
and there is currently no more use case of backward seek.

By doing so, we gain 1MB of memory when using PXE.
This commit is contained in:
Hervé Poussineau 2021-01-30 11:01:19 +01:00
parent ebb7088b02
commit 90f9666308

View file

@ -32,7 +32,6 @@ static ULONG _FileSize = 0;
static ULONG _FilePosition = 0;
static ULONG _PacketPosition = 0;
static UCHAR _Packet[1024]; // Should be a value which can be transferred well in one packet over the network
static UCHAR* _CachedFile = NULL;
static ULONG _CachedLength = 0;
static PPXE
@ -127,11 +126,6 @@ static ARC_STATUS PxeClose(ULONG FileId)
return EIO;
_OpenFile = NO_FILE;
if (_CachedFile)
{
FrLdrTempFree(_CachedFile, TAG_PXE_FILE);
_CachedFile = NULL;
}
return ESUCCESS;
}
@ -186,11 +180,6 @@ static ARC_STATUS PxeOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId)
}
_FileSize = sizeData.FileSize;
if (_FileSize < 1024 * 1024)
{
_CachedFile = FrLdrTempAlloc(_FileSize, TAG_PXE_FILE);
// Don't check for allocation failure, we support _CachedFile == NULL
}
_CachedLength = 0;
RtlZeroMemory(&openData, sizeof(openData));
@ -199,14 +188,7 @@ static ARC_STATUS PxeOpen(CHAR* Path, OPENMODE OpenMode, ULONG* FileId)
openData.PacketSize = sizeof(_Packet);
if (!CallPxe(PXENV_TFTP_OPEN, &openData))
{
if (_CachedFile)
{
FrLdrTempFree(_CachedFile, TAG_PXE_FILE);
_CachedFile = NULL;
}
return ENOENT;
}
_FilePosition = 0;
_PacketPosition = 0;
@ -236,10 +218,7 @@ static ARC_STATUS PxeRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
i = N;
else
i = _CachedLength - _FilePosition;
if (_CachedFile)
RtlCopyMemory(Buffer, _CachedFile + _FilePosition, i);
else
RtlCopyMemory(Buffer, _Packet + _FilePosition - _PacketPosition, i);
RtlCopyMemory(Buffer, _Packet + _FilePosition - _PacketPosition, i);
_FilePosition += i;
Buffer = (UCHAR*)Buffer + i;
*Count += i;
@ -249,8 +228,6 @@ static ARC_STATUS PxeRead(ULONG FileId, VOID* Buffer, ULONG N, ULONG* Count)
if (!CallPxe(PXENV_TFTP_READ, &readData))
return EIO;
if (_CachedFile)
RtlCopyMemory(_CachedFile + _CachedLength, _Packet, readData.BufferSize);
_PacketPosition = _CachedLength;
_CachedLength += readData.BufferSize;
}
@ -268,7 +245,7 @@ static ARC_STATUS PxeSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMo
if (Position->HighPart != 0 || SeekMode != SeekAbsolute)
return EINVAL;
if (!_CachedFile && Position->LowPart < _FilePosition)
if (Position->LowPart < _FilePosition)
{
// Close and reopen the file to go to position 0
if (PxeClose(FileId) != ESUCCESS)
@ -286,10 +263,6 @@ static ARC_STATUS PxeSeek(ULONG FileId, LARGE_INTEGER* Position, SEEKMODE SeekMo
{
if (!CallPxe(PXENV_TFTP_READ, &readData))
return EIO;
if (_CachedFile)
{
RtlCopyMemory(_CachedFile + _CachedLength, _Packet, readData.BufferSize);
}
_PacketPosition = _CachedLength;
_CachedLength += readData.BufferSize;
}