On Jun 26, 2008, at 09:19, Alvaro Herrera wrote:
> PG_GETARG_TEXT_P can detoast the datum, which creates a copy.
Thanks. I've just completely refactored things to look more like the
approach taken by varlena.c, both in terms of when stuff gets freed
and in terms of coding style. It's more verbose, but I feel much more
comfortable with memory management now that I'm following a known
implementation more closely. :-)
So now I've changed citextcmp to this:
static int
citextcmp (text * left, text * right)
{ char * lcstr, * rcstr; int result;
lcstr = cilower( left ); rcstr = cilower( right );
result = varstr_cmp( cilower( left ), VARSIZE_ANY_EXHDR(left), cilower( right ),
VARSIZE_ANY_EXHDR(right) );
pfree( lcstr ); pfree( rcstr ); return result;
}
And now all of the operator functions are freeing memory using
PG_FREE_IF_COPY() like this:
Datum
citext_cmp(PG_FUNCTION_ARGS)
{text * left = PG_GETARG_TEXT_PP(0);text * right = PG_GETARG_TEXT_PP(1);int32 result;
result = citextcmp(left, right);
PG_FREE_IF_COPY(left, 0);PG_FREE_IF_COPY(right, 1);
PG_RETURN_INT32( result );
}
The only functions that don't do that are citext_smaller() and
citext_larger():
Datum
citext_smaller(PG_FUNCTION_ARGS)
{text * left = PG_GETARG_TEXT_PP(0);text * right = PG_GETARG_TEXT_PP(1);text * result;
result = citextcmp(left, right) < 0 ? left : right;PG_RETURN_TEXT_P(result);
}
This is just how varlena.c does it, but I am wondering if something
*should* be freed there.
Thanks a bunch!
Best,
David