kiba-engine
linear.c
Go to the documentation of this file.
1 
7 
8 #include <kiba/core/log.h>
9 #include <kiba/core/memory.h>
10 
21 typedef struct linear_allocator_block {
23  usize size;
25  usize cur_offset;
29  void *data;
31 
43 
45  linear_allocator_block *block = memory_page_allocate(sizeof(linear_allocator_block) + size, &size);
46  if (block == KB_NULL) {
47  KB_ERROR("could not allocate {usize} bytes for the linear_allocator_block", size);
48  return KB_NULL;
49  }
50  block->size = size;
51  block->cur_offset = 0;
52  block->next = next;
53  block->data = block + 1;
54  KB_TRACE("created linear_allocator_block with size {usize}", size);
55  return block;
56 }
57 
58 /* Linear allocator */
59 
62  if (block == KB_NULL) {
63  KB_ERROR("could not allocate memory for initial linear_allocator_block");
64  return false;
65  }
71  alloc->state = block;
72  KB_TRACE("created linear_allocator with initial size {usize}", size);
73  return true;
74 }
75 
77  linear_allocator_block *block = alloc->state;
78  linear_allocator_block *prev_block = KB_NULL;
79  while (block != KB_NULL) {
80  prev_block = block;
81  block = block->next;
82  KB_TRACE("destroying linear_allocator_block with size {usize}", prev_block->size);
83  memory_page_free(prev_block);
84  }
85  KB_TRACE("destroyed linear_allocator");
86 }
87 
89  linear_allocator_block *block = alloc->state;
90  linear_allocator_block *fitting_block = KB_NULL;
91  linear_allocator_block *prev_block = KB_NULL;
92  while (block != KB_NULL) {
93  if (size <= block->size - block->cur_offset) {
94  fitting_block = block;
95  break;
96  } else {
97  prev_block = block;
98  block = block->next;
99  }
100  }
101  if (fitting_block == KB_NULL) {
102  KB_TRACE("did not find fitting block, creating new one");
103  usize new_size = prev_block->size;
104  if (size > new_size) {
105  KB_DEBUG(
106  "new block size {usize} is not enough for requested allocation of {usize} bytes, using the requested "
107  "size as new block size",
108  new_size,
109  size);
110  new_size = size;
111  }
112  fitting_block = linear_allocator_block_create(new_size, alloc->state);
113  if (fitting_block == KB_NULL) {
114  KB_ERROR("could not allocate memory for linear_allocator_block");
115  return KB_NULL;
116  }
117  alloc->state = fitting_block; // fresh block is the first in the list
118  }
119  void *ret = (u8 *) fitting_block->data + fitting_block->cur_offset;
120  fitting_block->cur_offset += size;
121  return ret;
122 }
123 
124 void linear_allocator_free(allocator *alloc, void *mem, usize size) {
125  UNUSED(size);
126  UNUSED(alloc);
127  UNUSED(mem);
128  KB_INFO("linear_allocator is not able to free individual allocations");
129 }
130 
132  linear_allocator_block *block = alloc->state;
133  while (block) {
134  block->cur_offset = 0;
135  block = block->next;
136  }
137  KB_TRACE("reset linear_allocator");
138 }
void allocator_implementation(allocator *alloc, allocator_impl_destroy_fn destroy, allocator_impl_allocate_fn allocate, allocator_impl_free_fn free, allocator_impl_free_all_fn free_all)
Set allocator implementation.
Definition: allocator.c:149
Lightweight layer between platform and other engine components to enable tracing/monitoring.
#define UNUSED(x)
Mark parameter as unused.
Definition: defines.h:21
#define KB_NULL
Value of an invalid ptr (nullptr).
Definition: defines.h:18
void linear_allocator_free(allocator *alloc, void *mem, usize size)
Definition: linear.c:124
void linear_allocator_destroy(allocator *alloc)
Definition: linear.c:76
void * linear_allocator_allocate(allocator *alloc, usize size)
Definition: linear.c:88
b8 linear_allocator_create(allocator *alloc, usize size)
Definition: linear.c:60
void linear_allocator_free_all(allocator *alloc)
Definition: linear.c:131
struct linear_allocator_block linear_allocator_block
Internal state of the linear_allocator.
linear_allocator_block * linear_allocator_block_create(usize size, linear_allocator_block *next)
Create a new linear_allocator_block.
Definition: linear.c:44
Function signatures for linear allocator implementation.
Logging system.
#define KB_DEBUG(...)
Log entry with debug log level.
Definition: log.h:163
#define KB_ERROR(...)
Log entry with error log level.
Definition: log.h:142
#define KB_TRACE(...)
Log entry with trace log level.
Definition: log.h:164
#define KB_INFO(...)
Log entry with info log level.
Definition: log.h:162
Central allocator structure.
Definition: allocator.h:87
void * state
Type specific state of the allocator.
Definition: allocator.h:91
Internal state of the linear_allocator.
Definition: linear.c:21
usize cur_offset
Current offset into the block for the next allocation.
Definition: linear.c:25
struct linear_allocator_block * next
Pointer to the next block in the linked list.
Definition: linear.c:27
usize size
Size of the managable space.
Definition: linear.c:23
void * data
Pointer to beginning of managed memory block.
Definition: linear.c:29