From 220db7ccd8c88aafea4629f00e8be6f9f073ed00 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 25 Mar 2008 22:42:46 +0000 Subject: Simplify and standardize conversions between TEXT datums and ordinary C strings. This patch introduces four support functions cstring_to_text, cstring_to_text_with_len, text_to_cstring, and text_to_cstring_buffer, and two macros CStringGetTextDatum and TextDatumGetCString. A number of existing macros that provided variants on these themes were removed. Most of the places that need to make such conversions now require just one function or macro call, in place of the multiple notational layers that used to be needed. There are no longer any direct calls of textout or textin, and we got most of the places that were using handmade conversions via memcpy (there may be a few still lurking, though). This commit doesn't make any serious effort to eliminate transient memory leaks caused by detoasting toasted text objects before they reach text_to_cstring. We changed PG_GETARG_TEXT_P to PG_GETARG_TEXT_PP in a few places where it was easy, but much more could be done. Brendan Jurd and Tom Lane --- src/backend/utils/adt/arrayfuncs.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'src/backend/utils/adt/arrayfuncs.c') diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index b2beb18bc5f..f34ee3b773e 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.141 2008/02/29 20:58:33 alvherre Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.142 2008/03/25 22:42:43 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1533,39 +1533,33 @@ Datum array_dims(PG_FUNCTION_ARGS) { ArrayType *v = PG_GETARG_ARRAYTYPE_P(0); - text *result; char *p; - int nbytes, - i; + int i; int *dimv, *lb; - /* Sanity check: does it look like an array at all? */ - if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM) - PG_RETURN_NULL(); - - nbytes = ARR_NDIM(v) * 33 + 1; - /* * 33 since we assume 15 digits per number + ':' +'[]' * - * +1 allows for temp trailing null + * +1 for trailing null */ + char buf[MAXDIM * 33 + 1]; - result = (text *) palloc(nbytes + VARHDRSZ); - p = VARDATA(result); + /* Sanity check: does it look like an array at all? */ + if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM) + PG_RETURN_NULL(); dimv = ARR_DIMS(v); lb = ARR_LBOUND(v); + p = buf; for (i = 0; i < ARR_NDIM(v); i++) { sprintf(p, "[%d:%d]", lb[i], dimv[i] + lb[i] - 1); p += strlen(p); } - SET_VARSIZE(result, strlen(VARDATA(result)) + VARHDRSZ); - PG_RETURN_TEXT_P(result); + PG_RETURN_TEXT_P(cstring_to_text(buf)); } /* -- cgit v1.2.3