-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.h
56 lines (44 loc) · 1.15 KB
/
memory.h
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
//
// Created by fathi on 10/21/2024.
//
#ifndef TIGE_MEMORY_H
#define TIGE_MEMORY_H
#include <stdlib.h>
#include <memory.h>
typedef struct Context Context;
typedef struct Heap Heap;
typedef struct HeapBlock HeapBlock;
typedef struct Stack Stack;
typedef struct TObject TObject;
struct HeapBlock {
TObject *object;
size_t size;
HeapBlock *next;
};
struct Heap {
HeapBlock *blocks;
size_t total_allocated; // total bytes allocated
size_t total_freed; // total bytes freed
};
struct Stack {
Value *values;
int capacity;
int sp; // stack pointer (-1 indicates empty stack)
};
/// stack functions
Stack *create_stack(int initial_capacity);
void destroy_stack(Stack *stack);
bool push_stack(Stack *stack, Value value);
bool pop_stack(Stack *stack, Value *value);
bool peek_stack(Stack *stack, Value *value);
// heap functions
Heap *create_heap();
void destroy_heap(Heap *heap);
uint8_t *heap_alloc(size_t size);
bool heap_free(uint8_t *ptr);
// ..
void *vm_malloc(size_t size);
void vm_free(void *ptr);
void* tige_alloc(Context* ctx, size_t size);
void tige_free(Context* ctx, void* ptr);
#endif //TIGE_MEMORY_H