LCOV - code coverage report
Current view: top level - include/zix - allocator.h (source / functions) Hit Total Coverage
Test: Code coverage Lines: 20 20 100.0 %
Date: 2026-11-26 02:32:31 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 12 12 100.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2021-2025 David Robillard <d@drobilla.net>
       2                 :            : // SPDX-License-Identifier: ISC
       3                 :            : 
       4                 :            : #ifndef ZIX_ALLOCATOR_H
       5                 :            : #define ZIX_ALLOCATOR_H
       6                 :            : 
       7                 :            : #include <zix/attributes.h>
       8                 :            : 
       9                 :            : #include <stddef.h>
      10                 :            : 
      11                 :            : ZIX_BEGIN_DECLS
      12                 :            : 
      13                 :            : /**
      14                 :            :    @defgroup zix_allocator Allocator
      15                 :            :    @ingroup zix_allocation
      16                 :            :    @{
      17                 :            : */
      18                 :            : 
      19                 :            : /**
      20                 :            :    A memory allocator.
      21                 :            : 
      22                 :            :    This object-like structure provides an interface like the standard C
      23                 :            :    functions malloc(), calloc(), realloc(), free(), and aligned_alloc().  It
      24                 :            :    contains function pointers that differ from their standard counterparts by
      25                 :            :    taking a context parameter (a pointer to this struct), which allows the user
      26                 :            :    to implement custom stateful allocators.
      27                 :            : */
      28                 :            : typedef struct ZixAllocatorImpl ZixAllocator;
      29                 :            : 
      30                 :            : /**
      31                 :            :    General malloc-like memory allocation function.
      32                 :            : 
      33                 :            :    This works like the standard C malloc(), except has an additional handle
      34                 :            :    parameter for implementing stateful allocators without static data.
      35                 :            : */
      36                 :            : typedef void* ZIX_ALLOCATED (*ZixMallocFunc)( //
      37                 :            :   ZixAllocator* ZIX_NULLABLE allocator,
      38                 :            :   size_t                     size);
      39                 :            : 
      40                 :            : /**
      41                 :            :    General calloc-like memory allocation function.
      42                 :            : 
      43                 :            :    This works like the standard C calloc(), except has an additional handle
      44                 :            :    parameter for implementing stateful allocators without static data.
      45                 :            : */
      46                 :            : typedef void* ZIX_ALLOCATED (*ZixCallocFunc)( //
      47                 :            :   ZixAllocator* ZIX_NULLABLE allocator,
      48                 :            :   size_t                     nmemb,
      49                 :            :   size_t                     size);
      50                 :            : 
      51                 :            : /**
      52                 :            :    General realloc-like memory reallocation function.
      53                 :            : 
      54                 :            :    This works like the standard C remalloc(), except has an additional handle
      55                 :            :    parameter for implementing stateful allocators without static data.
      56                 :            : */
      57                 :            : typedef void* ZIX_ALLOCATED (*ZixReallocFunc)( //
      58                 :            :   ZixAllocator* ZIX_NULLABLE allocator,
      59                 :            :   void* ZIX_NULLABLE         ptr,
      60                 :            :   size_t                     size);
      61                 :            : 
      62                 :            : /**
      63                 :            :    General free-like memory deallocation function.
      64                 :            : 
      65                 :            :    This works like the standard C remalloc(), except has an additional handle
      66                 :            :    parameter for implementing stateful allocators without static data.
      67                 :            : */
      68                 :            : typedef void (*ZixFreeFunc)( //
      69                 :            :   ZixAllocator* ZIX_NULLABLE allocator,
      70                 :            :   void* ZIX_NULLABLE         ptr);
      71                 :            : 
      72                 :            : /**
      73                 :            :    General aligned_alloc-like memory deallocation function.
      74                 :            : 
      75                 :            :    This works like the standard C aligned_alloc(), except has an additional
      76                 :            :    handle parameter for implementing stateful allocators without static data.
      77                 :            : */
      78                 :            : typedef void* ZIX_ALLOCATED (*ZixAlignedAllocFunc)( //
      79                 :            :   ZixAllocator* ZIX_NULLABLE allocator,
      80                 :            :   size_t                     alignment,
      81                 :            :   size_t                     size);
      82                 :            : 
      83                 :            : /**
      84                 :            :    General aligned memory deallocation function.
      85                 :            : 
      86                 :            :    This works like the standard C free(), but must be used to free memory
      87                 :            :    allocated with the aligned_alloc() method of the allocator.  This allows
      88                 :            :    portability to systems (like Windows) that can't use the same free function
      89                 :            :    in these cases.
      90                 :            : */
      91                 :            : typedef void (*ZixAlignedFreeFunc)( //
      92                 :            :   ZixAllocator* ZIX_NULLABLE allocator,
      93                 :            :   void* ZIX_NULLABLE         ptr);
      94                 :            : 
      95                 :            : /// Definition of ZixAllocator
      96                 :            : struct ZixAllocatorImpl {
      97                 :            :   ZixMallocFunc ZIX_UNSPECIFIED       malloc;        ///< Allocate
      98                 :            :   ZixCallocFunc ZIX_UNSPECIFIED       calloc;        ///< Allocate and zero
      99                 :            :   ZixReallocFunc ZIX_UNSPECIFIED      realloc;       ///< Reallocate
     100                 :            :   ZixFreeFunc ZIX_UNSPECIFIED         free;          ///< Free
     101                 :            :   ZixAlignedAllocFunc ZIX_UNSPECIFIED aligned_alloc; ///< Allocate aligned
     102                 :            :   ZixAlignedFreeFunc ZIX_UNSPECIFIED  aligned_free;  ///< Free aligned
     103                 :            : };
     104                 :            : 
     105                 :            : /// Return the default allocator which simply uses the system allocator
     106                 :            : ZIX_CONST_API ZixAllocator* ZIX_NONNULL
     107                 :            : zix_default_allocator(void);
     108                 :            : 
     109                 :            : /// Convenience wrapper that defers to malloc() if allocator is null
     110                 :            : ZIX_MALLOC_FUNC ZIX_ALLOC_SIZE(2) static inline void* ZIX_ALLOCATED
     111                 :        465 : zix_malloc(ZixAllocator* const ZIX_NULLABLE allocator, const size_t size)
     112                 :            : {
     113         [ +  + ]:        465 :   ZixAllocator* const actual = allocator ? allocator : zix_default_allocator();
     114                 :            : 
     115                 :        465 :   return actual->malloc(actual, size);
     116                 :            : }
     117                 :            : 
     118                 :            : /// Convenience wrapper that defers to calloc() if allocator is null
     119                 :            : ZIX_MALLOC_FUNC ZIX_ALLOC_COUNT_SIZE(2, 3) static inline void* ZIX_ALLOCATED
     120                 :     603765 : zix_calloc(ZixAllocator* const ZIX_NULLABLE allocator,
     121                 :            :            const size_t                     nmemb,
     122                 :            :            const size_t                     size)
     123                 :            : {
     124         [ +  + ]:     603765 :   ZixAllocator* const actual = allocator ? allocator : zix_default_allocator();
     125                 :            : 
     126                 :     603765 :   return actual->calloc(actual, nmemb, size);
     127                 :            : }
     128                 :            : 
     129                 :            : /// Convenience wrapper that defers to realloc() if allocator is null
     130                 :            : ZIX_NODISCARD ZIX_ALLOC_SIZE(3) static inline void* ZIX_ALLOCATED
     131                 :         45 : zix_realloc(ZixAllocator* const ZIX_NULLABLE allocator,
     132                 :            :             void* const ZIX_NULLABLE         ptr,
     133                 :            :             const size_t                     size)
     134                 :            : {
     135         [ +  + ]:         45 :   ZixAllocator* const actual = allocator ? allocator : zix_default_allocator();
     136                 :            : 
     137                 :         45 :   return actual->realloc(actual, ptr, size);
     138                 :            : }
     139                 :            : 
     140                 :            : /// Convenience wrapper that defers to free() if allocator is null
     141                 :            : static inline void
     142                 :     603004 : zix_free(ZixAllocator* const ZIX_NULLABLE allocator,
     143                 :            :          void* const ZIX_NULLABLE         ptr)
     144                 :            : {
     145         [ +  + ]:     603004 :   ZixAllocator* const actual = allocator ? allocator : zix_default_allocator();
     146                 :            : 
     147                 :     603004 :   actual->free(actual, ptr);
     148                 :     603004 : }
     149                 :            : 
     150                 :            : /// Convenience wrapper that defers to the system allocator if allocator is null
     151                 :            : ZIX_MALLOC_FUNC ZIX_ALLOC_SIZE(3) static inline void* ZIX_ALLOCATED
     152                 :       7014 : zix_aligned_alloc(ZixAllocator* const ZIX_NULLABLE allocator,
     153                 :            :                   const size_t                     alignment,
     154                 :            :                   const size_t                     size)
     155                 :            : {
     156         [ +  + ]:       7014 :   ZixAllocator* const actual = allocator ? allocator : zix_default_allocator();
     157                 :            : 
     158                 :       7014 :   return actual->aligned_alloc(actual, alignment, size);
     159                 :            : }
     160                 :            : 
     161                 :            : /// Convenience wrapper that defers to the system allocator if allocator is null
     162                 :            : static inline void
     163                 :       7027 : zix_aligned_free(ZixAllocator* const ZIX_NULLABLE allocator,
     164                 :            :                  void* const ZIX_NULLABLE         ptr)
     165                 :            : {
     166         [ +  + ]:       7027 :   ZixAllocator* const actual = allocator ? allocator : zix_default_allocator();
     167                 :            : 
     168                 :       7027 :   actual->aligned_free(actual, ptr);
     169                 :       7027 : }
     170                 :            : 
     171                 :            : /**
     172                 :            :    @}
     173                 :            : */
     174                 :            : 
     175                 :            : ZIX_END_DECLS
     176                 :            : 
     177                 :            : #endif /* ZIX_ALLOCATOR_H */

Generated by: LCOV version 1.16