summaryrefslogtreecommitdiff
path: root/Source/Kernel/SyscallManager/Res.ns.cpp
blob: 1779a0088418ab688b23d0bf7de743083a9cb858 (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
#include "Res.ns.h"

#include <VirtualTerminal.iface.h>
#include <Process.iface.h>
#include <Thread.iface.h>
#include <TaskManager/Task.ns.h>

namespace Res {

Ressource** ressources = 0;
u32int size = 0;

void expand() {	//Expands size of ressources array of 20 entries
	size += 20;
	Ressource** tmp = (Ressource**)Mem::alloc(size * sizeof(Ressource*));
	for (u32int i = 0; i < size; i++) {
		if (i < size - 20) tmp[i] = ressources[i];
		else tmp[i] = 0;
	}
	Mem::free(ressources);
	ressources = tmp;
}

u32int registerRes(Ressource* r) {
	if (ressources == 0 or size == 0) {
		ressources = (Ressource**)Mem::alloc(20 * sizeof(Ressource*)); 
		size = 20;
		for (u32int i = 0; i < 20; i++) ressources[i] = 0;
	}
	for (u32int i = 0; i < size; i++) {
		if (ressources[i] == 0) {
			ressources[i] = r;
			return i;
		}
	}
	expand();
	return registerRes(r);
}

void unregisterRes(u32int id) {
	ressources[id] = 0;
}

u32int call(u32int ressource, u8int wat, u32int a, u32int b, u32int c, u32int d, u32int e) {
	if (ressource == 0xFFFFFE) {	//TODO : return ressource id for some stuff for current process
		if (a == VT_IFACE_OBJTYPE) return Task::currProcess()->getVirtualTerminal()->resId();
		if (a == PR_IFACE_OBJTYPE) return Task::currProcess()->resId();
		if (a == TH_IFACE_OBJTYPE) return Task::currThread()->resId();
		return 0;
	} else {
		if (ressource > size or ressources[ressource] == 0) {
			return (u32int) - 1;
		} else {
			return ressources[ressource]->doCall(wat, a, b, c, d, e);
		}
	}
}

}