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

Commit 8396447

Browse files
committed
Create libpgcommon, and move pg_malloc et al to it
libpgcommon is a new static library to allow sharing code among the various frontend programs and backend; this lets us eliminate duplicate implementations of common routines. We avoid libpgport, because that's intended as a place for porting issues; per discussion, it seems better to keep them separate. The first use case, and the only implemented by this patch, is pg_malloc and friends, which many frontend programs were already using. At the same time, we can use this to provide palloc emulation functions for the frontend; this way, some palloc-using files in the backend can also be used by the frontend cleanly. To do this, we change palloc() in the backend to be a function instead of a macro on top of MemoryContextAlloc(). This was previously believed to cause loss of performance, but this implementation has been tweaked by Tom and Andres so that on modern compilers it provides a slight improvement over the previous one. This lets us clean up some places that were already with localized hacks. Most of the pg_malloc/palloc changes in this patch were authored by Andres Freund. Zoltán Böszörményi also independently provided a form of that. libpgcommon infrastructure was authored by Álvaro.
1 parent 0cb1fac commit 8396447

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+332
-684
lines changed

contrib/oid2name/oid2name.c

-50
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ struct options
5050
/* function prototypes */
5151
static void help(const char *progname);
5252
void get_opts(int, char **, struct options *);
53-
void *pg_malloc(size_t size);
54-
void *pg_realloc(void *ptr, size_t size);
55-
char *pg_strdup(const char *str);
5653
void add_one_elt(char *eltname, eary *eary);
5754
char *get_comma_elts(eary *eary);
5855
PGconn *sql_conn(struct options *);
@@ -201,53 +198,6 @@ help(const char *progname)
201198
progname, progname);
202199
}
203200

204-
void *
205-
pg_malloc(size_t size)
206-
{
207-
void *ptr;
208-
209-
/* Avoid unportable behavior of malloc(0) */
210-
if (size == 0)
211-
size = 1;
212-
ptr = malloc(size);
213-
if (!ptr)
214-
{
215-
fprintf(stderr, "out of memory\n");
216-
exit(1);
217-
}
218-
return ptr;
219-
}
220-
221-
void *
222-
pg_realloc(void *ptr, size_t size)
223-
{
224-
void *result;
225-
226-
/* Avoid unportable behavior of realloc(NULL, 0) */
227-
if (ptr == NULL && size == 0)
228-
size = 1;
229-
result = realloc(ptr, size);
230-
if (!result)
231-
{
232-
fprintf(stderr, "out of memory\n");
233-
exit(1);
234-
}
235-
return result;
236-
}
237-
238-
char *
239-
pg_strdup(const char *str)
240-
{
241-
char *result = strdup(str);
242-
243-
if (!result)
244-
{
245-
fprintf(stderr, "out of memory\n");
246-
exit(1);
247-
}
248-
return result;
249-
}
250-
251201
/*
252202
* add_one_elt
253203
*

contrib/pg_upgrade/check.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/check.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/controldata.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/controldata.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/dump.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/dump.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/exec.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/exec.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/file.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/file.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/function.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/function.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/info.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/info.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/option.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/option.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "miscadmin.h"
1313

contrib/pg_upgrade/page.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/page.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/parallel.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/parallel.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/pg_upgrade.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636

3737

38-
#include "postgres.h"
38+
#include "postgres_fe.h"
3939

4040
#include "pg_upgrade.h"
4141

contrib/pg_upgrade/pg_upgrade.h

-4
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,6 @@ void
451451
prep_status(const char *fmt,...)
452452
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
453453
void check_ok(void);
454-
char *pg_strdup(const char *s);
455-
void *pg_malloc(size_t size);
456-
void *pg_realloc(void *ptr, size_t size);
457-
void pg_free(void *ptr);
458454
const char *getErrorText(int errNum);
459455
unsigned int str2uint(const char *str);
460456
void pg_putenv(const char *var, const char *val);

contrib/pg_upgrade/relfilenode.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/relfilenode.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/server.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/server.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/tablespace.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/tablespace.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/util.c

+1-50
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/util.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

@@ -213,55 +213,6 @@ get_user_info(char **user_name)
213213
}
214214

215215

216-
void *
217-
pg_malloc(size_t size)
218-
{
219-
void *p;
220-
221-
/* Avoid unportable behavior of malloc(0) */
222-
if (size == 0)
223-
size = 1;
224-
p = malloc(size);
225-
if (p == NULL)
226-
pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
227-
return p;
228-
}
229-
230-
void *
231-
pg_realloc(void *ptr, size_t size)
232-
{
233-
void *p;
234-
235-
/* Avoid unportable behavior of realloc(NULL, 0) */
236-
if (ptr == NULL && size == 0)
237-
size = 1;
238-
p = realloc(ptr, size);
239-
if (p == NULL)
240-
pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
241-
return p;
242-
}
243-
244-
245-
void
246-
pg_free(void *ptr)
247-
{
248-
if (ptr != NULL)
249-
free(ptr);
250-
}
251-
252-
253-
char *
254-
pg_strdup(const char *s)
255-
{
256-
char *result = strdup(s);
257-
258-
if (result == NULL)
259-
pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
260-
261-
return result;
262-
}
263-
264-
265216
/*
266217
* getErrorText()
267218
*

contrib/pg_upgrade/version.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/version.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pg_upgrade/version_old_8_3.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* contrib/pg_upgrade/version_old_8_3.c
88
*/
99

10-
#include "postgres.h"
10+
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
1313

contrib/pgbench/pgbench.c

-53
Original file line numberDiff line numberDiff line change
@@ -320,59 +320,6 @@ static char *select_only = {
320320
static void setalarm(int seconds);
321321
static void *threadRun(void *arg);
322322

323-
324-
/*
325-
* routines to check mem allocations and fail noisily.
326-
*/
327-
static void *
328-
pg_malloc(size_t size)
329-
{
330-
void *result;
331-
332-
/* Avoid unportable behavior of malloc(0) */
333-
if (size == 0)
334-
size = 1;
335-
result = malloc(size);
336-
if (!result)
337-
{
338-
fprintf(stderr, "out of memory\n");
339-
exit(1);
340-
}
341-
return result;
342-
}
343-
344-
static void *
345-
pg_realloc(void *ptr, size_t size)
346-
{
347-
void *result;
348-
349-
/* Avoid unportable behavior of realloc(NULL, 0) */
350-
if (ptr == NULL && size == 0)
351-
size = 1;
352-
result = realloc(ptr, size);
353-
if (!result)
354-
{
355-
fprintf(stderr, "out of memory\n");
356-
exit(1);
357-
}
358-
return result;
359-
}
360-
361-
static char *
362-
pg_strdup(const char *s)
363-
{
364-
char *result;
365-
366-
result = strdup(s);
367-
if (!result)
368-
{
369-
fprintf(stderr, "out of memory\n");
370-
exit(1);
371-
}
372-
return result;
373-
}
374-
375-
376323
static void
377324
usage(void)
378325
{

src/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ top_builddir = ..
1313
include Makefile.global
1414

1515
SUBDIRS = \
16+
common \
1617
port \
1718
timezone \
1819
backend \

0 commit comments

Comments
 (0)