Understanding Rust memory allocation API?

Basically, @vedantroy, you should know that most implementations of Rust allocation APIs do already bundle the associated allocation length within their own bookkeeping, and thus can/may disregard the .size parameter of a dealloc call.

That being said, since there may very well be some platform where some implementation may wish to receive the .size data on dealloc explicitly (IIRC, this may be the case on Windows), then Rust has made the choice to make that part of its API, to be maximally compatible with allocator implementations.

Obviously, this leads to the "freeing end" having to meet this API requirement, as per the very definition of an API contract.

This was deemed acceptable, since when requesting to free something, the owner ought to already be able to know, either statically / at compile-time, or at runtime, how big the thing they own is. Indeed, more generally, one ought to remember at all times how big the thing they are manipulating is :wink:

Aside

I also mentioned some alternatives in the post you mentioned

3 Likes