News for hackers. Stuff that matters.
Re:The Horror in the Standard Library by asveikau on Saturday May 06, 2017 @12:15AM (#14278639)
If free() took a size, what would that buy you?

You are aware that malloc implementations tend to stick the size just before the part returned to the caller, right? eg. let's say you store size at p, return p+4 to caller, then have free() subtract 4 again to get at this "header"... So I'm guessing that's not your suggestion because free() wouldn't work at all without that kind of hack. Or more broadly, free() or realloc() and others already need to have some way to determine the size of the allocation based on the pointer, so they track that somehow in a way opaque to the caller.

So then what...? You want programs to be able to give back a prefix of the buffer? or...?

Re:The Horror in the Standard Library by jzwinck on Saturday May 06, 2017 @01:09AM (#14278787)
With the status quo, say you call malloc() once. After a long while you call free(). Now the first thing free() needs to do is figure out the size, which is stored "before" the pointer. Until that load is complete, it can't know how large the allocation was, so it can't prefetch other necessary data, e.g. the free list for a given chunk size. This could add 120 cycles of latency to get a value that the caller probably already had in L1 cache or could compute with a single multiply.
Re:The Horror in the Standard Library by spc476 on Saturday May 06, 2017 @01:15AM (#14278801)
Or a call to strlen() which makes your 120 cycles of latency start to look good ...

Edit to add: I've used both malloc()/free() and a custom memory allocation API that required the size to be passed in. I found the second API to be much more of a pain to use over the long term. Besides, it wastes memory because the memory API will have to track the size anyway to detect misuse of the API (or else blindly trust that the right size is passed in and hilarity ensues when it's not ...).

Re:The Horror in the Standard Library by makapuf on Saturday May 06, 2017 @04:11AM (#14279263)
Well you could use free() whenever it's practical and free_sz () when you can get the performance.
Re:The Horror in the Standard Library by asveikau on Saturday May 06, 2017 @04:24AM (#14279298)
I actually added this line:

> Or more broadly, free() or realloc() and others already need to have some way to determine the size of the allocation based on the pointer

When I remembered I'd read a malloc implementation somewhere which actually didn't store the size in a header, but bakes in assumptions that you can determine the size of an allocation based on its address. So my naive thing isn't the only option, and I think if you have the kinds of concerns you raise there could be mitigations to be had.

Re:The Horror in the Standard Library by naasking on Saturday May 06, 2017 @01:27AM (#14278839)
They only store the size in a header because free() needs to know the size on deallocation. Think about all the C string functions that take buffer length parameters and what an outcry there would be if we eliminated all the size parameters and placed string lengths as headers before the buffer itself.

It's terrible for performance because you're giving up control, even if it is a little safer overall.

Re:The Horror in the Standard Library by asveikau on Saturday May 06, 2017 @04:21AM (#14279292)
That'd be silly. Among many other problems, it would mean you can't do things like take a substring of a const buffer via pointer arithmetic, because you'd need to add a header in the middle.

It's better to pick exactly one of: (1) embrace the true nature of allocations and figure out lengths yourself or (2) if you are less comfortable with that, use some other language which doesn't expose any of this.

Re:The Horror in the Standard Library by naasking on Saturday May 06, 2017 @09:47AM (#14280208)
> That'd be silly. Among many other problems, it would mean you can't do things like take a substring of a const buffer via pointer arithmetic, because you'd need to add a header in the middle

Right, and now you understand​ why free should have always accepted a size parameter, just like malloc.

Re:The Horror in the Standard Library by asveikau on Saturday May 06, 2017 @10:47AM (#14280460)
No, I don't. Doing zero-copy substrings of a read only buffer and an allocator chopping up a writable buffer into multiple chunks are very different scenarios.
Re:The Horror in the Standard Library by naasking on Saturday May 06, 2017 @11:41PM (#14283724)
Not for this purpose. It's all about knowing where the boundaries are.