Re: Passing parameters to a C function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: Rodrigo Sakai <rodrigo(dot)sakai(at)poli(dot)usp(dot)br>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Passing parameters to a C function
Date: 2007-05-30 15:00:39
Message-ID: 3825.1180537239@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Martijn van Oosterhout <kleptog(at)svana(dot)org> writes:
> On Wed, May 30, 2007 at 11:26:01AM -0300, Rodrigo Sakai wrote:
>> But using version 1 calling convention it won't work! So, how can I pass the
>> 'a' and 'b' variables in complex_add(?, ?)?

> Use the DirectFunctionCalln functions in fmgr.

There are boatloads of examples in the existing datatype code, for
instance this function in geo_ops.c, which is just applying
close_sb() followed by dist_pb():

Datum
dist_sb(PG_FUNCTION_ARGS)
{
LSEG *lseg = PG_GETARG_LSEG_P(0);
BOX *box = PG_GETARG_BOX_P(1);
Point *tmp;
Datum result;

tmp = DatumGetPointP(DirectFunctionCall2(close_sb,
LsegPGetDatum(lseg),
BoxPGetDatum(box)));
result = DirectFunctionCall2(dist_pb,
PointPGetDatum(tmp),
BoxPGetDatum(box));

PG_RETURN_DATUM(result);
}

All that casting to and from Datum is a bit of a pain, but it's worth it
to have a uniform, portable function API.

I strongly suggest adding some appropriate cast macros for your own
datatype, like the ones being used here, instead of dealing directly
with low-level operations like PG_GETARG_POINTER.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Gregory Stark 2007-05-30 15:02:26 Re: TOAST usage setting
Previous Message Martijn van Oosterhout 2007-05-30 14:41:20 Re: Passing parameters to a C function