LCOV - code coverage report
Current view: top level - src/fe_utils - simple_list.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 46 58 79.3 %
Date: 2024-04-25 00:11:39 Functions: 6 8 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * Simple list facilities for frontend code
       4             :  *
       5             :  * Data structures for simple lists of OIDs and strings.  The support for
       6             :  * these is very primitive compared to the backend's List facilities, but
       7             :  * it's all we need in, eg, pg_dump.
       8             :  *
       9             :  *
      10             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
      11             :  * Portions Copyright (c) 1994, Regents of the University of California
      12             :  *
      13             :  * src/fe_utils/simple_list.c
      14             :  *
      15             :  *-------------------------------------------------------------------------
      16             :  */
      17             : #include "postgres_fe.h"
      18             : 
      19             : #include "fe_utils/simple_list.h"
      20             : 
      21             : 
      22             : /*
      23             :  * Append an OID to the list.
      24             :  */
      25             : void
      26         128 : simple_oid_list_append(SimpleOidList *list, Oid val)
      27             : {
      28             :     SimpleOidListCell *cell;
      29             : 
      30         128 :     cell = (SimpleOidListCell *) pg_malloc(sizeof(SimpleOidListCell));
      31         128 :     cell->next = NULL;
      32         128 :     cell->val = val;
      33             : 
      34         128 :     if (list->tail)
      35          42 :         list->tail->next = cell;
      36             :     else
      37          86 :         list->head = cell;
      38         128 :     list->tail = cell;
      39         128 : }
      40             : 
      41             : /*
      42             :  * Is OID present in the list?
      43             :  */
      44             : bool
      45       64146 : simple_oid_list_member(SimpleOidList *list, Oid val)
      46             : {
      47             :     SimpleOidListCell *cell;
      48             : 
      49       78808 :     for (cell = list->head; cell; cell = cell->next)
      50             :     {
      51       14790 :         if (cell->val == val)
      52         128 :             return true;
      53             :     }
      54       64018 :     return false;
      55             : }
      56             : 
      57             : /*
      58             :  * Append a string to the list.
      59             :  *
      60             :  * The given string is copied, so it need not survive past the call.
      61             :  */
      62             : void
      63        8292 : simple_string_list_append(SimpleStringList *list, const char *val)
      64             : {
      65             :     SimpleStringListCell *cell;
      66             : 
      67             :     cell = (SimpleStringListCell *)
      68        8292 :         pg_malloc(offsetof(SimpleStringListCell, val) + strlen(val) + 1);
      69             : 
      70        8292 :     cell->next = NULL;
      71        8292 :     cell->touched = false;
      72        8292 :     strcpy(cell->val, val);
      73             : 
      74        8292 :     if (list->tail)
      75        7604 :         list->tail->next = cell;
      76             :     else
      77         688 :         list->head = cell;
      78        8292 :     list->tail = cell;
      79        8292 : }
      80             : 
      81             : /*
      82             :  * Is string present in the list?
      83             :  *
      84             :  * If found, the "touched" field of the first match is set true.
      85             :  */
      86             : bool
      87         324 : simple_string_list_member(SimpleStringList *list, const char *val)
      88             : {
      89             :     SimpleStringListCell *cell;
      90             : 
      91         490 :     for (cell = list->head; cell; cell = cell->next)
      92             :     {
      93         206 :         if (strcmp(cell->val, val) == 0)
      94             :         {
      95          40 :             cell->touched = true;
      96          40 :             return true;
      97             :         }
      98             :     }
      99         284 :     return false;
     100             : }
     101             : 
     102             : /*
     103             :  * Destroy an OID list
     104             :  */
     105             : void
     106           0 : simple_oid_list_destroy(SimpleOidList *list)
     107             : {
     108             :     SimpleOidListCell *cell;
     109             : 
     110           0 :     cell = list->head;
     111           0 :     while (cell != NULL)
     112             :     {
     113             :         SimpleOidListCell *next;
     114             : 
     115           0 :         next = cell->next;
     116           0 :         pg_free(cell);
     117           0 :         cell = next;
     118             :     }
     119           0 : }
     120             : 
     121             : /*
     122             :  * Destroy a string list
     123             :  */
     124             : void
     125          52 : simple_string_list_destroy(SimpleStringList *list)
     126             : {
     127             :     SimpleStringListCell *cell;
     128             : 
     129          52 :     cell = list->head;
     130         124 :     while (cell != NULL)
     131             :     {
     132             :         SimpleStringListCell *next;
     133             : 
     134          72 :         next = cell->next;
     135          72 :         pg_free(cell);
     136          72 :         cell = next;
     137             :     }
     138          52 : }
     139             : 
     140             : /*
     141             :  * Find first not-touched list entry, if there is one.
     142             :  */
     143             : const char *
     144           0 : simple_string_list_not_touched(SimpleStringList *list)
     145             : {
     146             :     SimpleStringListCell *cell;
     147             : 
     148           0 :     for (cell = list->head; cell; cell = cell->next)
     149             :     {
     150           0 :         if (!cell->touched)
     151           0 :             return cell->val;
     152             :     }
     153           0 :     return NULL;
     154             : }
     155             : 
     156             : /*
     157             :  * Append a pointer to the list.
     158             :  *
     159             :  * Caller must ensure that the pointer remains valid.
     160             :  */
     161             : void
     162       16776 : simple_ptr_list_append(SimplePtrList *list, void *ptr)
     163             : {
     164             :     SimplePtrListCell *cell;
     165             : 
     166       16776 :     cell = (SimplePtrListCell *) pg_malloc(sizeof(SimplePtrListCell));
     167       16776 :     cell->next = NULL;
     168       16776 :     cell->ptr = ptr;
     169             : 
     170       16776 :     if (list->tail)
     171       16260 :         list->tail->next = cell;
     172             :     else
     173         516 :         list->head = cell;
     174       16776 :     list->tail = cell;
     175       16776 : }

Generated by: LCOV version 1.14