| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* serialize.d - serialize a bunch of lisp data |
| 6 |
* |
| 7 |
* Copyright (c) 2010 Juan Jose Garcia Ripoll |
| 8 |
* |
| 9 |
* See file 'LICENSE' for the copyright details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <ecl/ecl.h> |
| 14 |
#include <string.h> |
| 15 |
#define ECL_DEFINE_AET_SIZE |
| 16 |
#include <ecl/internal.h> |
| 17 |
|
| 18 |
#ifdef ECL_EXTERNALIZABLE |
| 19 |
|
| 20 |
struct fake_package { |
| 21 |
_ECL_HDR; |
| 22 |
cl_object name; |
| 23 |
}; |
| 24 |
|
| 25 |
struct fake_symbol { |
| 26 |
_ECL_HDR; |
| 27 |
cl_object name; |
| 28 |
cl_object pack; |
| 29 |
}; |
| 30 |
|
| 31 |
#define ROUND_TO_WORD(int) \ |
| 32 |
((int + sizeof(cl_fixnum) - 1) & ~(sizeof(cl_fixnum) - 1)) |
| 33 |
#define ROUNDED_SIZE(name) \ |
| 34 |
ROUND_TO_WORD(sizeof(struct name)) |
| 35 |
|
| 36 |
static cl_index object_size[] = { |
| 37 |
0, /* t_start */ |
| 38 |
ROUNDED_SIZE(ecl_cons), /* t_list */ |
| 39 |
0, /* t_character = 2 */ |
| 40 |
0, /* t_fixnum = 3 */ |
| 41 |
ROUNDED_SIZE(ecl_bignum), /* t_bignum = 4 */ |
| 42 |
ROUNDED_SIZE(ecl_ratio), /* t_ratio */ |
| 43 |
ROUNDED_SIZE(ecl_singlefloat), /* t_singlefloat */ |
| 44 |
ROUNDED_SIZE(ecl_doublefloat), /* t_doublefloat */ |
| 45 |
ROUNDED_SIZE(ecl_long_float), /* t_longfloat */ |
| 46 |
ROUNDED_SIZE(ecl_complex), /* t_complex */ |
| 47 |
ROUNDED_SIZE(fake_symbol), /* t_symbol */ |
| 48 |
ROUNDED_SIZE(fake_package), /* t_package */ |
| 49 |
ROUNDED_SIZE(ecl_hashtable), /* t_hashtable */ |
| 50 |
ROUNDED_SIZE(ecl_array), /* t_array */ |
| 51 |
ROUNDED_SIZE(ecl_vector), /* t_vector */ |
| 52 |
#ifdef ECL_UNICODE |
| 53 |
ROUNDED_SIZE(ecl_string), /* t_string */ |
| 54 |
#endif |
| 55 |
ROUNDED_SIZE(ecl_base_string), /* t_base_string */ |
| 56 |
ROUNDED_SIZE(ecl_vector), /* t_bitvector */ |
| 57 |
ROUNDED_SIZE(ecl_stream), /* t_stream */ |
| 58 |
ROUNDED_SIZE(ecl_random), /* t_random */ |
| 59 |
ROUNDED_SIZE(ecl_readtable), /* t_readtable */ |
| 60 |
ROUNDED_SIZE(ecl_pathname), /* t_pathname */ |
| 61 |
ROUNDED_SIZE(ecl_bytecodes), /* t_bytecodes */ |
| 62 |
ROUNDED_SIZE(ecl_bclosure), /* t_bclosure */ |
| 63 |
ROUNDED_SIZE(ecl_cfun), /* t_cfun */ |
| 64 |
ROUNDED_SIZE(ecl_cfunfixed), /* t_cfunfixed */ |
| 65 |
ROUNDED_SIZE(ecl_cclosure), /* t_cclosure */ |
| 66 |
ROUNDED_SIZE(ecl_instance), /* t_instance */ |
| 67 |
#ifdef ECL_THREADS |
| 68 |
ROUNDED_SIZE(ecl_process), /* t_process */ |
| 69 |
ROUNDED_SIZE(ecl_lock), /* t_lock */ |
| 70 |
ROUNDED_SIZE(ecl_rwlock), /* t_rwlock */ |
| 71 |
ROUNDED_SIZE(ecl_condition_variable), /* t_condition_variable */ |
| 72 |
ROUNDED_SIZE(ecl_semaphore), /* t_semaphore */ |
| 73 |
ROUNDED_SIZE(ecl_barrier), /* t_barrier */ |
| 74 |
ROUNDED_SIZE(ecl_mailbox), /* t_mailbox */ |
| 75 |
#endif |
| 76 |
ROUNDED_SIZE(ecl_codeblock), /* t_codeblock */ |
| 77 |
ROUNDED_SIZE(ecl_foreign), /* t_foreign */ |
| 78 |
ROUNDED_SIZE(ecl_stack_frame), /* t_frame */ |
| 79 |
ROUNDED_SIZE(ecl_weak_pointer) /* t_weak_pointer */ |
| 80 |
#ifdef ECL_SSE2 |
| 81 |
, ROUNDED_SIZE(ecl_sse_pack) /* t_sse_pack */ |
| 82 |
#endif |
| 83 |
}; |
| 84 |
|
| 85 |
typedef struct pool { |
| 86 |
cl_object data; /* vector of bytes containing the serialized objects */ |
| 87 |
cl_object hash; /* hashtable mapping already serialized objects to indices in data */ |
| 88 |
cl_object queue; /* queue of objects to be serialized */ |
| 89 |
cl_object last; /* last cons cell of queue */ |
| 90 |
} *pool_t; |
| 91 |
|
| 92 |
static cl_index |
| 93 |
alloc(pool_t pool, cl_index size) |
| 94 |
{ |
| 95 |
cl_index bytes = ROUND_TO_WORD(size); |
| 96 |
cl_index fillp = pool->data->vector.fillp; |
| 97 |
cl_index next_fillp = fillp + bytes; |
| 98 |
if (next_fillp >= pool->data->vector.dim) { |
| 99 |
cl_index new_dim = next_fillp + next_fillp / 2; |
| 100 |
pool->data = si_adjust_vector(pool->data, ecl_make_fixnum(new_dim)); |
| 101 |
} |
| 102 |
pool->data->vector.fillp = next_fillp; |
| 103 |
return fillp; |
| 104 |
} |
| 105 |
|
| 106 |
/* Set the tag bits of an index into the array of serialized objects |
| 107 |
to zero to make it distinguishable from an ordinary fixnum */ |
| 108 |
static cl_object |
| 109 |
fix_to_ptr(cl_object ptr) |
| 110 |
{ |
| 111 |
cl_fixnum i = (cl_fixnum)ptr; |
| 112 |
return (cl_object)(i & ~ECL_IMMEDIATE_TAG); |
| 113 |
} |
| 114 |
|
| 115 |
static cl_object |
| 116 |
enqueue(pool_t pool, cl_object what) |
| 117 |
{ |
| 118 |
cl_object index; |
| 119 |
if (ECL_FIXNUMP(what) || ECL_CHARACTERP(what) || what == OBJNULL) { |
| 120 |
return what; |
| 121 |
} |
| 122 |
if (Null(what)) |
| 123 |
return what; |
| 124 |
index = ecl_gethash_safe(what, pool->hash, OBJNULL); |
| 125 |
if (index == OBJNULL) { |
| 126 |
cl_object cons; |
| 127 |
index = ecl_make_fixnum(pool->hash->hash.entries); |
| 128 |
ecl_sethash(what, pool->hash, index); |
| 129 |
cons = ecl_cons(what, ECL_NIL); |
| 130 |
ECL_RPLACD(pool->last, cons); |
| 131 |
pool->last = cons; |
| 132 |
} |
| 133 |
return fix_to_ptr(index); |
| 134 |
} |
| 135 |
|
| 136 |
typedef struct { |
| 137 |
_ECL_HDR; |
| 138 |
cl_object car, cdr; |
| 139 |
} large_cons; |
| 140 |
typedef large_cons *large_cons_ptr; |
| 141 |
|
| 142 |
static cl_index |
| 143 |
serialize_bits(pool_t pool, void *data, cl_index size) |
| 144 |
{ |
| 145 |
cl_index index = alloc(pool, size); |
| 146 |
memcpy(pool->data->vector.self.b8 + index, data, size); |
| 147 |
return index; |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
serialize_object_ptr(pool_t pool, cl_object *ptr, cl_index dim) |
| 152 |
{ |
| 153 |
cl_index index = serialize_bits(pool, ptr, dim*sizeof(cl_object)); |
| 154 |
for (; dim; dim--, index += sizeof(cl_object)) { |
| 155 |
cl_object *p = (cl_object *)(pool->data->vector.self.b8 + index); |
| 156 |
*p = enqueue(pool, *p); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
static void serialize_vector(pool_t pool, cl_object v); |
| 161 |
|
| 162 |
static void |
| 163 |
serialize_displaced_vector(pool_t pool, cl_object v) |
| 164 |
{ |
| 165 |
cl_object disp = v->vector.displaced; |
| 166 |
cl_object to = ECL_CONS_CAR(disp); |
| 167 |
if (Null(to)) { |
| 168 |
v->vector.displaced = ECL_NIL; |
| 169 |
serialize_vector(pool, v); |
| 170 |
} else { |
| 171 |
/* TODO: Implement serialization of displaced bit vectors */ |
| 172 |
if (v->vector.elttype == ecl_aet_bit) { |
| 173 |
FEerror("ECL can not yet serialize displaced bitvectors", 0); |
| 174 |
} |
| 175 |
cl_index index = (v->vector.self.b8 - to->vector.self.b8) / ecl_aet_size[v->vector.elttype]; |
| 176 |
v->vector.displaced = enqueue(pool, to); |
| 177 |
v->vector.self.b8 = (uint8_t*)index; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
static size_t |
| 182 |
vector_self_size(cl_object v) { |
| 183 |
if (v->vector.elttype == ecl_aet_bit) { |
| 184 |
return ROUND_TO_WORD((v->vector.dim + (CHAR_BIT-1))/CHAR_BIT); |
| 185 |
} else { |
| 186 |
return ROUND_TO_WORD(v->vector.dim * ecl_aet_size[v->vector.elttype]); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
static void |
| 191 |
serialize_vector(pool_t pool, cl_object v) |
| 192 |
{ |
| 193 |
if (!Null(v->vector.displaced)) { |
| 194 |
serialize_displaced_vector(pool, v); |
| 195 |
} else if (v->vector.elttype == ecl_aet_object) { |
| 196 |
serialize_object_ptr(pool, v->vector.self.t, v->vector.dim); |
| 197 |
} else { |
| 198 |
serialize_bits(pool, v->vector.self.b8, vector_self_size(v)); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
static void |
| 203 |
serialize_bignum(pool_t pool, cl_object b) |
| 204 |
{ |
| 205 |
int8_t sign = _ecl_big_sign(ecl_bignum(buffer)); |
| 206 |
serialize_bits(pool, &sign, 1); |
| 207 |
cl_index bytes = (_ecl_big_bits(buffer) + 7) / 8; |
| 208 |
serialize_bits(pool, &bytes, sizeof(cl_index)); |
| 209 |
cl_index index = alloc(pool, bytes); |
| 210 |
cl_index bytes_written; |
| 211 |
mpz_export(pool->data->vector.self.b8 + index, &bytes_written, |
| 212 |
1, 1, 1, 0, ecl_bignum(buffer)); |
| 213 |
} |
| 214 |
|
| 215 |
static void |
| 216 |
serialize_hashtable(pool_t pool, cl_object h) |
| 217 |
{ |
| 218 |
/* FIXME: Serializing all of h->hash.data is a big waste if the |
| 219 |
hashtable has only a small number of entries. */ |
| 220 |
cl_index size = h->hash.size; |
| 221 |
cl_index index = serialize_bits(pool, h->hash.data, size*sizeof(struct ecl_hashtable_entry)); |
| 222 |
for (; size; size--, index += sizeof(struct ecl_hashtable_entry)) { |
| 223 |
struct ecl_hashtable_entry *p = (struct ecl_hashtable_entry *)(pool->data->vector.self.b8 + index); |
| 224 |
p->key = enqueue(pool, p->key); |
| 225 |
p->value = enqueue(pool, p->value); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
static void |
| 230 |
serialize_one(pool_t pool, cl_object what) |
| 231 |
{ |
| 232 |
cl_index bytes, index; |
| 233 |
cl_object buffer; |
| 234 |
if (ECL_LISTP(what)) { |
| 235 |
cl_index bytes = ROUND_TO_WORD(sizeof(large_cons)); |
| 236 |
cl_index index = alloc(pool, bytes); |
| 237 |
large_cons_ptr cons = |
| 238 |
(large_cons_ptr)(pool->data->vector.self.b8 + index); |
| 239 |
memset(cons, 0, bytes); |
| 240 |
cons->t = t_list; |
| 241 |
if (!Null(what)) { |
| 242 |
cons->car = enqueue(pool, ECL_CONS_CAR(what)); |
| 243 |
cons->cdr = enqueue(pool, ECL_CONS_CDR(what)); |
| 244 |
} |
| 245 |
return; |
| 246 |
} |
| 247 |
bytes = object_size[ecl_t_of(what)]; |
| 248 |
index = alloc(pool, bytes); |
| 249 |
buffer = (cl_object)(pool->data->vector.self.b8 + index); |
| 250 |
memcpy(buffer, what, bytes); |
| 251 |
switch (ecl_t_of(what)) { |
| 252 |
case t_singlefloat: |
| 253 |
case t_doublefloat: |
| 254 |
case t_longfloat: |
| 255 |
break; |
| 256 |
case t_bignum: { |
| 257 |
serialize_bignum(pool, buffer); |
| 258 |
break; |
| 259 |
} |
| 260 |
case t_ratio: { |
| 261 |
buffer->ratio.den = enqueue(pool, buffer->ratio.den); |
| 262 |
buffer->ratio.num = enqueue(pool, buffer->ratio.num); |
| 263 |
break; |
| 264 |
} |
| 265 |
case t_complex: { |
| 266 |
buffer->gencomplex.real = enqueue(pool, buffer->gencomplex.real); |
| 267 |
buffer->gencomplex.imag = enqueue(pool, buffer->gencomplex.imag); |
| 268 |
break; |
| 269 |
} |
| 270 |
case t_hashtable: |
| 271 |
buffer->hash.sync_lock = enqueue(pool, buffer->hash.sync_lock); |
| 272 |
buffer->hash.rehash_size = enqueue(pool, buffer->hash.rehash_size); |
| 273 |
buffer->hash.threshold = enqueue(pool, buffer->hash.threshold); |
| 274 |
serialize_hashtable(pool, buffer); |
| 275 |
break; |
| 276 |
#ifdef ECL_UNICODE |
| 277 |
case t_string: |
| 278 |
#endif |
| 279 |
case t_vector: |
| 280 |
case t_bitvector: |
| 281 |
case t_base_string: { |
| 282 |
serialize_vector(pool, buffer); |
| 283 |
break; |
| 284 |
} |
| 285 |
case t_array: { |
| 286 |
serialize_bits(pool, buffer->array.dims, sizeof(cl_index) * buffer->array.rank); |
| 287 |
/* We might have allocated memory in pool->data and thus the |
| 288 |
adress of pool->data->vector.self might have changed, hence we |
| 289 |
have to reload buffer. */ |
| 290 |
buffer = (cl_object)(pool->data->vector.self.b8 + index); |
| 291 |
serialize_vector(pool, buffer); |
| 292 |
break; |
| 293 |
} |
| 294 |
case t_package: { |
| 295 |
struct fake_package *p = (struct fake_package *)buffer; |
| 296 |
p->name = enqueue(pool, what->pack.name); |
| 297 |
break; |
| 298 |
} |
| 299 |
case t_symbol: { |
| 300 |
struct fake_symbol *p = (struct fake_symbol *)buffer; |
| 301 |
p->name = enqueue(pool, what->symbol.name); |
| 302 |
p->pack = enqueue(pool, what->symbol.hpack); |
| 303 |
break; |
| 304 |
} |
| 305 |
case t_pathname: |
| 306 |
buffer->pathname.host = |
| 307 |
enqueue(pool, buffer->pathname.host); |
| 308 |
buffer->pathname.device = |
| 309 |
enqueue(pool, buffer->pathname.device); |
| 310 |
buffer->pathname.directory = |
| 311 |
enqueue(pool, buffer->pathname.directory); |
| 312 |
buffer->pathname.name = enqueue(pool, buffer->pathname.name); |
| 313 |
buffer->pathname.type = enqueue(pool, buffer->pathname.type); |
| 314 |
buffer->pathname.version = |
| 315 |
enqueue(pool, buffer->pathname.version); |
| 316 |
break; |
| 317 |
case t_random: { |
| 318 |
buffer->random.value = enqueue(pool, buffer->random.value); |
| 319 |
break; |
| 320 |
} |
| 321 |
case t_bclosure: { |
| 322 |
buffer->bclosure.code = enqueue(pool, buffer->bclosure.code); |
| 323 |
buffer->bclosure.lex = enqueue(pool, buffer->bclosure.lex); |
| 324 |
break; |
| 325 |
} |
| 326 |
case t_bytecodes: { |
| 327 |
buffer->bytecodes.name = enqueue(pool, buffer->bytecodes.name); |
| 328 |
buffer->bytecodes.definition = enqueue(pool, buffer->bytecodes.definition); |
| 329 |
buffer->bytecodes.data = enqueue(pool, buffer->bytecodes.data); |
| 330 |
buffer->bytecodes.flex = enqueue(pool, buffer->bytecodes.flex); |
| 331 |
buffer->bytecodes.file = enqueue(pool, buffer->bytecodes.file); |
| 332 |
buffer->bytecodes.file_position = enqueue(pool, buffer->bytecodes.file_position); |
| 333 |
buffer->bytecodes.code_size = serialize_bits(pool, buffer->bytecodes.code, |
| 334 |
buffer->bytecodes.code_size); |
| 335 |
break; |
| 336 |
} |
| 337 |
default: |
| 338 |
FEerror("Unable to serialize object ~A", 1, what); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
static void |
| 343 |
init_pool(pool_t pool, cl_object root) |
| 344 |
{ |
| 345 |
pool->data = si_make_vector(@'ext::byte8', |
| 346 |
ecl_make_fixnum(1024), |
| 347 |
ECL_T, |
| 348 |
ecl_make_fixnum(2 * sizeof(cl_index)), |
| 349 |
ECL_NIL, |
| 350 |
ecl_make_fixnum(0)); |
| 351 |
pool->hash = cl__make_hash_table(@'eql', ecl_make_fixnum(256), |
| 352 |
cl_core.rehash_size, |
| 353 |
cl_core.rehash_threshold); |
| 354 |
ecl_sethash(root, pool->hash, ecl_make_fixnum(0)); |
| 355 |
pool->queue = ecl_list1(root); |
| 356 |
pool->last = pool->queue; |
| 357 |
} |
| 358 |
|
| 359 |
static cl_object |
| 360 |
close_pool(pool_t pool) |
| 361 |
{ |
| 362 |
pool->data->vector.self.index[0] = pool->data->vector.fillp; |
| 363 |
pool->data->vector.self.index[1] = pool->hash->hash.entries; |
| 364 |
return pool->data; |
| 365 |
} |
| 366 |
|
| 367 |
cl_object |
| 368 |
si_serialize(cl_object root) |
| 369 |
{ |
| 370 |
struct pool pool[1]; |
| 371 |
init_pool(pool, root); |
| 372 |
while (!Null(pool->queue)) { |
| 373 |
cl_object what = ECL_CONS_CAR(pool->queue); |
| 374 |
serialize_one(pool, what); |
| 375 |
pool->queue = ECL_CONS_CDR(pool->queue); |
| 376 |
} |
| 377 |
@(return close_pool(pool)); |
| 378 |
} |
| 379 |
|
| 380 |
static void * |
| 381 |
reconstruct_bits(uint8_t *data, cl_index bytes) |
| 382 |
{ |
| 383 |
void *output = ecl_alloc_atomic(ROUND_TO_WORD(bytes)); |
| 384 |
memcpy(output, data, bytes); |
| 385 |
return output; |
| 386 |
} |
| 387 |
|
| 388 |
static void * |
| 389 |
reconstruct_object_ptr(uint8_t *data, cl_index bytes) |
| 390 |
{ |
| 391 |
void *output = ecl_alloc(ROUND_TO_WORD(bytes)); |
| 392 |
memcpy(output, data, bytes); |
| 393 |
return output; |
| 394 |
} |
| 395 |
|
| 396 |
static uint8_t * |
| 397 |
reconstruct_bytecodes(cl_object o, uint8_t *data) |
| 398 |
{ |
| 399 |
o->bytecodes.code = reconstruct_bits(data, o->bytecodes.code_size); |
| 400 |
data += o->bytecodes.code_size; |
| 401 |
return data; |
| 402 |
} |
| 403 |
|
| 404 |
static uint8_t * |
| 405 |
reconstruct_vector(cl_object v, uint8_t *data) |
| 406 |
{ |
| 407 |
if (v->vector.displaced == ECL_NIL) { |
| 408 |
cl_index bytes = vector_self_size(v); |
| 409 |
if (v->vector.elttype == ecl_aet_object) { |
| 410 |
v->vector.self.t = reconstruct_object_ptr(data, bytes); |
| 411 |
} else { |
| 412 |
v->vector.self.t = reconstruct_bits(data, bytes); |
| 413 |
} |
| 414 |
data += bytes; |
| 415 |
} |
| 416 |
return data; |
| 417 |
} |
| 418 |
|
| 419 |
static uint8_t * |
| 420 |
reconstruct_array(cl_object a, uint8_t *data) |
| 421 |
{ |
| 422 |
cl_index bytes = ROUND_TO_WORD(a->array.rank * sizeof(cl_index)); |
| 423 |
a->array.dims = reconstruct_bits(data, bytes); |
| 424 |
return reconstruct_vector(a, data + bytes); |
| 425 |
} |
| 426 |
|
| 427 |
static uint8_t * |
| 428 |
reconstruct_bignum(cl_object a, uint8_t *data) |
| 429 |
{ |
| 430 |
int8_t sign = (int8_t) *data; |
| 431 |
data += ROUND_TO_WORD(1); |
| 432 |
cl_index bytes = (cl_index) *data; |
| 433 |
data += ROUND_TO_WORD(sizeof(cl_index)); |
| 434 |
mpz_init(ecl_bignum(output)); |
| 435 |
mpz_import(ecl_bignum(output), bytes, 1, 1, 1, 0, data); |
| 436 |
if (sign == -1) { |
| 437 |
_ecl_big_neg(output, output); |
| 438 |
} |
| 439 |
data += ROUND_TO_WORD(bytes); |
| 440 |
} |
| 441 |
|
| 442 |
static uint8_t * |
| 443 |
reconstruct_hashtable(cl_object h, uint8_t *data) |
| 444 |
{ |
| 445 |
cl_index bytes = ROUND_TO_WORD(h->hash.size * sizeof(struct ecl_hashtable_entry)); |
| 446 |
h->hash.data = ecl_alloc(bytes); |
| 447 |
memcpy(h->hash.data, data, bytes); |
| 448 |
return data + bytes; |
| 449 |
} |
| 450 |
|
| 451 |
static uint8_t * |
| 452 |
duplicate_object(uint8_t *data, cl_object *output) |
| 453 |
{ |
| 454 |
cl_type t = ecl_t_of((cl_object)data); |
| 455 |
cl_object o = ecl_alloc_object(t); |
| 456 |
cl_index bytes = object_size[t]; |
| 457 |
memcpy(o, data, bytes); |
| 458 |
*output = o; |
| 459 |
return data + bytes; |
| 460 |
} |
| 461 |
|
| 462 |
static uint8_t * |
| 463 |
reconstruct_one(uint8_t *data, cl_object *output) |
| 464 |
{ |
| 465 |
cl_object o = (cl_object)data; |
| 466 |
switch (ecl_t_of(o)) { |
| 467 |
case t_list: { |
| 468 |
large_cons_ptr c = (large_cons_ptr)data; |
| 469 |
cl_object car = c->car; |
| 470 |
cl_object cdr = c->cdr; |
| 471 |
if (car == (cl_object) 0 && cdr == (cl_object) 0) { |
| 472 |
*output = ECL_NIL; |
| 473 |
} else { |
| 474 |
*output = ecl_cons(car, cdr); |
| 475 |
} |
| 476 |
data += ROUND_TO_WORD(sizeof(large_cons)); |
| 477 |
break; |
| 478 |
} |
| 479 |
case t_bignum: { |
| 480 |
data = duplicate_object(data, output); |
| 481 |
reconsturct_bignum(*output, data); |
| 482 |
break; |
| 483 |
} |
| 484 |
case t_hashtable: |
| 485 |
data = duplicate_object(data, output); |
| 486 |
data = reconstruct_hashtable(*output, data); |
| 487 |
break; |
| 488 |
#ifdef ECL_UNICODE |
| 489 |
case t_string: |
| 490 |
#endif |
| 491 |
case t_base_string: |
| 492 |
case t_vector: |
| 493 |
case t_bitvector: |
| 494 |
data = duplicate_object(data, output); |
| 495 |
data = reconstruct_vector(*output, data); |
| 496 |
break; |
| 497 |
case t_array: |
| 498 |
data = duplicate_object(data, output); |
| 499 |
data = reconstruct_array(*output, data); |
| 500 |
break; |
| 501 |
case t_package: |
| 502 |
*output = (cl_object)data; |
| 503 |
data += ROUND_TO_WORD(sizeof(struct fake_package)); |
| 504 |
break; |
| 505 |
case t_symbol: |
| 506 |
*output = (cl_object)data; |
| 507 |
data += ROUND_TO_WORD(sizeof(struct fake_symbol)); |
| 508 |
break; |
| 509 |
case t_bytecodes: |
| 510 |
data = duplicate_object(data, output); |
| 511 |
data = reconstruct_bytecodes(*output, data); |
| 512 |
break; |
| 513 |
default: |
| 514 |
data = duplicate_object(data, output); |
| 515 |
} |
| 516 |
return data; |
| 517 |
} |
| 518 |
|
| 519 |
static cl_object |
| 520 |
get_object(cl_object o_or_index, cl_object *o_list) |
| 521 |
{ |
| 522 |
if (ECL_IMMEDIATE(o_or_index) || o_or_index == OBJNULL) { |
| 523 |
return o_or_index; |
| 524 |
} else { |
| 525 |
cl_index i = (cl_index)o_or_index >> 2; |
| 526 |
return o_list[i]; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
static void |
| 531 |
fixup_vector(cl_object v, cl_object *o_list) |
| 532 |
{ |
| 533 |
if (!ECL_IMMEDIATE(v->vector.displaced)) { |
| 534 |
cl_object to = get_object(v->vector.displaced, o_list); |
| 535 |
if (to != ECL_NIL) { |
| 536 |
cl_index offset = (cl_index)v->vector.self.b8; |
| 537 |
v->vector.displaced = ECL_NIL; |
| 538 |
ecl_displace(v, to, ecl_make_fixnum(offset)); |
| 539 |
return; |
| 540 |
} |
| 541 |
} |
| 542 |
if (v->vector.elttype == ecl_aet_object) { |
| 543 |
cl_index i; |
| 544 |
cl_object *p = v->vector.self.t; |
| 545 |
for (i = v->vector.dim; i; i--, p++) { |
| 546 |
*p = get_object(*p, o_list); |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
static void |
| 552 |
fixup_hashtable(cl_object h, cl_object *o_list) |
| 553 |
{ |
| 554 |
cl_index i; |
| 555 |
for (i = 0; i < h->hash.size; i++) { |
| 556 |
h->hash.data[i].key = get_object(h->hash.data[i].key, o_list); |
| 557 |
h->hash.data[i].value = get_object(h->hash.data[i].value, o_list); |
| 558 |
} |
| 559 |
h->hash.rehash_size = get_object(h->hash.rehash_size, o_list); |
| 560 |
h->hash.threshold = get_object(h->hash.threshold, o_list); |
| 561 |
ecl_reconstruct_serialized_hashtable(h); |
| 562 |
} |
| 563 |
|
| 564 |
static void |
| 565 |
fixup(cl_object o, cl_object *o_list) |
| 566 |
{ |
| 567 |
if (ECL_LISTP(o)) { |
| 568 |
if (!Null(o)) { |
| 569 |
ECL_RPLACA(o, get_object(ECL_CONS_CAR(o), o_list)); |
| 570 |
ECL_RPLACD(o, get_object(ECL_CONS_CDR(o), o_list)); |
| 571 |
} |
| 572 |
return; |
| 573 |
} |
| 574 |
switch (ecl_t_of(o)) { |
| 575 |
case t_ratio: |
| 576 |
o->ratio.den = get_object(o->ratio.den, o_list); |
| 577 |
o->ratio.num = get_object(o->ratio.num, o_list); |
| 578 |
break; |
| 579 |
case t_complex: |
| 580 |
o->gencomplex.real = get_object(o->gencomplex.real, o_list); |
| 581 |
o->gencomplex.imag = get_object(o->gencomplex.imag, o_list); |
| 582 |
break; |
| 583 |
case t_hashtable: |
| 584 |
fixup_hashtable(o, o_list); |
| 585 |
break; |
| 586 |
#ifdef ECL_UNICODE |
| 587 |
case t_string: |
| 588 |
#endif |
| 589 |
case t_base_string: |
| 590 |
case t_vector: |
| 591 |
case t_bitvector: |
| 592 |
case t_array: |
| 593 |
fixup_vector(o, o_list); |
| 594 |
break; |
| 595 |
case t_pathname: |
| 596 |
o->pathname.host = get_object(o->pathname.host, o_list); |
| 597 |
o->pathname.device = |
| 598 |
get_object(o->pathname.device, o_list); |
| 599 |
o->pathname.directory = |
| 600 |
get_object(o->pathname.directory, o_list); |
| 601 |
o->pathname.name = get_object(o->pathname.name, o_list); |
| 602 |
o->pathname.type = get_object(o->pathname.type, o_list); |
| 603 |
o->pathname.version = |
| 604 |
get_object(o->pathname.version, o_list); |
| 605 |
break; |
| 606 |
case t_random: |
| 607 |
o->random.value = get_object(o->random.value, o_list); |
| 608 |
break; |
| 609 |
case t_bclosure: |
| 610 |
o->bclosure.code = get_object(o->bclosure.code, o_list); |
| 611 |
o->bclosure.lex = get_object(o->bclosure.lex, o_list); |
| 612 |
o->bclosure.entry = _ecl_bclosure_dispatch_vararg; |
| 613 |
break; |
| 614 |
case t_bytecodes: |
| 615 |
o->bytecodes.name = get_object(o->bytecodes.name, o_list); |
| 616 |
o->bytecodes.definition = get_object(o->bytecodes.definition, o_list); |
| 617 |
o->bytecodes.data = get_object(o->bytecodes.data, o_list); |
| 618 |
o->bytecodes.flex = get_object(o->bytecodes.flex, o_list); |
| 619 |
o->bytecodes.file = get_object(o->bytecodes.file, o_list); |
| 620 |
o->bytecodes.file_position = get_object(o->bytecodes.file_position, o_list); |
| 621 |
o->bytecodes.entry = _ecl_bytecodes_dispatch_vararg; |
| 622 |
break; |
| 623 |
default: |
| 624 |
break; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
cl_object |
| 629 |
ecl_deserialize(uint8_t *raw) |
| 630 |
{ |
| 631 |
cl_env_ptr the_env = ecl_process_env(); |
| 632 |
cl_index *data = (cl_index*)raw; |
| 633 |
cl_index i, num_el = data[1]; |
| 634 |
cl_object *output = ecl_alloc(sizeof(cl_object) * num_el); |
| 635 |
raw += 2*sizeof(cl_index); |
| 636 |
for (i = 0; i < num_el; i++) { |
| 637 |
raw = reconstruct_one(raw, output+i); |
| 638 |
} |
| 639 |
for (i = 0; i < num_el; i++) { |
| 640 |
cl_object package = output[i]; |
| 641 |
if (ecl_t_of(package) == t_package) { |
| 642 |
cl_object name = get_object(package->pack.name, |
| 643 |
output); |
| 644 |
output[i] = ecl_find_package_nolock(name); |
| 645 |
if (Null(output[i])) { |
| 646 |
unlikely_if (Null(the_env->packages_to_be_created_p)) { |
| 647 |
FEerror("There is no package with the name ~A.", |
| 648 |
1, name); |
| 649 |
} |
| 650 |
output[i] = _ecl_package_to_be_created(the_env, name); |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
for (i = 0; i < num_el; i++) { |
| 655 |
cl_object symbol = output[i]; |
| 656 |
if (ecl_t_of(symbol) == t_symbol) { |
| 657 |
struct fake_symbol *s = (struct fake_symbol *)symbol; |
| 658 |
cl_object name = get_object(s->name, output); |
| 659 |
cl_object pack = get_object(s->pack, output); |
| 660 |
if (Null(pack)) { |
| 661 |
output[i] = cl_make_symbol(name); |
| 662 |
} else { |
| 663 |
int flag; |
| 664 |
output[i] = ecl_intern(name, pack, &flag); |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
for (i = 0; i < num_el; i++) { |
| 669 |
fixup(output[i], output); |
| 670 |
} |
| 671 |
return output[0]; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
cl_object |
| 676 |
si_deserialize(cl_object data) |
| 677 |
{ |
| 678 |
@(return ecl_deserialize(data->vector.self.b8)); |
| 679 |
} |
| 680 |
|
| 681 |
#endif /* ECL_EXTERNALIZABLE */ |