-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.c
279 lines (239 loc) · 6.19 KB
/
counter.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <direct.h>
#include <io.h>
#else
#include <unistd.h>
#endif
#include <stdint.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <tempora/all.h>
#include <spirits/all.h>
#include <kaji/all.h>
#include "peom.h"
static uint8_t keep_running = 1;
void
handle_signal(int signal_type) {
printf("Got signal: %i\n", signal_type);
keep_running = 0;
}
int
msleep(unsigned int tms) {
#if defined(_WIN32)
Sleep(tms);
return 0;
#else
return usleep(tms * 1000);
#endif
}
//
struct mapped_data {
int32_t position[3];
uint8_t flag;
uint16_t timing;
char name[128];
};
typedef
struct mapped_data
mapped_data_t
;
static mapped_data_t default_mapped_data = {0};
void
printf_mapped_data(const mapped_data_t* memory);
uint8_t
treat_memory(int mode, kaji_t* kaji);
int main(int argc, char** argv) {
printf("Setting up signal handler ...\n");
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
char tmppath[TEMPORA_PATH_SIZE];
if (TEMPORA_ERROR == tempora_read(tmppath, TEMPORA_PATH_SIZE)) {
printf("Could not fetch directory for temporary files :/\n");
return -1;
}
strcat(tmppath, "/hope.ipc");
printf("Setup mapping file at: %s\n", tmppath);
struct stat fs;
printf("Checking if file already exits ...\n");
if (0 == stat(tmppath, &fs)) {
printf("Found file.\n");
printf("Size: %lld bytes\n", fs.st_size);
}
else {
printf("No temp file found. A new one will be created.\n");
}
kaji_log_activate();
kaji_t* kaji = kaji_materialize();
uint64_t bind_size = 13*1024*1024; // 1MB
fprintf(stderr, "Binding ...\n");
while (0 != kaji_bind(kaji, tmppath, bind_size)) {
fprintf(stderr, "Error binding :/ (errno: %i, %s)\n"
, errno, strerror(errno));
if (ENOMEM == errno) {
fprintf(stderr, "Resizing / filling %llu bytes ...\n", bind_size);
if (0 != kaji_file_expand(tmppath, bind_size)) {
fprintf(stderr, "Error expanding :/ (errno: %i, %s)\n"
, errno, strerror(errno));
return errno;
}
}
else if (ENOENT == errno) {
KAJI_LOG("Creating new temp file ...\n");
kaji_file_create(tmppath, bind_size);
}
else {
return 13;
}
fprintf(stderr, "Retrying binding ...\n");
}
if (1 < argc) {
int mode = atoi(argv[1]);
treat_memory(mode, kaji);
}
else {
printf("No mode provided :/\n");
}
kaji_print_spirits(kaji);
mapped_data_t* m = kaji_allocate(kaji, 3*sizeof(mapped_data_t));
for (uint8_t i = 0; i < 3; ++i) {
strcpy(m[i].name, "Hanswurst ißt gerne Senf!");
m[i].flag = i;
m[i].position[0] = 0x11*i;
m[i].position[1] = 0x22*i;
m[i].position[2] = 0x33*i;
m[i].timing = 0;
}
printf("Before allocation:\n");
kaji_print_spirits(kaji);
char* keks = kaji_allocate(kaji, sizeof(char) * 900);
if (NULL == keks) {
printf("ARRGH\n");
return 42;
}
strcpy(keks, kaji_examples_peom);
printf("After allocation:\n");
kaji_print_spirits(kaji);
printf("Freeing array ...\n");
kaji_free(kaji, m);
printf("After freeing array:\n");
kaji_print_spirits(kaji);
printf("Freeing poem ...\n");
kaji_free(kaji, m);
printf("Releasing ...\n");
if (0 != kaji_release(kaji)) {
fprintf(stderr, "Error releasing :/ (errno: %i, %s)\n"
, errno, strerror(errno));
}
printf("Dematerializing ...\n");
kaji_dematerialize(kaji);
return 0;
}
uint8_t
treat_memory(int mode, kaji_t* kaji) {
kaji_fragment_t one = {
.offset = 0,
.size = sizeof(mapped_data_t)
};
kaji_fragment_marshall(kaji, &one);
mapped_data_t* memory = (mapped_data_t*)one.data;
if (NULL == memory) {
printf("arrgh :/\n");
return 1;
}
kaji_fragment_t two = {
.offset = 3*sizeof(mapped_data_t),
.size = sizeof(mapped_data_t)
};
if (NULL == kaji_fragment_marshall(kaji, &two)) {
fprintf(stderr, "Could not marhal second fragment :/\n");
return -1;
}
mapped_data_t* other_memory = (mapped_data_t*)two.data;
if (NULL == other_memory) {
printf("arrgh other :/\n");
return 1;
}
if (0 == mode) {
printf("Reading stuff to shared memory ...\n");
printf_mapped_data(memory);
printf("Reading another bunch of shared memory ...\n");
printf_mapped_data(other_memory);
while (keep_running) {
printf("\rTiming:\t\t%u, Other timing:\t%u"
, memory->timing, other_memory->timing);
fflush(stdout);
msleep(11);
}
}
else if (1 == mode) {
printf("Writing stuff to shared memory ...\n");
uint64_t k = ~0;
uint32_t* kp = kaji_spell(kaji, sizeof(mapped_data_t)*2, (uint8_t*)&k, sizeof k);
*kp = 0xdeadf155;
printf("kp: %08x\n", *kp);
printf("kp: %08x\n", *(++kp));
strcpy(other_memory->name, "кохання це відповідь! љубав је одговор! Láska je odpovědí! A szerelem a válasz!");
other_memory->flag = 3;
other_memory->position[0] = 0x33;
other_memory->position[1] = 0x23;
other_memory->position[2] = 0x77;
strcpy(memory->name, "Hanswurst ißt gerne Senf!");
memory->flag = 7;
memory->position[0] = 1;
memory->position[1] = 13;
memory->position[2] = 23;
memory->timing = 1;
while (keep_running) {
msleep(333);
++(memory->timing);
#if 0
// Since we have access to the fragment, we instruct kaji to only
// sync the pages holding the fragment's data.
if (0 != kaji_fragment_sync(kaji, &one, 1)) {
printf("Error syncing ONE: errno(%i): %s\n"
, errno, strerror(errno));
}
#endif
other_memory->timing = memory->timing + 10;
#if 0
if (0 != kaji_fragment_sync(kaji, &two, 1)) {
printf("Error syncing TWO: errno(%i): %s\n"
, errno, strerror(errno));
}
#endif
#if 0
//Alternativly sync the whole file.
if (0 != kaji_sync(kaji, 0)) {
printf("Error syncing file: errno(%i): %s\n"
, errno, strerror(errno));
}
#endif
printf("\rTiming:\t%u, Other timing:\t%u"
, memory->timing, other_memory->timing);
fflush(stdout);
}
}
return 0;
}
void
printf_mapped_data(const mapped_data_t* memory) {
printf("Got data named: %s, flag: %u\n", memory->name, memory->flag);
printf("Position: {"); char k = 'x';
for (uint8_t i = 0; i < 3; ++i) {
printf("%c:%i%s"
, k+i
, memory->position[i]
, (i == 2 ? "" : ", ")
);
}
printf("}\n");
printf("Timing: %u\n", memory->timing);
}