diff options
| author | Tom Lane | 2012-10-02 19:35:10 +0000 |
|---|---|---|
| committer | Tom Lane | 2012-10-02 19:35:48 +0000 |
| commit | a563d941803535dbd27d4191fe7729497b7fdf31 (patch) | |
| tree | 79048d522a1b35451381970fcec821e18f3cf0ab /contrib | |
| parent | 779f80b75d448d61cf3388645505c9fd81000bb2 (diff) | |
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.)
Diffstat (limited to 'contrib')
| -rw-r--r-- | contrib/oid2name/oid2name.c | 42 | ||||
| -rw-r--r-- | contrib/pgbench/pgbench.c | 58 |
2 files changed, 50 insertions, 50 deletions
diff --git a/contrib/oid2name/oid2name.c b/contrib/oid2name/oid2name.c index 754eac5f8a2..17d4d80fc90 100644 --- a/contrib/oid2name/oid2name.c +++ b/contrib/oid2name/oid2name.c @@ -50,8 +50,8 @@ struct options /* function prototypes */ static void help(const char *progname); void get_opts(int, char **, struct options *); -void *myalloc(size_t size); -char *mystrdup(const char *str); +void *pg_malloc(size_t size); +char *pg_strdup(const char *str); void add_one_elt(char *eltname, eary *eary); char *get_comma_elts(eary *eary); PGconn *sql_conn(struct options *); @@ -104,7 +104,7 @@ get_opts(int argc, char **argv, struct options * my_opts) { /* specify the database */ case 'd': - my_opts->dbname = mystrdup(optarg); + my_opts->dbname = pg_strdup(optarg); break; /* specify one tablename to show */ @@ -129,17 +129,17 @@ get_opts(int argc, char **argv, struct options * my_opts) /* host to connect to */ case 'H': - my_opts->hostname = mystrdup(optarg); + my_opts->hostname = pg_strdup(optarg); break; /* port to connect to on remote host */ case 'p': - my_opts->port = mystrdup(optarg); + my_opts->port = pg_strdup(optarg); break; /* username */ case 'U': - my_opts->username = mystrdup(optarg); + my_opts->username = pg_strdup(optarg); break; /* display system tables */ @@ -201,7 +201,7 @@ help(const char *progname) } void * -myalloc(size_t size) +pg_malloc(size_t size) { void *ptr = malloc(size); @@ -214,7 +214,7 @@ myalloc(size_t size) } char * -mystrdup(const char *str) +pg_strdup(const char *str) { char *result = strdup(str); @@ -237,7 +237,7 @@ add_one_elt(char *eltname, eary *eary) if (eary->alloc == 0) { eary ->alloc = 8; - eary ->array = (char **) myalloc(8 * sizeof(char *)); + eary ->array = (char **) pg_malloc(8 * sizeof(char *)); } else if (eary->num >= eary->alloc) { @@ -252,7 +252,7 @@ add_one_elt(char *eltname, eary *eary) } } - eary ->array[eary->num] = mystrdup(eltname); + eary ->array[eary->num] = pg_strdup(eltname); eary ->num++; } @@ -272,7 +272,7 @@ get_comma_elts(eary *eary) length = 0; if (eary->num == 0) - return mystrdup(""); + return pg_strdup(""); /* * PQescapeString wants 2 * length + 1 bytes of breath space. Add two @@ -281,7 +281,7 @@ get_comma_elts(eary *eary) for (i = 0; i < eary->num; i++) length += strlen(eary->array[i]); - ret = (char *) myalloc(length * 2 + 4 * eary->num); + ret = (char *) pg_malloc(length * 2 + 4 * eary->num); ptr = ret; for (i = 0; i < eary->num; i++) @@ -401,7 +401,7 @@ sql_exec(PGconn *conn, const char *todo, bool quiet) nfields = PQnfields(res); /* for each field, get the needed width */ - length = (int *) myalloc(sizeof(int) * nfields); + length = (int *) pg_malloc(sizeof(int) * nfields); for (j = 0; j < nfields; j++) length[j] = strlen(PQfname(res, j)); @@ -424,7 +424,7 @@ sql_exec(PGconn *conn, const char *todo, bool quiet) l += length[j] + 2; } fprintf(stdout, "\n"); - pad = (char *) myalloc(l + 1); + pad = (char *) pg_malloc(l + 1); MemSet(pad, '-', l); pad[l] = '\0'; fprintf(stdout, "%s\n", pad); @@ -515,8 +515,8 @@ sql_exec_searchtables(PGconn *conn, struct options * opts) comma_filenodes = get_comma_elts(opts->filenodes); /* 80 extra chars for SQL expression */ - qualifiers = (char *) myalloc(strlen(comma_oids) + strlen(comma_tables) + - strlen(comma_filenodes) + 80); + qualifiers = (char *) pg_malloc(strlen(comma_oids) + strlen(comma_tables) + + strlen(comma_filenodes) + 80); ptr = qualifiers; if (opts->oids->num > 0) @@ -542,7 +542,7 @@ sql_exec_searchtables(PGconn *conn, struct options * opts) free(comma_filenodes); /* now build the query */ - todo = (char *) myalloc(650 + strlen(qualifiers)); + todo = (char *) pg_malloc(650 + strlen(qualifiers)); snprintf(todo, 650 + strlen(qualifiers), "SELECT pg_catalog.pg_relation_filenode(c.oid) as \"Filenode\", relname as \"Table Name\" %s\n" "FROM pg_catalog.pg_class c \n" @@ -582,11 +582,11 @@ main(int argc, char **argv) struct options *my_opts; PGconn *pgconn; - my_opts = (struct options *) myalloc(sizeof(struct options)); + my_opts = (struct options *) pg_malloc(sizeof(struct options)); - my_opts->oids = (eary *) myalloc(sizeof(eary)); - my_opts->tables = (eary *) myalloc(sizeof(eary)); - my_opts->filenodes = (eary *) myalloc(sizeof(eary)); + my_opts->oids = (eary *) pg_malloc(sizeof(eary)); + my_opts->tables = (eary *) pg_malloc(sizeof(eary)); + my_opts->filenodes = (eary *) pg_malloc(sizeof(eary)); my_opts->oids->num = my_opts->oids->alloc = 0; my_opts->tables->num = my_opts->tables->alloc = 0; diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c index f5ac3b1c6d6..509472262bc 100644 --- a/contrib/pgbench/pgbench.c +++ b/contrib/pgbench/pgbench.c @@ -295,7 +295,7 @@ static void *threadRun(void *arg); * routines to check mem allocations and fail noisily. */ static void * -xmalloc(size_t size) +pg_malloc(size_t size) { void *result; @@ -309,7 +309,7 @@ xmalloc(size_t size) } static void * -xrealloc(void *ptr, size_t size) +pg_realloc(void *ptr, size_t size) { void *result; @@ -323,7 +323,7 @@ xrealloc(void *ptr, size_t size) } static char * -xstrdup(const char *s) +pg_strdup(const char *s) { char *result; @@ -574,17 +574,17 @@ putVariable(CState *st, const char *context, char *name, char *value) } if (st->variables) - newvars = (Variable *) xrealloc(st->variables, + newvars = (Variable *) pg_realloc(st->variables, (st->nvariables + 1) * sizeof(Variable)); else - newvars = (Variable *) xmalloc(sizeof(Variable)); + newvars = (Variable *) pg_malloc(sizeof(Variable)); st->variables = newvars; var = &newvars[st->nvariables]; - var->name = xstrdup(name); - var->value = xstrdup(value); + var->name = pg_strdup(name); + var->value = pg_strdup(value); st->nvariables++; @@ -596,7 +596,7 @@ putVariable(CState *st, const char *context, char *name, char *value) char *val; /* dup then free, in case value is pointing at this variable */ - val = xstrdup(value); + val = pg_strdup(value); free(var->value); var->value = val; @@ -618,7 +618,7 @@ parseVariable(const char *sql, int *eaten) if (i == 1) return NULL; - name = xmalloc(i); + name = pg_malloc(i); memcpy(name, &sql[1], i - 1); name[i - 1] = '\0'; @@ -635,7 +635,7 @@ replaceVariable(char **sql, char *param, int len, char *value) { size_t offset = param - *sql; - *sql = xrealloc(*sql, strlen(*sql) - len + valueln + 1); + *sql = pg_realloc(*sql, strlen(*sql) - len + valueln + 1); param = *sql + offset; } @@ -971,7 +971,7 @@ top: { char *sql; - sql = xstrdup(command->argv[0]); + sql = pg_strdup(command->argv[0]); sql = assignVariables(st, sql); if (debug) @@ -1496,7 +1496,7 @@ parseQuery(Command *cmd, const char *raw_sql) char *sql, *p; - sql = xstrdup(raw_sql); + sql = pg_strdup(raw_sql); cmd->argc = 1; p = sql; @@ -1558,8 +1558,8 @@ process_commands(char *buf) return NULL; /* Allocate and initialize Command structure */ - my_commands = (Command *) xmalloc(sizeof(Command)); - my_commands->line = xstrdup(buf); + my_commands = (Command *) pg_malloc(sizeof(Command)); + my_commands->line = pg_strdup(buf); my_commands->command_num = num_commands++; my_commands->type = 0; /* until set */ my_commands->argc = 0; @@ -1573,7 +1573,7 @@ process_commands(char *buf) while (tok != NULL) { - my_commands->argv[j++] = xstrdup(tok); + my_commands->argv[j++] = pg_strdup(tok); my_commands->argc++; tok = strtok(NULL, delim); } @@ -1675,7 +1675,7 @@ process_commands(char *buf) switch (querymode) { case QUERY_SIMPLE: - my_commands->argv[0] = xstrdup(p); + my_commands->argv[0] = pg_strdup(p); my_commands->argc++; break; case QUERY_EXTENDED: @@ -1709,7 +1709,7 @@ process_file(char *filename) } alloc_num = COMMANDS_ALLOC_NUM; - my_commands = (Command **) xmalloc(sizeof(Command *) * alloc_num); + my_commands = (Command **) pg_malloc(sizeof(Command *) * alloc_num); if (strcmp(filename, "-") == 0) fd = stdin; @@ -1735,7 +1735,7 @@ process_file(char *filename) if (lineno >= alloc_num) { alloc_num += COMMANDS_ALLOC_NUM; - my_commands = xrealloc(my_commands, sizeof(Command *) * alloc_num); + my_commands = pg_realloc(my_commands, sizeof(Command *) * alloc_num); } } fclose(fd); @@ -1758,7 +1758,7 @@ process_builtin(char *tb) int alloc_num; alloc_num = COMMANDS_ALLOC_NUM; - my_commands = (Command **) xmalloc(sizeof(Command *) * alloc_num); + my_commands = (Command **) pg_malloc(sizeof(Command *) * alloc_num); lineno = 0; @@ -1789,7 +1789,7 @@ process_builtin(char *tb) if (lineno >= alloc_num) { alloc_num += COMMANDS_ALLOC_NUM; - my_commands = xrealloc(my_commands, sizeof(Command *) * alloc_num); + my_commands = pg_realloc(my_commands, sizeof(Command *) * alloc_num); } } @@ -1961,7 +1961,7 @@ main(int argc, char **argv) else if ((env = getenv("PGUSER")) != NULL && *env != '\0') login = env; - state = (CState *) xmalloc(sizeof(CState)); + state = (CState *) pg_malloc(sizeof(CState)); memset(state, 0, sizeof(CState)); 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) if (nclients > 1) { - state = (CState *) xrealloc(state, sizeof(CState) * nclients); + state = (CState *) pg_realloc(state, sizeof(CState) * nclients); memset(state + 1, 0, sizeof(CState) * (nclients - 1)); /* copy any -D switch values to all clients */ @@ -2308,7 +2308,7 @@ main(int argc, char **argv) } /* set up thread data structures */ - threads = (TState *) xmalloc(sizeof(TState) * nthreads); + threads = (TState *) pg_malloc(sizeof(TState) * nthreads); for (i = 0; i < nthreads; i++) { TState *thread = &threads[i]; @@ -2326,9 +2326,9 @@ main(int argc, char **argv) int t; thread->exec_elapsed = (instr_time *) - xmalloc(sizeof(instr_time) * num_commands); + pg_malloc(sizeof(instr_time) * num_commands); thread->exec_count = (int *) - xmalloc(sizeof(int) * num_commands); + pg_malloc(sizeof(int) * num_commands); for (t = 0; t < num_commands; t++) { @@ -2419,7 +2419,7 @@ threadRun(void *arg) int remains = nstate; /* number of remaining clients */ int i; - result = xmalloc(sizeof(TResult)); + result = pg_malloc(sizeof(TResult)); INSTR_TIME_SET_ZERO(result->conn_time); /* open log file if requested */ @@ -2632,7 +2632,7 @@ pthread_create(pthread_t *thread, fork_pthread *th; void *ret; - th = (fork_pthread *) xmalloc(sizeof(fork_pthread)); + th = (fork_pthread *) pg_malloc(sizeof(fork_pthread)); if (pipe(th->pipes) < 0) { free(th); @@ -2680,7 +2680,7 @@ pthread_join(pthread_t th, void **thread_return) if (thread_return != NULL) { /* assume result is TResult */ - *thread_return = xmalloc(sizeof(TResult)); + *thread_return = pg_malloc(sizeof(TResult)); if (read(th->pipes[0], *thread_return, sizeof(TResult)) != sizeof(TResult)) { free(*thread_return); @@ -2748,7 +2748,7 @@ pthread_create(pthread_t *thread, int save_errno; win32_pthread *th; - th = (win32_pthread *) xmalloc(sizeof(win32_pthread)); + th = (win32_pthread *) pg_malloc(sizeof(win32_pthread)); th->routine = start_routine; th->arg = arg; th->result = NULL; |
