-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-list.c
More file actions
69 lines (57 loc) · 1.3 KB
/
Copy pathstring-list.c
File metadata and controls
69 lines (57 loc) · 1.3 KB
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
#include "string-list.h"
#include "config.h"
#include "libks/arena.h"
#include "libks/list.h"
struct string_list_impl {
struct {
struct arena_scope *eternal_scope;
} arena;
};
union string_list_addr {
struct string_list_impl *impl;
struct string_list *list;
};
static struct string_list_impl *
list_to_impl(struct string_list *list)
{
union string_list_addr u = {.list = list};
u.impl--;
return u.impl;
}
static struct string_list *
impl_to_list(struct string_list_impl *impl)
{
union string_list_addr u = {.impl = impl};
u.impl++;
return u.list;
}
struct string_list *
strings_alloc(struct arena_scope *eternal_scope)
{
struct string_list_impl *impl;
struct string_list *strings;
impl = arena_malloc(eternal_scope, sizeof(*impl) + sizeof(*strings));
impl->arena.eternal_scope = eternal_scope;
strings = impl_to_list(impl);
LIST_INIT(strings);
return strings;
}
size_t
strings_len(const struct string_list *strings)
{
const struct string *str;
size_t len = 0;
LIST_FOREACH(str, strings)
len++;
return len;
}
struct string *
strings_append(struct string_list *strings, const char *val)
{
struct string_list_impl *impl = list_to_impl(strings);
struct string *str;
str = arena_malloc(impl->arena.eternal_scope, sizeof(*str));
str->val = val;
LIST_INSERT_TAIL(strings, str);
return str;
}