On 16.11.26 23:02, Tristan Partin wrote:
After talking to Peter off-list, I've come up with a new strategy by
splitting up __attribute__((malloc)) and
__attribute__((malloc(deallocator, arg))).
For those that are unaware, these two attributes, though named the same,
have two separate meanings:
malloc:
Attribute malloc indicates that a function is malloc-like, i.e., that
the pointer P returned by the function cannot alias any other pointer
valid when the function returns, and moreover no pointers to valid
objects occur in any storage addressed by P. In addition, GCC predicts
that a function with the attribute returns non-null in most cases.
Attached are patches which add pg_attribute_malloc and apply it to
various memory allocating functions.
This (variant of the) attribute does not provide any extra warnings or
static analysis, it only provides optimization opportunities. So I
think this should be accompanied by some evidence that this has useful
effects.
What I'm unclear about is exactly how this description is to be
interpreted for things like palloc(). The returned pointer does not
alias any other pointer that the calling function would normally use,
but the palloc machinery does (or might) save the pointer internally for
bookkeeping and cleanup, which might technically violate that
description? (But libc presumably does similar things for plain malloc?)
Also note that we already apply pg_attribute_returns_nonnull in many
cases, so some of the optimization opportunities mentioned for the
malloc attribute are already obtained that way.
The annotations in fe_memutils.h that are more straightforward wrappers
around libc functionality seem fine.
That said, you can observe pretty significant generated code size
reduction in copyfuncs.o, presumably because the compiler can now know
that the "from" node and the newly allocated "newnode" don't overlap.
So one could attempt to make an argument that this could speed up the
planner, for example. But it would be great to see some data on this.
Maybe that malloc(deallocator) attribute would have been the easier one
to start with ... ;-)