mirror of
https://github.com/postgres/postgres.git
synced 2025-07-05 07:21:24 +03:00
Store GUC data in a memory context, instead of using malloc().
The only real argument for using malloc directly was that we needed the ability to not throw error on OOM; but mcxt.c grew that feature awhile ago. Keeping the data in a memory context improves accountability and debuggability --- for example, without this it's almost impossible to detect memory leaks in the GUC code with anything less costly than valgrind. Moreover, the next patch in this series will add a hash table for GUC lookup, and it'd be pretty silly to be using palloc-dependent hash facilities alongside malloc'd storage of the underlying data. This is a bit invasive though, in particular causing an API break for GUC check hooks that want to modify the GUC's value or use an "extra" data structure. They must now use guc_malloc() and guc_free() instead of malloc() and free(). Failure to change affected code will result in assertion failures or worse; but thanks to recent effort in the mcxt infrastructure, it shouldn't be too hard to diagnose such oversights (at least in assert-enabled builds). One note is that this changes ParseLongOption() to return short-lived palloc'd not malloc'd data. There wasn't any caller for which the previous definition was better. Discussion: https://postgr.es/m/2982579.1662416866@sss.pgh.pa.us
This commit is contained in:
@ -148,7 +148,7 @@ check_datestyle(char **newval, void **extra, GucSource source)
|
||||
char *subval;
|
||||
void *subextra = NULL;
|
||||
|
||||
subval = strdup(GetConfigOptionResetString("datestyle"));
|
||||
subval = guc_strdup(LOG, GetConfigOptionResetString("datestyle"));
|
||||
if (!subval)
|
||||
{
|
||||
ok = false;
|
||||
@ -156,7 +156,7 @@ check_datestyle(char **newval, void **extra, GucSource source)
|
||||
}
|
||||
if (!check_datestyle(&subval, &subextra, source))
|
||||
{
|
||||
free(subval);
|
||||
guc_free(subval);
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
@ -165,8 +165,8 @@ check_datestyle(char **newval, void **extra, GucSource source)
|
||||
newDateStyle = myextra[0];
|
||||
if (!have_order)
|
||||
newDateOrder = myextra[1];
|
||||
free(subval);
|
||||
free(subextra);
|
||||
guc_free(subval);
|
||||
guc_free(subextra);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -187,9 +187,9 @@ check_datestyle(char **newval, void **extra, GucSource source)
|
||||
}
|
||||
|
||||
/*
|
||||
* Prepare the canonical string to return. GUC wants it malloc'd.
|
||||
* Prepare the canonical string to return. GUC wants it guc_malloc'd.
|
||||
*/
|
||||
result = (char *) malloc(32);
|
||||
result = (char *) guc_malloc(LOG, 32);
|
||||
if (!result)
|
||||
return false;
|
||||
|
||||
@ -221,13 +221,13 @@ check_datestyle(char **newval, void **extra, GucSource source)
|
||||
break;
|
||||
}
|
||||
|
||||
free(*newval);
|
||||
guc_free(*newval);
|
||||
*newval = result;
|
||||
|
||||
/*
|
||||
* Set up the "extra" struct actually used by assign_datestyle.
|
||||
*/
|
||||
myextra = (int *) malloc(2 * sizeof(int));
|
||||
myextra = (int *) guc_malloc(LOG, 2 * sizeof(int));
|
||||
if (!myextra)
|
||||
return false;
|
||||
myextra[0] = newDateStyle;
|
||||
@ -366,7 +366,7 @@ check_timezone(char **newval, void **extra, GucSource source)
|
||||
/*
|
||||
* Pass back data for assign_timezone to use
|
||||
*/
|
||||
*extra = malloc(sizeof(pg_tz *));
|
||||
*extra = guc_malloc(LOG, sizeof(pg_tz *));
|
||||
if (!*extra)
|
||||
return false;
|
||||
*((pg_tz **) *extra) = new_tz;
|
||||
@ -439,7 +439,7 @@ check_log_timezone(char **newval, void **extra, GucSource source)
|
||||
/*
|
||||
* Pass back data for assign_log_timezone to use
|
||||
*/
|
||||
*extra = malloc(sizeof(pg_tz *));
|
||||
*extra = guc_malloc(LOG, sizeof(pg_tz *));
|
||||
if (!*extra)
|
||||
return false;
|
||||
*((pg_tz **) *extra) = new_tz;
|
||||
@ -500,7 +500,7 @@ check_timezone_abbreviations(char **newval, void **extra, GucSource source)
|
||||
return true;
|
||||
}
|
||||
|
||||
/* OK, load the file and produce a malloc'd TimeZoneAbbrevTable */
|
||||
/* OK, load the file and produce a guc_malloc'd TimeZoneAbbrevTable */
|
||||
*extra = load_tzoffsets(*newval);
|
||||
|
||||
/* tzparser.c returns NULL on failure, reporting via GUC_check_errmsg */
|
||||
@ -647,7 +647,7 @@ check_transaction_deferrable(bool *newval, void **extra, GucSource source)
|
||||
bool
|
||||
check_random_seed(double *newval, void **extra, GucSource source)
|
||||
{
|
||||
*extra = malloc(sizeof(int));
|
||||
*extra = guc_malloc(LOG, sizeof(int));
|
||||
if (!*extra)
|
||||
return false;
|
||||
/* Arm the assign only if source of value is an interactive SET */
|
||||
@ -735,8 +735,8 @@ check_client_encoding(char **newval, void **extra, GucSource source)
|
||||
if (strcmp(*newval, canonical_name) != 0 &&
|
||||
strcmp(*newval, "UNICODE") != 0)
|
||||
{
|
||||
free(*newval);
|
||||
*newval = strdup(canonical_name);
|
||||
guc_free(*newval);
|
||||
*newval = guc_strdup(LOG, canonical_name);
|
||||
if (!*newval)
|
||||
return false;
|
||||
}
|
||||
@ -744,7 +744,7 @@ check_client_encoding(char **newval, void **extra, GucSource source)
|
||||
/*
|
||||
* Save the encoding's ID in *extra, for use by assign_client_encoding.
|
||||
*/
|
||||
*extra = malloc(sizeof(int));
|
||||
*extra = guc_malloc(LOG, sizeof(int));
|
||||
if (!*extra)
|
||||
return false;
|
||||
*((int *) *extra) = encoding;
|
||||
@ -847,7 +847,7 @@ check_session_authorization(char **newval, void **extra, GucSource source)
|
||||
ReleaseSysCache(roleTup);
|
||||
|
||||
/* Set up "extra" struct for assign_session_authorization to use */
|
||||
myextra = (role_auth_extra *) malloc(sizeof(role_auth_extra));
|
||||
myextra = (role_auth_extra *) guc_malloc(LOG, sizeof(role_auth_extra));
|
||||
if (!myextra)
|
||||
return false;
|
||||
myextra->roleid = roleid;
|
||||
@ -957,7 +957,7 @@ check_role(char **newval, void **extra, GucSource source)
|
||||
}
|
||||
|
||||
/* Set up "extra" struct for assign_role to use */
|
||||
myextra = (role_auth_extra *) malloc(sizeof(role_auth_extra));
|
||||
myextra = (role_auth_extra *) guc_malloc(LOG, sizeof(role_auth_extra));
|
||||
if (!myextra)
|
||||
return false;
|
||||
myextra->roleid = roleid;
|
||||
|
Reference in New Issue
Block a user