Page MenuHomeFreeBSD

Have a type-agnostic macro to determine the [u]int maximum value
Needs ReviewPublic

Authored by rscheff on Jan 13 2023, 11:02 AM.
Tags
None
Referenced Files
Unknown Object (File)
Jan 10 2024, 2:12 AM
Unknown Object (File)
May 21 2023, 12:23 AM
Unknown Object (File)
Apr 10 2023, 5:49 AM
Unknown Object (File)
Mar 25 2023, 6:03 PM
Unknown Object (File)
Feb 28 2023, 6:40 PM
Unknown Object (File)
Feb 10 2023, 7:16 PM
Subscribers
None

Details

Summary

Currently, when the size of a variable is changed, any
comparisons with INT_MAX, CHAR_MAX and so on have to be manually adjusted.

While these hardcoded values have some portability as they map to
machine-specific values, the case of changing the type of a variable is
not covered.

This provides a (semi) portable way of determining the maximum value
a variable of a given size can hold.

The assumption is to run on a 8-bit per byte architecture, and it will
not work for bit-fields.

Other than this, the entire range of UCHAR_MAX, UINT_MAX, USHRT_MAX,
ULONG_MAX, ULLONG_MAX could be replaced with UTYPE_MAX(variable)

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 49079
Build 45968: arc lint + arc unit

Event Timeline

rscheff created this revision.
sys/sys/limits.h
109

U for unsigned
S for signed

UTYPE_MAX
STYPE_MAX

Then also declare the minimum variant?

UTYPE_MIN
STYPE_MIN

Thinking more about this, maybe follow the style of min_t, like

U_MAX_T(type)
S_MAX_T(type)
U_MIN_T(type)
S_MIN_T(type)

It's more readable?