aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/stringinfo.c71
-rw-r--r--src/include/lib/stringinfo.h29
2 files changed, 89 insertions, 11 deletions
diff --git a/src/common/stringinfo.c b/src/common/stringinfo.c
index 55d2fbb864d..f4317003411 100644
--- a/src/common/stringinfo.c
+++ b/src/common/stringinfo.c
@@ -30,6 +30,40 @@
/*
+ * initStringInfoInternal
+ *
+ * Initialize a StringInfoData struct (with previously undefined contents)
+ * to describe an empty string.
+ * The initial memory allocation size is specified by 'initsize'.
+ * The valid range for 'initsize' is 1 to MaxAllocSize.
+ */
+static inline void
+initStringInfoInternal(StringInfo str, int initsize)
+{
+ Assert(initsize >= 1 && initsize <= MaxAllocSize);
+
+ str->data = (char *) palloc(initsize);
+ str->maxlen = initsize;
+ resetStringInfo(str);
+}
+
+/*
+ * makeStringInfoInternal(int initsize)
+ *
+ * Create an empty 'StringInfoData' & return a pointer to it.
+ * The initial memory allocation size is specified by 'initsize'.
+ * The valid range for 'initsize' is 1 to MaxAllocSize.
+ */
+static inline StringInfo
+makeStringInfoInternal(int initsize)
+{
+ StringInfo res = (StringInfo) palloc(sizeof(StringInfoData));
+
+ initStringInfoInternal(res, initsize);
+ return res;
+}
+
+/*
* makeStringInfo
*
* Create an empty 'StringInfoData' & return a pointer to it.
@@ -37,13 +71,20 @@
StringInfo
makeStringInfo(void)
{
- StringInfo res;
-
- res = (StringInfo) palloc(sizeof(StringInfoData));
-
- initStringInfo(res);
+ return makeStringInfoInternal(STRINGINFO_DEFAULT_SIZE);
+}
- return res;
+/*
+ * makeStringInfoExt(int initsize)
+ *
+ * Create an empty 'StringInfoData' & return a pointer to it.
+ * The initial memory allocation size is specified by 'initsize'.
+ * The valid range for 'initsize' is 1 to MaxAllocSize.
+ */
+StringInfo
+makeStringInfoExt(int initsize)
+{
+ return makeStringInfoInternal(initsize);
}
/*
@@ -55,11 +96,21 @@ makeStringInfo(void)
void
initStringInfo(StringInfo str)
{
- int size = 1024; /* initial default buffer size */
+ return initStringInfoInternal(str, STRINGINFO_DEFAULT_SIZE);
+}
- str->data = (char *) palloc(size);
- str->maxlen = size;
- resetStringInfo(str);
+/*
+ * initStringInfoExt
+ *
+ * Initialize a StringInfoData struct (with previously undefined contents)
+ * to describe an empty string.
+ * The initial memory allocation size is specified by 'initsize'.
+ * The valid range for 'initsize' is 1 to MaxAllocSize.
+ */
+void
+initStringInfoExt(StringInfo str, int initsize)
+{
+ initStringInfoInternal(str, initsize);
}
/*
diff --git a/src/include/lib/stringinfo.h b/src/include/lib/stringinfo.h
index 335208a9fda..c96df989bb0 100644
--- a/src/include/lib/stringinfo.h
+++ b/src/include/lib/stringinfo.h
@@ -55,11 +55,15 @@ typedef StringInfoData *StringInfo;
/*------------------------
- * There are four ways to create a StringInfo object initially:
+ * There are six ways to create a StringInfo object initially:
*
* StringInfo stringptr = makeStringInfo();
* Both the StringInfoData and the data buffer are palloc'd.
*
+ * StringInfo stringptr = makeStringInfoExt(initsize);
+ * Same as makeStringInfo except the data buffer is allocated
+ * with size 'initsize'.
+ *
* StringInfoData string;
* initStringInfo(&string);
* The data buffer is palloc'd but the StringInfoData is just local.
@@ -67,6 +71,11 @@ typedef StringInfoData *StringInfo;
* only live as long as the current routine.
*
* StringInfoData string;
+ * initStringInfoExt(&string, initsize);
+ * Same as initStringInfo except the data buffer is allocated
+ * with size 'initsize'.
+ *
+ * StringInfoData string;
* initReadOnlyStringInfo(&string, existingbuf, len);
* The StringInfoData's data field is set to point directly to the
* existing buffer and the StringInfoData's len is set to the given len.
@@ -100,6 +109,8 @@ typedef StringInfoData *StringInfo;
*-------------------------
*/
+#define STRINGINFO_DEFAULT_SIZE 1024 /* default initial allocation size */
+
/*------------------------
* makeStringInfo
* Create an empty 'StringInfoData' & return a pointer to it.
@@ -107,6 +118,14 @@ typedef StringInfoData *StringInfo;
extern StringInfo makeStringInfo(void);
/*------------------------
+ * makeStringInfoExt
+ * Create an empty 'StringInfoData' & return a pointer to it.
+ * The data buffer is allocated with size 'initsize'.
+ * The valid range for 'initsize' is 1 to MaxAllocSize.
+ */
+extern StringInfo makeStringInfoExt(int initsize);
+
+/*------------------------
* initStringInfo
* Initialize a StringInfoData struct (with previously undefined contents)
* to describe an empty string.
@@ -114,6 +133,14 @@ extern StringInfo makeStringInfo(void);
extern void initStringInfo(StringInfo str);
/*------------------------
+ * initStringInfoExt
+ * Initialize a StringInfoData struct (with previously undefined contents) to
+ * describe an empty string. The data buffer is allocated with size
+ * 'initsize'. The valid range for 'initsize' is 1 to MaxAllocSize.
+ */
+extern void initStringInfoExt(StringInfo str, int initsize);
+
+/*------------------------
* initReadOnlyStringInfo
* Initialize a StringInfoData struct from an existing string without copying
* the string. The caller is responsible for ensuring the given string