Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita9dcbb4

Browse files
committed
Add new StringInfo APIs to allow callers to specify the buffer size.
Previously StringInfo APIs allocated buffers with fixed initialallocation size of 1024 bytes. This may be too large and inappropriatefor 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 tocall makeStringInfoExt and initStringInfoExt respectively (via inlinehelper functions makeStringInfoInternal and initStringInfoInternal),with the default buffer size of 1024.Reviewed-by: Nathan Bossart, David Rowley, Michael Paquier, Gurjeet SinghDiscussion:https://postgr.es/m/20241225.123704.1194662271286702010.ishii%40postgresql.org
1 parentca9c6a5 commita9dcbb4

File tree

2 files changed

+89
-11
lines changed

2 files changed

+89
-11
lines changed

‎src/common/stringinfo.c

Lines changed: 61 additions & 10 deletions
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+
staticinlinevoid
41+
initStringInfoInternal(StringInfostr,intinitsize)
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+
staticinlineStringInfo
58+
makeStringInfoInternal(intinitsize)
59+
{
60+
StringInfores= (StringInfo)palloc(sizeof(StringInfoData));
61+
62+
initStringInfoInternal(res,initsize);
63+
returnres;
64+
}
65+
3266
/*
3367
* makeStringInfo
3468
*
@@ -37,13 +71,20 @@
3771
StringInfo
3872
makeStringInfo(void)
3973
{
40-
StringInfores;
41-
42-
res= (StringInfo)palloc(sizeof(StringInfoData));
43-
44-
initStringInfo(res);
74+
returnmakeStringInfoInternal(STRINGINFO_DEFAULT_SIZE);
75+
}
4576

46-
returnres;
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(intinitsize)
86+
{
87+
returnmakeStringInfoInternal(initsize);
4788
}
4889

4990
/*
@@ -55,11 +96,21 @@ makeStringInfo(void)
5596
void
5697
initStringInfo(StringInfostr)
5798
{
58-
intsize=1024;/* initial default buffer size */
99+
returninitStringInfoInternal(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(StringInfostr,intinitsize)
112+
{
113+
initStringInfoInternal(str,initsize);
63114
}
64115

65116
/*

‎src/include/lib/stringinfo.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,27 @@ typedef StringInfoData *StringInfo;
5555

5656

5757
/*------------------------
58-
* There arefour ways to create a StringInfo object initially:
58+
* There aresix 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+
#defineSTRINGINFO_DEFAULT_SIZE 1024/* default initial allocation size */
113+
103114
/*------------------------
104115
* makeStringInfo
105116
* Create an empty 'StringInfoData' & return a pointer to it.
106117
*/
107118
externStringInfomakeStringInfo(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+
externStringInfomakeStringInfoExt(intinitsize);
127+
109128
/*------------------------
110129
* initStringInfo
111130
* Initialize a StringInfoData struct (with previously undefined contents)
112131
* to describe an empty string.
113132
*/
114133
externvoidinitStringInfo(StringInfostr);
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+
externvoidinitStringInfoExt(StringInfostr,intinitsize);
142+
116143
/*------------------------
117144
* initReadOnlyStringInfo
118145
* Initialize a StringInfoData struct from an existing string without copying

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp