diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2008-03-25 22:42:46 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2008-03-25 22:42:46 +0000 |
commit | 220db7ccd8c88aafea4629f00e8be6f9f073ed00 (patch) | |
tree | 772c9dca19736eb9d417cb665a281686c3570b4c /src/backend/utils/adt/network.c | |
parent | f948197b4055bb0e22e508e9d6966217b7dbea3d (diff) | |
download | postgresql-220db7ccd8c88aafea4629f00e8be6f9f073ed00.tar.gz postgresql-220db7ccd8c88aafea4629f00e8be6f9f073ed00.zip |
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
Diffstat (limited to 'src/backend/utils/adt/network.c')
-rw-r--r-- | src/backend/utils/adt/network.c | 37 |
1 files changed, 5 insertions, 32 deletions
diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c index 7eb6ffe306f..8e58374e9b1 100644 --- a/src/backend/utils/adt/network.c +++ b/src/backend/utils/adt/network.c @@ -1,7 +1,7 @@ /* * PostgreSQL type definitions for the INET and CIDR types. * - * $PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.72 2007/11/15 21:14:39 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/network.c,v 1.73 2008/03/25 22:42:44 tgl Exp $ * * Jon Postel RIP 16 Oct 1998 */ @@ -601,8 +601,6 @@ Datum network_host(PG_FUNCTION_ARGS) { inet *ip = PG_GETARG_INET_P(0); - text *ret; - int len; char *ptr; char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")]; @@ -617,12 +615,7 @@ network_host(PG_FUNCTION_ARGS) if ((ptr = strchr(tmp, '/')) != NULL) *ptr = '\0'; - /* Return string as a text datum */ - len = strlen(tmp); - ret = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(ret, len + VARHDRSZ); - memcpy(VARDATA(ret), tmp, len); - PG_RETURN_TEXT_P(ret); + PG_RETURN_TEXT_P(cstring_to_text(tmp)); } /* @@ -634,7 +627,6 @@ Datum network_show(PG_FUNCTION_ARGS) { inet *ip = PG_GETARG_INET_P(0); - text *ret; int len; char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")]; @@ -651,21 +643,14 @@ network_show(PG_FUNCTION_ARGS) snprintf(tmp + len, sizeof(tmp) - len, "/%u", ip_bits(ip)); } - /* Return string as a text datum */ - len = strlen(tmp); - ret = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(ret, len + VARHDRSZ); - memcpy(VARDATA(ret), tmp, len); - PG_RETURN_TEXT_P(ret); + PG_RETURN_TEXT_P(cstring_to_text(tmp)); } Datum inet_abbrev(PG_FUNCTION_ARGS) { inet *ip = PG_GETARG_INET_P(0); - text *ret; char *dst; - int len; char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")]; dst = inet_net_ntop(ip_family(ip), ip_addr(ip), @@ -676,21 +661,14 @@ inet_abbrev(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION), errmsg("could not format inet value: %m"))); - /* Return string as a text datum */ - len = strlen(tmp); - ret = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(ret, len + VARHDRSZ); - memcpy(VARDATA(ret), tmp, len); - PG_RETURN_TEXT_P(ret); + PG_RETURN_TEXT_P(cstring_to_text(tmp)); } Datum cidr_abbrev(PG_FUNCTION_ARGS) { inet *ip = PG_GETARG_INET_P(0); - text *ret; char *dst; - int len; char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")]; dst = inet_cidr_ntop(ip_family(ip), ip_addr(ip), @@ -701,12 +679,7 @@ cidr_abbrev(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION), errmsg("could not format cidr value: %m"))); - /* Return string as a text datum */ - len = strlen(tmp); - ret = (text *) palloc(len + VARHDRSZ); - SET_VARSIZE(ret, len + VARHDRSZ); - memcpy(VARDATA(ret), tmp, len); - PG_RETURN_TEXT_P(ret); + PG_RETURN_TEXT_P(cstring_to_text(tmp)); } Datum |