Ylva And Malin
ym_allocator.h
Go to the documentation of this file.
1 #pragma once
2 #include <ym_core.h>
3 
4 typedef
5 enum
6 {
10  ym_alloc_strategy_region, // Make this buddy allocator in future, currently its fixed list
11 
12  // Add strategies for aligned allocations?
14 
15 // Used to send extra information about allocations.
16 typedef
17 struct
18 {
19  // Only if region
20  // Make into union if other members are added
21  int* slot_size; // Must be sorted Ascending
22  int* slot_count; // Number of blocks per slot
23  int count; // Size of both arrays
25 
26 #define YM_ALLOCATOR_SLOT_REGION_COUNT 4
27 
28 typedef
29 struct
30 {
31  // For debug, remove when done
32  int id;
33  // Eo for debug
34 
35 
36  void* mem;
40 
41  // These variables should be "private", i.e. not used in actual program
42  // But are needed publicly for testing, among other things.
43  union
44  {
45  struct
46  {
50  } free_list;
51  };
52 
53  // Think of how to add extra variables that might be needed?
54  // Maybe union?
55 } ym_allocator;
56 
57 
58 ym_errc
60  void* memory,
61  uint size,
62  ym_allocator_cfg* allocator_cfg, // Can be NULL, if N/A, or to use defaults
63  ym_allocator* out_allocator);
64 
65 ym_errc
67 
68 #define YM_MEMORY_TRACKING
69 
70 #ifdef YM_MEMORY_TRACKING
71 ym_errc
72 ym_allocate(ym_allocator* allocator, int size, void** ptr, char* file, int line);
73 #else
74 ym_errc
75 ym_allocate(ym_allocator* allocator, int size, void** ptr);
76 #endif
77 
78 #ifdef YM_MEMORY_TRACKING
79 ym_errc
80 ym_deallocate(ym_allocator* allocator, int size, void* ptr, char* file, int line);
81 #else
82 ym_errc
83 ym_deallocate(ym_allocator* allocator, int size, void* ptr);
84 #endif
85 
86 #ifdef YM_MEMORY_TRACKING
87 #define YM_ALLOCATE(allocator, size, ptr) \
88  ym_allocate(allocator, size, ptr, __FILE__, __LINE__);
89 #else
90 #define YM_ALLOCATE(allocator, size, ptr) \
91  ym_allocate(allocator, size, ptr);
92 #endif
93 
94 #ifdef YM_MEMORY_TRACKING
95 #define YM_DEALLOCATE(allocator, size, ptr) \
96  ym_deallocate(allocator, size, ptr, __FILE__, __LINE__);
97 #else
98 #define YM_DEALLOCATE(allocator, size, ptr) \
99  ym_deallocate(allocator, size, ptr);
100 #endif
void * mem
Definition: ym_allocator.h:36
u16 used
Definition: ym_allocator.h:38
u16 size
Definition: ym_allocator.h:37
int count
Definition: ym_allocator.h:23
ym_alloc_strategy strategy
Definition: ym_allocator.h:39
int * slot_count
Definition: ym_allocator.h:22
uint16_t u16
Definition: ym_types.h:9
int GLenum UINT size
Definition: wglext.h:321
Definition: ym_allocator.h:28
Definition: ym_allocator.h:16
ym_errc
Used to indicate erroneous behavior within a a function.
Definition: ym_error.h:36
Definition: ym_allocator.h:10
ym_errc ym_allocate(ym_allocator *allocator, int size, void **ptr, char *file, int line)
ym_alloc_strategy
Definition: ym_allocator.h:4
#define YM_ALLOCATOR_SLOT_REGION_COUNT
Definition: ym_allocator.h:26
Definition: ym_allocator.h:7
int id
Definition: ym_allocator.h:32
Definition: ym_allocator.h:9
ym_errc ym_destroy_allocator(ym_allocator *allocator)
Definition: ym_allocator.c:251
ym_errc ym_deallocate(ym_allocator *allocator, int size, void *ptr, char *file, int line)
Definition: ym_allocator.h:8
unsigned uint
Definition: ym_types.h:22
int * slot_size
Definition: ym_allocator.h:21
ym_errc ym_create_allocator(ym_alloc_strategy strategy, void *memory, uint size, ym_allocator_cfg *allocator_cfg, ym_allocator *out_allocator)
Definition: ym_allocator.c:232
uint8_t u8
Definition: ym_types.h:8