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

#include <list>
#include <vector>

using namespace std;

std::vector<concept*> concepts;

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

bool referenced(ref r) {
	for (ref r2 : concepts) {
		if (r2 == r) {
			continue;
		}
		for (auto & l : r2->links) {
			if (ref(l.first) == r) {
				return true;
			}
			if (ref(l.second) == r) {
				return true;
			}
		}
	}
	return false;
}

void dealloc(ref r) {
	if (referenced(r)) {
		throw std::logic_error("concept is referenced");
	}
	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");
}