summaryrefslogtreecommitdiff
path: root/Source/Kernel/SyscallManager/Res.ns.cpp
blob: 048d17abcbdcda32b9f27d265c3d3cfe92cc4331 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "Res.ns.h"

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

namespace Res {

typedef u32int (*staticcall)(u8int, u32int, u32int, u32int, u32int);

struct static_call_t {
	u32int id;
	staticcall call;
};

static_call_t staticCalls[] = {
	{VTIF_OBJTYPE, VirtualTerminal::scall},
	{PRIF_OBJTYPE, Process::scall},
	{THIF_OBJTYPE, Thread::scall},
	{FNIF_OBJTYPE, FSNode::scall},
	{0, 0}
};

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
		for (u32int i = 0; staticCalls[i].id != 0; i++) {
			if (staticCalls[i].id == a) return staticCalls[i].call(wat, b, c, d, e);
		}
		return 0;
	} else {
		if (ressource > size or ressources[ressource] == 0) {
			return (u32int) - 1;
		} else {
			return ressources[ressource]->doCall(wat, a, b, c, d, e);
		}
	}
}

}