Line data Source code
1 : // System Includes 2 : #include "string.h" // memset 3 : 4 : // Internal Includes 5 : #include "kirke/allocator.h" 6 : 7 : /** 8 : * \brief Allocator is a simple vtable for functions which manage memory. 9 : */ 10 : typedef struct Allocator { 11 : /** 12 : * This is a pointer to a function which will allocate new memory. 13 : */ 14 : void* ( *alloc )( unsigned long long size, void* allocator_data ); 15 : /** 16 : * This is a pointer to a function which will reallocate an existing region of memory. 17 : */ 18 : void* ( *realloc )( void* pointer, unsigned long long size, void* allocator_data ); 19 : /** 20 : * This is a pointer to a function which will free a region of memory which was allocated by this 21 : * Allocator. 22 : */ 23 : void ( *free )( void* pointer, void* allocator_data ); 24 : /** 25 : * This is user-specified callback method, which will be called in case one of the allocation methods fails. 26 : * This field may be NULL at the user's discretion 27 : */ 28 : void ( *out_of_memory )( void* allocator_data ); 29 : /** 30 : * This is a pointer to user-supplied memory, which will be1 passed to all of the above methods 31 : */ 32 : void* allocator_data; 33 : } Allocator; 34 : 35 : 36 38 : Allocator* allocator__create( 37 : void* ( *alloc_function )( unsigned long long size, void* allocator_data ), 38 : void* ( *realloc_function )( void* pointer, unsigned long long size, void* allocator_data ), 39 : void ( *free_function )( void* pointer, void* allocator_data ), 40 : void ( *out_of_memory_function )( void* allocator_data ), 41 : void* allocator_data 42 : ){ 43 38 : Allocator* allocator = alloc_function( sizeof( Allocator ), allocator_data ); 44 : 45 38 : if( allocator == NULL ){ 46 1 : if( out_of_memory_function != NULL ){ 47 1 : out_of_memory_function( allocator_data ); 48 : } 49 : } 50 : else{ 51 37 : allocator->alloc = alloc_function; 52 37 : allocator->realloc = realloc_function; 53 37 : allocator->free = free_function; 54 37 : allocator->out_of_memory = out_of_memory_function; 55 37 : allocator->allocator_data = allocator_data; 56 : } 57 : 58 38 : return allocator; 59 : } 60 : 61 37 : void allocator__destroy( Allocator* allocator ){ 62 37 : if( allocator != NULL ){ 63 37 : allocator->free( allocator, allocator->allocator_data ); 64 : } 65 37 : } 66 : 67 : 68 58 : void* allocator__alloc( Allocator* allocator, unsigned long long size ){ 69 58 : void* new_memory = allocator->alloc( size, allocator->allocator_data ); 70 : 71 58 : if( new_memory == NULL ){ 72 0 : if( allocator->out_of_memory != NULL ){ 73 0 : allocator->out_of_memory( allocator->allocator_data ); 74 : } 75 : } 76 : 77 58 : return new_memory; 78 : } 79 : 80 1 : void* allocator__calloc( Allocator* allocator, unsigned long long count, unsigned long long size ){ 81 1 : void* new_memory = allocator->alloc( count * size, allocator->allocator_data ); 82 : 83 1 : if( new_memory == NULL ){ 84 0 : if( allocator->out_of_memory != NULL ){ 85 0 : allocator->out_of_memory( allocator->allocator_data ); 86 : } 87 : } 88 : else{ 89 1 : memset( new_memory, 0, count * size ); 90 : } 91 : 92 1 : return new_memory; 93 : } 94 : 95 43 : void *allocator__realloc( Allocator* allocator, void* pointer, unsigned long long size ){ 96 43 : void* new_memory = allocator->realloc( pointer, size, allocator->allocator_data ); 97 : 98 43 : if( new_memory == NULL ){ 99 0 : if( allocator->out_of_memory != NULL ){ 100 0 : allocator->out_of_memory( allocator->allocator_data ); 101 : } 102 : } 103 : 104 43 : return new_memory; 105 : } 106 : 107 48 : void allocator__free( Allocator* allocator, void* pointer ){ 108 48 : allocator->free( pointer, allocator->allocator_data ); 109 48 : }