summaryrefslogtreecommitdiff
path: root/src/common.cc
blob: aa734b36466368233756a9d3967e2199a6fb6b3f (plain)
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
#ifndef COMMON_CC
#define COMMON_CC

#include <cstdint>

/* typedefs for common types */

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;

typedef uintptr_t usize;
typedef intptr_t isize;

/* slice and string handling */

template<typename T>
struct Slice {
  T* ptr;
  usize length;

  Slice(T* ptr, usize length) : ptr(ptr), length(length) {}

  T* operator[](usize index) {
    return ptr + index;
  }
};

typedef Slice<u8> String;

/* allocator handling */

typedef u8* (*Allocator_Allocate)(u8* self, usize length, usize align);
typedef void (*Allocator_Deallocate)(u8* self, u8* ptr);

struct Allocator {
  u8* self;
  Allocator_Allocate allocate;
  Allocator_Deallocate deallocate;
};

u8* allocate(Allocator* allocator, usize size, usize align) {
  return allocator->allocate(allocator->self, size, align);
}

void deallocate(Allocator* allocator, u8* ptr) {
  allocator->deallocate(allocator->self, ptr);
}

#endif