Skip to content

refactor: remove magic numbers#64

Open
Alan632 wants to merge 8 commits into
microsoft:mainfrom
Alan632:echo_driver-remove_magic_numbers
Open

refactor: remove magic numbers#64
Alan632 wants to merge 8 commits into
microsoft:mainfrom
Alan632:echo_driver-remove_magic_numbers

Conversation

@Alan632

@Alan632 Alan632 commented Jun 10, 2026

Copy link
Copy Markdown

Summary

Decompose magic numbers into const's adding relevant documentation.

@Alan632 Alan632 self-assigned this Jun 10, 2026

/// Non-zero char literal (of one to four chars) for pool tag used in
/// `ExAllocatePool2`
const MEMORY_TAG: u32 = u32::from_be_bytes(*b"sam1");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't match the existing tag

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that it was different from the original C echo sample driver so I changed it to match the C version.

https://github.com/microsoft/Windows-driver-samples/blob/e99ae832b48b245404f9bd750af4864247b061e8/general/echo/kmdf/driver/DriverSync/queue.c#L652

Comment thread general/echo/kmdf/exe/src/main.rs Outdated
Comment on lines +73 to +82
/// Transfer length, in bytes, for the first synchronous write/read test.
static SYNC_TEST_SMALL_LENGTH: u32 = 512;
/// Transfer length, in bytes, for the second synchronous write/read test.
static SYNC_TEST_LARGE_LENGTH: u32 = 30 * 1024;
/// Completion key associated with the device handle on the I/O completion port.
static COMPLETION_PORT_KEY: usize = 1;
/// Number of concurrent threads allowed to run for the I/O completion port.
/// Zero lets the system allow as many concurrent threads as there are
/// processors.
static COMPLETION_PORT_CONCURRENT_THREADS: u32 = 0;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are these static instead of const?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I was just following the semantics of the previous static variable definitions above them.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment on lines +33 to +45
/// Number of 100-nanosecond intervals in one millisecond. WDF relative times
/// are expressed in 100-nanosecond units, so this converts a millisecond delay
/// into the units WDF expects.
const RELATIVE_100_NS_INTERVALS_PER_MS: i64 = 10_000;

/// Delay, in milliseconds, before the periodic timer first fires once the
/// device has started.
const START_TIMER_DUE_TIME_MS: i64 = 100;

/// 100ms relative time (in 100-nanosecond units). The negative sign marks the
/// value as a relative (rather than absolute) timeout.
const WDF_REL_TIMEOUT_IN_MS: i64 = -START_TIMER_DUE_TIME_MS * RELATIVE_100_NS_INTERVALS_PER_MS;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than introduce more constants here, is it possible to just use the conversion function in the rust standard library: https://doc.rust-lang.org/core/time/struct.Duration.html

they are available in core

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, I changed the extra timer constants to utilize Duration, it should help with readability now.

/// Set timer period in ms
const TIMER_PERIOD: u32 = 1000 * 10;
/// Max write length, in bytes, for testing
const MAX_WRITE_LENGTH: usize = 40 * BYTES_PER_KB;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless these are actually being used in multiple places, it'd be nice to scope them locally to where they are needed

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I applied this to the other constants as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants