summaryrefslogtreecommitdiff
path: root/src/common/stringinfo.c
diff options
context:
space:
mode:
authorTatsuo Ishii2025-01-10 23:02:56 +0000
committerTatsuo Ishii2025-01-10 23:23:46 +0000
commita9dcbb4d5c00b703e727e14055ed70c525a86418 (patch)
treeb54947628737f971f084598c3962040f93d8ff98 /src/common/stringinfo.c
parentca9c6a5680d7c7dece0f7209ee7ce20c9dfe0840 (diff)
Add new StringInfo APIs to allow callers to specify the buffer size.
Previously StringInfo APIs allocated buffers with fixed initial allocation size of 1024 bytes. This may be too large and inappropriate for some callers that can do with smaller memory buffers. To fix this, introduce new APIs that allow callers to specify initial buffer size. extern StringInfo makeStringInfoExt(int initsize); extern void initStringInfoExt(StringInfo str, int initsize); Existing APIs (makeStringInfo() and initStringInfo()) are changed to call makeStringInfoExt and initStringInfoExt respectively (via inline helper functions makeStringInfoInternal and initStringInfoInternal), with the default buffer size of 1024. Reviewed-by: Nathan Bossart, David Rowley, Michael Paquier, Gurjeet Singh Discussion: https://postgr.es/m/20241225.123704.1194662271286702010.ishii%40postgresql.org
Diffstat (limited to 'src/common/stringinfo.c')
-rw-r--r--src/common/stringinfo.c71
1 files changed, 61 insertions, 10 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);
}
/*