Re:How to implement strings
by Asooka on Saturday November 03, 2018 @06:02PM
(#18372741)
You can get the size of a malloc'd block with malloc_usable_size. Can't say if it's actually O(1) though.
[ Parent
| on HN ]
You had my hopes up for a minute there, until I checked portability…
malloc_usable_size() is a GNU extension, so usable on Linux. MacOS has malloc_size().
One web page [1] says the GNU one is about 10 cpu cycles and the MacOS one is about 50 cpu cycles, so not the end of the world, but maybe a little long for string length checking.
I didn’t check any BSDs and I have to wonder what the effect of changing allocators in Linux has. Certainly they could have a performance impact and possible a ‘does not implement’ problem.
[1] https://lemire.me/blog/2017/09/15/how-fast-are-malloc_size-a...