Re: Proposal: add new API to stringinfo
От
Tatsuo Ishii
Тема
Re: Proposal: add new API to stringinfo
Дата
Msg-id
20250104.135439.2265480069984935562.ishii@postgresql.org
Ответ на
Re: Proposal: add new API to stringinfo (Andrew Dunstan)
Список
Дерево обсуждения
Proposal: add new API to stringinfo Tatsuo Ishii <ishii@postgresql.org>
Re: Proposal: add new API to stringinfo David Rowley <dgrowleyml@gmail.com>
Re: Proposal: add new API to stringinfo Tatsuo Ishii <ishii@postgresql.org>
>> With opinions from Michael and David , what about following additional
>> APIs?
>>
>> #define STRINGINFO_DEFAULT_SIZE 1024 /* default initial allocation size
>> #*/
>> #define STRINGINFO_SMALL_SIZE 64 /* small initial allocation size */
>>
>> #define makeStringInfo makeStringInfoExtended(STRINGINFO_DEFAULT_SIZE)
>> #define initStringInfo(str) initStringInfoExtended(str,
>> #STRINGINFO_DEFAULT_SIZE)
>>
>> extern StringInfo makeStringInfoExtended(int initsize);
>> extern void initStringInfoExtended(StringInfo str, int initsize);
>>
>
>
> Seems like a good idea.
Thanks.
> Minor nit: the definition of the macro should contain parentheses, like so:
> #define makeStringInfo() ....
Indeed. Thanks for pointing it out.
Attached is the patch for this.
Best reagards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp
From fe0f556264d0d25469da93896c312d7957936531 Mon Sep 17 00:00:00 2001
From: Tatsuo Ishii
Date: Sat, 4 Jan 2025 13:27:50 +0900
Subject: [PATCH v1] Add new StringInfo APIs.
Previously StringInfo only provided APIs with fixed initial memory
allocation size for the data buffer: 1024 bytes. This is inappropreate
for some callers that need less memory buffer. To fix this, new APIs
that accept the initial memory allocation size parameter are added:
extern StringInfo makeStringInfoExtended(int initsize);
extern void initStringInfoExtended(StringInfo str, int initsize);
Existing APIs (makeStringInfo() and initStringInfo()) now become
macros to call makeStringInfoExtended() and initStringInfoExtended()
respectively. This means that callers of makeStringInfo() and
initStringInfo() need to be recompiled.
---
src/common/stringinfo.c | 23 +++++++++++++----------
src/include/lib/stringinfo.h | 34 +++++++++++++++++++++++++++++++---
2 files changed, 44 insertions(+), 13 deletions(-)
diff --git a/src/common/stringinfo.c b/src/common/stringinfo.c
index 55d2fbb864d..6838f9659aa 100644
--- a/src/common/stringinfo.c
+++ b/src/common/stringinfo.c
@@ -30,35 +30,38 @@
/*
- * makeStringInfo
+ * makeStringInfoExtended(int initsize)
*
* Create an empty 'StringInfoData' & return a pointer to it.
+ * The initial memory allocation size is specified by 'initsize'.
*/
StringInfo
-makeStringInfo(void)
+makeStringInfoExtended(int initsize)
{
StringInfo res;
+ Assert(initsize > 0);
+
res = (StringInfo) palloc(sizeof(StringInfoData));
- initStringInfo(res);
+ initStringInfoExtended(res, initsize);
return res;
}
/*
- * initStringInfo
+ * initStringInfoExtended
*
- * Initialize a StringInfoData struct (with previously undefined contents)
- * to describe an empty string.
+ * Initialize a StringInfoData struct (with previously undefined contents).
+ * The initial memory allocation size is specified by 'initsize'.
*/
void
-initStringInfo(StringInfo str)
+initStringInfoExtended(StringInfo str, int initsize)
{
- int size = 1024; /* initial default buffer size */
+ Assert(initsize > 0);
- str->data = (char *) palloc(size);
- str->maxlen = size;
+ str->data = (char *) palloc(initsize);
+ str->maxlen = initsize;
resetStringInfo(str);
}
diff --git a/src/include/lib/stringinfo.h b/src/include/lib/stringinfo.h
index 335208a9fda..37e91b73714 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 = makeStringInfoExtended(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;
+ * initStringInfoExtended(&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,18 +109,37 @@ typedef StringInfoData *StringInfo;
*-------------------------
*/
+#define STRINGINFO_DEFAULT_SIZE 1024 /* default initial allocation size */
+#define STRINGINFO_SMALL_SIZE 64 /* small initial allocation size */
+
/*------------------------
* makeStringInfo
* Create an empty 'StringInfoData' & return a pointer to it.
*/
-extern StringInfo makeStringInfo(void);
+#define makeStringInfo() makeStringInfoExtended(STRINGINFO_DEFAULT_SIZE)
+
+/*------------------------
+ * makeStringInfoExtended
+ * Create an empty 'StringInfoData' & return a pointer to it.
+ * The data buffer is allocated with size 'initsize'.
+ */
+extern StringInfo makeStringInfoExtended(int initsize);
/*------------------------
* initStringInfo
* Initialize a StringInfoData struct (with previously undefined contents)
* to describe an empty string.
*/
-extern void initStringInfo(StringInfo str);
+#define initStringInfo(str) \
+ initStringInfoExtended(str, STRINGINFO_DEFAULT_SIZE)
+
+/*------------------------
+ * initStringInfoExtended
+ * Initialize a StringInfoData struct (with previously undefined contents)
+ * to describe an empty string.
+ * The data buffer is allocated with size 'initsize'.
+ */
+extern void initStringInfoExtended(StringInfo str, int initsize);
/*------------------------
* initReadOnlyStringInfo
--
2.25.1
В списке pgsql-hackers по дате отправления