Mirai's Miscellaneous Misadventures

M21 / core / behaviors.c

1// copyright 2022 zamfofex
2// license: AGPLv3 or later
3
4#include <mimimi/behaviors.h>
5#include <mimimi/compound-behaviors.h>
6#include <mimimi/chapters.h>
7#include <mimimi/allocators.h>
8
9struct mimimi_compound_behavior
10{
11	struct mimimi_allocator *allocator;
12	struct mimimi_behavior *behavior;
13	int count;
14	struct mimimi_behavior **behaviors;
15};
16
17static void mimimi_compound_behave(void *data)
18{
19	struct mimimi_compound_behavior *compound = data;
20	for (int i = 0 ; i < compound->count ; i++)
21	{
22		struct mimimi_behavior *behavior = compound->behaviors[i];
23		(*behavior->behave)(behavior->data);
24	}
25}
26
27static void mimimi_compound_finish(void *data)
28{
29	struct mimimi_compound_behavior *compound = data;
30	for (int i = 0 ; i < compound->count ; i++)
31	{
32		struct mimimi_behavior *behavior = compound->behaviors[i];
33		(*behavior->finish)(behavior->data);
34	}
35	
36	(*compound->allocator->deallocate)(compound->behaviors);
37	(*compound->allocator->deallocate)(compound->behavior);
38	(*compound->allocator->deallocate)(compound);
39}
40
41struct mimimi_behavior *mimimi_compound_behavior(struct mimimi_allocator *allocator)
42{
43	struct mimimi_compound_behavior *data = (*allocator->allocate)(sizeof *data);
44	data->allocator = allocator;
45	
46	data->behavior = (*allocator->allocate)(sizeof *data->behavior);
47	data->behavior->behave = &mimimi_compound_behave;
48	data->behavior->finish = &mimimi_compound_behave;
49	data->behavior->data = data;
50	
51	data->count = 0;
52	data->behaviors = allocator->null;
53	
54	return data->behavior;
55}
56
57void mimimi_compound(struct mimimi_behavior *behavior, struct mimimi_behavior *other)
58{
59	struct mimimi_compound_behavior *compound = behavior->data;
60	
61	compound->count++;
62	compound->behaviors = (*compound->allocator->reallocate)(compound->behaviors, compound->count * sizeof *compound->behaviors);
63	compound->behaviors[compound->count - 1] = other;
64}
65
66void mimimi_ignore(void *data) { }
67
68static struct mimimi_behavior mimimi_noop_value = {&mimimi_ignore, &mimimi_ignore};
69struct mimimi_behavior *mimimi_noop = &mimimi_noop_value;
70
71struct mimimi_function
72{
73	struct mimimi_allocator *allocator;
74	struct mimimi_behavior *behavior;
75	struct mimimi_behavior *other;
76};
77
78static void mimimi_function_behave(void *data)
79{
80	struct mimimi_function *function = data;
81	(*function->other->behave)(function->other->data);
82}
83
84static void mimimi_function_finish(void *data)
85{
86	struct mimimi_function *function = data;
87	(*function->other->finish)(function->other->data);
88	(*function->allocator->deallocate)(function->other);
89	(*function->allocator->deallocate)(function->behavior);
90	(*function->allocator->deallocate)(function);
91}
92
93struct mimimi_behavior *mimimi_function(void *data, void (*behave)(void *data), struct mimimi_allocator *allocator)
94{
95	struct mimimi_function *function = (*allocator->allocate)(sizeof *function);
96	function->allocator = allocator;
97	
98	function->other = (*allocator->allocate)(sizeof *function->other);
99	function->other->behave = behave;
100	function->other->finish = &mimimi_ignore;
101	function->other->data = data;
102	
103	function->behavior = (*allocator->allocate)(sizeof *function->behavior);
104	function->behavior->behave = &mimimi_function_behave;
105	function->behavior->finish = &mimimi_function_finish;
106	function->behavior->data = function;
107	
108	return function->behavior;
109}
110
111struct mimimi_behavior *mimimi_finalizer(void *data, void (*finish)(void *data), struct mimimi_allocator *allocator)
112{
113	struct mimimi_function *function = (*allocator->allocate)(sizeof *function);
114	function->allocator = allocator;
115	
116	function->other = (*allocator->allocate)(sizeof *function->other);
117	function->other->behave = &mimimi_ignore;
118	function->other->finish = finish;
119	function->other->data = data;
120	
121	function->behavior = (*allocator->allocate)(sizeof *function->behavior);
122	function->behavior->behave = &mimimi_function_behave;
123	function->behavior->finish = &mimimi_function_finish;
124	function->behavior->data = function;
125	
126	return function->behavior;
127}
128
129struct mimimi_behavior *mimimi_allocator_behavior(void **data, unsigned int size, struct mimimi_allocator *allocator)
130{
131	*data = (*allocator->allocate)(size);
132	return mimimi_finalizer(*data, allocator->deallocate, allocator);
133}
134
135void *mimimi_compound_allocate(struct mimimi_behavior *compound, unsigned int size, struct mimimi_allocator *allocator)
136{
137	void *data;
138	mimimi_compound(compound, mimimi_allocator_behavior(&data, size, allocator));
139	return data;
140}