summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/memorystore.cpp
blob: 9be7ba52bb5bedb1f934d84eb04813f244ea9d94 (plain)
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
#include "memorystore.hpp"

#include <unordered_set>

using namespace std;

auto & concepts()
{
	static std::unordered_set<concept*> concepts;
	return concepts;
}

ref alloc(concept * moved) {
	ref r = moved ? moved : new concept();
	concepts().insert(r.ptr);
	return r;
}

concept* referenced(ref r) {
	for (ref r2 : concepts()) {
		if (r2 == r) {
			continue;
		}
		for (auto & l : r2->links) {
			if (ref(l.first) == r) {
				return r2.ptr;
			}
			if (ref(l.second) == r) {
				return r2.ptr;
			}
		}
	}
	return 0;
}

void dealloc(ref r) {
	concept * referenced = ::referenced(r);
	if (referenced) {
		throw std::logic_error("concept '" + r.name()->data + "' is referenced by '" + ref(referenced).name()->data + '"');
	}
	for (
		auto it = concepts().begin();
		it != concepts().end();
		++ it)
	{
		if (ref(*it) == r) {
			concepts().erase(it);
			delete r.ptr;
			return;
		}
	}
	throw std::logic_error("concept not held");
}