Skip to content

Commit

Permalink
Library - Add 16-128k IoBatch/EventResult pool for read and write
Browse files Browse the repository at this point in the history
Most read & write in the real world are made with 4-128k buffers. Adding pools for those sizes avoids the repetitive alloc and free cost.
  • Loading branch information
Liryna committed Feb 13, 2022
1 parent cf3ee14 commit a04b410
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 34 deletions.
3 changes: 2 additions & 1 deletion dokan/cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
VOID DispatchCleanup(PDOKAN_IO_EVENT IoEvent) {
CheckFileName(IoEvent->EventContext->Operation.Cleanup.FileName);

CreateDispatchCommon(IoEvent, 0, /*ClearBuffer=*/TRUE);
CreateDispatchCommon(IoEvent, 0, /*UseExtraMemoryPool=*/FALSE,
/*ClearNonPoolBuffer=*/TRUE);

IoEvent->EventResult->Status = STATUS_SUCCESS; // return success at any case

Expand Down
3 changes: 2 additions & 1 deletion dokan/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ VOID DispatchCreate(PDOKAN_IO_EVENT IoEvent) {

CheckFileName(fileName);

CreateDispatchCommon(IoEvent, 0, /*ClearBuffer=*/TRUE);
CreateDispatchCommon(IoEvent, 0, /*UseExtraMemoryPool=*/FALSE,
/*ClearNonPoolBuffer=*/TRUE);

assert(IoEvent->DokanOpenInfo == NULL);

Expand Down
3 changes: 2 additions & 1 deletion dokan/directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,8 @@ VOID DispatchDirectoryInformation(PDOKAN_IO_EVENT IoEvent) {

CreateDispatchCommon(IoEvent,
IoEvent->EventContext->Operation.Directory.BufferLength,
/*ClearBuffer=*/TRUE);
/*UseExtraMemoryPool=*/FALSE,
/*ClearNonPoolBuffer=*/TRUE);

IoEvent->EventResult->Operation.Directory.Index =
IoEvent->EventContext->Operation.Directory.FileIndex;
Expand Down
64 changes: 51 additions & 13 deletions dokan/dokan.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,23 @@ VOID HandleProcessIoFatalError(PDOKAN_INSTANCE DokanInstance,
OnDeviceIoCtlFailed(DokanInstance, Result);
}

VOID FreeIoEventResult(PEVENT_INFORMATION EventResult, BOOL PoolAllocated) {
VOID FreeIoEventResult(PEVENT_INFORMATION EventResult, ULONG EventResultSize,
BOOL PoolAllocated) {
if (EventResult) {
if (PoolAllocated) {
PushEventResult(EventResult);
if (EventResultSize <= DOKAN_EVENT_INFO_DEFAULT_SIZE) {
PushEventResult(EventResult);
} else if (EventResultSize <= DOKAN_EVENT_INFO_16K_SIZE) {
Push16KEventResult(EventResult);
} else if (EventResultSize <= DOKAN_EVENT_INFO_32K_SIZE) {
Push32KEventResult(EventResult);
} else if (EventResultSize <= DOKAN_EVENT_INFO_64K_SIZE) {
Push64KEventResult(EventResult);
} else if (EventResultSize <= DOKAN_EVENT_INFO_128K_SIZE) {
Push128KEventResult(EventResult);
} else {
assert(FALSE);
}
} else {
FreeEventResult(EventResult);
}
Expand Down Expand Up @@ -419,11 +432,13 @@ DWORD SendAndPullEventInformation(PDOKAN_IO_EVENT IoEvent,
DWORD lastError = 0;
PCHAR inputBuffer = NULL;
DWORD eventInfoSize = 0;
ULONG eventResultSize = 0;
PEVENT_INFORMATION eventInfo = NULL;
BOOL eventInfoPollAllocated = FALSE;

if (IoEvent && IoEvent->EventResult) {
eventInfo = IoEvent->EventResult;
eventResultSize = IoEvent->EventResultSize;
eventInfoPollAllocated = IoEvent->PoolAllocated;
inputBuffer = (PCHAR)eventInfo;
eventInfoSize =
Expand Down Expand Up @@ -455,7 +470,7 @@ DWORD SendAndPullEventInformation(PDOKAN_IO_EVENT IoEvent,
)) {
lastError = GetLastError();
if (eventInfo) {
FreeIoEventResult(eventInfo, eventInfoPollAllocated);
FreeIoEventResult(eventInfo, eventResultSize, eventInfoPollAllocated);
}
if (!IoBatch->DokanInstance->FileSystemStopped) {
DokanDbgPrintW(
Expand All @@ -466,7 +481,7 @@ DWORD SendAndPullEventInformation(PDOKAN_IO_EVENT IoEvent,
return lastError;
}
if (eventInfo) {
FreeIoEventResult(eventInfo, eventInfoPollAllocated);
FreeIoEventResult(eventInfo, eventResultSize, eventInfoPollAllocated);
}
return 0;
}
Expand Down Expand Up @@ -897,7 +912,7 @@ ULONG DispatchGetEventInformationLength(ULONG bufferSize) {
FIELD_OFFSET(EVENT_INFORMATION, Buffer[0]) + bufferSize);
}

VOID CreateDispatchCommon(PDOKAN_IO_EVENT IoEvent, ULONG SizeOfEventInfo, BOOL ClearBuffer) {
VOID CreateDispatchCommon(PDOKAN_IO_EVENT IoEvent, ULONG SizeOfEventInfo, BOOL UseExtraMemoryPool, BOOL ClearNonPoolBuffer) {
assert(IoEvent != NULL);
assert(IoEvent->EventResult == NULL && IoEvent->EventResultSize == 0);

Expand All @@ -906,15 +921,38 @@ VOID CreateDispatchCommon(PDOKAN_IO_EVENT IoEvent, ULONG SizeOfEventInfo, BOOL C
IoEvent->EventResultSize = DOKAN_EVENT_INFO_DEFAULT_SIZE;
IoEvent->PoolAllocated = TRUE;
} else {
IoEvent->EventResultSize =
DispatchGetEventInformationLength(SizeOfEventInfo);
IoEvent->EventResult = (PEVENT_INFORMATION)malloc(IoEvent->EventResultSize);
if (!IoEvent->EventResult) {
return;
if (UseExtraMemoryPool) {
if (SizeOfEventInfo <= (16 * 1024)) {
IoEvent->EventResult = Pop16KEventResult();
IoEvent->EventResultSize = DOKAN_EVENT_INFO_16K_SIZE;
IoEvent->PoolAllocated = TRUE;
} else if (SizeOfEventInfo <= (32 * 1024)) {
IoEvent->EventResult = Pop32KEventResult();
IoEvent->EventResultSize = DOKAN_EVENT_INFO_32K_SIZE;
IoEvent->PoolAllocated = TRUE;
} else if (SizeOfEventInfo <= (64 * 1024)) {
IoEvent->EventResult = Pop64KEventResult();
IoEvent->EventResultSize = DOKAN_EVENT_INFO_64K_SIZE;
IoEvent->PoolAllocated = TRUE;
} else if (SizeOfEventInfo <= (128 * 1024)) {
IoEvent->EventResult = Pop128KEventResult();
IoEvent->EventResultSize = DOKAN_EVENT_INFO_128K_SIZE;
IoEvent->PoolAllocated = TRUE;
}
}
if (IoEvent->EventResult == NULL) {
IoEvent->EventResultSize =
DispatchGetEventInformationLength(SizeOfEventInfo);
IoEvent->EventResult =
(PEVENT_INFORMATION)malloc(IoEvent->EventResultSize);
if (!IoEvent->EventResult) {
return;
}
ZeroMemory(IoEvent->EventResult,
ClearNonPoolBuffer
? IoEvent->EventResultSize
: FIELD_OFFSET(EVENT_INFORMATION, Buffer[0]));
}
ZeroMemory(IoEvent->EventResult,
ClearBuffer ? IoEvent->EventResultSize
: FIELD_OFFSET(EVENT_INFORMATION, Buffer[0]));
}
assert(IoEvent->EventResult &&
IoEvent->EventResultSize >=
Expand Down
Loading

0 comments on commit a04b410

Please sign in to comment.