LCOV - code coverage report
Current view: top level - libdw - libdw_alloc.c (source / functions) Coverage Total Hit
Test: elfutils-0.197 Lines: 69.5 % 59 41
Test Date: 2026-10-19 20:07:25 Functions: 40.0 % 5 2
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 75.0 % 16 12

             Branch data     Line data    Source code
       1                 :             : /* Memory handling for libdw.
       2                 :             :    Copyright (C) 2003, 2004, 2006 Red Hat, Inc.
       3                 :             :    This file is part of elfutils.
       4                 :             :    Written by Ulrich Drepper <drepper@redhat.com>, 2003.
       5                 :             : 
       6                 :             :    This file is free software; you can redistribute it and/or modify
       7                 :             :    it under the terms of either
       8                 :             : 
       9                 :             :      * the GNU Lesser General Public License as published by the Free
      10                 :             :        Software Foundation; either version 3 of the License, or (at
      11                 :             :        your option) any later version
      12                 :             : 
      13                 :             :    or
      14                 :             : 
      15                 :             :      * the GNU General Public License as published by the Free
      16                 :             :        Software Foundation; either version 2 of the License, or (at
      17                 :             :        your option) any later version
      18                 :             : 
      19                 :             :    or both in parallel, as here.
      20                 :             : 
      21                 :             :    elfutils is distributed in the hope that it will be useful, but
      22                 :             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      23                 :             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      24                 :             :    General Public License for more details.
      25                 :             : 
      26                 :             :    You should have received copies of the GNU General Public License and
      27                 :             :    the GNU Lesser General Public License along with this program.  If
      28                 :             :    not, see <http://www.gnu.org/licenses/>.  */
      29                 :             : 
      30                 :             : #ifdef HAVE_CONFIG_H
      31                 :             : # include <config.h>
      32                 :             : #endif
      33                 :             : 
      34                 :             : #include <errno.h>
      35                 :             : #include <stdlib.h>
      36                 :             : #include "libdwP.h"
      37                 :             : #include "system.h"
      38                 :             : #include <stdatomic.h>
      39                 :             : 
      40                 :             : #define THREAD_ID_UNSET ((size_t) -1)
      41                 :             : static __thread size_t thread_id = THREAD_ID_UNSET;
      42                 :             : static atomic_size_t next_id = 0;
      43                 :             : 
      44                 :             : struct libdw_memblock *
      45                 :    10578856 : __libdw_alloc_tail (Dwarf *dbg)
      46                 :             : {
      47         [ +  + ]:    10578856 :   if (thread_id == THREAD_ID_UNSET)
      48                 :        1171 :     thread_id = atomic_fetch_add (&next_id, 1);
      49                 :             : 
      50                 :    10578856 :   pthread_rwlock_rdlock (&dbg->mem_rwl);
      51         [ +  + ]:    10578856 :   if (thread_id >= dbg->mem_stacks)
      52                 :             :     {
      53                 :        1760 :       pthread_rwlock_unlock (&dbg->mem_rwl);
      54                 :        1760 :       pthread_rwlock_wrlock (&dbg->mem_rwl);
      55                 :             : 
      56                 :             :       /* Another thread may have already reallocated. In theory using an
      57                 :             :          atomic would be faster, but given that this only happens once per
      58                 :             :          thread per Dwarf, some minor slowdown should be fine.  */
      59         [ +  - ]:        1760 :       if (thread_id >= dbg->mem_stacks)
      60                 :             :         {
      61                 :        1760 :           dbg->mem_tails = realloc (dbg->mem_tails, (thread_id+1)
      62                 :        1760 :                                     * sizeof (struct libdw_memblock *));
      63         [ -  + ]:        1760 :           if (dbg->mem_tails == NULL)
      64                 :             :             {
      65                 :           0 :               pthread_rwlock_unlock (&dbg->mem_rwl);
      66                 :           0 :               dbg->oom_handler();
      67                 :             :             }
      68         [ +  + ]:        4689 :           for (size_t i = dbg->mem_stacks; i <= thread_id; i++)
      69                 :        2929 :             dbg->mem_tails[i] = NULL;
      70                 :        1760 :           dbg->mem_stacks = thread_id + 1;
      71                 :        1760 :           ANNOTATE_HAPPENS_BEFORE (&dbg->mem_tails);
      72                 :             :         }
      73                 :             : 
      74                 :        1760 :       pthread_rwlock_unlock (&dbg->mem_rwl);
      75                 :        1760 :       pthread_rwlock_rdlock (&dbg->mem_rwl);
      76                 :             :     }
      77                 :             : 
      78                 :             :   /* At this point, we have an entry in the tail array.  */
      79                 :    10578856 :   ANNOTATE_HAPPENS_AFTER (&dbg->mem_tails);
      80                 :    10578856 :   struct libdw_memblock *result = dbg->mem_tails[thread_id];
      81         [ +  + ]:    10578856 :   if (result == NULL)
      82                 :             :     {
      83                 :        1760 :       result = malloc (dbg->mem_default_size);
      84         [ -  + ]:        1760 :       if (result == NULL)
      85                 :             :         {
      86                 :           0 :           pthread_rwlock_unlock (&dbg->mem_rwl);
      87                 :           0 :           dbg->oom_handler();
      88                 :             :         }
      89                 :        1760 :       result->size = dbg->mem_default_size
      90                 :        1760 :                      - offsetof (struct libdw_memblock, mem);
      91                 :        1760 :       result->remaining = result->size;
      92                 :        1760 :       result->prev = NULL;
      93                 :        1760 :       dbg->mem_tails[thread_id] = result;
      94                 :             :     }
      95                 :    10578856 :   pthread_rwlock_unlock (&dbg->mem_rwl);
      96                 :    10578856 :   return result;
      97                 :             : }
      98                 :             : 
      99                 :             : /* Can only be called after a allocation for this thread has already
     100                 :             :    been done, to possibly undo it.  */
     101                 :             : struct libdw_memblock *
     102                 :           0 : __libdw_thread_tail (Dwarf *dbg)
     103                 :             : {
     104                 :           0 :   struct libdw_memblock *result;
     105                 :           0 :   pthread_rwlock_rdlock (&dbg->mem_rwl);
     106                 :           0 :   result = dbg->mem_tails[thread_id];
     107                 :           0 :   pthread_rwlock_unlock (&dbg->mem_rwl);
     108                 :           0 :   return result;
     109                 :             : }
     110                 :             : 
     111                 :             : void *
     112                 :       94634 : __libdw_allocate (Dwarf *dbg, size_t minsize, size_t align)
     113                 :             : {
     114                 :       94634 :   size_t size = MAX (dbg->mem_default_size,
     115                 :             :                      (align - 1 +
     116                 :             :                       2 * minsize + offsetof (struct libdw_memblock, mem)));
     117                 :       94634 :   struct libdw_memblock *newp = malloc (size);
     118         [ -  + ]:       94634 :   if (newp == NULL)
     119                 :           0 :     dbg->oom_handler ();
     120                 :             : 
     121                 :       94634 :   uintptr_t result = ((uintptr_t) newp->mem + align - 1) & ~(align - 1);
     122                 :             : 
     123                 :       94634 :   newp->size = size - offsetof (struct libdw_memblock, mem);
     124                 :       94634 :   newp->remaining = (uintptr_t) newp + size - (result + minsize);
     125                 :             : 
     126                 :       94634 :   pthread_rwlock_rdlock (&dbg->mem_rwl);
     127                 :       94634 :   newp->prev = dbg->mem_tails[thread_id];
     128                 :       94634 :   dbg->mem_tails[thread_id] = newp;
     129                 :       94634 :   pthread_rwlock_unlock (&dbg->mem_rwl);
     130                 :             : 
     131                 :       94634 :   return (void *) result;
     132                 :             : }
     133                 :             : 
     134                 :             : 
     135                 :             : Dwarf_OOM
     136                 :           0 : dwarf_new_oom_handler (Dwarf *dbg, Dwarf_OOM handler)
     137                 :             : {
     138                 :           0 :   Dwarf_OOM old = dbg->oom_handler;
     139                 :           0 :   dbg->oom_handler = handler;
     140                 :           0 :   return old;
     141                 :             : }
     142                 :             : 
     143                 :             : 
     144                 :             : void
     145                 :             : __attribute ((noreturn)) attribute_hidden
     146                 :           0 : __libdw_oom (void)
     147                 :             : {
     148                 :           0 :   while (1)
     149                 :           0 :     error (EXIT_FAILURE, ENOMEM, "libdw");
     150                 :             : }
        

Generated by: LCOV version 2.0-1