Expand palloc/pg_malloc API
Peter Eisentraut <peter.eisentraut@enterprisedb.com>
From: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2022-05-17T11:41:03Z
Lists: pgsql-hackers
Attachments
- 0001-Expand-palloc-pg_malloc-API.patch (text/plain) patch 0001
This adds additional variants of palloc, pg_malloc, etc. that
encapsulate common usage patterns and provide more type safety.
Examples:
- result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
+ result = palloc_obj(IndexBuildResult);
- collector->tuples = (IndexTuple *) palloc(sizeof(IndexTuple) *
collector->lentuples);
+ collector->tuples = palloc_array(IndexTuple, collector->lentuples);
One common point is that the new interfaces all have a return type that
automatically matches what they are allocating, so you don't need any
casts nor have to manually make sure the size matches the expected
result. Besides the additional safety, the notation is also more
compact, as you can see above.
Inspired by the talloc library.
The interesting changes are in fe_memutils.h and palloc.h. The rest of
the patch is just randomly sprinkled examples to test/validate the new
additions.
Commits
-
Add repalloc0 and repalloc0_array
- b4b7ce8061d3 16.0 landed
-
Expand palloc/pg_malloc API for more type safety
- 2864f7755611 10.23 landed
- e962235fe1f6 11.18 landed
- 7dd9b469bc56 12.13 landed
- 172882292451 13.9 landed
- b7f37af7c195 14.6 landed
- 7fe55d5e12b6 15.0 landed
- 2016055a92f2 16.0 landed
-
Assorted examples of expanded type-safer palloc/pg_malloc API
- 5015e1e1b58f 16.0 landed