Re: Expand palloc/pg_malloc API

From: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>
To: Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Expand palloc/pg_malloc API
Date: 2022-05-17 11:49:19
Message-ID: CALj2ACU+9qnHVq82v6CjG+LD5yTc=gnXxFc2SJ=uPJFtSA3=0Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, May 17, 2022 at 5:11 PM Peter Eisentraut
<peter(dot)eisentraut(at)enterprisedb(dot)com> wrote:
>
> 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.

It seems interesting. Are we always type-casting explicitly the output
of palloc/palloc0? Does this mean the compiler takes care of
type-casting the returned void * to the target type?

I see lots of instances where there's no explicit type-casting to the
target variable type -
retval = palloc(sizeof(GISTENTRY));
Interval *p = palloc(sizeof(Interval));
macaddr *v = palloc0(sizeof(macaddr)); and so on.

Regards,
Bharath Rupireddy.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nikolay Shaplov 2022-05-17 12:09:55 Re: [PATCH] New [relation] option engine
Previous Message Mahendra Singh Thalor 2022-05-17 11:44:23 Re: Collecting statistics about contents of JSONB columns