Skip to content

Commit a9dcbb4

Browse files
committed
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
1 parent ca9c6a5 commit a9dcbb4

File tree

2 files changed

+89
-11
lines changed

2 files changed

+89
-11
lines changed

src/common/stringinfo.c

+61-10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,40 @@
2929
#include "lib/stringinfo.h"
3030

3131

32+
/*
33+
* initStringInfoInternal
34+
*
35+
* Initialize a StringInfoData struct (with previously undefined contents)
36+
* to describe an empty string.
37+
* The initial memory allocation size is specified by 'initsize'.
38+
* The valid range for 'initsize' is 1 to MaxAllocSize.
39+
*/
40+
static inline void
41+
initStringInfoInternal(StringInfo str, int initsize)
42+
{
43+
Assert(initsize >= 1 && initsize <= MaxAllocSize);
44+
45+
str->data = (char *) palloc(initsize);
46+
str->maxlen = initsize;
47+
resetStringInfo(str);
48+
}
49+
50+
/*
51+
* makeStringInfoInternal(int initsize)
52+
*
53+
* Create an empty 'StringInfoData' & return a pointer to it.
54+
* The initial memory allocation size is specified by 'initsize'.
55+
* The valid range for 'initsize' is 1 to MaxAllocSize.
56+
*/
57+
static inline StringInfo
58+
makeStringInfoInternal(int initsize)
59+
{
60+
StringInfo res = (StringInfo) palloc(sizeof(StringInfoData));
61+
62+
initStringInfoInternal(res, initsize);
63+
return res;
64+
}
65+
3266
/*
3367
* makeStringInfo
3468
*
@@ -37,13 +71,20 @@
3771
StringInfo
3872
makeStringInfo(void)
3973
{
40-
StringInfo res;
41-
42-
res = (StringInfo) palloc(sizeof(StringInfoData));
43-
44-
initStringInfo(res);
74+
return makeStringInfoInternal(STRINGINFO_DEFAULT_SIZE);
75+
}
4576

46-
return res;
77+
/*
78+
* makeStringInfoExt(int initsize)
79+
*
80+
* Create an empty 'StringInfoData' & return a pointer to it.
81+
* The initial memory allocation size is specified by 'initsize'.
82+
* The valid range for 'initsize' is 1 to MaxAllocSize.
83+
*/
84+
StringInfo
85+
makeStringInfoExt(int initsize)
86+
{
87+
return makeStringInfoInternal(initsize);
4788
}
4889

4990
/*
@@ -55,11 +96,21 @@ makeStringInfo(void)
5596
void
5697
initStringInfo(StringInfo str)
5798
{
58-
int size = 1024; /* initial default buffer size */
99+
return initStringInfoInternal(str, STRINGINFO_DEFAULT_SIZE);
100+
}
59101

60-
str->data = (char *) palloc(size);
61-
str->maxlen = size;
62-
resetStringInfo(str);
102+
/*
103+
* initStringInfoExt
104+
*
105+
* Initialize a StringInfoData struct (with previously undefined contents)
106+
* to describe an empty string.
107+
* The initial memory allocation size is specified by 'initsize'.
108+
* The valid range for 'initsize' is 1 to MaxAllocSize.
109+
*/
110+
void
111+
initStringInfoExt(StringInfo str, int initsize)
112+
{
113+
initStringInfoInternal(str, initsize);
63114
}
64115

65116
/*

src/include/lib/stringinfo.h

+28-1
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,27 @@ typedef StringInfoData *StringInfo;
5555

5656

5757
/*------------------------
58-
* There are four ways to create a StringInfo object initially:
58+
* There are six ways to create a StringInfo object initially:
5959
*
6060
* StringInfo stringptr = makeStringInfo();
6161
* Both the StringInfoData and the data buffer are palloc'd.
6262
*
63+
* StringInfo stringptr = makeStringInfoExt(initsize);
64+
* Same as makeStringInfo except the data buffer is allocated
65+
* with size 'initsize'.
66+
*
6367
* StringInfoData string;
6468
* initStringInfo(&string);
6569
* The data buffer is palloc'd but the StringInfoData is just local.
6670
* This is the easiest approach for a StringInfo object that will
6771
* only live as long as the current routine.
6872
*
6973
* StringInfoData string;
74+
* initStringInfoExt(&string, initsize);
75+
* Same as initStringInfo except the data buffer is allocated
76+
* with size 'initsize'.
77+
*
78+
* StringInfoData string;
7079
* initReadOnlyStringInfo(&string, existingbuf, len);
7180
* The StringInfoData's data field is set to point directly to the
7281
* existing buffer and the StringInfoData's len is set to the given len.
@@ -100,19 +109,37 @@ typedef StringInfoData *StringInfo;
100109
*-------------------------
101110
*/
102111

112+
#define STRINGINFO_DEFAULT_SIZE 1024 /* default initial allocation size */
113+
103114
/*------------------------
104115
* makeStringInfo
105116
* Create an empty 'StringInfoData' & return a pointer to it.
106117
*/
107118
extern StringInfo makeStringInfo(void);
108119

120+
/*------------------------
121+
* makeStringInfoExt
122+
* Create an empty 'StringInfoData' & return a pointer to it.
123+
* The data buffer is allocated with size 'initsize'.
124+
* The valid range for 'initsize' is 1 to MaxAllocSize.
125+
*/
126+
extern StringInfo makeStringInfoExt(int initsize);
127+
109128
/*------------------------
110129
* initStringInfo
111130
* Initialize a StringInfoData struct (with previously undefined contents)
112131
* to describe an empty string.
113132
*/
114133
extern void initStringInfo(StringInfo str);
115134

135+
/*------------------------
136+
* initStringInfoExt
137+
* Initialize a StringInfoData struct (with previously undefined contents) to
138+
* describe an empty string. The data buffer is allocated with size
139+
* 'initsize'. The valid range for 'initsize' is 1 to MaxAllocSize.
140+
*/
141+
extern void initStringInfoExt(StringInfo str, int initsize);
142+
116143
/*------------------------
117144
* initReadOnlyStringInfo
118145
* Initialize a StringInfoData struct from an existing string without copying

0 commit comments

Comments
 (0)