Remix.run Logo
wahern an hour ago

To play devil's advocate, the problem with exposing more info is you limit the allocator design space.

1) If the app knew about and could use the additional underlying capacity, memory checkers wouldn't be able to find small overflows

2) Calling realloc instead of knowing existing capacity doesn't really provide better performance, at least not asymptotically.

3) If you need to know the requested allocation size, usually its for a dynamic array. Constantly requesting the size from the allocator would often have horrible performance if the size wasn't stored in adjacent metadata, but instead required a lookup operation, as is the case with hardened allocators. In practice hardened allocators probably wouldn't be a thing, or alternatively it would be idiomatic to store the size separately anyhow.

4) Reference counts would impose significant space and layout limitations despite most allocations never needing it.

There's another group who would argue the allocator should require passing the size and alignment to the free function, so allocators can be optimized better. Most of the time this is known statically. I think C2y will add APIs like this, though there's also a proposal to standardize an interface to query allocation capacity. Altogether I wouldn't be surprised if someday people appreciated the balance struck by malloc/free/realloc, especially as a minimal interface for overlaying application-specific allocators or injecting instrumentation. It has a certain elegance, in the sense of nothing left to remove.