-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathserialise.c
410 lines (338 loc) · 10.1 KB
/
serialise.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#ifdef __linux__
#define _GNU_SOURCE
#endif
#include "serialise.h"
#include "../sched/scheduler.h"
#include "../lang/lang.h"
#include "ponyassert.h"
#include <string.h>
#if defined(PLATFORM_IS_POSIX_BASED)
#include <dlfcn.h>
#else
#include <Windows.h>
#endif
#define HIGH_BIT ((size_t)1 << ((sizeof(size_t) * 8) - 1))
#define ALL_BITS ((size_t)(~0))
PONY_EXTERN_C_BEGIN
static size_t desc_table_size = 0;
static pony_type_t** desc_table = NULL;
PONY_EXTERN_C_END
typedef struct
{
pony_type_t* t;
size_t size;
size_t alloc;
char* ptr;
} ponyint_array_t;
struct serialise_t
{
uintptr_t key;
uintptr_t value;
pony_type_t* t;
int mutability;
bool block;
};
static size_t serialise_hash(serialise_t* p)
{
return ponyint_hash_size(p->key);
}
static bool serialise_cmp(serialise_t* a, serialise_t* b)
{
return a->key == b->key;
}
static void serialise_free(serialise_t* p)
{
POOL_FREE(serialise_t, p);
}
DEFINE_HASHMAP(ponyint_serialise, ponyint_serialise_t,
serialise_t, serialise_hash, serialise_cmp, serialise_free);
static void recurse(pony_ctx_t* ctx, void* p, void* f)
{
if(f != NULL)
{
ctx->stack = ponyint_gcstack_push(ctx->stack, p);
ctx->stack = ponyint_gcstack_push(ctx->stack, f);
}
}
static void serialise_cleanup(pony_ctx_t* ctx)
{
ponyint_gc_discardstack(ctx);
ctx->serialise_size = 0;
ponyint_serialise_destroy(&ctx->serialise);
}
static void custom_deserialise(pony_ctx_t* ctx)
{
size_t i = HASHMAP_BEGIN;
serialise_t* s;
while((s = ponyint_serialise_next(&ctx->serialise, &i)) != NULL)
{
if (s->t != NULL && s->t->custom_deserialise != NULL)
{
s->t->custom_deserialise((void *)(s->value),
(void*)((uintptr_t)ctx->serialise_buffer + s->key + s->t->size));
}
}
}
bool ponyint_serialise_setup()
{
#if defined(PLATFORM_IS_POSIX_BASED)
void* tbl_size_sym = dlsym(RTLD_DEFAULT, "__PonyDescTableSize");
void* tbl_ptr_sym = dlsym(RTLD_DEFAULT, "__PonyDescTablePtr");
#else
HMODULE program = GetModuleHandle(NULL);
if(program == NULL)
return false;
void* tbl_size_sym = (void*)GetProcAddress(program, "__PonyDescTableSize");
void* tbl_ptr_sym = (void*)GetProcAddress(program, "__PonyDescTablePtr");
#endif
if((tbl_size_sym == NULL) || (tbl_ptr_sym == NULL))
return false;
desc_table_size = *(size_t*)tbl_size_sym;
desc_table = *(pony_type_t***)tbl_ptr_sym;
return true;
}
void ponyint_serialise_object(pony_ctx_t* ctx, void* p, pony_type_t* t,
int mutability)
{
serialise_t k;
k.key = (uintptr_t)p;
size_t index = HASHMAP_UNKNOWN;
serialise_t* s = ponyint_serialise_get(&ctx->serialise, &k, &index);
if(s != NULL)
{
// If it was traced as opaque, and now is not opaque, change it and trace.
if((s->mutability != PONY_TRACE_OPAQUE) ||
(mutability == PONY_TRACE_OPAQUE))
return;
} else {
// Put an entry in the map and reserve space.
s = POOL_ALLOC(serialise_t);
s->key = (uintptr_t)p;
s->value = ctx->serialise_size;
s->t = t;
s->block = false;
// didn't find it in the map but index is where we can put the
// new one without another search
ponyint_serialise_putindex(&ctx->serialise, s, index);
ctx->serialise_size += t->size;
if(t->custom_serialise_space)
{
ctx->serialise_size += t->custom_serialise_space(p);
}
}
// Set (or update) mutability.
s->mutability = mutability;
if(mutability != PONY_TRACE_OPAQUE)
recurse(ctx, p, t->serialise_trace);
}
void ponyint_serialise_actor(pony_ctx_t* ctx, pony_actor_t* actor)
{
(void)ctx;
(void)actor;
serialise_cleanup(ctx);
ctx->serialise_throw();
abort();
}
PONY_API void pony_serialise_reserve(pony_ctx_t* ctx, void* p, size_t size)
{
// Reserve a block of memory to serialise into. This is only needed for
// String and Array[A].
serialise_t k;
k.key = (uintptr_t)p;
size_t index = HASHMAP_UNKNOWN;
serialise_t* s = ponyint_serialise_get(&ctx->serialise, &k, &index);
if(s != NULL)
return;
// Put an entry in the map and reserve space.
s = POOL_ALLOC(serialise_t);
s->key = (uintptr_t)p;
s->value = ctx->serialise_size;
s->t = NULL;
s->mutability = PONY_TRACE_OPAQUE;
s->block = true;
// didn't find it in the map but index is where we can put the
// new one without another search
ponyint_serialise_putindex(&ctx->serialise, s, index);
ctx->serialise_size += size;
}
PONY_API size_t pony_serialise_offset(pony_ctx_t* ctx, void* p)
{
if(p == NULL)
return ALL_BITS;
serialise_t k;
k.key = (uintptr_t)p;
size_t index = HASHMAP_UNKNOWN;
serialise_t* s = ponyint_serialise_get(&ctx->serialise, &k, &index);
// If we are in the map, return the offset.
if(s != NULL)
{
if(s->block || (s->t != NULL && s->t->serialise != NULL))
return s->value;
else
return ALL_BITS;
}
// If we are not in the map, we are an untraced primitive. Return the type id
// with the high bit set.
pony_type_t* t = *(pony_type_t**)p;
return (size_t)t->id | HIGH_BIT;
}
PONY_API void pony_serialise(pony_ctx_t* ctx, void* p, pony_type_t* t,
void* out, serialise_alloc_fn alloc_fn, serialise_throw_fn throw_fn)
{
// This can raise an error.
pony_assert(ctx->stack == NULL);
ctx->trace_object = ponyint_serialise_object;
ctx->trace_actor = ponyint_serialise_actor;
ctx->serialise_size = 0;
ctx->serialise_alloc = alloc_fn;
ctx->serialise_throw = throw_fn;
if(t != NULL)
pony_traceknown(ctx, p, t, PONY_TRACE_MUTABLE);
else
pony_traceunknown(ctx, p, PONY_TRACE_MUTABLE);
ponyint_gc_handlestack(ctx);
ponyint_array_t* r = (ponyint_array_t*)out;
r->size = ctx->serialise_size;
r->alloc = r->size;
r->ptr = (char*)alloc_fn(ctx, r->size);
size_t i = HASHMAP_BEGIN;
serialise_t* s;
while((s = ponyint_serialise_next(&ctx->serialise, &i)) != NULL)
{
if(!(s->block) && s->t != NULL && s->t->serialise != NULL)
s->t->serialise(ctx, (void*)s->key, r->ptr, s->value, s->mutability);
}
serialise_cleanup(ctx);
}
PONY_API void* pony_deserialise_offset(pony_ctx_t* ctx, pony_type_t* t,
uintptr_t offset)
{
// if all the bits of the offset are set, it is either a Pointer[A] a or a
// MaybePointer[A].
if(offset == ALL_BITS)
return NULL;
// If the high bit of the offset is set, it is either an unserialised
// primitive, or an unserialised field in an opaque object.
if((offset & HIGH_BIT) != 0)
{
offset &= ~HIGH_BIT;
if(offset > desc_table_size)
return NULL;
// Return the global instance, if there is one. It's ok to return null if
// there is no global instance, as this will then be an unserialised
// field in an opaque object.
t = desc_table[offset];
return t->instance;
}
// Lookup the offset, return the associated object if there is one.
serialise_t k;
k.key = offset;
size_t index = HASHMAP_UNKNOWN;
serialise_t* s = ponyint_serialise_get(&ctx->serialise, &k, &index);
if(s != NULL)
return (void*)s->value;
// If we haven't been passed a type descriptor, read one.
if(t == NULL)
{
// Make sure we have space to read a type id.
if((offset + sizeof(uintptr_t)) > ctx->serialise_size)
{
serialise_cleanup(ctx);
ctx->serialise_throw();
abort();
}
// Turn the type id into a descriptor pointer.
uintptr_t id = *(uintptr_t*)((uintptr_t)ctx->serialise_buffer + offset);
t = desc_table[id];
}
// If it's a primitive, return the global instance.
if(t->instance != NULL)
return t->instance;
// Make sure we have space to read the object.
if((offset + t->size) > ctx->serialise_size)
{
serialise_cleanup(ctx);
ctx->serialise_throw();
abort();
}
// Allocate the object, memcpy to it.
void* object;
if(t->final == NULL)
object = ctx->serialise_alloc(ctx, t->size);
else
object = ctx->serialise_alloc_final(ctx, t->size);
memcpy(object, (void*)((uintptr_t)ctx->serialise_buffer + offset), t->size);
// Store a mapping of offset to object.
s = POOL_ALLOC(serialise_t);
s->key = offset;
s->value = (uintptr_t)object;
s->t = t;
// didn't find it in the map but index is where we can put the
// new one without another search
ponyint_serialise_putindex(&ctx->serialise, s, index);
recurse(ctx, object, t->deserialise);
return object;
}
PONY_API void* pony_deserialise_block(pony_ctx_t* ctx, uintptr_t offset,
size_t size)
{
if(offset == ALL_BITS)
return NULL;
// Allocate the block, memcpy to it.
if((offset + size) > ctx->serialise_size)
{
serialise_cleanup(ctx);
ctx->serialise_throw();
abort();
}
void* block = ctx->serialise_alloc(ctx, size);
memcpy(block, (void*)((uintptr_t)ctx->serialise_buffer + offset), size);
return block;
}
PONY_API void* pony_deserialise_raw(pony_ctx_t* ctx, uintptr_t offset,
deserialise_raw_fn ds_fn)
{
if(offset == ALL_BITS)
return NULL;
// Lookup the offset, return the associated object if there is one.
serialise_t k;
k.key = offset;
size_t index = HASHMAP_UNKNOWN;
serialise_t* s = ponyint_serialise_get(&ctx->serialise, &k, &index);
if(s != NULL)
return (void*)s->value;
void* object = ds_fn((void*)((uintptr_t)ctx->serialise_buffer + offset),
ctx->serialise_size - offset);
if(object == NULL)
{
serialise_cleanup(ctx);
ctx->serialise_throw();
abort();
}
// Store a mapping of offset to object.
s = POOL_ALLOC(serialise_t);
s->key = offset;
s->value = (uintptr_t)object;
s->t = NULL;
// didn't find it in the map but index is where we can put the
// new one without another search
ponyint_serialise_putindex(&ctx->serialise, s, index);
return object;
}
PONY_API void* pony_deserialise(pony_ctx_t* ctx, pony_type_t* t, void* in,
serialise_alloc_fn alloc_fn, serialise_alloc_fn alloc_final_fn,
serialise_throw_fn throw_fn)
{
// This can raise an error.
ponyint_array_t* r = (ponyint_array_t*)in;
ctx->serialise_buffer = r->ptr;
ctx->serialise_size = r->size;
ctx->serialise_alloc = alloc_fn;
ctx->serialise_alloc_final = alloc_final_fn;
ctx->serialise_throw = throw_fn;
void* object = pony_deserialise_offset(ctx, t, 0);
ponyint_gc_handlestack(ctx);
custom_deserialise(ctx);
serialise_cleanup(ctx);
return object;
}