asrt
Automated System Runtime Testing library
Loading...
Searching...
No Matches
allocator.h
1
11#ifndef ASRT_ALLOCATOR_H
12#define ASRT_ALLOCATOR_H
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18#include "./asrt_assert.h"
19#include "./span.h"
20
21#include <stdlib.h>
22#include <string.h>
23
24
28{
29 void* ptr;
30 void* ( *alloc )( void* ptr, uint32_t size );
31 void ( *free )( void* ptr, void* mem );
32};
33
35static inline void* asrt_alloc( struct asrt_allocator* a, uint32_t size )
36{
37 ASRT_ASSERT( a && a->free );
38 return a->alloc( a->ptr, size );
39}
40
42static inline void asrt_free( struct asrt_allocator* a, void** mem )
43{
44 ASRT_ASSERT( a && a->free );
45 ASRT_ASSERT( *mem );
46 a->free( a->ptr, *mem );
47 *mem = NULL;
48}
49
52char* asrt_realloc_str( struct asrt_allocator* a, struct asrt_span* buff );
53
54static inline void* asrt_call_malloc( void* ptr, uint32_t size )
55{
56 (void) ptr; // context pointer unused by default malloc
57 return malloc( size );
58}
59
60static inline void asrt_call_free( void* ptr, void* mem )
61{
62 (void) ptr; // context pointer unused by default free
63 free( mem );
64}
65
67static inline struct asrt_allocator asrt_default_allocator( void )
68{
69 return ( struct asrt_allocator ){
70 .ptr = NULL,
71 .alloc = &asrt_call_malloc,
72 .free = &asrt_call_free,
73 };
74}
75
76#ifdef __cplusplus
77}
78#endif
79
80#endif
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee ...
Definition: allocator.h:28
void * ptr
Opaque context passed to alloc/free.
Definition: allocator.h:29
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee ...
Definition: span.h:23