Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a563d94

Browse files
committed
Standardize naming of malloc/realloc/strdup wrapper functions.
We had a number of variants on the theme of "malloc or die", with the majority named like "pg_malloc", but by no means all. Standardize on the names pg_malloc, pg_malloc0, pg_realloc, pg_strdup. Get rid of pg_calloc entirely in favor of using pg_malloc0. This is an essentially cosmetic change, so no back-patch. (I did find a couple of places where psql and pg_dump were using plain malloc or strdup instead of the pg_ versions, but they don't look significant enough to bother back-patching.)
1 parent 779f80b commit a563d94

28 files changed

+223
-256
lines changed

contrib/oid2name/oid2name.c

+21-21
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ struct options
5050
/* function prototypes */
5151
static void help(const char *progname);
5252
void get_opts(int, char **, struct options *);
53-
void *myalloc(size_t size);
54-
char *mystrdup(const char *str);
53+
void *pg_malloc(size_t size);
54+
char *pg_strdup(const char *str);
5555
void add_one_elt(char *eltname, eary *eary);
5656
char *get_comma_elts(eary *eary);
5757
PGconn *sql_conn(struct options *);
@@ -104,7 +104,7 @@ get_opts(int argc, char **argv, struct options * my_opts)
104104
{
105105
/* specify the database */
106106
case 'd':
107-
my_opts->dbname = mystrdup(optarg);
107+
my_opts->dbname = pg_strdup(optarg);
108108
break;
109109

110110
/* specify one tablename to show */
@@ -129,17 +129,17 @@ get_opts(int argc, char **argv, struct options * my_opts)
129129

130130
/* host to connect to */
131131
case 'H':
132-
my_opts->hostname = mystrdup(optarg);
132+
my_opts->hostname = pg_strdup(optarg);
133133
break;
134134

135135
/* port to connect to on remote host */
136136
case 'p':
137-
my_opts->port = mystrdup(optarg);
137+
my_opts->port = pg_strdup(optarg);
138138
break;
139139

140140
/* username */
141141
case 'U':
142-
my_opts->username = mystrdup(optarg);
142+
my_opts->username = pg_strdup(optarg);
143143
break;
144144

145145
/* display system tables */
@@ -201,7 +201,7 @@ help(const char *progname)
201201
}
202202

203203
void *
204-
myalloc(size_t size)
204+
pg_malloc(size_t size)
205205
{
206206
void *ptr = malloc(size);
207207

@@ -214,7 +214,7 @@ myalloc(size_t size)
214214
}
215215

216216
char *
217-
mystrdup(const char *str)
217+
pg_strdup(const char *str)
218218
{
219219
char *result = strdup(str);
220220

@@ -237,7 +237,7 @@ add_one_elt(char *eltname, eary *eary)
237237
if (eary->alloc == 0)
238238
{
239239
eary ->alloc = 8;
240-
eary ->array = (char **) myalloc(8 * sizeof(char *));
240+
eary ->array = (char **) pg_malloc(8 * sizeof(char *));
241241
}
242242
else if (eary->num >= eary->alloc)
243243
{
@@ -252,7 +252,7 @@ add_one_elt(char *eltname, eary *eary)
252252
}
253253
}
254254

255-
eary ->array[eary->num] = mystrdup(eltname);
255+
eary ->array[eary->num] = pg_strdup(eltname);
256256
eary ->num++;
257257
}
258258

@@ -272,7 +272,7 @@ get_comma_elts(eary *eary)
272272
length = 0;
273273

274274
if (eary->num == 0)
275-
return mystrdup("");
275+
return pg_strdup("");
276276

277277
/*
278278
* PQescapeString wants 2 * length + 1 bytes of breath space. Add two
@@ -281,7 +281,7 @@ get_comma_elts(eary *eary)
281281
for (i = 0; i < eary->num; i++)
282282
length += strlen(eary->array[i]);
283283

284-
ret = (char *) myalloc(length * 2 + 4 * eary->num);
284+
ret = (char *) pg_malloc(length * 2 + 4 * eary->num);
285285
ptr = ret;
286286

287287
for (i = 0; i < eary->num; i++)
@@ -401,7 +401,7 @@ sql_exec(PGconn *conn, const char *todo, bool quiet)
401401
nfields = PQnfields(res);
402402

403403
/* for each field, get the needed width */
404-
length = (int *) myalloc(sizeof(int) * nfields);
404+
length = (int *) pg_malloc(sizeof(int) * nfields);
405405
for (j = 0; j < nfields; j++)
406406
length[j] = strlen(PQfname(res, j));
407407

@@ -424,7 +424,7 @@ sql_exec(PGconn *conn, const char *todo, bool quiet)
424424
l += length[j] + 2;
425425
}
426426
fprintf(stdout, "\n");
427-
pad = (char *) myalloc(l + 1);
427+
pad = (char *) pg_malloc(l + 1);
428428
MemSet(pad, '-', l);
429429
pad[l] = '\0';
430430
fprintf(stdout, "%s\n", pad);
@@ -515,8 +515,8 @@ sql_exec_searchtables(PGconn *conn, struct options * opts)
515515
comma_filenodes = get_comma_elts(opts->filenodes);
516516

517517
/* 80 extra chars for SQL expression */
518-
qualifiers = (char *) myalloc(strlen(comma_oids) + strlen(comma_tables) +
519-
strlen(comma_filenodes) + 80);
518+
qualifiers = (char *) pg_malloc(strlen(comma_oids) + strlen(comma_tables) +
519+
strlen(comma_filenodes) + 80);
520520
ptr = qualifiers;
521521

522522
if (opts->oids->num > 0)
@@ -542,7 +542,7 @@ sql_exec_searchtables(PGconn *conn, struct options * opts)
542542
free(comma_filenodes);
543543

544544
/* now build the query */
545-
todo = (char *) myalloc(650 + strlen(qualifiers));
545+
todo = (char *) pg_malloc(650 + strlen(qualifiers));
546546
snprintf(todo, 650 + strlen(qualifiers),
547547
"SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n"
548548
"FROM pg_catalog.pg_class c \n"
@@ -582,11 +582,11 @@ main(int argc, char **argv)
582582
struct options *my_opts;
583583
PGconn *pgconn;
584584

585-
my_opts = (struct options *) myalloc(sizeof(struct options));
585+
my_opts = (struct options *) pg_malloc(sizeof(struct options));
586586

587-
my_opts->oids = (eary *) myalloc(sizeof(eary));
588-
my_opts->tables = (eary *) myalloc(sizeof(eary));
589-
my_opts->filenodes = (eary *) myalloc(sizeof(eary));
587+
my_opts->oids = (eary *) pg_malloc(sizeof(eary));
588+
my_opts->tables = (eary *) pg_malloc(sizeof(eary));
589+
my_opts->filenodes = (eary *) pg_malloc(sizeof(eary));
590590

591591
my_opts->oids->num = my_opts->oids->alloc = 0;
592592
my_opts->tables->num = my_opts->tables->alloc = 0;

contrib/pgbench/pgbench.c

+29-29
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ static void *threadRun(void *arg);
295295
* routines to check mem allocations and fail noisily.
296296
*/
297297
static void *
298-
xmalloc(size_t size)
298+
pg_malloc(size_t size)
299299
{
300300
void *result;
301301

@@ -309,7 +309,7 @@ xmalloc(size_t size)
309309
}
310310

311311
static void *
312-
xrealloc(void *ptr, size_t size)
312+
pg_realloc(void *ptr, size_t size)
313313
{
314314
void *result;
315315

@@ -323,7 +323,7 @@ xrealloc(void *ptr, size_t size)
323323
}
324324

325325
static char *
326-
xstrdup(const char *s)
326+
pg_strdup(const char *s)
327327
{
328328
char *result;
329329

@@ -574,17 +574,17 @@ putVariable(CState *st, const char *context, char *name, char *value)
574574
}
575575

576576
if (st->variables)
577-
newvars = (Variable *) xrealloc(st->variables,
577+
newvars = (Variable *) pg_realloc(st->variables,
578578
(st->nvariables + 1) * sizeof(Variable));
579579
else
580-
newvars = (Variable *) xmalloc(sizeof(Variable));
580+
newvars = (Variable *) pg_malloc(sizeof(Variable));
581581

582582
st->variables = newvars;
583583

584584
var = &newvars[st->nvariables];
585585

586-
var->name = xstrdup(name);
587-
var->value = xstrdup(value);
586+
var->name = pg_strdup(name);
587+
var->value = pg_strdup(value);
588588

589589
st->nvariables++;
590590

@@ -596,7 +596,7 @@ putVariable(CState *st, const char *context, char *name, char *value)
596596
char *val;
597597

598598
/* dup then free, in case value is pointing at this variable */
599-
val = xstrdup(value);
599+
val = pg_strdup(value);
600600

601601
free(var->value);
602602
var->value = val;
@@ -618,7 +618,7 @@ parseVariable(const char *sql, int *eaten)
618618
if (i == 1)
619619
return NULL;
620620

621-
name = xmalloc(i);
621+
name = pg_malloc(i);
622622
memcpy(name, &sql[1], i - 1);
623623
name[i - 1] = '\0';
624624

@@ -635,7 +635,7 @@ replaceVariable(char **sql, char *param, int len, char *value)
635635
{
636636
size_t offset = param - *sql;
637637

638-
*sql = xrealloc(*sql, strlen(*sql) - len + valueln + 1);
638+
*sql = pg_realloc(*sql, strlen(*sql) - len + valueln + 1);
639639
param = *sql + offset;
640640
}
641641

@@ -971,7 +971,7 @@ doCustom(TState *thread, CState *st, instr_time *conn_time, FILE *logfile)
971971
{
972972
char *sql;
973973

974-
sql = xstrdup(command->argv[0]);
974+
sql = pg_strdup(command->argv[0]);
975975
sql = assignVariables(st, sql);
976976

977977
if (debug)
@@ -1496,7 +1496,7 @@ parseQuery(Command *cmd, const char *raw_sql)
14961496
char *sql,
14971497
*p;
14981498

1499-
sql = xstrdup(raw_sql);
1499+
sql = pg_strdup(raw_sql);
15001500
cmd->argc = 1;
15011501

15021502
p = sql;
@@ -1558,8 +1558,8 @@ process_commands(char *buf)
15581558
return NULL;
15591559

15601560
/* Allocate and initialize Command structure */
1561-
my_commands = (Command *) xmalloc(sizeof(Command));
1562-
my_commands->line = xstrdup(buf);
1561+
my_commands = (Command *) pg_malloc(sizeof(Command));
1562+
my_commands->line = pg_strdup(buf);
15631563
my_commands->command_num = num_commands++;
15641564
my_commands->type = 0; /* until set */
15651565
my_commands->argc = 0;
@@ -1573,7 +1573,7 @@ process_commands(char *buf)
15731573

15741574
while (tok != NULL)
15751575
{
1576-
my_commands->argv[j++] = xstrdup(tok);
1576+
my_commands->argv[j++] = pg_strdup(tok);
15771577
my_commands->argc++;
15781578
tok = strtok(NULL, delim);
15791579
}
@@ -1675,7 +1675,7 @@ process_commands(char *buf)
16751675
switch (querymode)
16761676
{
16771677
case QUERY_SIMPLE:
1678-
my_commands->argv[0] = xstrdup(p);
1678+
my_commands->argv[0] = pg_strdup(p);
16791679
my_commands->argc++;
16801680
break;
16811681
case QUERY_EXTENDED:
@@ -1709,7 +1709,7 @@ process_file(char *filename)
17091709
}
17101710

17111711
alloc_num = COMMANDS_ALLOC_NUM;
1712-
my_commands = (Command **) xmalloc(sizeof(Command *) * alloc_num);
1712+
my_commands = (Command **) pg_malloc(sizeof(Command *) * alloc_num);
17131713

17141714
if (strcmp(filename, "-") == 0)
17151715
fd = stdin;
@@ -1735,7 +1735,7 @@ process_file(char *filename)
17351735
if (lineno >= alloc_num)
17361736
{
17371737
alloc_num += COMMANDS_ALLOC_NUM;
1738-
my_commands = xrealloc(my_commands, sizeof(Command *) * alloc_num);
1738+
my_commands = pg_realloc(my_commands, sizeof(Command *) * alloc_num);
17391739
}
17401740
}
17411741
fclose(fd);
@@ -1758,7 +1758,7 @@ process_builtin(char *tb)
17581758
int alloc_num;
17591759

17601760
alloc_num = COMMANDS_ALLOC_NUM;
1761-
my_commands = (Command **) xmalloc(sizeof(Command *) * alloc_num);
1761+
my_commands = (Command **) pg_malloc(sizeof(Command *) * alloc_num);
17621762

17631763
lineno = 0;
17641764

@@ -1789,7 +1789,7 @@ process_builtin(char *tb)
17891789
if (lineno >= alloc_num)
17901790
{
17911791
alloc_num += COMMANDS_ALLOC_NUM;
1792-
my_commands = xrealloc(my_commands, sizeof(Command *) * alloc_num);
1792+
my_commands = pg_realloc(my_commands, sizeof(Command *) * alloc_num);
17931793
}
17941794
}
17951795

@@ -1961,7 +1961,7 @@ main(int argc, char **argv)
19611961
else if ((env = getenv("PGUSER")) != NULL && *env != '\0')
19621962
login = env;
19631963

1964-
state = (CState *) xmalloc(sizeof(CState));
1964+
state = (CState *) pg_malloc(sizeof(CState));
19651965
memset(state, 0, sizeof(CState));
19661966

19671967
while ((c = getopt_long(argc, argv, "ih:nvp:dSNc:j:Crs:t:T:U:lf:D:F:M:", long_options, &optindex)) != -1)
@@ -2184,7 +2184,7 @@ main(int argc, char **argv)
21842184

21852185
if (nclients > 1)
21862186
{
2187-
state = (CState *) xrealloc(state, sizeof(CState) * nclients);
2187+
state = (CState *) pg_realloc(state, sizeof(CState) * nclients);
21882188
memset(state + 1, 0, sizeof(CState) * (nclients - 1));
21892189

21902190
/* copy any -D switch values to all clients */
@@ -2308,7 +2308,7 @@ main(int argc, char **argv)
23082308
}
23092309

23102310
/* set up thread data structures */
2311-
threads = (TState *) xmalloc(sizeof(TState) * nthreads);
2311+
threads = (TState *) pg_malloc(sizeof(TState) * nthreads);
23122312
for (i = 0; i < nthreads; i++)
23132313
{
23142314
TState *thread = &threads[i];
@@ -2326,9 +2326,9 @@ main(int argc, char **argv)
23262326
int t;
23272327

23282328
thread->exec_elapsed = (instr_time *)
2329-
xmalloc(sizeof(instr_time) * num_commands);
2329+
pg_malloc(sizeof(instr_time) * num_commands);
23302330
thread->exec_count = (int *)
2331-
xmalloc(sizeof(int) * num_commands);
2331+
pg_malloc(sizeof(int) * num_commands);
23322332

23332333
for (t = 0; t < num_commands; t++)
23342334
{
@@ -2419,7 +2419,7 @@ threadRun(void *arg)
24192419
int remains = nstate; /* number of remaining clients */
24202420
int i;
24212421

2422-
result = xmalloc(sizeof(TResult));
2422+
result = pg_malloc(sizeof(TResult));
24232423
INSTR_TIME_SET_ZERO(result->conn_time);
24242424

24252425
/* open log file if requested */
@@ -2632,7 +2632,7 @@ pthread_create(pthread_t *thread,
26322632
fork_pthread *th;
26332633
void *ret;
26342634

2635-
th = (fork_pthread *) xmalloc(sizeof(fork_pthread));
2635+
th = (fork_pthread *) pg_malloc(sizeof(fork_pthread));
26362636
if (pipe(th->pipes) < 0)
26372637
{
26382638
free(th);
@@ -2680,7 +2680,7 @@ pthread_join(pthread_t th, void **thread_return)
26802680
if (thread_return != NULL)
26812681
{
26822682
/* assume result is TResult */
2683-
*thread_return = xmalloc(sizeof(TResult));
2683+
*thread_return = pg_malloc(sizeof(TResult));
26842684
if (read(th->pipes[0], *thread_return, sizeof(TResult)) != sizeof(TResult))
26852685
{
26862686
free(*thread_return);
@@ -2748,7 +2748,7 @@ pthread_create(pthread_t *thread,
27482748
int save_errno;
27492749
win32_pthread *th;
27502750

2751-
th = (win32_pthread *) xmalloc(sizeof(win32_pthread));
2751+
th = (win32_pthread *) pg_malloc(sizeof(win32_pthread));
27522752
th->routine = start_routine;
27532753
th->arg = arg;
27542754
th->result = NULL;

0 commit comments

Comments
 (0)