LCOV - code coverage report
Current view: top level - lib/dpkg - nfmalloc.c (source / functions) Hit Total Coverage
Test: dpkg 1.21.11 C code coverage Lines: 19 19 100.0 %
Date: 2022-12-03 00:40:01 Functions: 5 5 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 18 30 60.0 %

           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 if (!dbobs_init) { nfobstack_init(); }
      42                 :            : 
      43                 :        105 : static void nfobstack_init(void) {
      44                 :        105 :   obstack_init(&db_obs);
      45                 :        105 :   dbobs_init = true;
      46                 :        105 :   obstack_chunk_size(&db_obs) = CHUNK_SIZE;
      47                 :        105 : }
      48                 :            : 
      49                 :            : void *
      50                 :        846 : nfmalloc(size_t size)
      51                 :            : {
      52         [ +  + ]:        846 :   OBSTACK_INIT;
      53   [ -  +  -  +  :        846 :   return obstack_alloc(&db_obs, size);
                   -  + ]
      54                 :            : }
      55                 :            : 
      56                 :        226 : char *nfstrsave(const char *string) {
      57         [ +  + ]:        226 :   OBSTACK_INIT;
      58   [ -  +  -  +  :        226 :   return obstack_copy0 (&db_obs, string, strlen(string));
                   -  + ]
      59                 :            : }
      60                 :            : 
      61                 :            : char *
      62                 :        101 : nfstrnsave(const char *string, size_t size)
      63                 :            : {
      64         [ +  + ]:        101 :   OBSTACK_INIT;
      65   [ -  +  -  +  :        101 :   return obstack_copy0(&db_obs, string, size);
                   -  + ]
      66                 :            : }
      67                 :            : 
      68                 :         53 : void nffreeall(void) {
      69         [ +  + ]:         53 :   if (dbobs_init) {
      70   [ -  +  -  - ]:         48 :     obstack_free(&db_obs, NULL);
      71                 :         48 :     dbobs_init = false;
      72                 :            :   }
      73                 :         53 : }

Generated by: LCOV version 1.16