aboutsummaryrefslogtreecommitdiffstats
path: root/build/4coder_byp/custom/4coder_system_allocator.cpp
blob: 491a48b679e6ea6235576e8b613970aa77c1fa73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * 4coder malloc base allocator
 */

// TOP

internal void*
base_reserve__system(void *user_data, u64 size, u64 *size_out, String_Const_u8 location){
  u64 extra_size = 128;
  u64 increased_size = size + extra_size;
  size = round_up_u64(increased_size, KB(4));
  *size_out = size - extra_size;
  void *ptr = system_memory_allocate(size, location);
  *(u64*)ptr = size;
  ptr = (u8*)ptr + extra_size;
  return(ptr);
}

internal void
base_free__system(void *user_data, void *ptr){
  u64 extra_size = 128;
  ptr = (u8*)ptr - extra_size;
  u64 size = *(u64*)ptr;
  system_memory_free(ptr, size);
}

internal Base_Allocator
make_base_allocator_system(void){
  return(make_base_allocator(base_reserve__system, 0, 0, base_free__system, 0, 0));
}

global Base_Allocator base_allocator_system = {};

internal Base_Allocator*
get_base_allocator_system(void){
  if (base_allocator_system.reserve == 0){
    base_allocator_system = make_base_allocator_system();
  }
  return(&base_allocator_system);
}

internal Arena
make_arena_system(u64 chunk_size, u64 align){
  return(make_arena(get_base_allocator_system(), chunk_size, align));
}

internal Arena
make_arena_system(u64 chunk_size){
  return(make_arena_system(chunk_size, 8));
}

internal Arena
make_arena_system(void){
  return(make_arena_system(KB(16), 8));
}

// BOTTOM