Skip to content

filesystem_space.cpp: Consider avoiding reinterpret_cast #357

Description

static_assert(sizeof(uintmax_t) == sizeof(ULARGE_INTEGER) && alignof(uintmax_t) == alignof(ULARGE_INTEGER),
"Size and alignment must match for reinterpret_cast<PULARGE_INTEGER>");
[[nodiscard]] __std_win_error _Fs_space_attempt(wchar_t* const _Temp_buffer, const DWORD _Temp_buffer_characters,
const wchar_t* const _Target, uintmax_t* const _Available, uintmax_t* const _Total_bytes,
uintmax_t* const _Free_bytes) noexcept {
if (GetVolumePathNameW(_Target, _Temp_buffer, _Temp_buffer_characters)) {
if (GetDiskFreeSpaceExW(_Temp_buffer, reinterpret_cast<PULARGE_INTEGER>(_Available),
reinterpret_cast<PULARGE_INTEGER>(_Total_bytes), reinterpret_cast<PULARGE_INTEGER>(_Free_bytes))) {
return __std_win_error::_Success;
}
}
return __std_win_error{GetLastError()};
}

In #356 (comment), Charles Milette (@sylveon) asked whether this static_assert and reinterpret_cast technique is necessary. I believe that we could avoid this by defining ULARGE_INTEGER local variables, passing their addresses to GetDiskFreeSpaceExW(), and then if successful, writing their QuadParts to _Available, _Total_bytes, and _Free_bytes. This would involve a few more instructions, but I don't believe that the code size and runtime impact would be observable, and avoiding reinterpret_cast may be worth it.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSomething can be improvedwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions