Header And Logo

PostgreSQL
| The world's most advanced open source database.

fe_memutils.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * fe_memutils.c
00004  *    memory management support for frontend code
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  *
00010  * IDENTIFICATION
00011  *    src/common/fe_memutils.c
00012  *
00013  *-------------------------------------------------------------------------
00014  */
00015 
00016 #ifndef FRONTEND
00017 #error "This file is not expected to be compiled for backend code"
00018 #endif
00019 
00020 #include "postgres_fe.h"
00021 
00022 void *
00023 pg_malloc(size_t size)
00024 {
00025     void       *tmp;
00026 
00027     /* Avoid unportable behavior of malloc(0) */
00028     if (size == 0)
00029         size = 1;
00030     tmp = malloc(size);
00031     if (!tmp)
00032     {
00033         fprintf(stderr, _("out of memory\n"));
00034         exit(EXIT_FAILURE);
00035     }
00036     return tmp;
00037 }
00038 
00039 void *
00040 pg_malloc0(size_t size)
00041 {
00042     void       *tmp;
00043 
00044     tmp = pg_malloc(size);
00045     MemSet(tmp, 0, size);
00046     return tmp;
00047 }
00048 
00049 void *
00050 pg_realloc(void *ptr, size_t size)
00051 {
00052    void       *tmp;
00053 
00054    /* Avoid unportable behavior of realloc(NULL, 0) */
00055    if (ptr == NULL && size == 0)
00056        size = 1;
00057    tmp = realloc(ptr, size);
00058    if (!tmp)
00059    {
00060         fprintf(stderr, _("out of memory\n"));
00061         exit(EXIT_FAILURE);
00062    }
00063    return tmp;
00064 }
00065 
00066 /*
00067  * "Safe" wrapper around strdup().
00068  */
00069 char *
00070 pg_strdup(const char *in)
00071 {
00072     char       *tmp;
00073 
00074     if (!in)
00075     {
00076         fprintf(stderr,
00077                 _("cannot duplicate null pointer (internal error)\n"));
00078         exit(EXIT_FAILURE);
00079     }
00080     tmp = strdup(in);
00081     if (!tmp)
00082     {
00083         fprintf(stderr, _("out of memory\n"));
00084         exit(EXIT_FAILURE);
00085     }
00086     return tmp;
00087 }
00088 
00089 void
00090 pg_free(void *ptr)
00091 {
00092     if (ptr != NULL)
00093         free(ptr);
00094 }
00095 
00096 /*
00097  * Frontend emulation of backend memory management functions.  Useful for
00098  * programs that compile backend files.
00099  */
00100 void *
00101 palloc(Size size)
00102 {
00103     return pg_malloc(size);
00104 }
00105 
00106 void *
00107 palloc0(Size size)
00108 {
00109     return pg_malloc0(size);
00110 }
00111 
00112 void
00113 pfree(void *pointer)
00114 {
00115     pg_free(pointer);
00116 }
00117 
00118 char *
00119 pstrdup(const char *in)
00120 {
00121     return pg_strdup(in);
00122 }
00123 
00124 void *
00125 repalloc(void *pointer, Size size)
00126 {
00127     return pg_realloc(pointer, size);
00128 }