Commits

Anonymous committed a563d94

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.)

  • Participants
  • Parent commits 779f80b

Comments (0)

Files changed (28)

File contrib/oid2name/oid2name.c

 /* 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 *);
 		{
 				/* specify the database */
 			case 'd':
-				my_opts->dbname = mystrdup(optarg);
+				my_opts->dbname = pg_strdup(optarg);
 				break;
 
 				/* specify one tablename to show */
 
 				/* 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 */
 }
 
 void *
-myalloc(size_t size)
+pg_malloc(size_t size)
 {
 	void	   *ptr = malloc(size);
 
 }
 
 char *
-mystrdup(const char *str)
+pg_strdup(const char *str)
 {
 	char	   *result = strdup(str);
 
 	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)
 	{
 		}
 	}
 
-	eary	  ->array[eary->num] = mystrdup(eltname);
+	eary	  ->array[eary->num] = pg_strdup(eltname);
 	eary	  ->num++;
 }
 
 				length = 0;
 
 	if (eary->num == 0)
-		return mystrdup("");
+		return pg_strdup("");
 
 	/*
 	 * PQescapeString wants 2 * length + 1 bytes of breath space.  Add two
 	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++)
 	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));
 
 			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);
 	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)
 	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"
 	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;

File contrib/pgbench/pgbench.c

  * routines to check mem allocations and fail noisily.
  */
 static void *
-xmalloc(size_t size)
+pg_malloc(size_t size)
 {
 	void	   *result;
 
 }
 
 static void *
-xrealloc(void *ptr, size_t size)
+pg_realloc(void *ptr, size_t size)
 {
 	void	   *result;
 
 }
 
 static char *
-xstrdup(const char *s)
+pg_strdup(const char *s)
 {
 	char	   *result;
 
 		}
 
 		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++;
 
 		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;
 	if (i == 1)
 		return NULL;
 
-	name = xmalloc(i);
+	name = pg_malloc(i);
 	memcpy(name, &sql[1], i - 1);
 	name[i - 1] = '\0';
 
 	{
 		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;
 	}
 
 		{
 			char	   *sql;
 
-			sql = xstrdup(command->argv[0]);
+			sql = pg_strdup(command->argv[0]);
 			sql = assignVariables(st, sql);
 
 			if (debug)
 	char	   *sql,
 			   *p;
 
-	sql = xstrdup(raw_sql);
+	sql = pg_strdup(raw_sql);
 	cmd->argc = 1;
 
 	p = sql;
 		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;
 
 		while (tok != NULL)
 		{
-			my_commands->argv[j++] = xstrdup(tok);
+			my_commands->argv[j++] = pg_strdup(tok);
 			my_commands->argc++;
 			tok = strtok(NULL, delim);
 		}
 		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:
 	}
 
 	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;
 		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);
 	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;
 
 		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);
 		}
 	}
 
 	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)
 
 	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 */
 	}
 
 	/* 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];
 			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++)
 			{
 	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 */
 	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);
 	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);
 	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;

File src/bin/initdb/initdb.c

 static char backend_exec[MAXPGPATH];
 
 static void *pg_malloc(size_t size);
-static char *xstrdup(const char *s);
+static char *pg_strdup(const char *s);
 static char **replace_token(char **lines,
 			  const char *token, const char *replacement);
 
 }
 
 static char *
-xstrdup(const char *s)
+pg_strdup(const char *s)
 {
 	char	   *result;
 
 	rewind(infile);
 	nlines = 0;
 	while (fgets(buffer, maxlength + 1, infile) != NULL)
-		result[nlines++] = xstrdup(buffer);
+		result[nlines++] = pg_strdup(buffer);
 
 	fclose(infile);
 	free(buffer);
 	}
 #endif
 
-	return xstrdup(pw->pw_name);
+	return pg_strdup(pw->pw_name);
 }
 
 static char *
 	char		result[20];
 
 	sprintf(result, "%d", enc);
-	return xstrdup(result);
+	return pg_strdup(result);
 }
 
 /*
 	 * underscore.	Just for paranoia, we also stop at '.' or '@'.
 	 */
 	if (lc_type == NULL)
-		langname = xstrdup("");
+		langname = pg_strdup("");
 	else
 	{
-		ptr = langname = xstrdup(lc_type);
+		ptr = langname = pg_strdup(lc_type);
 		while (*ptr && *ptr != '_' && *ptr != '.' && *ptr != '@')
 			ptr++;
 		*ptr = '\0';
 	 * there doesn't seem to be any compelling reason to do that.
 	 */
 	snprintf(cmd, sizeof(cmd), "LC_COLLATE=%s", lc_collate);
-	putenv(xstrdup(cmd));
+	putenv(pg_strdup(cmd));
 
 	snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype);
-	putenv(xstrdup(cmd));
+	putenv(pg_strdup(cmd));
 
 	unsetenv("LC_ALL");
 
 		while (i > 0 && (pwdbuf[i - 1] == '\r' || pwdbuf[i - 1] == '\n'))
 			pwdbuf[--i] = '\0';
 
-		pwd1 = xstrdup(pwdbuf);
+		pwd1 = pg_strdup(pwdbuf);
 
 	}
 	printf(_("setting password ... "));
 				minor = 0,
 				micro = 0;
 	char	   *endptr;
-	char	   *vstr = xstrdup(PG_VERSION);
+	char	   *vstr = pg_strdup(PG_VERSION);
 	char	   *ptr;
 
 	ptr = vstr + (strlen(vstr) - 1);
 	save = setlocale(LC_TIME, NULL);
 	if (!save)
 		return result;
-	save = xstrdup(save);
+	save = pg_strdup(save);
 
 	setlocale(LC_TIME, locale);
 
 		return false;			/* won't happen, we hope */
 
 	/* save may be pointing at a modifiable scratch variable, so copy it. */
-	save = xstrdup(save);
+	save = pg_strdup(save);
 
 	/* set the locale with setlocale, to see if it accepts it. */
 	res = setlocale(category, locale);
 
 	/* save canonical name if requested. */
 	if (res && canonname)
-		*canonname = xstrdup(res);
+		*canonname = pg_strdup(res);
 
 	/* restore old value. */
 	if (!setlocale(category, save))
 	if (check_locale_name(LC_CTYPE, lc_ctype, &canonname))
 		lc_ctype = canonname;
 	else
-		lc_ctype = xstrdup(setlocale(LC_CTYPE, NULL));
+		lc_ctype = pg_strdup(setlocale(LC_CTYPE, NULL));
 	if (check_locale_name(LC_COLLATE, lc_collate, &canonname))
 		lc_collate = canonname;
 	else
-		lc_collate = xstrdup(setlocale(LC_COLLATE, NULL));
+		lc_collate = pg_strdup(setlocale(LC_COLLATE, NULL));
 	if (check_locale_name(LC_NUMERIC, lc_numeric, &canonname))
 		lc_numeric = canonname;
 	else
-		lc_numeric = xstrdup(setlocale(LC_NUMERIC, NULL));
+		lc_numeric = pg_strdup(setlocale(LC_NUMERIC, NULL));
 	if (check_locale_name(LC_TIME, lc_time, &canonname))
 		lc_time = canonname;
 	else
-		lc_time = xstrdup(setlocale(LC_TIME, NULL));
+		lc_time = pg_strdup(setlocale(LC_TIME, NULL));
 	if (check_locale_name(LC_MONETARY, lc_monetary, &canonname))
 		lc_monetary = canonname;
 	else
-		lc_monetary = xstrdup(setlocale(LC_MONETARY, NULL));
+		lc_monetary = pg_strdup(setlocale(LC_MONETARY, NULL));
 #if defined(LC_MESSAGES) && !defined(WIN32)
 	if (check_locale_name(LC_MESSAGES, lc_messages, &canonname))
 		lc_messages = canonname;
 	else
-		lc_messages = xstrdup(setlocale(LC_MESSAGES, NULL));
+		lc_messages = pg_strdup(setlocale(LC_MESSAGES, NULL));
 #else
 	/* when LC_MESSAGES is not available, use the LC_CTYPE setting */
 	if (check_locale_name(LC_CTYPE, lc_messages, &canonname))
 		lc_messages = canonname;
 	else
-		lc_messages = xstrdup(setlocale(LC_CTYPE, NULL));
+		lc_messages = pg_strdup(setlocale(LC_CTYPE, NULL));
 #endif
 }
 
 		switch (c)
 		{
 			case 'A':
-				authmethodlocal = authmethodhost = xstrdup(optarg);
+				authmethodlocal = authmethodhost = pg_strdup(optarg);
 
 				/*
 				 * When ident is specified, use peer for local connections.
 					authmethodhost = "ident";
 				break;
 			case 10:
-				authmethodlocal = xstrdup(optarg);
+				authmethodlocal = pg_strdup(optarg);
 				break;
 			case 11:
-				authmethodhost = xstrdup(optarg);
+				authmethodhost = pg_strdup(optarg);
 				break;
 			case 'D':
-				pg_data = xstrdup(optarg);
+				pg_data = pg_strdup(optarg);
 				break;
 			case 'E':
-				encoding = xstrdup(optarg);
+				encoding = pg_strdup(optarg);
 				break;
 			case 'W':
 				pwprompt = true;
 				break;
 			case 'U':
-				username = xstrdup(optarg);
+				username = pg_strdup(optarg);
 				break;
 			case 'd':
 				debug = true;
 				do_sync = false;
 				break;
 			case 'L':
-				share_path = xstrdup(optarg);
+				share_path = pg_strdup(optarg);
 				break;
 			case 1:
-				locale = xstrdup(optarg);
+				locale = pg_strdup(optarg);
 				break;
 			case 2:
-				lc_collate = xstrdup(optarg);
+				lc_collate = pg_strdup(optarg);
 				break;
 			case 3:
-				lc_ctype = xstrdup(optarg);
+				lc_ctype = pg_strdup(optarg);
 				break;
 			case 4:
-				lc_monetary = xstrdup(optarg);
+				lc_monetary = pg_strdup(optarg);
 				break;
 			case 5:
-				lc_numeric = xstrdup(optarg);
+				lc_numeric = pg_strdup(optarg);
 				break;
 			case 6:
-				lc_time = xstrdup(optarg);
+				lc_time = pg_strdup(optarg);
 				break;
 			case 7:
-				lc_messages = xstrdup(optarg);
+				lc_messages = pg_strdup(optarg);
 				break;
 			case 8:
 				locale = "C";
 				break;
 			case 9:
-				pwfilename = xstrdup(optarg);
+				pwfilename = pg_strdup(optarg);
 				break;
 			case 's':
 				show_setting = true;
 				break;
 			case 'T':
-				default_text_search_config = xstrdup(optarg);
+				default_text_search_config = pg_strdup(optarg);
 				break;
 			case 'X':
-				xlog_dir = xstrdup(optarg);
+				xlog_dir = pg_strdup(optarg);
 				break;
 			default:
 				/* getopt_long already emitted a complaint */
 	 */
 	if (optind < argc && strlen(pg_data) == 0)
 	{
-		pg_data = xstrdup(argv[optind]);
+		pg_data = pg_strdup(argv[optind]);
 		optind++;
 	}
 
 		if (pgdenv && strlen(pgdenv))
 		{
 			/* PGDATA found */
-			pg_data = xstrdup(pgdenv);
+			pg_data = pg_strdup(pgdenv);
 		}
 		else
 		{
 
 		ZeroMemory(&pi, sizeof(pi));
 
-		cmdline = xstrdup(GetCommandLine());
+		cmdline = pg_strdup(GetCommandLine());
 
 		putenv("PG_RESTRICT_EXEC=1");
 

File src/bin/pg_basebackup/pg_basebackup.c

 	uint32		hi,
 				lo;
 
-	param = xmalloc0(sizeof(logstreamer_param));
+	param = pg_malloc0(sizeof(logstreamer_param));
 	param->timeline = timeline;
 	param->sysidentifier = sysidentifier;
 
 				progname, PQntuples(res), PQnfields(res), 1, 3);
 		disconnect_and_exit(1);
 	}
-	sysidentifier = strdup(PQgetvalue(res, 0, 0));
+	sysidentifier = pg_strdup(PQgetvalue(res, 0, 0));
 	timeline = atoi(PQgetvalue(res, 0, 1));
 	PQclear(res);
 
 		switch (c)
 		{
 			case 'D':
-				basedir = xstrdup(optarg);
+				basedir = pg_strdup(optarg);
 				break;
 			case 'F':
 				if (strcmp(optarg, "p") == 0 || strcmp(optarg, "plain") == 0)
 				}
 				break;
 			case 'l':
-				label = xstrdup(optarg);
+				label = pg_strdup(optarg);
 				break;
 			case 'z':
 #ifdef HAVE_LIBZ
 				}
 				break;
 			case 'h':
-				dbhost = xstrdup(optarg);
+				dbhost = pg_strdup(optarg);
 				break;
 			case 'p':
-				dbport = xstrdup(optarg);
+				dbport = pg_strdup(optarg);
 				break;
 			case 'U':
-				dbuser = xstrdup(optarg);
+				dbuser = pg_strdup(optarg);
 				break;
 			case 'w':
 				dbgetpassword = -1;

File src/bin/pg_basebackup/pg_receivexlog.c

 		switch (c)
 		{
 			case 'D':
-				basedir = xstrdup(optarg);
+				basedir = pg_strdup(optarg);
 				break;
 			case 'h':
-				dbhost = xstrdup(optarg);
+				dbhost = pg_strdup(optarg);
 				break;
 			case 'p':
 				if (atoi(optarg) <= 0)
 							progname, optarg);
 					exit(1);
 				}
-				dbport = xstrdup(optarg);
+				dbport = pg_strdup(optarg);
 				break;
 			case 'U':
-				dbuser = xstrdup(optarg);
+				dbuser = pg_strdup(optarg);
 				break;
 			case 'w':
 				dbgetpassword = -1;

File src/bin/pg_basebackup/receivelog.c

 	}
 
 	/* New, empty, file. So pad it to 16Mb with zeroes */
-	zerobuf = xmalloc0(XLOG_BLCKSZ);
+	zerobuf = pg_malloc0(XLOG_BLCKSZ);
 	for (bytes = 0; bytes < XLogSegSize; bytes += XLOG_BLCKSZ)
 	{
 		if (write(f, zerobuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)

File src/bin/pg_basebackup/streamutil.c

 PGconn	   *conn = NULL;
 
 /*
- * strdup() and malloc() replacements that prints an error and exits
+ * strdup() and malloc() replacements that print an error and exit
  * if something goes wrong. Can never return NULL.
  */
 char *
-xstrdup(const char *s)
+pg_strdup(const char *s)
 {
 	char	   *result;
 
 }
 
 void *
-xmalloc0(int size)
+pg_malloc0(size_t size)
 {
 	void	   *result;
 
 	if (dbport)
 		argcount++;
 
-	keywords = xmalloc0((argcount + 1) * sizeof(*keywords));
-	values = xmalloc0((argcount + 1) * sizeof(*values));
+	keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
+	values = pg_malloc0((argcount + 1) * sizeof(*values));
 
 	keywords[0] = "dbname";
 	values[0] = "replication";

File src/bin/pg_basebackup/streamutil.h

 	}
 
 
-char	   *xstrdup(const char *s);
-void	   *xmalloc0(int size);
+extern char *pg_strdup(const char *s);
+extern void *pg_malloc0(size_t size);
 
-PGconn	   *GetConnection(void);
+extern PGconn *GetConnection(void);

File src/bin/pg_ctl/pg_ctl.c

    the supplied arguments. */
 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
 static void *pg_malloc(size_t size);
-static char *xstrdup(const char *s);
+static char *pg_strdup(const char *s);
 static void do_advice(void);
 static void do_help(void);
 static void set_mode(char *modeopt);
 
 
 static char *
-xstrdup(const char *s)
+pg_strdup(const char *s)
 {
 	char	   *result;
 
 	rewind(infile);
 	nlines = 0;
 	while (fgets(buffer, maxlength + 1, infile) != NULL)
-		result[nlines++] = xstrdup(buffer);
+		result[nlines++] = pg_strdup(buffer);
 
 	fclose(infile);
 	free(buffer);
 	if (exec_path == NULL)
 		my_exec_path = find_other_exec_or_die(argv0, "postgres", PG_BACKEND_VERSIONSTR);
 	else
-		my_exec_path = xstrdup(exec_path);
+		my_exec_path = pg_strdup(exec_path);
 
 	snprintf(cmd, MAXPGPATH, SYSTEMQUOTE "\"%s\" %s%s -C data_directory" SYSTEMQUOTE,
 			 my_exec_path, pgdata_opt ? pgdata_opt : "", post_opts ?
 		*strchr(filename, '\n') = '\0';
 
 	free(pg_data);
-	pg_data = xstrdup(filename);
+	pg_data = pg_strdup(filename);
 	canonicalize_path(pg_data);
 }
 
 						char	   *pgdata_D;
 						char	   *env_var = pg_malloc(strlen(optarg) + 8);
 
-						pgdata_D = xstrdup(optarg);
+						pgdata_D = pg_strdup(optarg);
 						canonicalize_path(pgdata_D);
 						snprintf(env_var, strlen(optarg) + 8, "PGDATA=%s",
 								 pgdata_D);
 						break;
 					}
 				case 'l':
-					log_file = xstrdup(optarg);
+					log_file = pg_strdup(optarg);
 					break;
 				case 'm':
 					set_mode(optarg);
 					break;
 				case 'N':
-					register_servicename = xstrdup(optarg);
+					register_servicename = pg_strdup(optarg);
 					break;
 				case 'o':
-					post_opts = xstrdup(optarg);
+					post_opts = pg_strdup(optarg);
 					break;
 				case 'p':
-					exec_path = xstrdup(optarg);
+					exec_path = pg_strdup(optarg);
 					break;
 				case 'P':
-					register_password = xstrdup(optarg);
+					register_password = pg_strdup(optarg);
 					break;
 				case 's':
 					silent_mode = true;
 					break;
 				case 'U':
 					if (strchr(optarg, '\\'))
-						register_username = xstrdup(optarg);
+						register_username = pg_strdup(optarg);
 					else
 						/* Prepend .\ for local accounts */
 					{
-						register_username = malloc(strlen(optarg) + 3);
-						if (!register_username)
-						{
-							write_stderr(_("%s: out of memory\n"), progname);
-							exit(1);
-						}
+						register_username = pg_malloc(strlen(optarg) + 3);
 						strcpy(register_username, ".\\");
 						strcat(register_username, optarg);
 					}
 	pg_config = getenv("PGDATA");
 	if (pg_config)
 	{
-		pg_config = xstrdup(pg_config);
+		pg_config = pg_strdup(pg_config);
 		canonicalize_path(pg_config);
-		pg_data = xstrdup(pg_config);
+		pg_data = pg_strdup(pg_config);
 	}
 
 	/* -D might point at config-only directory; if so find the real PGDATA */

File src/bin/pg_dump/compress_io.c

 		exit_horribly(modulename, "not built with zlib support\n");
 #endif
 
-	cs = (CompressorState *) pg_calloc(1, sizeof(CompressorState));
+	cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
 	cs->writeF = writeF;
 	cs->comprAlg = alg;
 

File src/bin/pg_dump/dumpmem.c

 }
 
 void *
-pg_calloc(size_t nmemb, size_t size)
+pg_malloc0(size_t size)
 {
 	void	   *tmp;
 
-	tmp = calloc(nmemb, size);
-	if (!tmp)
-		exit_horribly(NULL, "out of memory\n");
+	tmp = pg_malloc(size);
+	MemSet(tmp, 0, size);
 	return tmp;
 }
 

File src/bin/pg_dump/dumpmem.h

 
 extern char *pg_strdup(const char *string);
 extern void *pg_malloc(size_t size);
-extern void *pg_calloc(size_t nmemb, size_t size);
+extern void *pg_malloc0(size_t size);
 extern void *pg_realloc(void *ptr, size_t size);
 
 #endif   /* DUMPMEM_H */

File src/bin/pg_dump/pg_backup_archiver.c

 {
 	RestoreOptions *opts;
 
-	opts = (RestoreOptions *) pg_calloc(1, sizeof(RestoreOptions));
+	opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions));
 
 	/* set any fields that shouldn't default to zeroes */
 	opts->format = archUnknown;
 	ArchiveHandle *AH = (ArchiveHandle *) AHX;
 	TocEntry   *newToc;
 
-	newToc = (TocEntry *) pg_calloc(1, sizeof(TocEntry));
+	newToc = (TocEntry *) pg_malloc0(sizeof(TocEntry));
 
 	AH->tocCount++;
 	if (dumpId > AH->maxDumpId)
 	DumpId		maxDumpId = AH->maxDumpId;
 	TocEntry   *te;
 
-	AH->tocsByDumpId = (TocEntry **) pg_calloc(maxDumpId + 1, sizeof(TocEntry *));
-	AH->tableDataId = (DumpId *) pg_calloc(maxDumpId + 1, sizeof(DumpId));
+	AH->tocsByDumpId = (TocEntry **) pg_malloc0((maxDumpId + 1) * sizeof(TocEntry *));
+	AH->tableDataId = (DumpId *) pg_malloc0((maxDumpId + 1) * sizeof(DumpId));
 
 	for (te = AH->toc->next; te != AH->toc; te = te->next)
 	{
 		free(AH->lookahead);
 
 	AH->lookaheadSize = 512;
-	AH->lookahead = pg_calloc(1, 512);
+	AH->lookahead = pg_malloc0(512);
 	AH->lookaheadLen = 0;
 	AH->lookaheadPos = 0;
 
 	write_msg(modulename, "allocating AH for %s, format %d\n", FileSpec, fmt);
 #endif
 
-	AH = (ArchiveHandle *) pg_calloc(1, sizeof(ArchiveHandle));
+	AH = (ArchiveHandle *) pg_malloc0(sizeof(ArchiveHandle));
 
 	/* AH->debugLevel = 100; */
 
 	AH->currTablespace = NULL;	/* ditto */
 	AH->currWithOids = -1;		/* force SET */
 
-	AH->toc = (TocEntry *) pg_calloc(1, sizeof(TocEntry));
+	AH->toc = (TocEntry *) pg_malloc0(sizeof(TocEntry));
 
 	AH->toc->next = AH->toc;
 	AH->toc->prev = AH->toc;
 
 	for (i = 0; i < AH->tocCount; i++)
 	{
-		te = (TocEntry *) pg_calloc(1, sizeof(TocEntry));
+		te = (TocEntry *) pg_malloc0(sizeof(TocEntry));
 		te->dumpId = ReadInt(AH);
 
 		if (te->dumpId > AH->maxDumpId)
 
 	ahlog(AH, 2, "entering restore_toc_entries_parallel\n");
 
-	slots = (ParallelSlot *) pg_calloc(n_slots, sizeof(ParallelSlot));
+	slots = (ParallelSlot *) pg_malloc0(n_slots * sizeof(ParallelSlot));
 	pstate = (ParallelState *) pg_malloc(sizeof(ParallelState));
-	pstate->pse = (ParallelStateEntry *) pg_calloc(n_slots, sizeof(ParallelStateEntry));
+	pstate->pse = (ParallelStateEntry *) pg_malloc0(n_slots * sizeof(ParallelStateEntry));
 	pstate->numWorkers = ropt->number_of_jobs;
 	for (i = 0; i < pstate->numWorkers; i++)
 		unsetProcessIdentifier(&(pstate->pse[i]));
 
 	/* first time around only, make space for handles to listen on */
 	if (handles == NULL)
-		handles = (HANDLE *) pg_calloc(sizeof(HANDLE), n_slots);
+		handles = (HANDLE *) pg_malloc0(n_slots * sizeof(HANDLE));
 
 	/* set up list of handles to listen to */
 	for (snum = 0, tnum = 0; snum < n_slots; snum++)

File src/bin/pg_dump/pg_backup_custom.c

 	AH->DeClonePtr = _DeClone;
 
 	/* Set up a private area. */
-	ctx = (lclContext *) pg_calloc(1, sizeof(lclContext));
+	ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
 	AH->formatData = (void *) ctx;
 
 	/* Initialize LO buffering */
 {
 	lclTocEntry *ctx;
 
-	ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry));
+	ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
 	if (te->dataDumper)
 		ctx->dataState = K_OFFSET_POS_NOT_SET;
 	else
 
 	if (ctx == NULL)
 	{
-		ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry));
+		ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
 		te->formatData = (void *) ctx;
 	}
 

File src/bin/pg_dump/pg_backup_directory.c

 	AH->DeClonePtr = NULL;
 
 	/* Set up our private context */
-	ctx = (lclContext *) pg_calloc(1, sizeof(lclContext));
+	ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
 	AH->formatData = (void *) ctx;
 
 	ctx->dataFH = NULL;
 	lclTocEntry *tctx;
 	char		fn[MAXPGPATH];
 
-	tctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry));
+	tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
 	if (te->dataDumper)
 	{
 		snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId);
 
 	if (tctx == NULL)
 	{
-		tctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry));
+		tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
 		te->formatData = (void *) tctx;
 	}
 

File src/bin/pg_dump/pg_backup_tar.c

 	/*
 	 * Set up some special context used in compressing data.
 	 */
-	ctx = (lclContext *) pg_calloc(1, sizeof(lclContext));
+	ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
 	AH->formatData = (void *) ctx;
 	ctx->filePos = 0;
 	ctx->isSpecialScript = 0;
 	lclTocEntry *ctx;
 	char		fn[K_STD_BUF_SIZE];
 
-	ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry));
+	ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
 	if (te->dataDumper != NULL)
 	{
 #ifdef HAVE_LIBZ
 
 	if (ctx == NULL)
 	{
-		ctx = (lclTocEntry *) pg_calloc(1, sizeof(lclTocEntry));
+		ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
 		te->formatData = (void *) ctx;
 	}
 
 	}
 	else
 	{
-		tm = pg_calloc(1, sizeof(TAR_MEMBER));
+		tm = pg_malloc0(sizeof(TAR_MEMBER));
 
 #ifndef WIN32
 		tm->tmpFH = tmpfile();
 _tarPositionTo(ArchiveHandle *AH, const char *filename)
 {
 	lclContext *ctx = (lclContext *) AH->formatData;
-	TAR_MEMBER *th = pg_calloc(1, sizeof(TAR_MEMBER));
+	TAR_MEMBER *th = pg_malloc0(sizeof(TAR_MEMBER));
 	char		c;
 	char		header[512];
 	size_t		i,

File src/bin/pg_dump/pg_dump.c

 
 	*numFuncs = ntups;
 
-	finfo = (FuncInfo *) pg_calloc(ntups, sizeof(FuncInfo));
+	finfo = (FuncInfo *) pg_malloc0(ntups * sizeof(FuncInfo));
 
 	i_tableoid = PQfnumber(res, "tableoid");
 	i_oid = PQfnumber(res, "oid");
 	 * only one, because we don't yet know which tables might be inheritance
 	 * ancestors of the target table.
 	 */
-	tblinfo = (TableInfo *) pg_calloc(ntups, sizeof(TableInfo));
+	tblinfo = (TableInfo *) pg_malloc0(ntups * sizeof(TableInfo));
 
 	i_reltableoid = PQfnumber(res, "tableoid");
 	i_reloid = PQfnumber(res, "oid");

File src/bin/pg_dump/pg_dump_sort.c

 	bool		fixedloop;
 	int			i;
 
-	processed = (bool *) pg_calloc(getMaxDumpId() + 1, sizeof(bool));
+	processed = (bool *) pg_malloc0((getMaxDumpId() + 1) * sizeof(bool));
 	workspace = (DumpableObject **) pg_malloc(totObjs * sizeof(DumpableObject *));
 	fixedloop = false;
 

File src/bin/psql/command.c

 	{
 		char	   *prompt_text;
 
-		prompt_text = malloc(strlen(username) + 100);
+		prompt_text = pg_malloc(strlen(username) + 100);
 		snprintf(prompt_text, strlen(username) + 100,
 				 _("Password for user %s: "), username);
 		result = simple_prompt(prompt_text, 100, false);
 	}
 	else if (o_conn && user && strcmp(PQuser(o_conn), user) == 0)
 	{
-		password = strdup(PQpass(o_conn));
+		password = pg_strdup(PQpass(o_conn));
 	}
 
 	while (true)

File src/bin/psql/common.c

 }
 
 void *
-pg_malloc_zero(size_t size)
+pg_malloc0(size_t size)
 {
 	void	   *tmp;
 
 	tmp = pg_malloc(size);
-	memset(tmp, 0, size);
-	return tmp;
-}
-
-void *
-pg_calloc(size_t nmemb, size_t size)
-{
-	void	   *tmp;
-
-	tmp = calloc(nmemb, size);
-	if (!tmp)
-	{
-		psql_error("out of memory\n");
-		exit(EXIT_FAILURE);
-	}
+	MemSet(tmp, 0, size);
 	return tmp;
 }
 

File src/bin/psql/common.h

  */
 extern char *pg_strdup(const char *string);
 extern void *pg_malloc(size_t size);
-extern void *pg_malloc_zero(size_t size);
-extern void *pg_calloc(size_t nmemb, size_t size);
+extern void *pg_malloc0(size_t size);
 
 extern bool setQFout(const char *fname);
 

File src/bin/psql/copy.c

 		return NULL;
 	}
 
-	result = pg_calloc(1, sizeof(struct copy_options));
+	result = pg_malloc0(sizeof(struct copy_options));
 
 	result->before_tofrom = pg_strdup("");		/* initialize for appending */
 

File src/bin/psql/describe.c

 	tableinfo.hastriggers = strcmp(PQgetvalue(res, 0, 4), "t") == 0;
 	tableinfo.hasoids = strcmp(PQgetvalue(res, 0, 5), "t") == 0;
 	tableinfo.reloptions = (pset.sversion >= 80200) ?
-		strdup(PQgetvalue(res, 0, 6)) : NULL;
+		pg_strdup(PQgetvalue(res, 0, 6)) : NULL;
 	tableinfo.tablespace = (pset.sversion >= 80000) ?
 		atooid(PQgetvalue(res, 0, 7)) : 0;
 	tableinfo.reloftype = (pset.sversion >= 90000 &&
 						   strcmp(PQgetvalue(res, 0, 8), "") != 0) ?
-		strdup(PQgetvalue(res, 0, 8)) : NULL;
+		pg_strdup(PQgetvalue(res, 0, 8)) : NULL;
 	tableinfo.relpersistence = (pset.sversion >= 90100) ?
 		*(PQgetvalue(res, 0, 9)) : 0;
 	PQclear(res);
 	{
 		show_modifiers = true;
 		headers[cols++] = gettext_noop("Modifiers");
-		modifiers = pg_malloc_zero((numrows + 1) * sizeof(*modifiers));
+		modifiers = pg_malloc0((numrows + 1) * sizeof(*modifiers));
 	}
 
 	if (tableinfo.relkind == 'S')
 		return false;
 
 	nrows = PQntuples(res);
-	attr = pg_malloc_zero((nrows + 1) * sizeof(*attr));
+	attr = pg_malloc0((nrows + 1) * sizeof(*attr));
 
 	printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
 

File src/bin/psql/print.c

 static void print_aligned_vertical(const printTableContent *cont, FILE *fout);
 
 
-static void *
-pg_local_malloc(size_t size)
-{
-	void	   *tmp;
-
-	tmp = malloc(size);
-	if (!tmp)
-	{
-		fprintf(stderr, _("out of memory\n"));
-		exit(EXIT_FAILURE);
-	}
-	return tmp;
-}
-
-static void *
-pg_local_calloc(int count, size_t size)
-{
-	void	   *tmp;
-
-	tmp = calloc(count, size);
-	if (!tmp)
-	{
-		fprintf(stderr, _("out of memory\n"));
-		exit(EXIT_FAILURE);
-	}
-	return tmp;
-}
-
 static int
 integer_digits(const char *my_str)
 {
 				leading_digits;
 	int			groupdigits = atoi(grouping);
 	int			new_str_start = 0;
-	char	   *new_str = pg_local_malloc(
-									 strlen_with_numeric_locale(my_str) + 1);
+	char	   *new_str = pg_malloc(strlen_with_numeric_locale(my_str) + 1);
 
 	leading_digits = (int_len % groupdigits != 0) ?
 		int_len % groupdigits : groupdigits;
 	if (cont->ncolumns > 0)
 	{
 		col_count = cont->ncolumns;
-		width_header = pg_local_calloc(col_count, sizeof(*width_header));
-		width_average = pg_local_calloc(col_count, sizeof(*width_average));
-		max_width = pg_local_calloc(col_count, sizeof(*max_width));
-		width_wrap = pg_local_calloc(col_count, sizeof(*width_wrap));
-		max_nl_lines = pg_local_calloc(col_count, sizeof(*max_nl_lines));
-		curr_nl_line = pg_local_calloc(col_count, sizeof(*curr_nl_line));
-		col_lineptrs = pg_local_calloc(col_count, sizeof(*col_lineptrs));
-		max_bytes = pg_local_calloc(col_count, sizeof(*max_bytes));
-		format_buf = pg_local_calloc(col_count, sizeof(*format_buf));
-		header_done = pg_local_calloc(col_count, sizeof(*header_done));
-		bytes_output = pg_local_calloc(col_count, sizeof(*bytes_output));
-		wrap = pg_local_calloc(col_count, sizeof(*wrap));
+		width_header = pg_malloc0(col_count * sizeof(*width_header));
+		width_average = pg_malloc0(col_count * sizeof(*width_average));
+		max_width = pg_malloc0(col_count * sizeof(*max_width));
+		width_wrap = pg_malloc0(col_count * sizeof(*width_wrap));
+		max_nl_lines = pg_malloc0(col_count * sizeof(*max_nl_lines));
+		curr_nl_line = pg_malloc0(col_count * sizeof(*curr_nl_line));
+		col_lineptrs = pg_malloc0(col_count * sizeof(*col_lineptrs));
+		max_bytes = pg_malloc0(col_count * sizeof(*max_bytes));
+		format_buf = pg_malloc0(col_count * sizeof(*format_buf));
+		header_done = pg_malloc0(col_count * sizeof(*header_done));
+		bytes_output = pg_malloc0(col_count * sizeof(*bytes_output));
+		wrap = pg_malloc0(col_count * sizeof(*wrap));
 	}
 	else
 	{
 	for (i = 0; i < col_count; i++)
 	{
 		/* Add entry for ptr == NULL array termination */
-		col_lineptrs[i] = pg_local_calloc(max_nl_lines[i] + 1,
-										  sizeof(**col_lineptrs));
+		col_lineptrs[i] = pg_malloc0((max_nl_lines[i] + 1) *
+									 sizeof(**col_lineptrs));
 
-		format_buf[i] = pg_local_malloc(max_bytes[i] + 1);
+		format_buf[i] = pg_malloc(max_bytes[i] + 1);
 
 		col_lineptrs[i]->ptr = format_buf[i];
 	}
 	 * We now have all the information we need to setup the formatting
 	 * structures
 	 */
-	dlineptr = pg_local_malloc((sizeof(*dlineptr)) * (dheight + 1));
-	hlineptr = pg_local_malloc((sizeof(*hlineptr)) * (hheight + 1));
+	dlineptr = pg_malloc((sizeof(*dlineptr)) * (dheight + 1));
+	hlineptr = pg_malloc((sizeof(*hlineptr)) * (hheight + 1));
 
-	dlineptr->ptr = pg_local_malloc(dformatsize);
-	hlineptr->ptr = pg_local_malloc(hformatsize);
+	dlineptr->ptr = pg_malloc(dformatsize);
+	hlineptr->ptr = pg_malloc(hformatsize);
 
 	if (cont->opt->start_table)
 	{
 	content->ncolumns = ncolumns;
 	content->nrows = nrows;
 
-	content->headers = pg_local_calloc(ncolumns + 1,
-									   sizeof(*content->headers));
+	content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers));
 
-	content->cells = pg_local_calloc(ncolumns * nrows + 1,
-									 sizeof(*content->cells));
+	content->cells = pg_malloc0((ncolumns * nrows + 1) * sizeof(*content->cells));
 
 	content->cellmustfree = NULL;
 	content->footers = NULL;
 
-	content->aligns = pg_local_calloc(ncolumns + 1,
-									  sizeof(*content->align));
+	content->aligns = pg_malloc0((ncolumns + 1) * sizeof(*content->align));
 
 	content->header = content->headers;
 	content->cell = content->cells;
 	if (mustfree)
 	{
 		if (content->cellmustfree == NULL)
-			content->cellmustfree = pg_local_calloc(
-					   content->ncolumns * content->nrows + 1, sizeof(bool));
+			content->cellmustfree =
+				pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool));
 
 		content->cellmustfree[content->cellsadded] = true;
 	}
 {
 	printTableFooter *f;
 
-	f = pg_local_calloc(1, sizeof(*f));
+	f = pg_malloc0(sizeof(*f));
 	f->data = pg_strdup(footer);
 
 	if (content->footers == NULL)

File src/bin/psql/psqlscan.l

 {
 	PsqlScanState state;
 
-	state = (PsqlScanStateData *) pg_malloc_zero(sizeof(PsqlScanStateData));
+	state = (PsqlScanStateData *) pg_malloc0(sizeof(PsqlScanStateData));
 
 	psql_scan_reset(state);
 

File src/bin/psql/startup.c

 		password_prompt = pg_strdup(_("Password: "));
 	else
 	{
-		password_prompt = malloc(strlen(_("Password for user %s: ")) - 2 +
-								 strlen(options.username) + 1);
+		password_prompt = pg_malloc(strlen(_("Password for user %s: ")) - 2 +
+									strlen(options.username) + 1);
 		sprintf(password_prompt, _("Password for user %s: "),
 				options.username);
 	}

File src/bin/scripts/common.c

 	do
 	{
 #define PARAMS_ARRAY_SIZE	7
-		const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
-		const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
-
-		if (!keywords || !values)
-		{
-			fprintf(stderr, _("%s: out of memory\n"), progname);
-			exit(1);
-		}
+		const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
+		const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
 
 		keywords[0] = "host";
 		values[0] = pghost;
 	return tmp;
 }
 
+void *
+pg_malloc(size_t size)
+{
+	void	   *tmp;
+
+	tmp = malloc(size);
+	if (!tmp)
+	{
+		fprintf(stderr, _("out of memory\n"));
+		exit(EXIT_FAILURE);
+	}
+	return tmp;
+}
+
+void *
+pg_malloc0(size_t size)
+{
+	void	   *tmp;
+
+	tmp = pg_malloc(size);
+	MemSet(tmp, 0, size);
+	return tmp;
+}
+
 /*
  * Check yes/no answer in a localized way.	1=yes, 0=no, -1=neither.
  */

File src/bin/scripts/common.h

 extern void setup_cancel_handler(void);
 
 extern char *pg_strdup(const char *string);
+extern void *pg_malloc(size_t size);
+extern void *pg_malloc0(size_t size);
 
 #endif   /* COMMON_H */