LCOV - code coverage report
Current view: top level - lib/dpkg - nfmalloc.c (source / functions) Coverage Total Hit
Test: dpkg 1.22.7-3-g89f48 C code coverage Lines: 100.0 % 19 19
Test Date: 2024-07-17 02:53:43 Functions: 100.0 % 5 5
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 60.0 % 30 18

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * libdpkg - Debian packaging suite library routines
       3                 :             :  * nfmalloc.c - non-freeing malloc, used for in-core database
       4                 :             :  *
       5                 :             :  * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
       6                 :             :  *
       7                 :             :  * This is free software; you can redistribute it and/or modify
       8                 :             :  * it under the terms of the GNU General Public License as published by
       9                 :             :  * the Free Software Foundation; either version 2 of the License, or
      10                 :             :  * (at your option) any later version.
      11                 :             :  *
      12                 :             :  * This is distributed in the hope that it will be useful,
      13                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :             :  * GNU General Public License for more details.
      16                 :             :  *
      17                 :             :  * You should have received a copy of the GNU General Public License
      18                 :             :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      19                 :             :  */
      20                 :             : 
      21                 :             : #include <config.h>
      22                 :             : #include <compat.h>
      23                 :             : 
      24                 :             : #include <string.h>
      25                 :             : #include <stdlib.h>
      26                 :             : #include <obstack.h>
      27                 :             : 
      28                 :             : #include <dpkg/i18n.h>
      29                 :             : #include <dpkg/dpkg.h>
      30                 :             : #include <dpkg/dpkg-db.h>
      31                 :             : 
      32                 :             : #define obstack_chunk_alloc m_malloc
      33                 :             : #define obstack_chunk_free free
      34                 :             : 
      35                 :             : static struct obstack db_obs;
      36                 :             : static bool dbobs_init = false;
      37                 :             : 
      38                 :             : /* We use lots of mem, so use a large chunk. */
      39                 :             : #define CHUNK_SIZE 8192
      40                 :             : 
      41                 :             : #define OBSTACK_INIT do { if (!dbobs_init) { nfobstack_init(); } } while (0)
      42                 :             : 
      43                 :         113 : static void nfobstack_init(void) {
      44                 :         113 :   obstack_init(&db_obs);
      45                 :         113 :   dbobs_init = true;
      46                 :         113 :   obstack_chunk_size(&db_obs) = CHUNK_SIZE;
      47                 :         113 : }
      48                 :             : 
      49                 :             : void *
      50                 :         862 : nfmalloc(size_t size)
      51                 :             : {
      52         [ +  + ]:         862 :   OBSTACK_INIT;
      53                 :             :   /* cppcheck-suppress[nullPointerArithmetic]:
      54                 :             :    * False positive, imported module. */
      55   [ -  +  -  +  :         862 :   return obstack_alloc(&db_obs, size);
                   -  + ]
      56                 :             : }
      57                 :             : 
      58                 :         258 : char *nfstrsave(const char *string) {
      59         [ +  + ]:         258 :   OBSTACK_INIT;
      60                 :             :   /* cppcheck-suppress[nullPointerArithmetic]:
      61                 :             :    * False positive, imported module. */
      62   [ -  +  -  +  :         258 :   return obstack_copy0 (&db_obs, string, strlen(string));
                   -  + ]
      63                 :             : }
      64                 :             : 
      65                 :             : char *
      66                 :         109 : nfstrnsave(const char *string, size_t size)
      67                 :             : {
      68         [ +  + ]:         109 :   OBSTACK_INIT;
      69                 :             :   /* cppcheck-suppress[nullPointerArithmetic]:
      70                 :             :    * False positive, imported module. */
      71   [ -  +  -  +  :         109 :   return obstack_copy0(&db_obs, string, size);
                   -  + ]
      72                 :             : }
      73                 :             : 
      74                 :          53 : void nffreeall(void) {
      75         [ +  + ]:          53 :   if (dbobs_init) {
      76                 :             :     /* cppcheck-suppress[nullPointerArithmetic,pointerLessThanZero]:
      77                 :             :      * False positive, imported module. */
      78   [ -  +  -  - ]:          48 :     obstack_free(&db_obs, NULL);
      79                 :          48 :     dbobs_init = false;
      80                 :             :   }
      81                 :          53 : }
        

Generated by: LCOV version 2.0-1