LCOV - code coverage report
Current view: top level - src/common - fe_memutils.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 56.8 % 74 42
Test Date: 2026-01-26 10:56:24 Functions: 78.6 % 14 11
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 59.4 % 32 19

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * fe_memutils.c
       4                 :             :  *        memory management support for frontend code
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, 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                 :      503329 : pg_malloc_internal(size_t size, int flags)
      24                 :             : {
      25                 :      503329 :         void       *tmp;
      26                 :             : 
      27                 :             :         /* Avoid unportable behavior of malloc(0) */
      28         [ +  + ]:      503329 :         if (size == 0)
      29                 :           2 :                 size = 1;
      30                 :      503329 :         tmp = malloc(size);
      31         [ +  - ]:      503329 :         if (tmp == NULL)
      32                 :             :         {
      33         [ #  # ]:           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         [ +  + ]:      503329 :         if ((flags & MCXT_ALLOC_ZERO) != 0)
      42   [ +  -  +  +  :      851998 :                 MemSet(tmp, 0, size);
          +  -  +  +  +  
                      + ]
      43                 :      503329 :         return tmp;
      44                 :      503329 : }
      45                 :             : 
      46                 :             : void *
      47                 :      158597 : pg_malloc(size_t size)
      48                 :             : {
      49                 :      158597 :         return pg_malloc_internal(size, 0);
      50                 :             : }
      51                 :             : 
      52                 :             : void *
      53                 :      333407 : pg_malloc0(size_t size)
      54                 :             : {
      55                 :      333407 :         return pg_malloc_internal(size, MCXT_ALLOC_ZERO);
      56                 :             : }
      57                 :             : 
      58                 :             : void *
      59                 :        9852 : pg_malloc_extended(size_t size, int flags)
      60                 :             : {
      61                 :        9852 :         return pg_malloc_internal(size, flags);
      62                 :             : }
      63                 :             : 
      64                 :             : void *
      65                 :         178 : pg_realloc(void *ptr, size_t size)
      66                 :             : {
      67                 :         178 :         void       *tmp;
      68                 :             : 
      69                 :             :         /* Avoid unportable behavior of realloc(NULL, 0) */
      70   [ +  +  +  - ]:         178 :         if (ptr == NULL && size == 0)
      71                 :           0 :                 size = 1;
      72                 :         178 :         tmp = realloc(ptr, size);
      73         [ +  - ]:         178 :         if (!tmp)
      74                 :             :         {
      75                 :           0 :                 fprintf(stderr, _("out of memory\n"));
      76                 :           0 :                 exit(EXIT_FAILURE);
      77                 :             :         }
      78                 :         356 :         return tmp;
      79                 :         178 : }
      80                 :             : 
      81                 :             : /*
      82                 :             :  * "Safe" wrapper around strdup().
      83                 :             :  */
      84                 :             : char *
      85                 :      516272 : pg_strdup(const char *in)
      86                 :             : {
      87                 :      516272 :         char       *tmp;
      88                 :             : 
      89         [ +  - ]:      516272 :         if (!in)
      90                 :             :         {
      91                 :           0 :                 fprintf(stderr,
      92                 :           0 :                                 _("cannot duplicate null pointer (internal error)\n"));
      93                 :           0 :                 exit(EXIT_FAILURE);
      94                 :             :         }
      95                 :      516272 :         tmp = strdup(in);
      96         [ +  - ]:      516272 :         if (!tmp)
      97                 :             :         {
      98                 :           0 :                 fprintf(stderr, _("out of memory\n"));
      99                 :           0 :                 exit(EXIT_FAILURE);
     100                 :             :         }
     101                 :     1032544 :         return tmp;
     102                 :      516272 : }
     103                 :             : 
     104                 :             : void
     105                 :      555058 : pg_free(void *ptr)
     106                 :             : {
     107                 :      555058 :         free(ptr);
     108                 :      555058 : }
     109                 :             : 
     110                 :             : /*
     111                 :             :  * Frontend emulation of backend memory management functions.  Useful for
     112                 :             :  * programs that compile backend files.
     113                 :             :  */
     114                 :             : void *
     115                 :        1473 : palloc(Size size)
     116                 :             : {
     117                 :        1473 :         return pg_malloc_internal(size, 0);
     118                 :             : }
     119                 :             : 
     120                 :             : void *
     121                 :           0 : palloc0(Size size)
     122                 :             : {
     123                 :           0 :         return pg_malloc_internal(size, MCXT_ALLOC_ZERO);
     124                 :             : }
     125                 :             : 
     126                 :             : void *
     127                 :           0 : palloc_extended(Size size, int flags)
     128                 :             : {
     129                 :           0 :         return pg_malloc_internal(size, flags);
     130                 :             : }
     131                 :             : 
     132                 :             : void
     133                 :         577 : pfree(void *pointer)
     134                 :             : {
     135                 :         577 :         pg_free(pointer);
     136                 :         577 : }
     137                 :             : 
     138                 :             : char *
     139                 :          89 : pstrdup(const char *in)
     140                 :             : {
     141                 :          89 :         return pg_strdup(in);
     142                 :             : }
     143                 :             : 
     144                 :             : char *
     145                 :           0 : pnstrdup(const char *in, Size size)
     146                 :             : {
     147                 :           0 :         char       *tmp;
     148                 :           0 :         int                     len;
     149                 :             : 
     150         [ #  # ]:           0 :         if (!in)
     151                 :             :         {
     152                 :           0 :                 fprintf(stderr,
     153                 :           0 :                                 _("cannot duplicate null pointer (internal error)\n"));
     154                 :           0 :                 exit(EXIT_FAILURE);
     155                 :             :         }
     156                 :             : 
     157                 :           0 :         len = strnlen(in, size);
     158                 :           0 :         tmp = malloc(len + 1);
     159         [ #  # ]:           0 :         if (tmp == NULL)
     160                 :             :         {
     161                 :           0 :                 fprintf(stderr, _("out of memory\n"));
     162                 :           0 :                 exit(EXIT_FAILURE);
     163                 :             :         }
     164                 :             : 
     165                 :           0 :         memcpy(tmp, in, len);
     166                 :           0 :         tmp[len] = '\0';
     167                 :             : 
     168                 :           0 :         return tmp;
     169                 :           0 : }
     170                 :             : 
     171                 :             : void *
     172                 :           2 : repalloc(void *pointer, Size size)
     173                 :             : {
     174                 :           2 :         return pg_realloc(pointer, size);
     175                 :             : }
        

Generated by: LCOV version 2.3.2-1