On Sat, 2007-03-03 at 10:57 -0800, Joe Conway wrote: > I have used pfree(var.data) combined with initStringInfo(&var) in a few > places (e.g. in tablefunc.c).
Thanks, fixed. I also did a bit more searching and found some more places where resetStringInfo() could be used to replace the previous custom coding. Patch attached and applied. -Neil
Index: contrib/tablefunc/tablefunc.c =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/contrib/tablefunc/tablefunc.c,v retrieving revision 1.46 diff -c -p -r1.46 tablefunc.c *** contrib/tablefunc/tablefunc.c 5 Jan 2007 22:19:18 -0000 1.46 --- contrib/tablefunc/tablefunc.c 3 Mar 2007 19:23:28 -0000 *************** build_tuplestore_recursively(char *key_f *** 1378,1392 **** "incompatible."))); } 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); --- 1378,1389 ---- "incompatible."))); } + initStringInfo(&branchstr); + initStringInfo(&chk_branchstr); + initStringInfo(&chk_current_key); + for (i = 0; i < proc; i++) { /* initialize branch for this pass */ appendStringInfo(&branchstr, "%s", branch); appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim); *************** build_tuplestore_recursively(char *key_f *** 1459,1468 **** tupstore); /* reset branch for next pass */ ! xpfree(branchstr.data); ! xpfree(chk_branchstr.data); ! xpfree(chk_current_key.data); } } return tupstore; --- 1456,1469 ---- tupstore); /* reset branch for next pass */ ! resetStringInfo(&branchstr); ! resetStringInfo(&chk_branchstr); ! resetStringInfo(&chk_current_key); } + + xpfree(branchstr.data); + xpfree(chk_branchstr.data); + xpfree(chk_current_key.data); } return tupstore; Index: src/backend/catalog/pg_shdepend.c =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/catalog/pg_shdepend.c,v retrieving revision 1.16 diff -c -p -r1.16 pg_shdepend.c *** src/backend/catalog/pg_shdepend.c 5 Jan 2007 22:19:25 -0000 1.16 --- src/backend/catalog/pg_shdepend.c 3 Mar 2007 19:08:53 -0000 *************** checkSharedDependencies(Oid classId, Oid *** 588,595 **** * 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'; if (numLocalDeps > 0) { --- 588,594 ---- * Note: we don't ever suppress per-database totals, which should be * OK as long as there aren't too many databases ... */ ! resetStringInfo(&descs); if (numLocalDeps > 0) { Index: src/backend/commands/copy.c =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/commands/copy.c,v retrieving revision 1.276 diff -c -p -r1.276 copy.c *** src/backend/commands/copy.c 20 Feb 2007 17:32:13 -0000 1.276 --- src/backend/commands/copy.c 3 Mar 2007 19:16:59 -0000 *************** CopySendEndOfRow(CopyState cstate) *** 466,474 **** break; } ! /* Reset fe_msgbuf to empty */ ! fe_msgbuf->len = 0; ! fe_msgbuf->data[0] = '\0'; } /* --- 466,472 ---- break; } ! resetStringInfo(fe_msgbuf); } /* *************** CopyReadLine(CopyState cstate) *** 2193,2201 **** { bool result; ! /* Reset line_buf to empty */ ! cstate->line_buf.len = 0; ! cstate->line_buf.data[0] = '\0'; /* Mark that encoding conversion hasn't occurred yet */ cstate->line_buf_converted = false; --- 2191,2197 ---- { bool result; ! resetStringInfo(&cstate->line_buf); /* Mark that encoding conversion hasn't occurred yet */ cstate->line_buf_converted = false; *************** CopyReadLine(CopyState cstate) *** 2262,2269 **** if (cvt != cstate->line_buf.data) { /* transfer converted data back to line_buf */ ! cstate->line_buf.len = 0; ! cstate->line_buf.data[0] = '\0'; appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt)); pfree(cvt); } --- 2258,2264 ---- if (cvt != cstate->line_buf.data) { /* transfer converted data back to line_buf */ ! resetStringInfo(&cstate->line_buf); appendBinaryStringInfo(&cstate->line_buf, cvt, strlen(cvt)); pfree(cvt); } *************** CopyReadAttributesText(CopyState cstate, *** 2686,2694 **** return 0; } ! /* reset attribute_buf to empty */ ! cstate->attribute_buf.len = 0; ! cstate->attribute_buf.data[0] = '\0'; /* * The de-escaped attributes will certainly not be longer than the input --- 2681,2687 ---- return 0; } ! resetStringInfo(&cstate->attribute_buf); /* * The de-escaped attributes will certainly not be longer than the input *************** CopyReadAttributesCSV(CopyState cstate, *** 2886,2894 **** return 0; } ! /* reset attribute_buf to empty */ ! cstate->attribute_buf.len = 0; ! cstate->attribute_buf.data[0] = '\0'; /* * The de-escaped attributes will certainly not be longer than the input --- 2879,2885 ---- return 0; } ! resetStringInfo(&cstate->attribute_buf); /* * The de-escaped attributes will certainly not be longer than the input *************** CopyReadBinaryAttribute(CopyState cstate *** 3040,3051 **** 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; enlargeStringInfo(&cstate->attribute_buf, fld_size); - if (CopyGetData(cstate, cstate->attribute_buf.data, fld_size, fld_size) != fld_size) ereport(ERROR, --- 3031,3039 ---- errmsg("invalid field size"))); /* reset attribute_buf to empty, and load raw data in it */ ! 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, Index: src/backend/lib/stringinfo.c =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/lib/stringinfo.c,v retrieving revision 1.44 diff -c -p -r1.44 stringinfo.c *** src/backend/lib/stringinfo.c 5 Jan 2007 22:19:29 -0000 1.44 --- src/backend/lib/stringinfo.c 3 Mar 2007 17:01:46 -0000 *************** initStringInfo(StringInfo str) *** 49,56 **** str->data = (char *) palloc(size); str->maxlen = size; ! str->len = 0; str->data[0] = '\0'; str->cursor = 0; } --- 49,68 ---- str->data = (char *) palloc(size); str->maxlen = size; ! 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; } Index: src/backend/libpq/pqcomm.c =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/libpq/pqcomm.c,v retrieving revision 1.190 diff -c -p -r1.190 pqcomm.c *** src/backend/libpq/pqcomm.c 13 Feb 2007 19:18:53 -0000 1.190 --- src/backend/libpq/pqcomm.c 3 Mar 2007 19:05:07 -0000 *************** pq_getstring(StringInfo s) *** 860,869 **** { int i; ! /* Reset string to empty */ ! s->len = 0; ! s->data[0] = '\0'; ! s->cursor = 0; /* Read until we get the terminating '\0' */ for (;;) --- 860,866 ---- { int i; ! resetStringInfo(s); /* Read until we get the terminating '\0' */ for (;;) *************** pq_getmessage(StringInfo s, int maxlen) *** 915,924 **** { int32 len; ! /* Reset message buffer to empty */ ! s->len = 0; ! s->data[0] = '\0'; ! s->cursor = 0; /* Read message length word */ if (pq_getbytes((char *) &len, 4) == EOF) --- 912,918 ---- { int32 len; ! resetStringInfo(s); /* Read message length word */ if (pq_getbytes((char *) &len, 4) == EOF) Index: src/backend/storage/lmgr/deadlock.c =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/storage/lmgr/deadlock.c,v retrieving revision 1.45 diff -c -p -r1.45 deadlock.c *** src/backend/storage/lmgr/deadlock.c 3 Mar 2007 18:46:40 -0000 1.45 --- src/backend/storage/lmgr/deadlock.c 3 Mar 2007 19:09:53 -0000 *************** DeadLockReport(void) *** 933,940 **** appendStringInfoChar(&buf, '\n'); /* reset buf2 to hold next object description */ ! buf2.len = 0; ! buf2.data[0] = '\0'; DescribeLockTag(&buf2, &info->locktag); --- 933,939 ---- appendStringInfoChar(&buf, '\n'); /* reset buf2 to hold next object description */ ! resetStringInfo(&buf2); DescribeLockTag(&buf2, &info->locktag); Index: src/backend/tcop/fastpath.c =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/tcop/fastpath.c,v retrieving revision 1.95 diff -c -p -r1.95 fastpath.c *** src/backend/tcop/fastpath.c 5 Jan 2007 22:19:39 -0000 1.95 --- src/backend/tcop/fastpath.c 3 Mar 2007 17:04:53 -0000 *************** parse_fcall_arguments(StringInfo msgBuf, *** 480,489 **** argsize))); /* Reset abuf to empty, and insert raw data into it */ ! abuf.len = 0; ! abuf.data[0] = '\0'; ! abuf.cursor = 0; ! appendBinaryStringInfo(&abuf, pq_getmsgbytes(msgBuf, argsize), argsize); --- 480,486 ---- argsize))); /* Reset abuf to empty, and insert raw data into it */ ! resetStringInfo(&abuf); appendBinaryStringInfo(&abuf, pq_getmsgbytes(msgBuf, argsize), argsize); *************** parse_fcall_arguments_20(StringInfo msgB *** 613,622 **** argsize))); /* Reset abuf to empty, and insert raw data into it */ ! abuf.len = 0; ! abuf.data[0] = '\0'; ! abuf.cursor = 0; ! appendBinaryStringInfo(&abuf, pq_getmsgbytes(msgBuf, argsize), argsize); --- 610,616 ---- argsize))); /* Reset abuf to empty, and insert raw data into it */ ! resetStringInfo(&abuf); appendBinaryStringInfo(&abuf, pq_getmsgbytes(msgBuf, argsize), argsize); Index: src/backend/tcop/postgres.c =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/tcop/postgres.c,v retrieving revision 1.526 diff -c -p -r1.526 postgres.c *** src/backend/tcop/postgres.c 2 Mar 2007 23:37:22 -0000 1.526 --- src/backend/tcop/postgres.c 3 Mar 2007 19:04:57 -0000 *************** InteractiveBackend(StringInfo inBuf) *** 205,214 **** printf("backend> "); fflush(stdout); ! /* Reset inBuf to empty */ ! inBuf->len = 0; ! inBuf->data[0] = '\0'; ! inBuf->cursor = 0; for (;;) { --- 205,211 ---- printf("backend> "); fflush(stdout); ! resetStringInfo(inBuf); for (;;) { Index: src/backend/utils/adt/rowtypes.c =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/utils/adt/rowtypes.c,v retrieving revision 1.18 diff -c -p -r1.18 rowtypes.c *** src/backend/utils/adt/rowtypes.c 5 Jan 2007 22:19:42 -0000 1.18 --- src/backend/utils/adt/rowtypes.c 3 Mar 2007 17:01:06 -0000 *************** record_in(PG_FUNCTION_ARGS) *** 168,175 **** /* Extract string for this column */ bool inquote = false; ! buf.len = 0; ! buf.data[0] = '\0'; while (inquote || !(*ptr == ',' || *ptr == ')')) { char ch = *ptr++; --- 168,174 ---- /* Extract string for this column */ bool inquote = false; ! resetStringInfo(&buf); while (inquote || !(*ptr == ',' || *ptr == ')')) { char ch = *ptr++; Index: src/backend/utils/adt/xml.c =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/utils/adt/xml.c,v retrieving revision 1.33 diff -c -p -r1.33 xml.c *** src/backend/utils/adt/xml.c 1 Mar 2007 14:52:04 -0000 1.33 --- src/backend/utils/adt/xml.c 3 Mar 2007 19:06:03 -0000 *************** xml_init(void) *** 835,842 **** else { /* Reset pre-existing buffer to empty */ ! xml_err_buf->data[0] = '\0'; ! xml_err_buf->len = 0; } /* Now that xml_err_buf exists, safe to call xml_errorHandler */ xmlSetGenericErrorFunc(NULL, xml_errorHandler); --- 835,841 ---- else { /* Reset pre-existing buffer to empty */ ! resetStringInfo(xml_err_buf); } /* Now that xml_err_buf exists, safe to call xml_errorHandler */ xmlSetGenericErrorFunc(NULL, xml_errorHandler); *************** xml_ereport(int level, int sqlcode, *** 1197,1204 **** if (xml_err_buf->len > 0) { detail = pstrdup(xml_err_buf->data); ! xml_err_buf->data[0] = '\0'; ! xml_err_buf->len = 0; } else detail = NULL; --- 1196,1202 ---- if (xml_err_buf->len > 0) { detail = pstrdup(xml_err_buf->data); ! resetStringInfo(xml_err_buf); } else detail = NULL; Index: src/include/lib/stringinfo.h =================================================================== RCS file: /home/neilc/postgres/cvs_root/pgsql/src/include/lib/stringinfo.h,v retrieving revision 1.33 diff -c -p -r1.33 stringinfo.h *** src/include/lib/stringinfo.h 5 Jan 2007 22:19:55 -0000 1.33 --- src/include/lib/stringinfo.h 3 Mar 2007 17:01:46 -0000 *************** extern StringInfo makeStringInfo(void); *** 79,84 **** --- 79,91 ---- 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) * and append it to whatever is already in str. More space is allocated
---------------------------(end of broadcast)--------------------------- TIP 1: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to [EMAIL PROTECTED] so that your message can get through to the mailing list cleanly