1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-21 00:42:43 +03:00

Make various variables const (read-only).

These changes should generally improve correctness/maintainability.
A nice side benefit is that several kilobytes move from initialized
data to text segment, allowing them to be shared across processes and
probably reducing copy-on-write overhead while forking a new backend.
Unfortunately this doesn't seem to help libpq in the same way (at least
not when it's compiled with -fpic on x86_64), but we can hope the linker
at least collects all nominally-const data together even if it's not
actually part of the text segment.

Also, make pg_encname_tbl[] static in encnames.c, since there seems
no very good reason for any other code to use it; per a suggestion
from Wim Lewis, who independently submitted a patch that was mostly
a subset of this one.

Oskari Saarenmaa, with some editorialization by me
This commit is contained in:
Tom Lane
2014-01-18 16:04:11 -05:00
parent 7d7eee8bb7
commit 0d79c0a8cc
16 changed files with 101 additions and 130 deletions

View File

@@ -29,7 +29,13 @@
* Karel Zak, Aug 2001
* ----------
*/
pg_encname pg_encname_tbl[] =
typedef struct pg_encname
{
const char *name;
pg_enc encoding;
} pg_encname;
static const pg_encname pg_encname_tbl[] =
{
{
"abc", PG_WIN1258
@@ -285,15 +291,9 @@ pg_encname pg_encname_tbl[] =
}, /* alias for UHC */
{
"windows950", PG_BIG5
}, /* alias for BIG5 */
{
NULL, 0
} /* last */
} /* alias for BIG5 */
};
unsigned int pg_encname_tbl_sz = \
sizeof(pg_encname_tbl) / sizeof(pg_encname_tbl[0]) - 1;
/* ----------
* These are "official" encoding names.
* XXX must be sorted by the same order as enum pg_enc (in mb/pg_wchar.h)
@@ -304,7 +304,7 @@ sizeof(pg_encname_tbl) / sizeof(pg_encname_tbl[0]) - 1;
#else
#define DEF_ENC2NAME(name, codepage) { #name, PG_##name, codepage }
#endif
pg_enc2name pg_enc2name_tbl[] =
const pg_enc2name pg_enc2name_tbl[] =
{
DEF_ENC2NAME(SQL_ASCII, 0),
DEF_ENC2NAME(EUC_JP, 20932),
@@ -356,7 +356,7 @@ pg_enc2name pg_enc2name_tbl[] =
* This covers all encodings except MULE_INTERNAL, which is alien to gettext.
* ----------
*/
pg_enc2gettext pg_enc2gettext_tbl[] =
const pg_enc2gettext pg_enc2gettext_tbl[] =
{
{PG_SQL_ASCII, "US-ASCII"},
{PG_UTF8, "UTF-8"},
@@ -467,13 +467,15 @@ clean_encoding_name(const char *key, char *newkey)
/* ----------
* Search encoding by encoding name
*
* Returns encoding ID, or -1 for error
* ----------
*/
pg_encname *
pg_char_to_encname_struct(const char *name)
int
pg_char_to_encoding(const char *name)
{
unsigned int nel = pg_encname_tbl_sz;
pg_encname *base = pg_encname_tbl,
unsigned int nel = lengthof(pg_encname_tbl);
const pg_encname *base = pg_encname_tbl,
*last = base + nel - 1,
*position;
int result;
@@ -481,13 +483,13 @@ pg_char_to_encname_struct(const char *name)
*key;
if (name == NULL || *name == '\0')
return NULL;
return -1;
if (strlen(name) >= NAMEDATALEN)
{
#ifdef FRONTEND
fprintf(stderr, "encoding name too long\n");
return NULL;
return -1;
#else
ereport(ERROR,
(errcode(ERRCODE_NAME_TOO_LONG),
@@ -505,29 +507,14 @@ pg_char_to_encname_struct(const char *name)
{
result = strcmp(key, position->name);
if (result == 0)
return position;
return position->encoding;
}
if (result < 0)
last = position - 1;
else
base = position + 1;
}
return NULL;
}
/*
* Returns encoding or -1 for error
*/
int
pg_char_to_encoding(const char *name)
{
pg_encname *p;
if (!name)
return -1;
p = pg_char_to_encname_struct(name);
return p ? p->encoding : -1;
return -1;
}
#ifndef FRONTEND
@@ -545,7 +532,7 @@ pg_encoding_to_char(int encoding)
{
if (PG_VALID_ENCODING(encoding))
{
pg_enc2name *p = &pg_enc2name_tbl[encoding];
const pg_enc2name *p = &pg_enc2name_tbl[encoding];
Assert(encoding == p->encoding);
return p->name;

View File

@@ -55,9 +55,9 @@ static FmgrInfo *ToClientConvProc = NULL;
/*
* These variables track the currently-selected encodings.
*/
static pg_enc2name *ClientEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
static pg_enc2name *DatabaseEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
static pg_enc2name *MessageEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
static const pg_enc2name *ClientEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
static const pg_enc2name *DatabaseEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
static const pg_enc2name *MessageEncoding = &pg_enc2name_tbl[PG_SQL_ASCII];
/*
* During backend startup we can't set client encoding because we (a)

View File

@@ -1720,7 +1720,7 @@ pg_eucjp_increment(unsigned char *charptr, int length)
* XXX must be sorted by the same order as enum pg_enc (in mb/pg_wchar.h)
*-------------------------------------------------------------------
*/
pg_wchar_tbl pg_wchar_table[] = {
const pg_wchar_tbl pg_wchar_table[] = {
{pg_ascii2wchar_with_len, pg_wchar2single_with_len, pg_ascii_mblen, pg_ascii_dsplen, pg_ascii_verifier, 1}, /* PG_SQL_ASCII */
{pg_eucjp2wchar_with_len, pg_wchar2euc_with_len, pg_eucjp_mblen, pg_eucjp_dsplen, pg_eucjp_verifier, 3}, /* PG_EUC_JP */
{pg_euccn2wchar_with_len, pg_wchar2euc_with_len, pg_euccn_mblen, pg_euccn_dsplen, pg_euccn_verifier, 2}, /* PG_EUC_CN */