summaryrefslogtreecommitdiff
path: root/src/lib/memory.c
blob: 41ac90da2bdfd9917236b2dfa33772ad7a440be3 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "aplwc_internal.h"

struct memory
memory_init(uint8_t *data , size_t size)
{
	struct memory mem;

	mem.data = data;
	mem.size = size;
	mem.offset = 0;
	return mem;
}

static void *
alloc(struct memory *m, size_t size, size_t align)
{
	uintptr_t ptr;
	size_t alloc_size, adjust;
	void *p;

	ptr = ((uintptr_t)m->data) + m->offset;
	alloc_size = size;

	if(NEVER(align == 0))
		return NULL;
	adjust = ptr % align;

	if(adjust)
		alloc_size += align - adjust;

	if((m->size - m->offset) >= alloc_size){
		p = m->data+m->offset+alloc_size-size;
		assert((((uintptr_t)p) % align) == 0);
		m->offset += alloc_size;
		memset(p, 0, size);
	}else
		p = NULL;

	return p;
}

struct aplwc *
memory_alloc_aplwc(struct memory *m)
{
	struct aplwc *p;

	p = alloc(m, sizeof(struct aplwc), alignof(struct aplwc));
	return p;
}

void
memory_free_aplwc(struct memory *m, struct aplwc *p)
{
	UNUSED_PARAMETER(m);
	UNUSED_PARAMETER(p);
}

struct aplwc_line *
memory_alloc_aplwc_line(struct memory *m)
{
	struct aplwc_line *p;

	p = alloc(m, sizeof(struct aplwc_line), alignof(struct aplwc_line));
	return p;
}

void
memory_free_aplwc_line(struct memory *m, struct aplwc_line *p)
{
	UNUSED_PARAMETER(m);
	UNUSED_PARAMETER(p);
}

struct string *
memory_alloc_string(struct memory *m, size_t bytes)
{
	struct string *p;

	p = alloc(m, sizeof(struct string) + bytes, alignof(struct string));
	return p;
}

void
memory_free_string(struct memory *m, struct string *p)
{
	UNUSED_PARAMETER(m);
	UNUSED_PARAMETER(p);
}

struct token_list *
memory_alloc_token_list(struct memory *m, size_t tokens)
{
	struct token_list *p;

	p = alloc(m, sizeof(struct token_list) + (tokens * sizeof(struct token)), alignof(struct token_list));
	return p;
}

void
memory_free_token_list(struct memory *m, struct token_list *p)
{
	UNUSED_PARAMETER(m);
	UNUSED_PARAMETER(p);
}