mirror of
https://github.com/postgres/postgres.git
synced 2025-04-29 13:56:47 +03:00
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
This commit is contained in:
parent
ca9c6a5680
commit
a9dcbb4d5c
@ -29,6 +29,40 @@
|
|||||||
#include "lib/stringinfo.h"
|
#include "lib/stringinfo.h"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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
|
* makeStringInfo
|
||||||
*
|
*
|
||||||
@ -37,13 +71,20 @@
|
|||||||
StringInfo
|
StringInfo
|
||||||
makeStringInfo(void)
|
makeStringInfo(void)
|
||||||
{
|
{
|
||||||
StringInfo res;
|
return makeStringInfoInternal(STRINGINFO_DEFAULT_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
res = (StringInfo) palloc(sizeof(StringInfoData));
|
/*
|
||||||
|
* makeStringInfoExt(int initsize)
|
||||||
initStringInfo(res);
|
*
|
||||||
|
* Create an empty 'StringInfoData' & return a pointer to it.
|
||||||
return res;
|
* 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
|
void
|
||||||
initStringInfo(StringInfo str)
|
initStringInfo(StringInfo str)
|
||||||
{
|
{
|
||||||
int size = 1024; /* initial default buffer size */
|
return initStringInfoInternal(str, STRINGINFO_DEFAULT_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
str->data = (char *) palloc(size);
|
/*
|
||||||
str->maxlen = size;
|
* initStringInfoExt
|
||||||
resetStringInfo(str);
|
*
|
||||||
|
* 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -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();
|
* StringInfo stringptr = makeStringInfo();
|
||||||
* Both the StringInfoData and the data buffer are palloc'd.
|
* 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;
|
* StringInfoData string;
|
||||||
* initStringInfo(&string);
|
* initStringInfo(&string);
|
||||||
* The data buffer is palloc'd but the StringInfoData is just local.
|
* 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.
|
* only live as long as the current routine.
|
||||||
*
|
*
|
||||||
* StringInfoData string;
|
* StringInfoData string;
|
||||||
|
* initStringInfoExt(&string, initsize);
|
||||||
|
* Same as initStringInfo except the data buffer is allocated
|
||||||
|
* with size 'initsize'.
|
||||||
|
*
|
||||||
|
* StringInfoData string;
|
||||||
* initReadOnlyStringInfo(&string, existingbuf, len);
|
* initReadOnlyStringInfo(&string, existingbuf, len);
|
||||||
* The StringInfoData's data field is set to point directly to the
|
* 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.
|
* existing buffer and the StringInfoData's len is set to the given len.
|
||||||
@ -100,12 +109,22 @@ typedef StringInfoData *StringInfo;
|
|||||||
*-------------------------
|
*-------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define STRINGINFO_DEFAULT_SIZE 1024 /* default initial allocation size */
|
||||||
|
|
||||||
/*------------------------
|
/*------------------------
|
||||||
* makeStringInfo
|
* makeStringInfo
|
||||||
* Create an empty 'StringInfoData' & return a pointer to it.
|
* Create an empty 'StringInfoData' & return a pointer to it.
|
||||||
*/
|
*/
|
||||||
extern StringInfo makeStringInfo(void);
|
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
|
* initStringInfo
|
||||||
* Initialize a StringInfoData struct (with previously undefined contents)
|
* Initialize a StringInfoData struct (with previously undefined contents)
|
||||||
@ -113,6 +132,14 @@ extern StringInfo makeStringInfo(void);
|
|||||||
*/
|
*/
|
||||||
extern void initStringInfo(StringInfo str);
|
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
|
* initReadOnlyStringInfo
|
||||||
* Initialize a StringInfoData struct from an existing string without copying
|
* Initialize a StringInfoData struct from an existing string without copying
|
||||||
|
Loading…
x
Reference in New Issue
Block a user