LCOV - code coverage report
Current view: top level - include/mutable/util - malloc_allocator.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 12 15 80.0 %
Date: 2025-05-23 10:42:01 Functions: 2 2 100.0 %
Branches: 4 10 40.0 %

           Branch data     Line data    Source code
       1                 :            : #pragma once
       2                 :            : 
       3                 :            : #include <algorithm>
       4                 :            : #include <cerrno>
       5                 :            : #include <cstdint>
       6                 :            : #include <cstdlib>
       7                 :            : #include <cstring>
       8                 :            : #include <mutable/util/allocator_base.hpp>
       9                 :            : #include <stdexcept>
      10                 :            : 
      11                 :            : 
      12                 :            : namespace m {
      13                 :            : 
      14                 :            : /** This allocator serves allocations using malloc/free. */
      15                 :            : struct malloc_allocator : allocator<malloc_allocator>
      16                 :            : {
      17                 :            :     using base_type = allocator<malloc_allocator>;
      18                 :            :     using base_type::allocate;
      19                 :            :     using base_type::deallocate;
      20                 :            : 
      21                 :            :     /** Allocate `size` bytes aligned to `alignment`.  If `alignment` is `0`, the usual alignment of `malloc` applies.
      22                 :            :      * */
      23                 :        292 :     void * allocate(size_type size, size_type alignment = 0) {
      24                 :        292 :         errno = 0;
      25         [ +  + ]:        292 :         if (alignment) {
      26                 :        289 :             alignment = std::max(alignment, sizeof(void*));
      27                 :            :             void *ptr;
      28                 :        289 :             const int err = posix_memalign(&ptr, alignment, size);
      29         [ +  - ]:        289 :             if (err) [[unlikely]]
      30         [ #  # ]:          0 :                 throw std::runtime_error(strerror(err));
      31                 :        289 :             return ptr;
      32                 :            :         } else {
      33                 :          3 :             void *ptr = malloc(size);
      34         [ +  - ]:          3 :             if (ptr == nullptr) [[unlikely]] {
      35                 :          0 :                 const auto errsv = errno;
      36         [ #  # ]:          0 :                 throw std::runtime_error(strerror(errsv));
      37                 :            :             }
      38                 :          3 :             return ptr;
      39                 :            :         }
      40                 :        292 :     }
      41                 :            : 
      42                 :            :     /** Deallocate the allocation at `ptr` of size `size`. */
      43                 :        373 :     void deallocate(void *ptr, size_type size) { (void) size; free(ptr); }
      44                 :            : };
      45                 :            : 
      46                 :            : }

Generated by: LCOV version 1.16