How to find size of virtual address space?

I'm wondering if there is a standard easy to find out the size of the virtual address space? On x86-64 there are only 48 bits available, and I could hard code that in a cfg statement, but it would be lovely to be able to write portable code that doesn't rely on my reading Wikipedia correctly.

My goal is to take advantage of the fact that the number of elements in my data structure can't grow to be greater than 2^48 (or correct number for other architectures).

Unfortunately for most hardware related matters, correctness often depends on the programmer's reading of Wikipedia or better still manuals. For an instance although x86_64 allows 48 bit for its virtual address space, the most recent amd64 architecture (ie, x86_64 but specifically for chips made by AMD) allows for 52 bits - support depends on processor model.

Rust won't allow more than isize::MAX bytes for a struct, so it is an effective upper bound on containers like arrays, vecs, etc.

It's not a feature of x86-64 ISA. It's a feature of individual CPU products currently manufactured. In the future when the market wants larger address space manufacturers can make a new products with more transistors without ISA level breaking change.

x86_64 is a highly fragmented platform. How can we detect features of the CPU at runtime? Use cpuid instruction.

EAX=80000008h: Virtual and Physical address Sizes
Returns largest virtual and physical address sizes in EAX.

Bits 07-00: #Physical Address Bits.
Bits 15-8: #Linear Address Bits.
Bits 31-16: Reserved = 0.
3 Likes

That's too bad. I had hoped to know the upper bound at compile time so I could determine the number of bins required in my new AppendOnlyVec to store the largest possibly allocated vector.