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

Appearance settings

Commit 0d79c0a

Browse filesBrowse files
committed
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
1 parent 7d7eee8 commit 0d79c0a
Copy full SHA for 0d79c0a

File tree

Expand file treeCollapse file tree

16 files changed

+101
-130
lines changed
Filter options
Expand file treeCollapse file tree

16 files changed

+101
-130
lines changed

‎src/backend/catalog/objectaddress.c

Copy file name to clipboardExpand all lines: src/backend/catalog/objectaddress.c
+14-14Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ typedef struct
101101
* class? */
102102
} ObjectPropertyType;
103103

104-
static ObjectPropertyType ObjectProperty[] =
104+
static const ObjectPropertyType ObjectProperty[] =
105105
{
106106
{
107107
CastRelationId,
@@ -431,7 +431,7 @@ static ObjectAddress get_object_address_type(ObjectType objtype,
431431
List *objname, bool missing_ok);
432432
static ObjectAddress get_object_address_opcf(ObjectType objtype, List *objname,
433433
List *objargs, bool missing_ok);
434-
static ObjectPropertyType *get_object_property_data(Oid class_id);
434+
static const ObjectPropertyType *get_object_property_data(Oid class_id);
435435

436436
static void getRelationDescription(StringInfo buffer, Oid relid);
437437
static void getOpFamilyDescription(StringInfo buffer, Oid opfid);
@@ -1297,7 +1297,7 @@ get_object_namespace(const ObjectAddress *address)
12971297
HeapTuple tuple;
12981298
bool isnull;
12991299
Oid oid;
1300-
ObjectPropertyType *property;
1300+
const ObjectPropertyType *property;
13011301

13021302
/* If not owned by a namespace, just return InvalidOid. */
13031303
property = get_object_property_data(address->classId);
@@ -1329,71 +1329,71 @@ get_object_namespace(const ObjectAddress *address)
13291329
Oid
13301330
get_object_oid_index(Oid class_id)
13311331
{
1332-
ObjectPropertyType *prop = get_object_property_data(class_id);
1332+
const ObjectPropertyType *prop = get_object_property_data(class_id);
13331333

13341334
return prop->oid_index_oid;
13351335
}
13361336

13371337
int
13381338
get_object_catcache_oid(Oid class_id)
13391339
{
1340-
ObjectPropertyType *prop = get_object_property_data(class_id);
1340+
const ObjectPropertyType *prop = get_object_property_data(class_id);
13411341

13421342
return prop->oid_catcache_id;
13431343
}
13441344

13451345
int
13461346
get_object_catcache_name(Oid class_id)
13471347
{
1348-
ObjectPropertyType *prop = get_object_property_data(class_id);
1348+
const ObjectPropertyType *prop = get_object_property_data(class_id);
13491349

13501350
return prop->name_catcache_id;
13511351
}
13521352

13531353
AttrNumber
13541354
get_object_attnum_name(Oid class_id)
13551355
{
1356-
ObjectPropertyType *prop = get_object_property_data(class_id);
1356+
const ObjectPropertyType *prop = get_object_property_data(class_id);
13571357

13581358
return prop->attnum_name;
13591359
}
13601360

13611361
AttrNumber
13621362
get_object_attnum_namespace(Oid class_id)
13631363
{
1364-
ObjectPropertyType *prop = get_object_property_data(class_id);
1364+
const ObjectPropertyType *prop = get_object_property_data(class_id);
13651365

13661366
return prop->attnum_namespace;
13671367
}
13681368

13691369
AttrNumber
13701370
get_object_attnum_owner(Oid class_id)
13711371
{
1372-
ObjectPropertyType *prop = get_object_property_data(class_id);
1372+
const ObjectPropertyType *prop = get_object_property_data(class_id);
13731373

13741374
return prop->attnum_owner;
13751375
}
13761376

13771377
AttrNumber
13781378
get_object_attnum_acl(Oid class_id)
13791379
{
1380-
ObjectPropertyType *prop = get_object_property_data(class_id);
1380+
const ObjectPropertyType *prop = get_object_property_data(class_id);
13811381

13821382
return prop->attnum_acl;
13831383
}
13841384

13851385
AclObjectKind
13861386
get_object_aclkind(Oid class_id)
13871387
{
1388-
ObjectPropertyType *prop = get_object_property_data(class_id);
1388+
const ObjectPropertyType *prop = get_object_property_data(class_id);
13891389

13901390
return prop->acl_kind;
13911391
}
13921392

13931393
bool
13941394
get_object_namensp_unique(Oid class_id)
13951395
{
1396-
ObjectPropertyType *prop = get_object_property_data(class_id);
1396+
const ObjectPropertyType *prop = get_object_property_data(class_id);
13971397

13981398
return prop->is_nsp_name_unique;
13991399
}
@@ -1419,10 +1419,10 @@ is_objectclass_supported(Oid class_id)
14191419
/*
14201420
* Find ObjectProperty structure by class_id.
14211421
*/
1422-
static ObjectPropertyType *
1422+
static const ObjectPropertyType *
14231423
get_object_property_data(Oid class_id)
14241424
{
1425-
static ObjectPropertyType *prop_last = NULL;
1425+
static const ObjectPropertyType *prop_last = NULL;
14261426
int index;
14271427

14281428
/*

‎src/backend/commands/conversioncmds.c

Copy file name to clipboardExpand all lines: src/backend/commands/conversioncmds.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ CreateConversionCommand(CreateConversionStmt *stmt)
4646
const char *from_encoding_name = stmt->for_encoding_name;
4747
const char *to_encoding_name = stmt->to_encoding_name;
4848
List *func_name = stmt->func_name;
49-
static Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
49+
static const Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
5050
char result[1];
5151

5252
/* Convert list of names to a name and namespace */

‎src/backend/regex/regc_lex.c

Copy file name to clipboardExpand all lines: src/backend/regex/regc_lex.c
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,10 +716,10 @@ static int /* not actually used, but convenient for RETV */
716716
lexescape(struct vars * v)
717717
{
718718
chr c;
719-
static chr alert[] = {
719+
static const chr alert[] = {
720720
CHR('a'), CHR('l'), CHR('e'), CHR('r'), CHR('t')
721721
};
722-
static chr esc[] = {
722+
static const chr esc[] = {
723723
CHR('E'), CHR('S'), CHR('C')
724724
};
725725
const chr *save;

‎src/backend/regex/regcomp.c

Copy file name to clipboardExpand all lines: src/backend/regex/regcomp.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ struct vars
274274

275275

276276
/* static function list */
277-
static struct fns functions = {
277+
static const struct fns functions = {
278278
rfree, /* regfree insides */
279279
};
280280

‎src/backend/regex/regerror.c

Copy file name to clipboardExpand all lines: src/backend/regex/regerror.c
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
#include "regex/regguts.h"
3535

3636
/* unknown-error explanation */
37-
static char unk[] = "*** unknown regex error code 0x%x ***";
37+
static const char unk[] = "*** unknown regex error code 0x%x ***";
3838

3939
/* struct to map among codes, code names, and explanations */
40-
static struct rerr
40+
static const struct rerr
4141
{
4242
int code;
4343
const char *name;
@@ -62,7 +62,7 @@ pg_regerror(int errcode, /* error code, or REG_ATOI or REG_ITOA */
6262
char *errbuf, /* result buffer (unless errbuf_size==0) */
6363
size_t errbuf_size) /* available space in errbuf, can be 0 */
6464
{
65-
struct rerr *r;
65+
const struct rerr *r;
6666
const char *msg;
6767
char convbuf[sizeof(unk) + 50]; /* 50 = plenty for int */
6868
size_t len;

‎src/backend/tsearch/wparser_def.c

Copy file name to clipboardExpand all lines: src/backend/tsearch/wparser_def.c
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ p_isspecial(TParser *prs)
789789
*/
790790
if (GetDatabaseEncoding() == PG_UTF8 && prs->usewide)
791791
{
792-
static pg_wchar strange_letter[] = {
792+
static const pg_wchar strange_letter[] = {
793793
/*
794794
* use binary search, so elements should be ordered
795795
*/
@@ -1023,7 +1023,7 @@ p_isspecial(TParser *prs)
10231023
0xAA34, /* CHAM CONSONANT SIGN RA */
10241024
0xAA4D /* CHAM CONSONANT SIGN FINAL H */
10251025
};
1026-
pg_wchar *StopLow = strange_letter,
1026+
const pg_wchar *StopLow = strange_letter,
10271027
*StopHigh = strange_letter + lengthof(strange_letter),
10281028
*StopMiddle;
10291029
pg_wchar c;

‎src/backend/utils/adt/datetime.c

Copy file name to clipboardExpand all lines: src/backend/utils/adt/datetime.c
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ const int day_tab[2][13] =
5959
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}
6060
};
6161

62-
char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
62+
const char *const months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
6363
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL};
6464

65-
char *days[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
65+
const char *const days[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
6666
"Thursday", "Friday", "Saturday", NULL};
6767

6868

@@ -186,7 +186,7 @@ static const datetkn datetktbl[] = {
186186

187187
static int szdatetktbl = sizeof datetktbl / sizeof datetktbl[0];
188188

189-
static datetkn deltatktbl[] = {
189+
static const datetkn deltatktbl[] = {
190190
/* text, token, lexval */
191191
{"@", IGNORE_DTF, 0}, /* postgres relative prefix */
192192
{DAGO, AGO, 0}, /* "ago" indicates negative time offset */

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.