LCOV - differential code coverage report
Current view: top level - src/common - fe_memutils.c (source / functions) Coverage Total Hit UIC UBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 73.8 % 65 48 2 15 18 1 29 2 17 2
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 14 14 7 1 6 7
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * fe_memutils.c
       4                 :  *    memory management support for frontend code
       5                 :  *
       6                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
       7                 :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :  *
       9                 :  *
      10                 :  * IDENTIFICATION
      11                 :  *    src/common/fe_memutils.c
      12                 :  *
      13                 :  *-------------------------------------------------------------------------
      14                 :  */
      15                 : 
      16                 : #ifndef FRONTEND
      17                 : #error "This file is not expected to be compiled for backend code"
      18                 : #endif
      19                 : 
      20                 : #include "postgres_fe.h"
      21                 : 
      22                 : static inline void *
      23 CBC     2678707 : pg_malloc_internal(size_t size, int flags)
      24                 : {
      25                 :     void       *tmp;
      26                 : 
      27                 :     /* Avoid unportable behavior of malloc(0) */
      28         2678707 :     if (size == 0)
      29            2581 :         size = 1;
      30         2678707 :     tmp = malloc(size);
      31         2678707 :     if (tmp == NULL)
      32                 :     {
      33 UBC           0 :         if ((flags & MCXT_ALLOC_NO_OOM) == 0)
      34                 :         {
      35               0 :             fprintf(stderr, _("out of memory\n"));
      36               0 :             exit(EXIT_FAILURE);
      37                 :         }
      38               0 :         return NULL;
      39                 :     }
      40                 : 
      41 CBC     2678707 :     if ((flags & MCXT_ALLOC_ZERO) != 0)
      42         3419756 :         MemSet(tmp, 0, size);
      43         2678707 :     return tmp;
      44                 : }
      45                 : 
      46                 : void *
      47         1210683 : pg_malloc(size_t size)
      48                 : {
      49         1210683 :     return pg_malloc_internal(size, 0);
      50                 : }
      51                 : 
      52                 : void *
      53          995756 : pg_malloc0(size_t size)
      54                 : {
      55          995756 :     return pg_malloc_internal(size, MCXT_ALLOC_ZERO);
      56                 : }
      57                 : 
      58                 : void *
      59           44711 : pg_malloc_extended(size_t size, int flags)
      60                 : {
      61           44711 :     return pg_malloc_internal(size, flags);
      62                 : }
      63                 : 
      64                 : void *
      65           25313 : pg_realloc(void *ptr, size_t size)
      66                 : {
      67                 :     void       *tmp;
      68                 : 
      69                 :     /* Avoid unportable behavior of realloc(NULL, 0) */
      70           25313 :     if (ptr == NULL && size == 0)
      71 UBC           0 :         size = 1;
      72 CBC       25313 :     tmp = realloc(ptr, size);
      73           25313 :     if (!tmp)
      74                 :     {
      75 UBC           0 :         fprintf(stderr, _("out of memory\n"));
      76               0 :         exit(EXIT_FAILURE);
      77                 :     }
      78 CBC       25313 :     return tmp;
      79                 : }
      80                 : 
      81                 : /*
      82                 :  * "Safe" wrapper around strdup().
      83                 :  */
      84                 : char *
      85         9328493 : pg_strdup(const char *in)
      86                 : {
      87                 :     char       *tmp;
      88                 : 
      89         9328493 :     if (!in)
      90                 :     {
      91 UBC           0 :         fprintf(stderr,
      92               0 :                 _("cannot duplicate null pointer (internal error)\n"));
      93               0 :         exit(EXIT_FAILURE);
      94                 :     }
      95 CBC     9328493 :     tmp = strdup(in);
      96         9328493 :     if (!tmp)
      97                 :     {
      98 UBC           0 :         fprintf(stderr, _("out of memory\n"));
      99               0 :         exit(EXIT_FAILURE);
     100                 :     }
     101 CBC     9328493 :     return tmp;
     102                 : }
     103                 : 
     104                 : void
     105         1717727 : pg_free(void *ptr)
     106                 : {
     107 GNC     1717727 :     free(ptr);
     108 GIC     1717727 : }
     109                 : 
     110                 : /*
     111                 :  * Frontend emulation of backend memory management functions.  Useful for
     112                 :  * programs that compile backend files.
     113                 :  */
     114 ECB             : void *
     115 GIC      426781 : palloc(Size size)
     116 ECB             : {
     117 GIC      426781 :     return pg_malloc_internal(size, 0);
     118                 : }
     119                 : 
     120 ECB             : void *
     121 GIC         447 : palloc0(Size size)
     122 ECB             : {
     123 GIC         447 :     return pg_malloc_internal(size, MCXT_ALLOC_ZERO);
     124                 : }
     125                 : 
     126 ECB             : void *
     127 GIC         329 : palloc_extended(Size size, int flags)
     128 ECB             : {
     129 GIC         329 :     return pg_malloc_internal(size, flags);
     130                 : }
     131                 : 
     132 ECB             : void
     133 GIC      521479 : pfree(void *pointer)
     134 ECB             : {
     135 CBC      521479 :     pg_free(pointer);
     136 GIC      521479 : }
     137                 : 
     138 ECB             : char *
     139 GIC      581511 : pstrdup(const char *in)
     140 ECB             : {
     141 GIC      581511 :     return pg_strdup(in);
     142                 : }
     143                 : 
     144 ECB             : char *
     145 GIC         128 : pnstrdup(const char *in, Size size)
     146                 : {
     147                 :     char       *tmp;
     148                 :     int         len;
     149 ECB             : 
     150 GIC         128 :     if (!in)
     151 EUB             :     {
     152 UBC           0 :         fprintf(stderr,
     153               0 :                 _("cannot duplicate null pointer (internal error)\n"));
     154 UIC           0 :         exit(EXIT_FAILURE);
     155                 :     }
     156 ECB             : 
     157 CBC         128 :     len = strnlen(in, size);
     158             128 :     tmp = malloc(len + 1);
     159 GIC         128 :     if (tmp == NULL)
     160 EUB             :     {
     161 UBC           0 :         fprintf(stderr, _("out of memory\n"));
     162 UIC           0 :         exit(EXIT_FAILURE);
     163                 :     }
     164 ECB             : 
     165 CBC         128 :     memcpy(tmp, in, len);
     166 GIC         128 :     tmp[len] = '\0';
     167 ECB             : 
     168 GIC         128 :     return tmp;
     169                 : }
     170                 : 
     171 ECB             : void *
     172 GIC         323 : repalloc(void *pointer, Size size)
     173 ECB             : {
     174 GIC         323 :     return pg_realloc(pointer, size);
     175                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a