summaryrefslogtreecommitdiff
path: root/src/lib/memory.c
blob: 24d3d68f61c6e355b5a65dd17ba83108b37d4431 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "aplwc_internal.h"

void
memory_init(struct aplwc *aplwc, uint8_t *data , size_t size)
{
	aplwc->data = data;
	aplwc->size = size;
	aplwc->offset = 0;
}

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

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

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

	if(adjust)
		alloc_size += align - adjust;

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

	return p;
}

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

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

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

struct aplwc_line *
memory_alloc_aplwc_line(struct aplwc *aplwc)
{
	struct aplwc_line *p;

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

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

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

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

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

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

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

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

struct aplwc_value *
memory_alloc_aplwc_value(struct aplwc *aplwc)
{
	struct aplwc_value *p;

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

void
memory_free_aplwc_value(struct aplwc *aplwc, struct aplwc_value *p)
{
	UNUSED_PARAMETER(aplwc);
	UNUSED_PARAMETER(p);
}

struct aplwc_module *
memory_alloc_aplwc_module(struct aplwc *aplwc)
{
	struct aplwc_module *p;

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

void
memory_free_aplwc_module(struct aplwc *aplwc, struct aplwc_module *p)
{
	UNUSED_PARAMETER(aplwc);
	UNUSED_PARAMETER(p);
}