LCOV - code coverage report
Current view: top level - test - failing_allocator.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 38 38 100.0 %
Date: 2026-11-26 02:32:31 Functions: 9 9 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 10 10 100.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2021-2024 David Robillard <d@drobilla.net>
       2                 :            : // SPDX-License-Identifier: ISC
       3                 :            : 
       4                 :            : #include "failing_allocator.h"
       5                 :            : 
       6                 :            : #include <zix/allocator.h>
       7                 :            : #include <zix/attributes.h>
       8                 :            : 
       9                 :            : #include <stdbool.h>
      10                 :            : #include <stddef.h>
      11                 :            : #include <stdint.h>
      12                 :            : 
      13                 :            : static bool
      14                 :       3998 : attempt(ZixFailingAllocator* const allocator)
      15                 :            : {
      16                 :       3998 :   ++allocator->n_allocations;
      17                 :            : 
      18         [ +  + ]:       3998 :   if (!allocator->n_remaining) {
      19                 :        245 :     return false;
      20                 :            :   }
      21                 :            : 
      22                 :       3753 :   --allocator->n_remaining;
      23                 :       3753 :   return true;
      24                 :            : }
      25                 :            : 
      26                 :            : ZIX_MALLOC_FUNC static void*
      27                 :        345 : zix_failing_malloc(ZixAllocator* const allocator, const size_t size)
      28                 :            : {
      29                 :        345 :   ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator;
      30                 :        345 :   ZixAllocator* const        base  = zix_default_allocator();
      31                 :            : 
      32         [ +  + ]:        345 :   return attempt(state) ? base->malloc(base, size) : NULL;
      33                 :            : }
      34                 :            : 
      35                 :            : ZIX_MALLOC_FUNC static void*
      36                 :       2440 : zix_failing_calloc(ZixAllocator* const allocator,
      37                 :            :                    const size_t        nmemb,
      38                 :            :                    const size_t        size)
      39                 :            : {
      40                 :       2440 :   ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator;
      41                 :       2440 :   ZixAllocator* const        base  = zix_default_allocator();
      42                 :            : 
      43         [ +  + ]:       2440 :   return attempt(state) ? base->calloc(base, nmemb, size) : NULL;
      44                 :            : }
      45                 :            : 
      46                 :            : static void*
      47                 :         12 : zix_failing_realloc(ZixAllocator* const allocator,
      48                 :            :                     void* const         ptr,
      49                 :            :                     const size_t        size)
      50                 :            : {
      51                 :         12 :   ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator;
      52                 :         12 :   ZixAllocator* const        base  = zix_default_allocator();
      53                 :            : 
      54         [ +  + ]:         12 :   return attempt(state) ? base->realloc(base, ptr, size) : NULL;
      55                 :            : }
      56                 :            : 
      57                 :            : static void
      58                 :       2645 : zix_failing_free(ZixAllocator* const allocator, void* const ptr)
      59                 :            : {
      60                 :            :   (void)allocator;
      61                 :            : 
      62                 :       2645 :   ZixAllocator* const base = zix_default_allocator();
      63                 :            : 
      64                 :       2645 :   base->free(base, ptr);
      65                 :       2645 : }
      66                 :            : 
      67                 :            : ZIX_MALLOC_FUNC static void*
      68                 :       1201 : zix_failing_aligned_alloc(ZixAllocator* const allocator,
      69                 :            :                           const size_t        alignment,
      70                 :            :                           const size_t        size)
      71                 :            : {
      72                 :       1201 :   ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator;
      73                 :       1201 :   ZixAllocator* const        base  = zix_default_allocator();
      74                 :            : 
      75         [ +  + ]:       1201 :   return attempt(state) ? base->aligned_alloc(base, alignment, size) : NULL;
      76                 :            : }
      77                 :            : 
      78                 :            : static void
      79                 :       1199 : zix_failing_aligned_free(ZixAllocator* const allocator, void* const ptr)
      80                 :            : {
      81                 :            :   (void)allocator;
      82                 :            : 
      83                 :       1199 :   ZixAllocator* const base = zix_default_allocator();
      84                 :            : 
      85                 :       1199 :   base->aligned_free(base, ptr);
      86                 :       1199 : }
      87                 :            : 
      88                 :            : ZIX_CONST_FUNC ZixFailingAllocator
      89                 :         28 : zix_failing_allocator(void)
      90                 :            : {
      91                 :         28 :   ZixFailingAllocator failing_allocator = {
      92                 :            :     {
      93                 :            :       zix_failing_malloc,
      94                 :            :       zix_failing_calloc,
      95                 :            :       zix_failing_realloc,
      96                 :            :       zix_failing_free,
      97                 :            :       zix_failing_aligned_alloc,
      98                 :            :       zix_failing_aligned_free,
      99                 :            :     },
     100                 :            :     0,
     101                 :            :     SIZE_MAX,
     102                 :            :   };
     103                 :            : 
     104                 :         28 :   return failing_allocator;
     105                 :            : }
     106                 :            : 
     107                 :            : size_t
     108                 :        252 : zix_failing_allocator_reset(ZixFailingAllocator* const allocator,
     109                 :            :                             const size_t               n_allowed)
     110                 :            : {
     111                 :        252 :   const size_t n_allocations = allocator->n_allocations;
     112                 :            : 
     113                 :        252 :   allocator->n_allocations = 0U;
     114                 :        252 :   allocator->n_remaining   = n_allowed;
     115                 :            : 
     116                 :        252 :   return n_allocations;
     117                 :            : }

Generated by: LCOV version 1.16