Skip to content

Commit

Permalink
Firefly driver: Postpone variable definitions until first use
Browse files Browse the repository at this point in the history
Motivation:
* Improves readability and maintainability.
* Have been supported since C99.
* Can potentially reduce stack size usage when variables are confined to a local scope.

Downside:
* It's a bit more cumbersome to manually determine the stack size of a function when all local variables are no longer listed together. However, tools can still do this effectively.
  • Loading branch information
Fredrik Orderud committed Jan 31, 2024
1 parent c657bc7 commit 4c8f0b6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 36 deletions.
15 changes: 6 additions & 9 deletions hid/firefly/driver/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ Return Value:
--*/
{
WDF_OBJECT_ATTRIBUTES attributes;
NTSTATUS status;
PDEVICE_CONTEXT pDeviceContext;
WDFDEVICE device;
WDFMEMORY memory;
size_t bufferLength;

UNREFERENCED_PARAMETER(Driver);

PAGED_CODE();
Expand All @@ -69,9 +62,11 @@ Return Value:
//
WdfFdoInitSetFilter(DeviceInit);

WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, DEVICE_CONTEXT);

status = WdfDeviceCreate(&DeviceInit, &attributes, &device);
WDFDEVICE device;
NTSTATUS status = WdfDeviceCreate(&DeviceInit, &attributes, &device);
if (!NT_SUCCESS(status)) {
KdPrint(("FireFly: WdfDeviceCreate, Error %x\n", status));
return status;
Expand All @@ -80,7 +75,7 @@ Return Value:
//
// Driver Framework always zero initializes an objects context memory
//
pDeviceContext = WdfObjectGet_DEVICE_CONTEXT(device);
PDEVICE_CONTEXT pDeviceContext = WdfObjectGet_DEVICE_CONTEXT(device);

//
// Initialize our WMI support
Expand All @@ -104,6 +99,7 @@ Return Value:
//
attributes.ParentObject = device;

WDFMEMORY memory;
status = WdfDeviceAllocAndQueryProperty(device,
DevicePropertyPhysicalDeviceObjectName,
NonPagedPoolNx,
Expand All @@ -115,6 +111,7 @@ Return Value:
return STATUS_UNSUCCESSFUL;
}

size_t bufferLength;
pDeviceContext->PdoName.Buffer = WdfMemoryGetBuffer(memory, &bufferLength);

if (pDeviceContext->PdoName.Buffer == NULL) {
Expand Down
6 changes: 2 additions & 4 deletions hid/firefly/driver/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ DriverEntry(
IN PUNICODE_STRING RegistryPath
)
{
WDF_DRIVER_CONFIG params;
NTSTATUS status;

KdPrint(("FireFly: DriverEntry - WDF version built on %s %s\n",
__DATE__, __TIME__));

WDF_DRIVER_CONFIG params;
WDF_DRIVER_CONFIG_INIT(
&params,
FireFlyEvtDeviceAdd
Expand All @@ -50,7 +48,7 @@ DriverEntry(
// Create the framework WDFDRIVER object, with the handle
// to it returned in Driver.
//
status = WdfDriverCreate(DriverObject,
NTSTATUS status = WdfDriverCreate(DriverObject,
RegistryPath,
WDF_NO_OBJECT_ATTRIBUTES,
&params,
Expand Down
28 changes: 11 additions & 17 deletions hid/firefly/driver/vfeature.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,16 @@ Return Value:
--*/
{
WDF_MEMORY_DESCRIPTOR inputDescriptor, outputDescriptor;
NTSTATUS status;
HID_COLLECTION_INFORMATION collectionInformation = {0};
PHIDP_PREPARSED_DATA preparsedData;
HIDP_CAPS caps;
USAGE usage;
ULONG usageLength;
PCHAR report;
WDFIOTARGET hidTarget;
WDF_IO_TARGET_OPEN_PARAMS openParams;

PAGED_CODE();

//
// Preinit for error.
//
preparsedData = NULL;
report = NULL;
hidTarget = NULL;
PHIDP_PREPARSED_DATA preparsedData = NULL;
PCHAR report = NULL;
WDFIOTARGET hidTarget = NULL;

status = WdfIoTargetCreate(WdfObjectContextGetObject(DeviceContext),
NTSTATUS status = WdfIoTargetCreate(WdfObjectContextGetObject(DeviceContext),
WDF_NO_OBJECT_ATTRIBUTES,
&hidTarget);
if (!NT_SUCCESS(status)) {
Expand All @@ -99,6 +88,7 @@ Return Value:
//
// Open it up, write access only!
//
WDF_IO_TARGET_OPEN_PARAMS openParams;
WDF_IO_TARGET_OPEN_PARAMS_INIT_OPEN_BY_NAME(
&openParams,
&DeviceContext->PdoName,
Expand All @@ -117,6 +107,8 @@ Return Value:
}


WDF_MEMORY_DESCRIPTOR outputDescriptor;
HID_COLLECTION_INFORMATION collectionInformation = {0};
WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(&outputDescriptor,
(PVOID) &collectionInformation,
sizeof(HID_COLLECTION_INFORMATION));
Expand Down Expand Up @@ -165,6 +157,7 @@ Return Value:
//
// Now get the capabilities.
//
HIDP_CAPS caps;
RtlZeroMemory(&caps, sizeof(HIDP_CAPS));

status = HidP_GetCaps(preparsedData, &caps);
Expand Down Expand Up @@ -195,8 +188,8 @@ Return Value:
//
// Edit the report to reflect the enabled feature
//
usage = FeatureId;
usageLength = 1;
USAGE usage = FeatureId;
ULONG usageLength = 1;

status = HidP_SetUsages(
HidP_Feature,
Expand All @@ -214,6 +207,7 @@ Return Value:
}
}

WDF_MEMORY_DESCRIPTOR inputDescriptor;
WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(&inputDescriptor,
report,
caps.FeatureReportByteLength);
Expand Down
11 changes: 5 additions & 6 deletions hid/firefly/driver/wmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,38 @@ WmiInitialize(
PDEVICE_CONTEXT DeviceContext
)
{
WDF_WMI_PROVIDER_CONFIG providerConfig;
WDF_WMI_INSTANCE_CONFIG instanceConfig;
WDF_OBJECT_ATTRIBUTES woa;
WDFWMIINSTANCE instance;
NTSTATUS status;
DECLARE_CONST_UNICODE_STRING(mofRsrcName, MOFRESOURCENAME);

UNREFERENCED_PARAMETER(DeviceContext);

PAGED_CODE();

status = WdfDeviceAssignMofResourceName(Device, &mofRsrcName);
NTSTATUS status = WdfDeviceAssignMofResourceName(Device, &mofRsrcName);
if (!NT_SUCCESS(status)) {
KdPrint(("FireFly: Error in WdfDeviceAssignMofResourceName %x\n", status));
return status;
}

WDF_WMI_PROVIDER_CONFIG providerConfig;
WDF_WMI_PROVIDER_CONFIG_INIT(&providerConfig, &FireflyDeviceInformation_GUID);
providerConfig.MinInstanceBufferSize = sizeof(FireflyDeviceInformation);

WDF_WMI_INSTANCE_CONFIG instanceConfig;
WDF_WMI_INSTANCE_CONFIG_INIT_PROVIDER_CONFIG(&instanceConfig, &providerConfig);
instanceConfig.Register = TRUE;
instanceConfig.EvtWmiInstanceQueryInstance = EvtWmiInstanceQueryInstance;
instanceConfig.EvtWmiInstanceSetInstance = EvtWmiInstanceSetInstance;
instanceConfig.EvtWmiInstanceSetItem = EvtWmiInstanceSetItem;

WDF_OBJECT_ATTRIBUTES woa;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&woa, FireflyDeviceInformation);

//
// No need to store the WDFWMIINSTANCE in the device context because it is
// passed back in the WMI instance callbacks and is not referenced outside
// of those callbacks.
//
WDFWMIINSTANCE instance;
status = WdfWmiInstanceCreate(Device, &instanceConfig, &woa, &instance);

if (NT_SUCCESS(status)) {
Expand Down

0 comments on commit 4c8f0b6

Please sign in to comment.