forked from GaliLeo03/FakeDos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmem.cpp
More file actions
137 lines (110 loc) · 3.51 KB
/
Copy pathmem.cpp
File metadata and controls
137 lines (110 loc) · 3.51 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <mem.h>
#include <json.h>
#include <iostream>
#include <sstream>
using namespace std;
string current_user;
int apcount;
int mem_capacity = 1024;
string jsonmem;
void init_mem(){
Json::StyledStreamWriter writer;
Json::Value root, user, taskid;
root["loginuser"] = user;
root["taskid"] = taskid;
jsonmem = root.toStyledString();
}
void user_mem_alloc(string & current_user){
Json::Reader reader;
Json::Value root, user, taskid;
if (reader.parse(jsonmem, root)){
if (!root["loginuser"].isMember(current_user)){
user[(char*)current_user.c_str()] = 64;
root["loginuser"].append(user);
}
else{
root["loginuser"][current_user] = 64; // 64KB represent OS memory usage
}
int index;
for (int i = 0; i < user.size(); i++){
if (root["loginuser"][i].isMember(current_user)){
index = i;
}
}
// define taskid for loginuser
string id = to_string(index);
Json::Value task;
task["ap1"] = id + "01";
task["ap2"] = id + "02";
task["ap3"] = id + "03";
root["taskid"] = task;
}
jsonmem = root.toStyledString();
}
template <typename ValueType>
void task_mem_alloc(int taskid, int memusage, ValueType Args){
Json::Reader reader;
Json::Value root, task, mem, var;
string id = to_string(taskid);
if (reader.parse(jsonmem, root)){
if (!root.isMember(id)){
root[to_string(id)] = task;
mem["memusage"] = memusage;
task.append(mem);
root["loginuser"][current_user] = root["loginuser"][current_user].asInt() + 16;
//////////////////////////////
// write variable into memory
//////////////////////////////
}
}
jsonmem = root.toStyledString();
}
void user_mem_free(string & current_user){
Json::Reader reader;
Json::Value root, user, taskid;
if (reader.parse(jsonmem, root)){
int index;
for (int i = 0; i < user.size(); i++){
if (root["loginuser"][i].isMember(current_user)){
index = i;
root["loginuser"][i][current_user] = 0;
}
}
string id = to_string(index);
for (int i = 1; i < apcount; i++)
if (root.isMember(id + to_string(i))){
root.removeMember(id + to_string(i));
}
}
jsonmem = root.toStyledString();
}
void task_mem_free(int taskid){
Json::Reader reader;
Json::Value root, task;
string id = to_string(taskid);
if (reader.parse(jsonmem, root)){
string mem = root[id][0]["memusage"].asString();
root[id][0]["memusage"] = 0;
int userid = stoi(id.substr(0, 1));
root["loginuser"][userid][current_user] = root["loginuser"][userid][current_user].asInt() - stoi(mem);
root.removeMember(id);
}
jsonmem = root.toStyledString();
}
bool limit_check(vector<string> user_name, int mem){
Json::Reader reader;
Json::Value root, task;
int usage = 0;
if (reader.parse(jsonmem, root)){
for (int i = 0; i<user_name.size(); i++){
usage += root["loginuser"][i][user_name[i]].asInt();
}
}
if ((usage + mem) >= mem_capacity){
return false;
}else return true;
}