Created
February 27, 2021 06:04
-
-
Save pmttavara/e12a10a53fb69878046fcc203c2b88dd to your computer and use it in GitHub Desktop.
Allocator abstraction that works for most use cases, according to Andrei Alexandrescu (+ Jon Blow)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
typedef struct Memory_Block { | |
uint8_t *data; | |
size_t num_bytes; | |
} Memory_Block; | |
typedef enum Allocator_Mode { | |
Allocator_Mode_Get_Alignment, /* size_t get_alignment(void); */ | |
Allocator_Mode_Get_Good_Size, /* size_t get_good_size(size_t input_size); */ | |
Allocator_Mode_Allocate, /* Memory_Block allocate(size_t size); */ | |
Allocator_Mode_Allocate_Aligned, /* Memory_Block allocate_aligned(size_t size, size_t alignment); */ | |
Allocator_Mode_Allocate_All, /* Memory_Block allocate_all(void); */ | |
Allocator_Mode_Expand_In_Place, /* bool expand_in_place(Memory_Block *block, size_t delta); */ | |
Allocator_Mode_Reallocate, /* bool reallocate(Memory_Block *block, size_t new_size); */ | |
Allocator_Mode_Reallocate_Aligned, /* bool reallocate_aligned(Memory_Block *block, size_t new_size, size_t alignment); */ | |
Allocator_Mode_Owns, /* bool owns(Memory_Block block); */ | |
Allocator_Mode_Free, /* void free(Memory_Block block); */ | |
Allocator_Mode_Free_All /* void free_all(void); */ | |
} Allocator_Mode; | |
typedef size_t Allocator_Proc(void *allocator_data, Memory_Block *block, Allocator_Mode mode, size_t size, size_t alignment, uintptr_t allocator_options); | |
typedef struct Allocator { | |
Allocator_Proc *proc; | |
void *data; | |
} Allocator; | |
#ifdef __cplusplus | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment