From 90d76525c5cc2f3f4781351a1d99be839dfa2874 Mon Sep 17 00:00:00 2001 From: Neil Conway Date: Sat, 3 Mar 2007 19:32:55 +0000 Subject: [PATCH] Add resetStringInfo(), which clears the content of a StringInfo, and fixup various places in the tree that were clearing a StringInfo by hand. Making this function a part of the API simplifies client code slightly, and avoids needlessly peeking inside the StringInfo interface. --- contrib/tablefunc/tablefunc.c | 21 +++++++++++---------- src/backend/catalog/pg_shdepend.c | 5 ++--- src/backend/commands/copy.c | 26 +++++++------------------- src/backend/lib/stringinfo.c | 16 ++++++++++++++-- src/backend/libpq/pqcomm.c | 12 +++--------- src/backend/storage/lmgr/deadlock.c | 5 ++--- src/backend/tcop/fastpath.c | 12 +++--------- src/backend/tcop/postgres.c | 7 ++----- src/backend/utils/adt/rowtypes.c | 5 ++--- src/backend/utils/adt/xml.c | 8 +++----- src/include/lib/stringinfo.h | 9 ++++++++- 11 files changed, 57 insertions(+), 69 deletions(-) diff --git a/contrib/tablefunc/tablefunc.c b/contrib/tablefunc/tablefunc.c index 4e68428e0d..f3b1eb2a8b 100644 --- a/contrib/tablefunc/tablefunc.c +++ b/contrib/tablefunc/tablefunc.c @@ -1378,15 +1378,12 @@ build_tuplestore_recursively(char *key_fld, "incompatible."))); } + initStringInfo(&branchstr); + initStringInfo(&chk_branchstr); + initStringInfo(&chk_current_key); + for (i = 0; i < proc; i++) { - /* start a new branch */ - initStringInfo(&branchstr); - - /* need these to check for recursion */ - initStringInfo(&chk_branchstr); - initStringInfo(&chk_current_key); - /* initialize branch for this pass */ appendStringInfo(&branchstr, "%s", branch); appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim); @@ -1459,10 +1456,14 @@ build_tuplestore_recursively(char *key_fld, tupstore); /* reset branch for next pass */ - xpfree(branchstr.data); - xpfree(chk_branchstr.data); - xpfree(chk_current_key.data); + resetStringInfo(&branchstr); + resetStringInfo(&chk_branchstr); + resetStringInfo(&chk_current_key); } + + xpfree(branchstr.data); + xpfree(chk_branchstr.data); + xpfree(chk_current_key.data); } return tupstore; diff --git a/src/backend/catalog/pg_shdepend.c b/src/backend/catalog/pg_shdepend.c index e1c8dff131..9ad012db49 100644 --- a/src/backend/catalog/pg_shdepend.c +++ b/src/backend/catalog/pg_shdepend.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/catalog/pg_shdepend.c,v 1.16 2007/01/05 22:19:25 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/catalog/pg_shdepend.c,v 1.17 2007/03/03 19:32:54 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -588,8 +588,7 @@ checkSharedDependencies(Oid classId, Oid objectId) * Note: we don't ever suppress per-database totals, which should be * OK as long as there aren't too many databases ... */ - descs.len = 0; /* reset to empty */ - descs.data[0] = '\0'; + resetStringInfo(&descs); if (numLocalDeps > 0) { diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index 30118d5237..17f0135981 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.276 2007/02/20 17:32:13 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.277 2007/03/03 19:32:54 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -466,9 +466,7 @@ CopySendEndOfRow(CopyState cstate) break; } - /* Reset fe_msgbuf to empty */ - fe_msgbuf->len = 0; - fe_msgbuf->data[0] = '\0'; + resetStringInfo(fe_msgbuf); } /* @@ -2193,9 +2191,7 @@ CopyReadLine(CopyState cstate) { bool result; - /* Reset line_buf to empty */ - cstate->line_buf.len = 0; - cstate->line_buf.data[0] = '\0'; + resetStringInfo(&cstate->line_buf); /* Mark that encoding conversion hasn't occurred yet */ cstate->line_buf_converted = false; @@ -2262,8 +2258,7 @@ CopyReadLine(CopyState cstate) if (cvt != cstate->line_buf.data) { /* transfer converted data back to line_buf */ - cstate->line_buf.len = 0; - cstate->line_buf.data[0] = '\0'; + resetStringInfo(&cstate->line_buf); appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt)); pfree(cvt); } @@ -2686,9 +2681,7 @@ CopyReadAttributesText(CopyState cstate, int maxfields, char **fieldvals) return 0; } - /* reset attribute_buf to empty */ - cstate->attribute_buf.len = 0; - cstate->attribute_buf.data[0] = '\0'; + resetStringInfo(&cstate->attribute_buf); /* * The de-escaped attributes will certainly not be longer than the input @@ -2886,9 +2879,7 @@ CopyReadAttributesCSV(CopyState cstate, int maxfields, char **fieldvals) return 0; } - /* reset attribute_buf to empty */ - cstate->attribute_buf.len = 0; - cstate->attribute_buf.data[0] = '\0'; + resetStringInfo(&cstate->attribute_buf); /* * The de-escaped attributes will certainly not be longer than the input @@ -3040,12 +3031,9 @@ CopyReadBinaryAttribute(CopyState cstate, errmsg("invalid field size"))); /* reset attribute_buf to empty, and load raw data in it */ - cstate->attribute_buf.len = 0; - cstate->attribute_buf.data[0] = '\0'; - cstate->attribute_buf.cursor = 0; + resetStringInfo(&cstate->attribute_buf); enlargeStringInfo(&cstate->attribute_buf, fld_size); - if (CopyGetData(cstate, cstate->attribute_buf.data, fld_size, fld_size) != fld_size) ereport(ERROR, diff --git a/src/backend/lib/stringinfo.c b/src/backend/lib/stringinfo.c index 826212d0aa..b0854ddc43 100644 --- a/src/backend/lib/stringinfo.c +++ b/src/backend/lib/stringinfo.c @@ -9,7 +9,7 @@ * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.44 2007/01/05 22:19:29 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/lib/stringinfo.c,v 1.45 2007/03/03 19:32:54 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -49,8 +49,20 @@ initStringInfo(StringInfo str) str->data = (char *) palloc(size); str->maxlen = size; - str->len = 0; + resetStringInfo(str); +} + +/* + * resetStringInfo + * + * Reset the StringInfo: the data buffer remains valid, but its + * previous content, if any, is cleared. + */ +void +resetStringInfo(StringInfo str) +{ str->data[0] = '\0'; + str->len = 0; str->cursor = 0; } diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c index a8f40249c9..d9439e7fd9 100644 --- a/src/backend/libpq/pqcomm.c +++ b/src/backend/libpq/pqcomm.c @@ -30,7 +30,7 @@ * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.190 2007/02/13 19:18:53 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.191 2007/03/03 19:32:54 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -860,10 +860,7 @@ pq_getstring(StringInfo s) { int i; - /* Reset string to empty */ - s->len = 0; - s->data[0] = '\0'; - s->cursor = 0; + resetStringInfo(s); /* Read until we get the terminating '\0' */ for (;;) @@ -915,10 +912,7 @@ pq_getmessage(StringInfo s, int maxlen) { int32 len; - /* Reset message buffer to empty */ - s->len = 0; - s->data[0] = '\0'; - s->cursor = 0; + resetStringInfo(s); /* Read message length word */ if (pq_getbytes((char *) &len, 4) == EOF) diff --git a/src/backend/storage/lmgr/deadlock.c b/src/backend/storage/lmgr/deadlock.c index ddbadfa036..f2130083ee 100644 --- a/src/backend/storage/lmgr/deadlock.c +++ b/src/backend/storage/lmgr/deadlock.c @@ -12,7 +12,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.45 2007/03/03 18:46:40 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.46 2007/03/03 19:32:54 neilc Exp $ * * Interface: * @@ -933,8 +933,7 @@ DeadLockReport(void) appendStringInfoChar(&buf, '\n'); /* reset buf2 to hold next object description */ - buf2.len = 0; - buf2.data[0] = '\0'; + resetStringInfo(&buf2); DescribeLockTag(&buf2, &info->locktag); diff --git a/src/backend/tcop/fastpath.c b/src/backend/tcop/fastpath.c index fba983eff6..cd1911f1e9 100644 --- a/src/backend/tcop/fastpath.c +++ b/src/backend/tcop/fastpath.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.95 2007/01/05 22:19:39 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/tcop/fastpath.c,v 1.96 2007/03/03 19:32:54 neilc Exp $ * * NOTES * This cruft is the server side of PQfn. @@ -480,10 +480,7 @@ parse_fcall_arguments(StringInfo msgBuf, struct fp_info * fip, argsize))); /* Reset abuf to empty, and insert raw data into it */ - abuf.len = 0; - abuf.data[0] = '\0'; - abuf.cursor = 0; - + resetStringInfo(&abuf); appendBinaryStringInfo(&abuf, pq_getmsgbytes(msgBuf, argsize), argsize); @@ -613,10 +610,7 @@ parse_fcall_arguments_20(StringInfo msgBuf, struct fp_info * fip, argsize))); /* Reset abuf to empty, and insert raw data into it */ - abuf.len = 0; - abuf.data[0] = '\0'; - abuf.cursor = 0; - + resetStringInfo(&abuf); appendBinaryStringInfo(&abuf, pq_getmsgbytes(msgBuf, argsize), argsize); diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index afb6b4db0a..cfb6731b23 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.526 2007/03/02 23:37:22 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.527 2007/03/03 19:32:54 neilc Exp $ * * NOTES * this is the "main" module of the postgres backend and @@ -205,10 +205,7 @@ InteractiveBackend(StringInfo inBuf) printf("backend> "); fflush(stdout); - /* Reset inBuf to empty */ - inBuf->len = 0; - inBuf->data[0] = '\0'; - inBuf->cursor = 0; + resetStringInfo(inBuf); for (;;) { diff --git a/src/backend/utils/adt/rowtypes.c b/src/backend/utils/adt/rowtypes.c index d7ea553c95..c856acb0dc 100644 --- a/src/backend/utils/adt/rowtypes.c +++ b/src/backend/utils/adt/rowtypes.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/rowtypes.c,v 1.18 2007/01/05 22:19:42 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/rowtypes.c,v 1.19 2007/03/03 19:32:55 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -168,8 +168,7 @@ record_in(PG_FUNCTION_ARGS) /* Extract string for this column */ bool inquote = false; - buf.len = 0; - buf.data[0] = '\0'; + resetStringInfo(&buf); while (inquote || !(*ptr == ',' || *ptr == ')')) { char ch = *ptr++; diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index 547d98df1e..921fe1d9f5 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.33 2007/03/01 14:52:04 petere Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.34 2007/03/03 19:32:55 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -835,8 +835,7 @@ xml_init(void) else { /* Reset pre-existing buffer to empty */ - xml_err_buf->data[0] = '\0'; - xml_err_buf->len = 0; + resetStringInfo(xml_err_buf); } /* Now that xml_err_buf exists, safe to call xml_errorHandler */ xmlSetGenericErrorFunc(NULL, xml_errorHandler); @@ -1197,8 +1196,7 @@ xml_ereport(int level, int sqlcode, if (xml_err_buf->len > 0) { detail = pstrdup(xml_err_buf->data); - xml_err_buf->data[0] = '\0'; - xml_err_buf->len = 0; + resetStringInfo(xml_err_buf); } else detail = NULL; diff --git a/src/include/lib/stringinfo.h b/src/include/lib/stringinfo.h index 8658101035..b5a7ebaf89 100644 --- a/src/include/lib/stringinfo.h +++ b/src/include/lib/stringinfo.h @@ -10,7 +10,7 @@ * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/lib/stringinfo.h,v 1.33 2007/01/05 22:19:55 momjian Exp $ + * $PostgreSQL: pgsql/src/include/lib/stringinfo.h,v 1.34 2007/03/03 19:32:55 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -78,6 +78,13 @@ extern StringInfo makeStringInfo(void); */ extern void initStringInfo(StringInfo str); +/*------------------------ + * resetStringInfo + * Clears the current content of the StringInfo, if any. The + * StringInfo remains valid. + */ +extern void resetStringInfo(StringInfo str); + /*------------------------ * appendStringInfo * Format text data under the control of fmt (an sprintf-style format string)