diff options
author | Tom Lane | 2014-01-18 21:04:11 +0000 |
---|---|---|
committer | Tom Lane | 2014-01-18 21:04:32 +0000 |
commit | 0d79c0a8cc20dbaa39112d78a9abb821c4ca3554 (patch) | |
tree | fbf44e5fc488b94c056aead354111d4e32167318 /src/backend/utils/mb/encnames.c | |
parent | 7d7eee8bb702d7796a0d7c5886c1f4685f2e2806 (diff) |
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
Diffstat (limited to 'src/backend/utils/mb/encnames.c')
-rw-r--r-- | src/backend/utils/mb/encnames.c | 55 |
1 files changed, 21 insertions, 34 deletions
diff --git a/src/backend/utils/mb/encnames.c b/src/backend/utils/mb/encnames.c index 772d4a5d056..38fae001eb2 100644 --- a/src/backend/utils/mb/encnames.c +++ b/src/backend/utils/mb/encnames.c @@ -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; |