| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmimgrb.cpp - T3 Image File Re-Builder |
| 15 | Function |
| 16 | This module re-builds an image file from the contents of memory after |
| 17 | the program has been "pre-initialized." This allows a program to |
| 18 | run through its static state initialization during the compilation |
| 19 | process, then store the result as a new image file with pre-initialized |
| 20 | state. Any initialization that must always happen for every run of |
| 21 | the program can be performed during this pre-initialization pass, |
| 22 | saving the time of doing the work each time the program is run. |
| 23 | |
| 24 | Mostly, we just copy the old image file to the new image file; most |
| 25 | parts of the image file are copied without changes. We update the |
| 26 | object stream, replacing the original objects with the objects in |
| 27 | their pre-initialized state, and we add any new strings dynamically |
| 28 | created during pre-initialization to the constant pool. |
| 29 | Notes |
| 30 | |
| 31 | Modified |
| 32 | 07/21/99 MJRoberts - Creation |
| 33 | */ |
| 34 | |
| 35 | #include <stdlib.h> |
| 36 | #include <memory.h> |
| 37 | |
| 38 | #include "t3std.h" |
| 39 | #include "vmfile.h" |
| 40 | #include "vmimage.h" |
| 41 | #include "vmpool.h" |
| 42 | #include "vmglob.h" |
| 43 | #include "vmwrtimg.h" |
| 44 | #include "vmobj.h" |
| 45 | #include "vmtobj.h" |
| 46 | #include "vmdict.h" |
| 47 | #include "vmhash.h" |
| 48 | #include "vmlst.h" |
| 49 | #include "vmbignum.h" |
| 50 | #include "vmstr.h" |
| 51 | #include "vmgram.h" |
| 52 | #include "vmmeta.h" |
| 53 | #include "vmimgrb.h" |
| 54 | #include "vmintcls.h" |
| 55 | #include "vmiter.h" |
| 56 | #include "vmvec.h" |
| 57 | #include "vmlookup.h" |
| 58 | #include "vmstack.h" |
| 59 | #include "vmbytarr.h" |
| 60 | #include "vmcset.h" |
| 61 | #include "vmfilobj.h" |
| 62 | #include "vmtmpfil.h" |
| 63 | #include "vmfilnam.h" |
| 64 | #include "vmpat.h" |
| 65 | #include "vmstrcmp.h" |
| 66 | #include "vmstrbuf.h" |
| 67 | #include "vmdynfunc.h" |
| 68 | #include "vmfref.h" |
| 69 | #include "vmdate.h" |
| 70 | #include "vmtzobj.h" |
| 71 | #include "vmtz.h" |
| 72 | |
| 73 | #ifdef TADSNET |
| 74 | #include "vmhttpsrv.h" |
| 75 | #include "vmhttpreq.h" |
| 76 | #endif |
| 77 | |
| 78 | |
| 79 | /* ------------------------------------------------------------------------ */ |
| 80 | /* |
| 81 | * Write a string to a buffer with a one-byte prefix |
| 82 | */ |
| 83 | static char *write_str_byte_prefix(char *ptr, const char *str, size_t len) |
| 84 | { |
| 85 | /* make sure it fits the byte prefix */ |
| 86 | if (len > 255) |
| 87 | err_throw(VMERR_STR_TOO_LONG); |
| 88 | |
| 89 | /* write the prefix and the string */ |
| 90 | oswp1(ptr, (char)len); |
| 91 | memcpy(ptr + 1, str, len); |
| 92 | |
| 93 | /* return the pointer to the next byte after the copied data */ |
| 94 | return ptr + len + 1; |
| 95 | } |
| 96 | |
| 97 | #if 0 // not currently needed |
| 98 | static char *write_str_byte_prefix(char *ptr, const char *str) |
| 99 | { |
| 100 | return write_str_byte_prefix(ptr, str, strlen(str)); |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | |
| 105 | /* ------------------------------------------------------------------------ */ |
| 106 | /* |
| 107 | * Rebuild the OBJS blocks and write the data to the file. This goes |
| 108 | * through the objects in memory and constructs fixed image-file |
| 109 | * versions of the objects, then writes them to OBJS blocks. |
| 110 | */ |
| 111 | static void vm_rewrite_objs_blocks(VMG_ CVmImageWriter *writer, |
| 112 | class CVmConstMapper *mapper) |
| 113 | { |
| 114 | size_t i; |
| 115 | |
| 116 | /* rewrite the image block for each of our defined metaclasses */ |
| 117 | for (i = 0 ; i < G_meta_table->get_count() ; ++i) |
| 118 | { |
| 119 | /* write this metaclass's objects */ |
| 120 | G_obj_table->rebuild_image(vmg_ i, writer, mapper); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* ------------------------------------------------------------------------ */ |
| 125 | /* |
| 126 | * Re-write the image file |
| 127 | */ |
| 128 | void vm_rewrite_image(VMG_ CVmFile *origfp, CVmFile *newfp, |
| 129 | ulong static_cs_ofs) |
| 130 | { |
| 131 | char buf[4096]; |
| 132 | int done; |
| 133 | CVmImageWriter *writer; |
| 134 | CVmConstMapper *const_mapper; |
| 135 | uchar xor_mask; |
| 136 | ulong code_page_size; |
| 137 | |
| 138 | /* we don't know the code page size yet */ |
| 139 | code_page_size = 0; |
| 140 | |
| 141 | /* choose an arbitrary XOR mask for our pages */ |
| 142 | xor_mask = 0xEF; |
| 143 | |
| 144 | /* create an image writer to help us write the output file */ |
| 145 | writer = new CVmImageWriter(newfp); |
| 146 | |
| 147 | /* create our constant mapper */ |
| 148 | const_mapper = new CVmConstMapper(vmg0_); |
| 149 | |
| 150 | /* |
| 151 | * clear all undo information - we don't save undo with the rebuilt |
| 152 | * file, so there's no reason to keep any of the objects that are |
| 153 | * referenced only in the undo records |
| 154 | */ |
| 155 | G_undo->drop_undo(vmg0_); |
| 156 | |
| 157 | /* discard everything on the stack */ |
| 158 | G_stk->discard(G_stk->get_depth()); |
| 159 | |
| 160 | /* |
| 161 | * perform a full garbage collection pass, to make sure we don't |
| 162 | * include any unreachable objects in the new image file |
| 163 | */ |
| 164 | G_obj_table->gc_full(vmg0_); |
| 165 | |
| 166 | /* add any metaclasses that weren't in the dependency table originally */ |
| 167 | G_obj_table->add_metadeps_for_instances(vmg0_); |
| 168 | |
| 169 | /* convert objects to constant data to the extent possible */ |
| 170 | G_obj_table->rebuild_image_convert_const_data(vmg_ const_mapper); |
| 171 | |
| 172 | /* |
| 173 | * copy the header (signature, UINT2 format version number, 32 |
| 174 | * reserved bytes, 24-byte compilation timestamp) to the new file |
| 175 | */ |
| 176 | origfp->read_bytes(buf, sizeof(VMIMAGE_SIG) - 1 + 2 + 32 + 24); |
| 177 | newfp->write_bytes(buf, sizeof(VMIMAGE_SIG) - 1 + 2 + 32 + 24); |
| 178 | |
| 179 | /* copy or replace the data blocks */ |
| 180 | for (done = FALSE ; !done ; ) |
| 181 | { |
| 182 | ulong siz; |
| 183 | |
| 184 | /* read the next block header */ |
| 185 | origfp->read_bytes(buf, 10); |
| 186 | |
| 187 | /* get the size */ |
| 188 | siz = t3rp4u(buf + 4); |
| 189 | |
| 190 | /* check the block type */ |
| 191 | if (CVmImageLoader::block_type_is(buf, "OBJS") |
| 192 | || CVmImageLoader::block_type_is(buf, "MCLD") |
| 193 | || CVmImageLoader::block_type_is(buf, "SINI")) |
| 194 | { |
| 195 | /* |
| 196 | * Simply skip all of the original OBJS (object data) or |
| 197 | * MCLD (metaclass dependency table) blocks -- we'll replace |
| 198 | * them with our re-built blocks. |
| 199 | * |
| 200 | * Also skip SINI (static initializer) blocks, since these |
| 201 | * are only needed for pre-initialization and can be |
| 202 | * discarded from the final image file. |
| 203 | */ |
| 204 | origfp->set_pos(origfp->get_pos() + (long)siz); |
| 205 | } |
| 206 | else if (CVmImageLoader::block_type_is(buf, "CPDF")) |
| 207 | { |
| 208 | char cpdf_buf[20]; |
| 209 | uint pool_id; |
| 210 | ulong pgcnt; |
| 211 | ulong pgsiz; |
| 212 | |
| 213 | /* read the pool definition */ |
| 214 | origfp->read_bytes(cpdf_buf, 10); |
| 215 | |
| 216 | /* get the ID, page count, and page size from the definition */ |
| 217 | pool_id = osrp2(cpdf_buf); |
| 218 | pgcnt = t3rp4u(cpdf_buf + 2); |
| 219 | pgsiz = t3rp4u(cpdf_buf + 6); |
| 220 | |
| 221 | /* |
| 222 | * if this is the constant pool (pool ID = 2), increase the |
| 223 | * page count by the extra constant pool pages we need for |
| 224 | * converting new object data to constants |
| 225 | */ |
| 226 | if (pool_id == 2) |
| 227 | { |
| 228 | /* add in our new count */ |
| 229 | pgcnt += const_mapper->get_page_count(); |
| 230 | |
| 231 | /* write the new count */ |
| 232 | oswp4(cpdf_buf + 2, pgcnt); |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * if this is the code pool (pool ID = 1), and we have |
| 237 | * static initializers, decrease the page count to exclude |
| 238 | * the static initializer pages (all of the static |
| 239 | * initializers are grouped at the high end of the code |
| 240 | * pool, so we can discard them and only them by truncating |
| 241 | * the code pool before the page containing the first static |
| 242 | * initializer) |
| 243 | */ |
| 244 | if (pool_id == 1 && static_cs_ofs != 0) |
| 245 | { |
| 246 | /* |
| 247 | * calculate the new count - it's the offset to the |
| 248 | * first static initializer divided by the size of each |
| 249 | * code page |
| 250 | */ |
| 251 | pgcnt = static_cs_ofs / pgsiz; |
| 252 | |
| 253 | /* write the new count */ |
| 254 | oswp4(cpdf_buf + 2, pgcnt); |
| 255 | |
| 256 | /* |
| 257 | * remember the code page size for later, when we're |
| 258 | * scanning the code pages themselves |
| 259 | */ |
| 260 | code_page_size = pgsiz; |
| 261 | } |
| 262 | |
| 263 | /* update the constant block definition */ |
| 264 | newfp->write_bytes(buf, 10); |
| 265 | newfp->write_bytes(cpdf_buf, 10); |
| 266 | } |
| 267 | else if (CVmImageLoader::block_type_is(buf, "CPPG")) |
| 268 | { |
| 269 | char cppg_buf[20]; |
| 270 | long start_pos; |
| 271 | uint pool_id; |
| 272 | ulong page_idx; |
| 273 | int keep_block; |
| 274 | |
| 275 | /* presume we're going to keep this block */ |
| 276 | keep_block = TRUE; |
| 277 | |
| 278 | /* |
| 279 | * This is a constant page - if it's in pool 2 (constants), |
| 280 | * use its XOR mask for any new pages we write. First, read |
| 281 | * the pool header, then seek back so we can copy the block |
| 282 | * unchanged. |
| 283 | */ |
| 284 | start_pos = origfp->get_pos(); |
| 285 | origfp->read_bytes(cppg_buf, 7); |
| 286 | origfp->set_pos(start_pos); |
| 287 | |
| 288 | /* get the pool ID and the page's index */ |
| 289 | pool_id = osrp2(cppg_buf); |
| 290 | page_idx = t3rp4u(cppg_buf + 2); |
| 291 | |
| 292 | /* if it's pool 2 (constants), read its XOR mask byte */ |
| 293 | if (pool_id == 2) |
| 294 | xor_mask = cppg_buf[6]; |
| 295 | |
| 296 | /* |
| 297 | * if it's pool 1 (code), and it's above the start of the |
| 298 | * static initializers, skip it - we don't want to copy |
| 299 | * static initializer code to the final image file, since |
| 300 | * they're only needed for static initialization, which we |
| 301 | * necessarily have finished by the time we reach this point |
| 302 | */ |
| 303 | if (pool_id == 1 |
| 304 | && static_cs_ofs != 0 |
| 305 | && page_idx * code_page_size >= static_cs_ofs) |
| 306 | { |
| 307 | /* this is a static initializer block - skip it */ |
| 308 | keep_block = FALSE; |
| 309 | } |
| 310 | |
| 311 | /* keep or skip the block, as appropriate */ |
| 312 | if (keep_block) |
| 313 | { |
| 314 | /* |
| 315 | * we only wanted to get information from this block, so |
| 316 | * go copy it as-is to the output |
| 317 | */ |
| 318 | goto copy_block; |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | /* skip past the block */ |
| 323 | origfp->set_pos(origfp->get_pos() + (long)siz); |
| 324 | } |
| 325 | } |
| 326 | else if (CVmImageLoader::block_type_is(buf, "EOF ")) |
| 327 | { |
| 328 | /* end of file - note that we're done after this block */ |
| 329 | done = TRUE; |
| 330 | |
| 331 | /* re-write the metaclass dependency block */ |
| 332 | G_meta_table->rebuild_image(writer); |
| 333 | |
| 334 | /* write our new OBJS blocks */ |
| 335 | vm_rewrite_objs_blocks(vmg_ writer, const_mapper); |
| 336 | |
| 337 | /* write our new constant pool pages */ |
| 338 | const_mapper->write_to_image_file(writer, xor_mask); |
| 339 | |
| 340 | /* copy the EOF block to the new file unchanged */ |
| 341 | goto copy_block; |
| 342 | } |
| 343 | else |
| 344 | { |
| 345 | copy_block: |
| 346 | /* |
| 347 | * For anything else, we'll simply copy the original block |
| 348 | * from the original image file unchanged. |
| 349 | */ |
| 350 | |
| 351 | /* write the block header unchanged */ |
| 352 | newfp->write_bytes(buf, 10); |
| 353 | |
| 354 | /* copy the block in chunks */ |
| 355 | while (siz != 0) |
| 356 | { |
| 357 | size_t cur; |
| 358 | |
| 359 | /* |
| 360 | * read as much as we can, up to the amount remaining or |
| 361 | * the buffer size, whichever is smaller |
| 362 | */ |
| 363 | cur = sizeof(buf); |
| 364 | if (cur > siz) |
| 365 | cur = (size_t)siz; |
| 366 | |
| 367 | /* read the data and write it out */ |
| 368 | origfp->read_bytes(buf, cur); |
| 369 | newfp->write_bytes(buf, cur); |
| 370 | |
| 371 | /* deduct this chunk from the amount remaining */ |
| 372 | siz -= cur; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /* we're done with the image writer */ |
| 378 | delete writer; |
| 379 | |
| 380 | /* we're done with the constant mapper */ |
| 381 | delete const_mapper; |
| 382 | } |
| 383 | |
| 384 | /* ------------------------------------------------------------------------ */ |
| 385 | /* |
| 386 | * Object Table extension - rebuild the image file representations for |
| 387 | * all objects of a particular metaclass. |
| 388 | */ |
| 389 | void CVmObjTable::rebuild_image(VMG_ int meta_dep_idx, CVmImageWriter *writer, |
| 390 | class CVmConstMapper *mapper) |
| 391 | { |
| 392 | int pass; |
| 393 | |
| 394 | /* write persistent and transient objects separately */ |
| 395 | for (pass = 1 ; pass <= 2 ; ++pass) |
| 396 | { |
| 397 | int trans; |
| 398 | |
| 399 | /* write persistent on pass 1, transient on pass 2 */ |
| 400 | trans = (pass == 2); |
| 401 | |
| 402 | /* write the objects for this pass */ |
| 403 | rebuild_image(vmg_ meta_dep_idx, writer, mapper, trans); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Write all of the transient or persistent objects of a given metaclass. |
| 409 | */ |
| 410 | void CVmObjTable::rebuild_image(VMG_ int meta_dep_idx, CVmImageWriter *writer, |
| 411 | class CVmConstMapper *mapper, int trans) |
| 412 | { |
| 413 | CVmObjPageEntry **pg; |
| 414 | size_t i; |
| 415 | vm_obj_id_t id; |
| 416 | char *buf; |
| 417 | ulong bufsiz; |
| 418 | int block_cnt; |
| 419 | ulong block_size; |
| 420 | int large_objects; |
| 421 | |
| 422 | /* we haven't written anything to the file yet */ |
| 423 | block_cnt = 0; |
| 424 | block_size = 0; |
| 425 | |
| 426 | /* presume we'll use small objects */ |
| 427 | large_objects = FALSE; |
| 428 | |
| 429 | /* |
| 430 | * allocate an initial object buffer - we'll expand this as needed |
| 431 | * if we find an object that doesn't fit |
| 432 | */ |
| 433 | bufsiz = 4096; |
| 434 | buf = (char *)t3malloc((size_t)bufsiz); |
| 435 | if (buf == 0) |
| 436 | err_throw(VMERR_OUT_OF_MEMORY); |
| 437 | |
| 438 | /* go through each page in the object table */ |
| 439 | for (id = 0, i = pages_used_, pg = pages_ ; i > 0 ; ++pg, --i) |
| 440 | { |
| 441 | size_t j; |
| 442 | CVmObjPageEntry *entry; |
| 443 | |
| 444 | /* start at the start of the page */ |
| 445 | j = VM_OBJ_PAGE_CNT; |
| 446 | entry = *pg; |
| 447 | |
| 448 | /* go through each entry on this page */ |
| 449 | for ( ; j > 0 ; --j, ++entry, ++id) |
| 450 | { |
| 451 | /* |
| 452 | * if this entry is in use, and its transient/persistent type |
| 453 | * matches the type we're writing, and its metaclass matches |
| 454 | * the one we're writing, write it out |
| 455 | */ |
| 456 | if (!entry->free_ |
| 457 | && (entry->transient_ != 0) == (trans != 0) |
| 458 | && (G_meta_table->get_dependency_index( |
| 459 | entry->get_vm_obj()->get_metaclass_reg()->get_reg_idx()) |
| 460 | == meta_dep_idx)) |
| 461 | { |
| 462 | ulong objsiz; |
| 463 | |
| 464 | /* |
| 465 | * if this object has been mapped to a constant value, |
| 466 | * there's no need to store it, since it is no longer |
| 467 | * reachable |
| 468 | */ |
| 469 | if (mapper->get_pool_addr(id) != 0) |
| 470 | continue; |
| 471 | |
| 472 | /* try building the object */ |
| 473 | objsiz = entry->get_vm_obj()->rebuild_image(vmg_ buf, bufsiz); |
| 474 | |
| 475 | /* if they need more space, reallocate the buffer */ |
| 476 | if (objsiz > bufsiz) |
| 477 | { |
| 478 | /* if the object is too large, throw an error */ |
| 479 | if (objsiz > OSMALMAX) |
| 480 | err_throw(VMERR_OBJ_SIZE_OVERFLOW); |
| 481 | |
| 482 | /* reallocate to next 4k increment */ |
| 483 | bufsiz = (objsiz + 4095) & ~4095; |
| 484 | buf = (char *)t3realloc(buf, (size_t)bufsiz); |
| 485 | if (buf == 0) |
| 486 | err_throw(VMERR_OUT_OF_MEMORY); |
| 487 | |
| 488 | /* try it again */ |
| 489 | objsiz = entry->get_vm_obj()-> |
| 490 | rebuild_image(vmg_ buf, bufsiz); |
| 491 | } |
| 492 | |
| 493 | /* if the object is not empty, write it out */ |
| 494 | if (objsiz != 0) |
| 495 | { |
| 496 | char prefix[20]; |
| 497 | |
| 498 | /* |
| 499 | * if this object's size exceeds 64k, and the |
| 500 | * current OBJS block is a small block, end this |
| 501 | * OBJS block and begin a large OBJS block |
| 502 | */ |
| 503 | if (objsiz > 65535 && !large_objects) |
| 504 | { |
| 505 | /* if we have a block open, end it */ |
| 506 | if (block_cnt != 0) |
| 507 | writer->end_objs_block(block_cnt); |
| 508 | |
| 509 | /* reset the count and size for the new block */ |
| 510 | block_cnt = 0; |
| 511 | block_size = 0; |
| 512 | |
| 513 | /* make the next block a large block */ |
| 514 | large_objects = TRUE; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * If this object plus its prefix would push this OBJS |
| 519 | * block over 64k, and we have a small-object block |
| 520 | * open, close it off and start a new block. Don't do |
| 521 | * this if we're already in a large-objects block, |
| 522 | * since that means we have a single object that we |
| 523 | * can't fit in 64k, meaning that it would be |
| 524 | * impossible to keep the block itself to within 64k. |
| 525 | */ |
| 526 | if (block_size + objsiz + 6 > 64000L |
| 527 | && block_cnt != 0 |
| 528 | && !large_objects) |
| 529 | { |
| 530 | /* close this block */ |
| 531 | writer->end_objs_block(block_cnt); |
| 532 | |
| 533 | /* reset the count and size for the new block */ |
| 534 | block_cnt = 0; |
| 535 | block_size = 0; |
| 536 | |
| 537 | /* |
| 538 | * go bak to the small-object block format if this |
| 539 | * object doesn't itself require a large-objects |
| 540 | * block |
| 541 | */ |
| 542 | if (objsiz <= 65535) |
| 543 | large_objects = FALSE; |
| 544 | } |
| 545 | |
| 546 | /* if this is the first object, write the header */ |
| 547 | if (block_cnt == 0) |
| 548 | writer->begin_objs_block(meta_dep_idx, large_objects, |
| 549 | trans); |
| 550 | |
| 551 | /* write the prefix information */ |
| 552 | oswp4(prefix, id); |
| 553 | if (large_objects) |
| 554 | { |
| 555 | /* write the 32-bit object size */ |
| 556 | oswp4(prefix + 4, objsiz); |
| 557 | |
| 558 | /* write the header */ |
| 559 | writer->write_objs_bytes(prefix, 8); |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | /* write the 16-bit object size */ |
| 564 | oswp2(prefix + 4, objsiz); |
| 565 | |
| 566 | /* write the header */ |
| 567 | writer->write_objs_bytes(prefix, 6); |
| 568 | } |
| 569 | |
| 570 | /* write the object data */ |
| 571 | writer->write_objs_bytes(buf, objsiz); |
| 572 | |
| 573 | /* count the object */ |
| 574 | ++block_cnt; |
| 575 | |
| 576 | /* count the size (including the prefix) */ |
| 577 | block_size += objsiz + 6; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* if we wrote any objects, end the OBJS block */ |
| 584 | if (block_cnt != 0) |
| 585 | writer->end_objs_block(block_cnt); |
| 586 | |
| 587 | /* delete our buffer */ |
| 588 | t3free(buf); |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Scan all active objects and convert objects to constant data where |
| 593 | * possible. Certain object metaclasses, such as strings and lists, can |
| 594 | * be represented in a rebuilt image file as constant data; this routine |
| 595 | * makes all of these conversions. |
| 596 | */ |
| 597 | void CVmObjTable::rebuild_image_convert_const_data |
| 598 | (VMG_ CVmConstMapper *const_mapper) |
| 599 | { |
| 600 | CVmObjPageEntry **pg; |
| 601 | size_t i; |
| 602 | vm_obj_id_t id; |
| 603 | |
| 604 | /* |
| 605 | * First pass: go through each page in the object table, and reserve |
| 606 | * space for the constant conversion. This assigns each object that |
| 607 | * can be converted its address in the new constant pool pages. |
| 608 | */ |
| 609 | for (id = 0, i = pages_used_, pg = pages_ ; i > 0 ; ++pg, --i) |
| 610 | { |
| 611 | size_t j; |
| 612 | CVmObjPageEntry *entry; |
| 613 | |
| 614 | /* start at the start of the page, but skip object ID = 0 */ |
| 615 | j = VM_OBJ_PAGE_CNT; |
| 616 | entry = *pg; |
| 617 | |
| 618 | /* go through each entry on this page */ |
| 619 | for ( ; j > 0 ; --j, ++entry, ++id) |
| 620 | { |
| 621 | /* |
| 622 | * if this entry is in use, tell it to reserve space for |
| 623 | * conversion to constant data |
| 624 | */ |
| 625 | if (!entry->free_) |
| 626 | entry->get_vm_obj() |
| 627 | ->reserve_const_data(vmg_ const_mapper, id); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /* prepare the constant mapper to begin storing */ |
| 632 | const_mapper->prepare_to_store_data(); |
| 633 | |
| 634 | /* |
| 635 | * Second pass: go through the objects once again and make the |
| 636 | * conversions. We must do this on a separate second pass because |
| 637 | * we must fix up all references to objects being converted, hence |
| 638 | * we must know the conversion status of every object to be |
| 639 | * converted before we can fix up anything. |
| 640 | */ |
| 641 | for (id = 0, i = pages_used_, pg = pages_ ; i > 0 ; ++pg, --i) |
| 642 | { |
| 643 | size_t j; |
| 644 | CVmObjPageEntry *entry; |
| 645 | |
| 646 | /* start at the start of the page, but skip object ID = 0 */ |
| 647 | j = VM_OBJ_PAGE_CNT; |
| 648 | entry = *pg; |
| 649 | |
| 650 | /* go through each entry on this page */ |
| 651 | for ( ; j > 0 ; --j, ++entry, ++id) |
| 652 | { |
| 653 | /* if this entry is in use, convert it if possible */ |
| 654 | if (!entry->free_) |
| 655 | entry->get_vm_obj() |
| 656 | ->convert_to_const_data(vmg_ const_mapper, id); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /* ------------------------------------------------------------------------ */ |
| 662 | /* |
| 663 | * Convert a value to constant data. If the value is an object that has |
| 664 | * reserved constant data space for itself, we'll update the value to |
| 665 | * reflect the constant data conversion for the value. |
| 666 | */ |
| 667 | static void convert_dh_to_const_data(VMG_ CVmConstMapper *mapper, char *p) |
| 668 | { |
| 669 | /* if the value is an object, check for conversion */ |
| 670 | if (vmb_get_dh_type(p) == VM_OBJ) |
| 671 | { |
| 672 | vm_obj_id_t obj; |
| 673 | ulong addr; |
| 674 | |
| 675 | /* get the object value */ |
| 676 | obj = vmb_get_dh_obj(p); |
| 677 | |
| 678 | /* look up the object's converted constant pool address */ |
| 679 | if ((addr = mapper->get_pool_addr(obj)) != 0) |
| 680 | { |
| 681 | vm_val_t val; |
| 682 | |
| 683 | /* this value has been converted - set the new value */ |
| 684 | val.typ = vm_objp(vmg_ obj)->get_convert_to_const_data_type(); |
| 685 | val.val.ofs = addr; |
| 686 | vmb_put_dh(p, &val); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Convert a vm_val_t value to constant data if appropriate. |
| 693 | */ |
| 694 | static void convert_val_to_const_data(VMG_ CVmConstMapper *mapper, |
| 695 | vm_val_t *val) |
| 696 | { |
| 697 | /* if it's an object, check for conversion */ |
| 698 | if (val->typ == VM_OBJ) |
| 699 | { |
| 700 | ulong addr; |
| 701 | |
| 702 | /* look up the object's converted constant pool address */ |
| 703 | if ((addr = mapper->get_pool_addr(val->val.obj)) != 0) |
| 704 | { |
| 705 | /* this value has been converted - set the new value */ |
| 706 | val->typ = vm_objp(vmg_ val->val.obj) |
| 707 | ->get_convert_to_const_data_type(); |
| 708 | val->val.ofs = addr; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /* ------------------------------------------------------------------------ */ |
| 714 | /* |
| 715 | * List Metaclass implementation - image rebuilding |
| 716 | */ |
| 717 | |
| 718 | /* |
| 719 | * Build an image file record for the list. We need this because we |
| 720 | * can't always convert a list to a constant value when rebuilding an |
| 721 | * image file; in particular, if the list exceeds the size of a constant |
| 722 | * pool page (unlikely but possible), we will have to store the list as |
| 723 | * object data. |
| 724 | */ |
| 725 | ulong CVmObjList::rebuild_image(VMG_ char *buf, ulong buflen) |
| 726 | { |
| 727 | size_t copy_size; |
| 728 | |
| 729 | /* calculate how much space we need to store the data */ |
| 730 | copy_size = calc_alloc(vmb_get_len(ext_)); |
| 731 | |
| 732 | /* make sure we have room for our data */ |
| 733 | if (copy_size > buflen) |
| 734 | return copy_size; |
| 735 | |
| 736 | /* copy the data */ |
| 737 | memcpy(buf, ext_, copy_size); |
| 738 | |
| 739 | /* return the size */ |
| 740 | return copy_size; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Reserve space in a rebuilt constant pool for converting our data to a |
| 745 | * constant value. |
| 746 | */ |
| 747 | void CVmObjList::reserve_const_data(VMG_ CVmConstMapper *mapper, |
| 748 | vm_obj_id_t self) |
| 749 | { |
| 750 | /* reserve the space for our list data */ |
| 751 | mapper->alloc_pool_space(self, calc_alloc(vmb_get_len(ext_))); |
| 752 | } |
| 753 | |
| 754 | |
| 755 | /* |
| 756 | * Convert to constant data. We must check each object that we |
| 757 | * reference via a modified property and convert it to a constant data |
| 758 | * item if necessary. Then, we must store our data in the constant |
| 759 | * pool, if we successfully reserved an address for it. |
| 760 | */ |
| 761 | void CVmObjList::convert_to_const_data(VMG_ CVmConstMapper *mapper, |
| 762 | vm_obj_id_t self) |
| 763 | { |
| 764 | size_t cnt; |
| 765 | char *p; |
| 766 | |
| 767 | /* get my element count */ |
| 768 | cnt = vmb_get_len(ext_); |
| 769 | |
| 770 | /* mark as referenced each object in our list */ |
| 771 | for (p = get_element_ptr(0) ; cnt != 0 ; --cnt, inc_element_ptr(&p)) |
| 772 | { |
| 773 | /* convert the value to constant data if possible */ |
| 774 | convert_dh_to_const_data(vmg_ mapper, p); |
| 775 | } |
| 776 | |
| 777 | /* |
| 778 | * if we managed to convert our object to constant data, store our |
| 779 | * value |
| 780 | */ |
| 781 | if (mapper->get_pool_addr(self)) |
| 782 | mapper->store_data(self, ext_, calc_alloc(vmb_get_len(ext_))); |
| 783 | } |
| 784 | |
| 785 | |
| 786 | /* ------------------------------------------------------------------------ */ |
| 787 | /* |
| 788 | * String Metaclass implementation - image rebuilding |
| 789 | */ |
| 790 | |
| 791 | /* |
| 792 | * Build an image file record for the string. We need this because we |
| 793 | * can't always convert a string to a constant value when rebuilding an |
| 794 | * image file; in particular, if the string exceeds the size of a |
| 795 | * constant pool page (unlikely but possible), we will have to store the |
| 796 | * string as object data. |
| 797 | */ |
| 798 | ulong CVmObjString::rebuild_image(VMG_ char *buf, ulong buflen) |
| 799 | { |
| 800 | size_t copy_size; |
| 801 | |
| 802 | /* calculate how much space we need to store the data */ |
| 803 | copy_size = vmb_get_len(ext_) + VMB_LEN; |
| 804 | |
| 805 | /* make sure we have room for our data */ |
| 806 | if (copy_size > buflen) |
| 807 | return copy_size; |
| 808 | |
| 809 | /* copy the data */ |
| 810 | memcpy(buf, ext_, copy_size); |
| 811 | |
| 812 | /* return the size */ |
| 813 | return copy_size; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Reserve space in a rebuilt constant pool for converting our data to a |
| 818 | * constant value. |
| 819 | */ |
| 820 | void CVmObjString::reserve_const_data(VMG_ CVmConstMapper *mapper, |
| 821 | vm_obj_id_t self) |
| 822 | { |
| 823 | /* reserve the space for our string data */ |
| 824 | mapper->alloc_pool_space(self, vmb_get_len(ext_) + VMB_LEN); |
| 825 | } |
| 826 | |
| 827 | |
| 828 | /* |
| 829 | * Convert to constant data. We don't reference any objects, so we need |
| 830 | * simply store our data in the constant pool, if we successfully |
| 831 | * reserved an address for it. |
| 832 | */ |
| 833 | void CVmObjString::convert_to_const_data(VMG_ CVmConstMapper *mapper, |
| 834 | vm_obj_id_t self) |
| 835 | { |
| 836 | /* |
| 837 | * if we managed to convert our object to constant data, store our |
| 838 | * value |
| 839 | */ |
| 840 | if (mapper->get_pool_addr(self)) |
| 841 | mapper->store_data(self, ext_, vmb_get_len(ext_) + VMB_LEN); |
| 842 | } |
| 843 | |
| 844 | |
| 845 | /* ------------------------------------------------------------------------ */ |
| 846 | /* |
| 847 | * TADS Object implementation - image rebuilding |
| 848 | */ |
| 849 | |
| 850 | |
| 851 | /* property comparison callback for qsort() */ |
| 852 | extern "C" |
| 853 | { |
| 854 | static int prop_compare(const void *p1, const void *p2) |
| 855 | { |
| 856 | uint id1, id2; |
| 857 | |
| 858 | /* get the ID's */ |
| 859 | id1 = osrp2(p1); |
| 860 | id2 = osrp2(p2); |
| 861 | |
| 862 | /* compare them and return the result */ |
| 863 | return (id1 < id2 ? -1 : id1 == id2 ? 0 : 1); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* |
| 868 | * build an image file record for the object |
| 869 | */ |
| 870 | ulong CVmObjTads::rebuild_image(VMG_ char *buf, ulong buflen) |
| 871 | { |
| 872 | /* |
| 873 | * Make sure the buffer is big enough. Start out with worst-case |
| 874 | * assumption that we'll need every allocated property slot; we might |
| 875 | * actually need fewer, since some of the slots could be empty by |
| 876 | * virtue of having been undone. We need space for our own header |
| 877 | * (UINT2 superclass count, UINT2 property count, UINT2 flags), plus a |
| 878 | * UINT4 per superclass, plus a (UINT2 + DATAHOLDER) per property. |
| 879 | */ |
| 880 | size_t max_size = (2 + 2 + 2) |
| 881 | + get_sc_count() * 4 |
| 882 | + (get_hdr()->prop_entry_free * 7); |
| 883 | |
| 884 | /* if it's more than we have available, ask for more space */ |
| 885 | if (max_size > buflen) |
| 886 | return max_size; |
| 887 | |
| 888 | /* |
| 889 | * set up our header - use a placeholder 0 for the property count |
| 890 | * for now, until we calculate the real value |
| 891 | */ |
| 892 | oswp2(buf, get_sc_count()); |
| 893 | oswp2(buf+2, 0); |
| 894 | oswp2(buf+4, get_li_obj_flags()); |
| 895 | char *p = buf + 6; |
| 896 | |
| 897 | /* copy the superclass list */ |
| 898 | for (size_t i = 0 ; i < get_sc_count() ; ++i, p += 4) |
| 899 | oswp4(p, get_sc(i)); |
| 900 | |
| 901 | /* remember where the properties start */ |
| 902 | char *props = p; |
| 903 | |
| 904 | /* copy the non-empty property slots */ |
| 905 | vm_tadsobj_prop *entry = get_hdr()->prop_entry_arr; |
| 906 | size_t prop_cnt = 0; |
| 907 | for (size_t i = get_hdr()->prop_entry_free ; i != 0 ; --i, ++entry) |
| 908 | { |
| 909 | /* if this slot is non-empty, store it */ |
| 910 | if (entry->val.typ != VM_EMPTY) |
| 911 | { |
| 912 | /* set up this slot */ |
| 913 | oswp2(p, entry->prop); |
| 914 | vmb_put_dh(p + 2, &entry->val); |
| 915 | |
| 916 | /* count the additional property */ |
| 917 | ++prop_cnt; |
| 918 | |
| 919 | /* move past it in the buffer */ |
| 920 | p += 7; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | /* fill in actual the property count now that we know it */ |
| 925 | oswp2(buf+2, prop_cnt); |
| 926 | |
| 927 | /* sort the new table */ |
| 928 | qsort(props, prop_cnt, 7, &prop_compare); |
| 929 | |
| 930 | /* return the final size */ |
| 931 | return p - buf; |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * Convert to constant data. We must check each object that we |
| 936 | * reference via a modified property and convert it to a constant data |
| 937 | * item if necessary. |
| 938 | */ |
| 939 | void CVmObjTads::convert_to_const_data(VMG_ CVmConstMapper *mapper, |
| 940 | vm_obj_id_t /*self*/) |
| 941 | { |
| 942 | size_t i; |
| 943 | vm_tadsobj_prop *entry; |
| 944 | |
| 945 | /* |
| 946 | * Scan the property entries. Note that we don't have to worry about |
| 947 | * the original properties, since they can only refer to data that was |
| 948 | * in the original image file, and hence never need to be converted -- |
| 949 | * if it was good enough for the original image file, it's good enough |
| 950 | * for us. |
| 951 | */ |
| 952 | for (i = get_hdr()->prop_entry_free, entry = get_hdr()->prop_entry_arr ; |
| 953 | i != 0 ; --i, ++entry) |
| 954 | { |
| 955 | /* if this slot is modified, convert it */ |
| 956 | if ((entry->flags & VMTO_PROP_MOD) != 0 |
| 957 | && entry->val.typ != VM_EMPTY) |
| 958 | { |
| 959 | /* convert the value */ |
| 960 | convert_val_to_const_data(vmg_ mapper, &entry->val); |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | |
| 966 | /* ------------------------------------------------------------------------ */ |
| 967 | /* |
| 968 | * Dictionary object implementation - image rebuilding |
| 969 | */ |
| 970 | |
| 971 | /* |
| 972 | * callback context for image rebuild |
| 973 | */ |
| 974 | struct rebuild_ctx |
| 975 | { |
| 976 | /* space needed so far */ |
| 977 | size_t space_needed; |
| 978 | |
| 979 | /* number of entries */ |
| 980 | size_t entry_cnt; |
| 981 | |
| 982 | /* next available output pointer */ |
| 983 | char *dst; |
| 984 | }; |
| 985 | |
| 986 | /* |
| 987 | * rebuild for image file |
| 988 | */ |
| 989 | ulong CVmObjDict::rebuild_image(VMG_ char *buf, ulong buflen) |
| 990 | { |
| 991 | rebuild_ctx ctx; |
| 992 | |
| 993 | /* |
| 994 | * calculate the amount of space we need - start with the comparator |
| 995 | * object, which needs four bytes, and the entry count, which needs two |
| 996 | * bytes |
| 997 | */ |
| 998 | ctx.space_needed = 6; |
| 999 | |
| 1000 | /* enumerate the entries to count space */ |
| 1001 | ctx.entry_cnt = 0; |
| 1002 | get_ext()->hashtab_->enum_entries(&rebuild_cb_1, &ctx); |
| 1003 | |
| 1004 | /* if we need more space than is available, ask for more */ |
| 1005 | if (ctx.space_needed > buflen) |
| 1006 | return ctx.space_needed; |
| 1007 | |
| 1008 | /* write the comparator object and the entry count */ |
| 1009 | oswp4(buf, get_ext()->comparator_); |
| 1010 | oswp2(buf + 4, ctx.entry_cnt); |
| 1011 | |
| 1012 | /* start writing after the count */ |
| 1013 | ctx.dst = buf + 6; |
| 1014 | |
| 1015 | /* enumerate the entries to write to the image buffer */ |
| 1016 | get_ext()->hashtab_->enum_entries(&rebuild_cb_2, &ctx); |
| 1017 | |
| 1018 | /* return the size */ |
| 1019 | return ctx.dst - buf; |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | * enumeration callback - rebuild phase 1: count the space needed for |
| 1024 | * this table entry |
| 1025 | */ |
| 1026 | void CVmObjDict::rebuild_cb_1(void *ctx0, class CVmHashEntry *entry0) |
| 1027 | { |
| 1028 | rebuild_ctx *ctx = (rebuild_ctx *)ctx0; |
| 1029 | CVmHashEntryDict *entry = (CVmHashEntryDict *)entry0; |
| 1030 | vm_dict_entry *cur; |
| 1031 | |
| 1032 | /* |
| 1033 | * count the space needed for this string - one byte for the length |
| 1034 | * prefix, the bytes of the name string, and two bytes for the item |
| 1035 | * count |
| 1036 | */ |
| 1037 | ctx->space_needed += 1 + entry->getlen() + 2; |
| 1038 | |
| 1039 | /* count this entry */ |
| 1040 | ++ctx->entry_cnt; |
| 1041 | |
| 1042 | /* count the items */ |
| 1043 | for (cur = entry->get_head() ; cur != 0 ; cur = cur->nxt_) |
| 1044 | { |
| 1045 | /* |
| 1046 | * add the space for this item - four byte for the object ID, |
| 1047 | * two bytes for the property ID |
| 1048 | */ |
| 1049 | ctx->space_needed += 6; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * enumeration callback - rebuild phase 2: write the entries to the |
| 1055 | * image buffer |
| 1056 | */ |
| 1057 | void CVmObjDict::rebuild_cb_2(void *ctx0, class CVmHashEntry *entry0) |
| 1058 | { |
| 1059 | rebuild_ctx *ctx = (rebuild_ctx *)ctx0; |
| 1060 | CVmHashEntryDict *entry = (CVmHashEntryDict *)entry0; |
| 1061 | vm_dict_entry *cur; |
| 1062 | size_t cnt; |
| 1063 | char *p; |
| 1064 | size_t rem; |
| 1065 | |
| 1066 | /* count the entries in our list */ |
| 1067 | for (cnt = 0, cur = entry->get_head() ; cur != 0 ; |
| 1068 | cur = cur->nxt_, ++cnt) ; |
| 1069 | |
| 1070 | /* write the length of the key string followed by the key string */ |
| 1071 | *ctx->dst++ = (char)entry->getlen(); |
| 1072 | memcpy(ctx->dst, entry->getstr(), entry->getlen()); |
| 1073 | |
| 1074 | /* xor the key string's bytes */ |
| 1075 | for (p = ctx->dst, rem = entry->getlen() ; rem != 0 ; |
| 1076 | --rem, ++p) |
| 1077 | *p ^= 0xBD; |
| 1078 | |
| 1079 | /* move past the key string */ |
| 1080 | ctx->dst += entry->getlen(); |
| 1081 | |
| 1082 | /* write the item count */ |
| 1083 | oswp2(ctx->dst, cnt); |
| 1084 | ctx->dst += 2; |
| 1085 | |
| 1086 | /* write the items */ |
| 1087 | for (cur = entry->get_head() ; cur != 0 ; cur = cur->nxt_) |
| 1088 | { |
| 1089 | /* write the object ID and property ID for this item */ |
| 1090 | oswp4(ctx->dst, (ulong)cur->obj_); |
| 1091 | oswp2(ctx->dst + 4, (uint)cur->prop_); |
| 1092 | ctx->dst += 6; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * callback context for constant data conversion |
| 1098 | */ |
| 1099 | struct cvt_const_ctx |
| 1100 | { |
| 1101 | /* constant mapper */ |
| 1102 | CVmConstMapper *mapper; |
| 1103 | }; |
| 1104 | |
| 1105 | /* |
| 1106 | * convert to constant data |
| 1107 | */ |
| 1108 | void CVmObjDict::convert_to_const_data(VMG_ CVmConstMapper *mapper, |
| 1109 | vm_obj_id_t self) |
| 1110 | { |
| 1111 | cvt_const_ctx ctx; |
| 1112 | |
| 1113 | /* make sure the comparator object isn't mappable to a constant */ |
| 1114 | if (mapper->get_pool_addr(get_ext()->comparator_) != 0) |
| 1115 | err_throw_a(VMERR_DICT_NO_CONST, 1, ERR_TYPE_TEXTCHAR_LEN, |
| 1116 | "<comparator>", 12); |
| 1117 | |
| 1118 | /* |
| 1119 | * Go through our dictionary and make sure we don't have any |
| 1120 | * references to constant data. We don't actually have to perform |
| 1121 | * any conversions, because we simply don't allow references to |
| 1122 | * anything but TADS-object objects in the dictionary. |
| 1123 | */ |
| 1124 | ctx.mapper = mapper; |
| 1125 | get_ext()->hashtab_->enum_entries(&cvt_const_cb, &ctx); |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * enumeration callback - convert to constant data |
| 1130 | */ |
| 1131 | void CVmObjDict::cvt_const_cb(void *ctx0, class CVmHashEntry *entry0) |
| 1132 | { |
| 1133 | cvt_const_ctx *ctx = (cvt_const_ctx *)ctx0; |
| 1134 | CVmHashEntryDict *entry = (CVmHashEntryDict *)entry0; |
| 1135 | vm_dict_entry *cur; |
| 1136 | |
| 1137 | /* inspect the items in this entry's list */ |
| 1138 | for (cur = entry->get_head() ; cur != 0 ; cur = cur->nxt_) |
| 1139 | { |
| 1140 | /* if this item refers to constant data, it's an error */ |
| 1141 | if (ctx->mapper->get_pool_addr(cur->obj_) != 0) |
| 1142 | err_throw_a(VMERR_DICT_NO_CONST, 1, ERR_TYPE_TEXTCHAR_LEN, |
| 1143 | entry->getstr(), entry->getlen()); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | /* ------------------------------------------------------------------------ */ |
| 1148 | /* |
| 1149 | * Grammar production object - image rebuilding operations |
| 1150 | */ |
| 1151 | |
| 1152 | /* |
| 1153 | * rebuild for image file |
| 1154 | */ |
| 1155 | ulong CVmObjGramProd::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1156 | { |
| 1157 | if (get_ext()->modified_) |
| 1158 | { |
| 1159 | /* |
| 1160 | * We've been modified at run-time, so we need to rebuild the image |
| 1161 | * data from the current alt list. First, save to a counting |
| 1162 | * stream just to determine how much space we need. |
| 1163 | */ |
| 1164 | CVmCountingStream cstr; |
| 1165 | save_to_stream(vmg_ &cstr); |
| 1166 | |
| 1167 | /* make sure we have enough space */ |
| 1168 | if ((ulong)cstr.get_len() > buflen) |
| 1169 | return cstr.get_len(); |
| 1170 | |
| 1171 | /* save to the buffer */ |
| 1172 | CVmMemoryStream mstr(buf, buflen); |
| 1173 | save_to_stream(vmg_ &mstr); |
| 1174 | |
| 1175 | /* return the stream length */ |
| 1176 | return cstr.get_len(); |
| 1177 | } |
| 1178 | else |
| 1179 | { |
| 1180 | /* |
| 1181 | * We weren't modified, so simply copy back our original image data |
| 1182 | * unchanged. |
| 1183 | */ |
| 1184 | |
| 1185 | /* make sure we have room */ |
| 1186 | if (get_ext()->image_data_size_ > buflen) |
| 1187 | return get_ext()->image_data_size_; |
| 1188 | |
| 1189 | /* copy the data */ |
| 1190 | memcpy(buf, get_ext()->image_data_, get_ext()->image_data_size_); |
| 1191 | |
| 1192 | /* return the size */ |
| 1193 | return get_ext()->image_data_size_; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | /* ------------------------------------------------------------------------ */ |
| 1198 | /* |
| 1199 | * BigNumber Metaclass implementation - image rebuilding |
| 1200 | */ |
| 1201 | |
| 1202 | /* |
| 1203 | * Build an image file record |
| 1204 | */ |
| 1205 | ulong CVmObjBigNum::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1206 | { |
| 1207 | size_t copy_size; |
| 1208 | |
| 1209 | /* calculate how much space we need to store the data */ |
| 1210 | copy_size = calc_alloc(get_prec(ext_)); |
| 1211 | |
| 1212 | /* make sure we have room for our data */ |
| 1213 | if (copy_size > buflen) |
| 1214 | return copy_size; |
| 1215 | |
| 1216 | /* copy the data */ |
| 1217 | memcpy(buf, ext_, copy_size); |
| 1218 | |
| 1219 | /* return the size */ |
| 1220 | return copy_size; |
| 1221 | } |
| 1222 | |
| 1223 | /* |
| 1224 | * Reserve space in a rebuilt constant pool for converting our data to a |
| 1225 | * constant value. |
| 1226 | */ |
| 1227 | void CVmObjBigNum::reserve_const_data(VMG_ CVmConstMapper *mapper, |
| 1228 | vm_obj_id_t self) |
| 1229 | { |
| 1230 | /* BigNumber values cannot be converted to constants */ |
| 1231 | } |
| 1232 | |
| 1233 | |
| 1234 | /* |
| 1235 | * Convert to constant data. We don't reference any other objects, so |
| 1236 | * we simply need to store our data in the constant pool, if we |
| 1237 | * successfully reserved an address for it. |
| 1238 | */ |
| 1239 | void CVmObjBigNum::convert_to_const_data(VMG_ CVmConstMapper *mapper, |
| 1240 | vm_obj_id_t self) |
| 1241 | { |
| 1242 | /* |
| 1243 | * if we managed to convert our object to constant data, store our |
| 1244 | * value |
| 1245 | */ |
| 1246 | if (mapper->get_pool_addr(self)) |
| 1247 | mapper->store_data(self, ext_, calc_alloc(get_prec(ext_))); |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | /* ------------------------------------------------------------------------ */ |
| 1252 | /* |
| 1253 | * Object-to-Constant mapper |
| 1254 | */ |
| 1255 | |
| 1256 | /* |
| 1257 | * initialize |
| 1258 | */ |
| 1259 | CVmConstMapper::CVmConstMapper(VMG0_) |
| 1260 | { |
| 1261 | vm_obj_id_t max_id; |
| 1262 | size_t i; |
| 1263 | |
| 1264 | /* get the maximum object ID we've allocated */ |
| 1265 | max_id = G_obj_table->get_max_used_obj_id(); |
| 1266 | |
| 1267 | /* |
| 1268 | * Allocate our master translation page list so that we have room |
| 1269 | * for enough pages to store the maximum object ID. We need one |
| 1270 | * slot for each page of 1024 translation elements. |
| 1271 | */ |
| 1272 | obj_addr_cnt_ = max_id / 1024; |
| 1273 | obj_addr_ = (ulong **)t3malloc(obj_addr_cnt_ * sizeof(obj_addr_[0])); |
| 1274 | if (obj_addr_ == 0) |
| 1275 | err_throw(VMERR_OUT_OF_MEMORY); |
| 1276 | |
| 1277 | /* clear out the page slots - we haven't allocated any pages yet */ |
| 1278 | for (i = 0 ; i < obj_addr_cnt_ ; ++i) |
| 1279 | obj_addr_[i] = 0; |
| 1280 | |
| 1281 | /* get the constant pool's page size */ |
| 1282 | page_size_ = G_const_pool->get_page_size(); |
| 1283 | |
| 1284 | /* |
| 1285 | * our first page is the next page after the last page in the |
| 1286 | * current pool |
| 1287 | */ |
| 1288 | first_page_idx_ = G_const_pool->get_page_count(); |
| 1289 | |
| 1290 | /* |
| 1291 | * get the starting address - we'll start writing our data at the |
| 1292 | * first page after all existing pages in the pool |
| 1293 | */ |
| 1294 | base_addr_ = page_size_ * G_const_pool->get_page_count(); |
| 1295 | |
| 1296 | /* allocate from our base address */ |
| 1297 | next_free_ = base_addr_; |
| 1298 | |
| 1299 | /* we have the entire first page available */ |
| 1300 | rem_ = page_size_; |
| 1301 | |
| 1302 | /* |
| 1303 | * we haven't allocated any page data yet (we'll do this after we've |
| 1304 | * reserved all space, when the client invokes |
| 1305 | * prepare_to_store_data()) |
| 1306 | */ |
| 1307 | pages_ = 0; |
| 1308 | pages_cnt_ = 0; |
| 1309 | } |
| 1310 | |
| 1311 | /* |
| 1312 | * delete |
| 1313 | */ |
| 1314 | CVmConstMapper::~CVmConstMapper() |
| 1315 | { |
| 1316 | size_t i; |
| 1317 | |
| 1318 | /* delete each page in our mapping table */ |
| 1319 | for (i = 0 ; i < obj_addr_cnt_ ; ++i) |
| 1320 | { |
| 1321 | /* free this page if we allocated it */ |
| 1322 | if (obj_addr_[i] != 0) |
| 1323 | t3free(obj_addr_[i]); |
| 1324 | } |
| 1325 | |
| 1326 | /* delete our mapping table */ |
| 1327 | t3free(obj_addr_); |
| 1328 | |
| 1329 | /* delete each constant pool page */ |
| 1330 | for (i = 0 ; i < pages_cnt_ ; ++i) |
| 1331 | t3free(pages_[i]); |
| 1332 | |
| 1333 | /* free the page list */ |
| 1334 | if (pages_ != 0) |
| 1335 | t3free(pages_); |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * Get an object's pool address, if it has one |
| 1340 | */ |
| 1341 | ulong CVmConstMapper::get_pool_addr(vm_obj_id_t obj_id) |
| 1342 | { |
| 1343 | /* determine if the page is mapped - if not, the object isn't mapped */ |
| 1344 | if (obj_addr_[obj_id / 1024] == 0) |
| 1345 | return 0; |
| 1346 | |
| 1347 | /* return the mapping entry */ |
| 1348 | return obj_addr_[obj_id / 1024][obj_id % 1024]; |
| 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | /* |
| 1353 | * Allocate space in the pool for an object's data |
| 1354 | */ |
| 1355 | ulong CVmConstMapper::alloc_pool_space(vm_obj_id_t obj_id, size_t len) |
| 1356 | { |
| 1357 | ulong ret; |
| 1358 | |
| 1359 | /* |
| 1360 | * if the data block is too large to fit on a constant pool page, we |
| 1361 | * can't store it |
| 1362 | */ |
| 1363 | if (len > page_size_) |
| 1364 | return 0; |
| 1365 | |
| 1366 | /* if the translation page isn't mapped yet, map it */ |
| 1367 | if (obj_addr_[obj_id / 1024] == 0) |
| 1368 | { |
| 1369 | /* allocate a new page */ |
| 1370 | obj_addr_[obj_id / 1024] = |
| 1371 | (ulong *)t3malloc(1024 * sizeof(obj_addr_[0][0])); |
| 1372 | |
| 1373 | /* if that failed, throw an error */ |
| 1374 | if (obj_addr_[obj_id / 1024] == 0) |
| 1375 | err_throw(VMERR_OUT_OF_MEMORY); |
| 1376 | |
| 1377 | /* clear the new page */ |
| 1378 | memset(obj_addr_[obj_id / 1024], 0, 1024 * sizeof(obj_addr_[0][0])); |
| 1379 | } |
| 1380 | |
| 1381 | /* if the block doesn't fit on this page, skip to the next page */ |
| 1382 | if (len > rem_) |
| 1383 | { |
| 1384 | /* skip past the remainder of this page */ |
| 1385 | next_free_ += rem_; |
| 1386 | |
| 1387 | /* we have the whole next page available now */ |
| 1388 | rem_ = page_size_; |
| 1389 | } |
| 1390 | |
| 1391 | /* remember the object ID's address in the translation list */ |
| 1392 | ret = obj_addr_[obj_id / 1024][obj_id % 1024] = next_free_; |
| 1393 | |
| 1394 | /* skip past the data */ |
| 1395 | next_free_ += len; |
| 1396 | rem_ -= len; |
| 1397 | |
| 1398 | /* return the new address */ |
| 1399 | return ret; |
| 1400 | } |
| 1401 | |
| 1402 | /* |
| 1403 | * Prepare to begin storing data |
| 1404 | */ |
| 1405 | void CVmConstMapper::prepare_to_store_data() |
| 1406 | { |
| 1407 | size_t i; |
| 1408 | |
| 1409 | /* figure out how many pages we need */ |
| 1410 | pages_cnt_ = calc_page_count(); |
| 1411 | |
| 1412 | /* allocate space for the list of pages */ |
| 1413 | pages_ = (vm_const_mapper_page **) |
| 1414 | t3malloc(pages_cnt_ * sizeof(pages_[0])); |
| 1415 | if (pages_ == 0) |
| 1416 | err_throw(VMERR_OUT_OF_MEMORY); |
| 1417 | |
| 1418 | /* allocate each page */ |
| 1419 | for (i = 0 ; i < pages_cnt_ ; ++i) |
| 1420 | { |
| 1421 | /* |
| 1422 | * allocate this page - allocate the header structure plus space |
| 1423 | * for the page data |
| 1424 | */ |
| 1425 | pages_[i] = (vm_const_mapper_page *) |
| 1426 | t3malloc(sizeof(pages_[i]) + page_size_); |
| 1427 | if (pages_[i] == 0) |
| 1428 | err_throw(VMERR_OUT_OF_MEMORY); |
| 1429 | |
| 1430 | /* the page has nothing stored yet */ |
| 1431 | pages_[i]->max_ofs_used = 0; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * Store an object's data |
| 1437 | */ |
| 1438 | void CVmConstMapper::store_data(vm_obj_id_t obj_id, |
| 1439 | const void *ptr, size_t len) |
| 1440 | { |
| 1441 | ulong addr; |
| 1442 | size_t page_idx; |
| 1443 | size_t page_ofs; |
| 1444 | |
| 1445 | /* get the pool address that was reserved for the object */ |
| 1446 | addr = get_pool_addr(obj_id); |
| 1447 | |
| 1448 | /* if the object had no space reserved, ignore the request */ |
| 1449 | if (addr == 0) |
| 1450 | return; |
| 1451 | |
| 1452 | /* figure out which page this address is in, and the offset in the page */ |
| 1453 | page_idx = (size_t)((addr - base_addr_) / page_size_); |
| 1454 | page_ofs = (size_t)((addr - base_addr_) % page_size_); |
| 1455 | |
| 1456 | /* |
| 1457 | * if this address takes us above the high-water mark for the page, |
| 1458 | * move the page's marker accordingly |
| 1459 | */ |
| 1460 | if (page_ofs + len > pages_[page_idx]->max_ofs_used) |
| 1461 | pages_[page_idx]->max_ofs_used = page_ofs + len; |
| 1462 | |
| 1463 | /* copy the data */ |
| 1464 | memcpy(pages_[page_idx]->buf + page_ofs, ptr, len); |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * Write the pool pages to an image file |
| 1469 | */ |
| 1470 | void CVmConstMapper::write_to_image_file(CVmImageWriter *writer, |
| 1471 | uchar xor_mask) |
| 1472 | { |
| 1473 | size_t i; |
| 1474 | |
| 1475 | /* go through each of our pages */ |
| 1476 | for (i = 0 ; i < pages_cnt_ ; ++i) |
| 1477 | { |
| 1478 | /* write this page - it's in pool 2 (the constant data pool) */ |
| 1479 | writer->write_pool_page(2, first_page_idx_ + i, |
| 1480 | pages_[i]->buf, pages_[i]->max_ofs_used, |
| 1481 | TRUE, xor_mask); |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | /* ------------------------------------------------------------------------ */ |
| 1486 | /* |
| 1487 | * metaclass table - image rewriter implementation |
| 1488 | */ |
| 1489 | |
| 1490 | /* |
| 1491 | * write the new metaclass dependency table |
| 1492 | */ |
| 1493 | void CVmMetaTable::rebuild_image(CVmImageWriter *writer) |
| 1494 | { |
| 1495 | size_t i; |
| 1496 | |
| 1497 | /* begin the new metaclass dependency table */ |
| 1498 | writer->begin_meta_dep(get_count()); |
| 1499 | |
| 1500 | /* write the new metaclass dependency table items */ |
| 1501 | for (i = 0 ; i < get_count() ; ++i) |
| 1502 | { |
| 1503 | vm_meta_entry_t *entry; |
| 1504 | ushort j; |
| 1505 | |
| 1506 | /* get this entry */ |
| 1507 | entry = get_entry(i); |
| 1508 | |
| 1509 | /* write this metaclass name */ |
| 1510 | writer->write_meta_dep_item(entry->image_meta_name_); |
| 1511 | |
| 1512 | /* |
| 1513 | * Write the property translation list. Note that xlat_func() |
| 1514 | * requires a 1-based index, so we loop from 1 to the count, |
| 1515 | * rather than following the usual C-style 0-based conventions. |
| 1516 | */ |
| 1517 | for (j = 1 ; j <= entry->func_xlat_cnt_ ; ++j) |
| 1518 | writer->write_meta_item_prop(entry->xlat_func(j)); |
| 1519 | } |
| 1520 | |
| 1521 | /* end the metaclass dependency table */ |
| 1522 | writer->end_meta_dep(); |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | /* ------------------------------------------------------------------------ */ |
| 1527 | /* |
| 1528 | * Hashtable Metaclass implementation - image rebuilding |
| 1529 | */ |
| 1530 | |
| 1531 | /* |
| 1532 | * Build an image file record |
| 1533 | */ |
| 1534 | ulong CVmObjLookupTable::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1535 | { |
| 1536 | size_t copy_size; |
| 1537 | vm_lookup_ext *ext = get_ext(); |
| 1538 | uint i; |
| 1539 | vm_lookup_val **bp; |
| 1540 | vm_lookup_val *val; |
| 1541 | char *dst; |
| 1542 | uint idx; |
| 1543 | |
| 1544 | /* |
| 1545 | * we need space for the fixed header (6 bytes), 2 bytes per bucket, |
| 1546 | * the entries themselves, and the default value |
| 1547 | */ |
| 1548 | copy_size = 6 |
| 1549 | + (ext->bucket_cnt * 2) |
| 1550 | + (ext->value_cnt * VMLOOKUP_VALUE_SIZE) |
| 1551 | + VMB_DATAHOLDER; |
| 1552 | |
| 1553 | /* make sure we have room for our data */ |
| 1554 | if (copy_size > buflen) |
| 1555 | return copy_size; |
| 1556 | |
| 1557 | /* write the fixed data */ |
| 1558 | oswp2(buf, ext->bucket_cnt); |
| 1559 | oswp2(buf + 2, ext->value_cnt); |
| 1560 | idx = ext->val_to_img_idx(ext->first_free); |
| 1561 | oswp2(buf + 4, idx); |
| 1562 | dst = buf + 6; |
| 1563 | |
| 1564 | /* write the buckets */ |
| 1565 | for (i = ext->bucket_cnt, bp = ext->buckets ; i != 0 ; --i, ++bp) |
| 1566 | { |
| 1567 | /* write this bucket's index */ |
| 1568 | idx = ext->val_to_img_idx(*bp); |
| 1569 | oswp2(dst, idx); |
| 1570 | dst += 2; |
| 1571 | } |
| 1572 | |
| 1573 | /* write the values */ |
| 1574 | for (i = ext->value_cnt, val = ext->idx_to_val(0) ; i != 0 ; --i, ++val) |
| 1575 | { |
| 1576 | /* store the key, value, and index */ |
| 1577 | vmb_put_dh(dst, &val->key); |
| 1578 | vmb_put_dh(dst + VMB_DATAHOLDER, &val->val); |
| 1579 | idx = ext->val_to_img_idx(val->nxt); |
| 1580 | oswp2(dst + VMB_DATAHOLDER*2, idx); |
| 1581 | |
| 1582 | /* skip the data in the output */ |
| 1583 | dst += VMLOOKUP_VALUE_SIZE; |
| 1584 | } |
| 1585 | |
| 1586 | /* write the default value */ |
| 1587 | vmb_put_dh(dst, &ext->default_value); |
| 1588 | dst += VMB_DATAHOLDER; |
| 1589 | |
| 1590 | /* return the size */ |
| 1591 | return copy_size; |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * Convert to constant data. We must convert each object we reference in |
| 1596 | * a key or in a value to constant data if possible. |
| 1597 | */ |
| 1598 | void CVmObjLookupTable::convert_to_const_data(VMG_ CVmConstMapper *mapper, |
| 1599 | vm_obj_id_t self) |
| 1600 | { |
| 1601 | uint i; |
| 1602 | vm_lookup_val *entry; |
| 1603 | |
| 1604 | /* run through each entry */ |
| 1605 | for (i = get_entry_count(), entry = get_ext()->idx_to_val(0) ; |
| 1606 | i != 0 ; --i, ++entry) |
| 1607 | { |
| 1608 | /* convert the key */ |
| 1609 | convert_val_to_const_data(vmg_ mapper, &entry->key); |
| 1610 | |
| 1611 | /* convert the value */ |
| 1612 | convert_val_to_const_data(vmg_ mapper, &entry->val); |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | /* ------------------------------------------------------------------------ */ |
| 1617 | /* |
| 1618 | * StringBuffer metaclass - image rebuilding |
| 1619 | */ |
| 1620 | ulong CVmObjStringBuffer::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1621 | { |
| 1622 | size_t copy_size; |
| 1623 | vm_strbuf_ext *ext = get_ext(); |
| 1624 | |
| 1625 | /* get our size */ |
| 1626 | copy_size = 12 + ext->len*2; |
| 1627 | |
| 1628 | /* make sure we have room for our data */ |
| 1629 | if (copy_size > buflen) |
| 1630 | return copy_size; |
| 1631 | |
| 1632 | /* write the header data */ |
| 1633 | oswp4(buf, ext->alo); |
| 1634 | oswp4(buf+4, ext->inc); |
| 1635 | oswp4(buf+8, ext->len); |
| 1636 | |
| 1637 | /* write the string */ |
| 1638 | memcpy(buf+12, ext->buf, ext->len*2); |
| 1639 | |
| 1640 | /* return the size */ |
| 1641 | return copy_size; |
| 1642 | } |
| 1643 | |
| 1644 | |
| 1645 | /* ------------------------------------------------------------------------ */ |
| 1646 | /* |
| 1647 | * IntrinsicClass Metaclass implementation - image rebuilding |
| 1648 | */ |
| 1649 | |
| 1650 | /* |
| 1651 | * Build an image file record |
| 1652 | */ |
| 1653 | ulong CVmObjClass::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1654 | { |
| 1655 | /* figure our size requirement: meta idx, mod id, state value */ |
| 1656 | size_t copy_size = 2 + 2 + 4 + VMB_DATAHOLDER; |
| 1657 | |
| 1658 | /* make sure we have room for our data */ |
| 1659 | if (copy_size > buflen) |
| 1660 | return copy_size; |
| 1661 | |
| 1662 | /* copy the data */ |
| 1663 | vm_intcls_ext *ext = get_ext(); |
| 1664 | oswp2(buf, (short)copy_size); |
| 1665 | oswp2(buf + 2, ext->meta_idx); |
| 1666 | oswp4(buf + 4, ext->mod_obj); |
| 1667 | vmb_put_dh(buf + 8, &ext->class_state); |
| 1668 | |
| 1669 | /* return the size */ |
| 1670 | return copy_size; |
| 1671 | } |
| 1672 | |
| 1673 | /* ------------------------------------------------------------------------ */ |
| 1674 | /* |
| 1675 | * Indexed Iterator |
| 1676 | */ |
| 1677 | |
| 1678 | /* |
| 1679 | * Build an image file record |
| 1680 | */ |
| 1681 | ulong CVmObjIterIdx::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1682 | { |
| 1683 | /* calculate our data size - just store our entire extension */ |
| 1684 | size_t copy_size = VMOBJITERIDX_EXT_SIZE; |
| 1685 | |
| 1686 | /* make sure we have room for our data */ |
| 1687 | if (copy_size > buflen) |
| 1688 | return copy_size; |
| 1689 | |
| 1690 | /* copy the data */ |
| 1691 | memcpy(buf, ext_, copy_size); |
| 1692 | |
| 1693 | /* return the size */ |
| 1694 | return copy_size; |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * Convert to constant data. We must convert our collection object |
| 1699 | * reference to constant data if possible. |
| 1700 | */ |
| 1701 | void CVmObjIterIdx::convert_to_const_data(VMG_ CVmConstMapper *mapper, |
| 1702 | vm_obj_id_t self) |
| 1703 | { |
| 1704 | /* convert our collection reference to constant data if possible */ |
| 1705 | convert_dh_to_const_data(vmg_ mapper, ext_); |
| 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | /* ------------------------------------------------------------------------ */ |
| 1710 | /* |
| 1711 | * Hashtable Iterator |
| 1712 | */ |
| 1713 | |
| 1714 | /* |
| 1715 | * Build an image file record |
| 1716 | */ |
| 1717 | ulong CVmObjIterLookupTable::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1718 | { |
| 1719 | size_t copy_size; |
| 1720 | |
| 1721 | /* calculate our data size - just store our entire extension */ |
| 1722 | copy_size = VMOBJITERLOOKUPTABLE_EXT_SIZE; |
| 1723 | |
| 1724 | /* make sure we have room for our data */ |
| 1725 | if (copy_size > buflen) |
| 1726 | return copy_size; |
| 1727 | |
| 1728 | /* copy the data */ |
| 1729 | memcpy(buf, ext_, copy_size); |
| 1730 | |
| 1731 | /* return the size */ |
| 1732 | return copy_size; |
| 1733 | } |
| 1734 | |
| 1735 | /* |
| 1736 | * Convert to constant data. We must convert our collection object |
| 1737 | * reference to constant data if possible. |
| 1738 | */ |
| 1739 | void CVmObjIterLookupTable::convert_to_const_data( |
| 1740 | VMG_ CVmConstMapper *mapper, vm_obj_id_t self) |
| 1741 | { |
| 1742 | /* convert our collection reference to constant data if possible */ |
| 1743 | convert_dh_to_const_data(vmg_ mapper, ext_); |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | /* ------------------------------------------------------------------------ */ |
| 1748 | /* |
| 1749 | * Vector Metaclass implementation - image rebuilding |
| 1750 | */ |
| 1751 | |
| 1752 | /* |
| 1753 | * Build an image file record |
| 1754 | */ |
| 1755 | ulong CVmObjVector::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1756 | { |
| 1757 | size_t copy_size; |
| 1758 | size_t ele_cnt; |
| 1759 | size_t alloc_cnt; |
| 1760 | |
| 1761 | /* |
| 1762 | * calculate how much space we need to store the data - store only |
| 1763 | * the data, not the undo bits |
| 1764 | */ |
| 1765 | ele_cnt = get_element_count(); |
| 1766 | alloc_cnt = get_allocated_count(); |
| 1767 | copy_size = 2*VMB_LEN + calc_alloc_ele(ele_cnt); |
| 1768 | |
| 1769 | /* make sure we have room for our data */ |
| 1770 | if (copy_size > buflen) |
| 1771 | return copy_size; |
| 1772 | |
| 1773 | /* save the allocation count and in-use element count */ |
| 1774 | vmb_put_len(buf, alloc_cnt); |
| 1775 | vmb_put_len(buf + VMB_LEN, ele_cnt); |
| 1776 | |
| 1777 | /* copy the element data */ |
| 1778 | memcpy(buf + 2*VMB_LEN, get_element_ptr(0), calc_alloc_ele(ele_cnt)); |
| 1779 | |
| 1780 | /* return the size */ |
| 1781 | return copy_size; |
| 1782 | } |
| 1783 | |
| 1784 | /* |
| 1785 | * Reserve space in a rebuilt constant pool for converting our data to a |
| 1786 | * constant value. |
| 1787 | */ |
| 1788 | void CVmObjVector::reserve_const_data(VMG_ CVmConstMapper *mapper, |
| 1789 | vm_obj_id_t self) |
| 1790 | { |
| 1791 | /* Vector values cannot be converted to constants */ |
| 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | /* |
| 1796 | * Convert to constant data. We must convert each object we reference to |
| 1797 | * constant data if possible; we use the same algorithm as CVmObjList. |
| 1798 | */ |
| 1799 | void CVmObjVector::convert_to_const_data(VMG_ CVmConstMapper *mapper, |
| 1800 | vm_obj_id_t self) |
| 1801 | { |
| 1802 | size_t cnt; |
| 1803 | char *p; |
| 1804 | |
| 1805 | /* get my element count */ |
| 1806 | cnt = get_element_count(); |
| 1807 | |
| 1808 | /* mark as referenced each object in our list */ |
| 1809 | for (p = get_element_ptr(0) ; cnt != 0 ; --cnt, inc_element_ptr(&p)) |
| 1810 | { |
| 1811 | /* convert the value to constant data if possible */ |
| 1812 | convert_dh_to_const_data(vmg_ mapper, p); |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | /* ------------------------------------------------------------------------ */ |
| 1817 | /* |
| 1818 | * ByteArray Metaclass implementation - image rebuilding |
| 1819 | */ |
| 1820 | |
| 1821 | /* |
| 1822 | * Build an image file record |
| 1823 | */ |
| 1824 | ulong CVmObjByteArray::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1825 | { |
| 1826 | ulong copy_size; |
| 1827 | ulong rem; |
| 1828 | ulong idx; |
| 1829 | |
| 1830 | /* we need four bytes for our count plus space for our byte array */ |
| 1831 | copy_size = 4 + get_element_count(); |
| 1832 | |
| 1833 | /* make sure we have room for our data */ |
| 1834 | if (copy_size > buflen) |
| 1835 | return copy_size; |
| 1836 | |
| 1837 | /* store our element count */ |
| 1838 | oswp4(buf, get_element_count()); |
| 1839 | buf += 4; |
| 1840 | |
| 1841 | /* copy our data in chunks */ |
| 1842 | for (idx = 1, rem = get_element_count() ; rem != 0 ; ) |
| 1843 | { |
| 1844 | unsigned char *p; |
| 1845 | size_t avail; |
| 1846 | size_t chunk; |
| 1847 | |
| 1848 | /* get the next chunk */ |
| 1849 | p = get_ele_ptr(idx, &avail); |
| 1850 | |
| 1851 | /* limit copying to the remaining size */ |
| 1852 | chunk = avail; |
| 1853 | if (chunk > rem) |
| 1854 | chunk = rem; |
| 1855 | |
| 1856 | /* store this chunk */ |
| 1857 | memcpy(buf, p, chunk); |
| 1858 | |
| 1859 | /* skip this chunk */ |
| 1860 | buf += chunk; |
| 1861 | idx += chunk; |
| 1862 | rem -= chunk; |
| 1863 | } |
| 1864 | |
| 1865 | /* return the size */ |
| 1866 | return copy_size; |
| 1867 | } |
| 1868 | |
| 1869 | /* ------------------------------------------------------------------------ */ |
| 1870 | /* |
| 1871 | * CharacterSet Metaclass implementation - image rebuilding |
| 1872 | */ |
| 1873 | |
| 1874 | /* |
| 1875 | * Build an image file record |
| 1876 | */ |
| 1877 | ulong CVmObjCharSet::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1878 | { |
| 1879 | ulong copy_size; |
| 1880 | |
| 1881 | /* we need two bytes for the name length count plus the name's bytes */ |
| 1882 | copy_size = 2 + get_ext_ptr()->charset_name_len; |
| 1883 | |
| 1884 | /* make sure we have room for our data */ |
| 1885 | if (copy_size > buflen) |
| 1886 | return copy_size; |
| 1887 | |
| 1888 | /* store the length of the name, and the name itself */ |
| 1889 | oswp2(buf, get_ext_ptr()->charset_name_len); |
| 1890 | memcpy(buf + 2, get_ext_ptr()->charset_name, |
| 1891 | get_ext_ptr()->charset_name_len); |
| 1892 | |
| 1893 | /* return the size */ |
| 1894 | return copy_size; |
| 1895 | } |
| 1896 | |
| 1897 | /* ------------------------------------------------------------------------ */ |
| 1898 | /* |
| 1899 | * File Metaclass implementation - image rebuilding |
| 1900 | */ |
| 1901 | |
| 1902 | /* |
| 1903 | * Build an image file record |
| 1904 | */ |
| 1905 | ulong CVmObjFile::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1906 | { |
| 1907 | ulong copy_size; |
| 1908 | |
| 1909 | /* |
| 1910 | * we need the character set object ID, the mode byte, the access |
| 1911 | * byte, and the uint32 flags |
| 1912 | */ |
| 1913 | copy_size = VMB_OBJECT_ID + 1 + 1 + 4; |
| 1914 | |
| 1915 | /* make sure we have room for our data */ |
| 1916 | if (copy_size > buflen) |
| 1917 | return copy_size; |
| 1918 | |
| 1919 | /* store the character set object ID */ |
| 1920 | vmb_put_objid(buf, get_ext()->charset); |
| 1921 | buf += VMB_OBJECT_ID; |
| 1922 | |
| 1923 | /* store the mode and access values */ |
| 1924 | *buf++ = get_ext()->mode; |
| 1925 | *buf++ = get_ext()->access; |
| 1926 | |
| 1927 | /* store the flags */ |
| 1928 | oswp4(buf, get_ext()->flags); |
| 1929 | |
| 1930 | /* return the size */ |
| 1931 | return copy_size; |
| 1932 | } |
| 1933 | |
| 1934 | /* ------------------------------------------------------------------------ */ |
| 1935 | /* |
| 1936 | * CVmObjTemporaryFile intrinsic object |
| 1937 | */ |
| 1938 | ulong CVmObjTemporaryFile::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1939 | { |
| 1940 | /* we don't store any extra data */ |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
| 1944 | /* ------------------------------------------------------------------------ */ |
| 1945 | /* |
| 1946 | * CVmObjFileName intrinsic object |
| 1947 | */ |
| 1948 | ulong CVmObjFileName::rebuild_image(VMG_ char *buf, ulong buflen) |
| 1949 | { |
| 1950 | /* get my extension */ |
| 1951 | vm_filnam_ext *ext = get_ext(); |
| 1952 | |
| 1953 | /* |
| 1954 | * if we're going to store the filename, we'll store it in universal |
| 1955 | * notation; do the conversion first so we can figure the length |
| 1956 | */ |
| 1957 | char *uni = 0; |
| 1958 | ulong needlen; |
| 1959 | size_t unilen = 0; |
| 1960 | err_try |
| 1961 | { |
| 1962 | if (ext->sfid == 0) |
| 1963 | { |
| 1964 | uni = to_universal(vmg0_); |
| 1965 | unilen = strlen(uni); |
| 1966 | } |
| 1967 | |
| 1968 | /* |
| 1969 | * figure the required size: special file ID, plus the universal |
| 1970 | * name with length prefix if the sfid is zero |
| 1971 | */ |
| 1972 | needlen = 4 + (ext->sfid != 0 ? 0 : VMB_LEN + unilen); |
| 1973 | |
| 1974 | /* if we have room, store the data */ |
| 1975 | if (buflen >= needlen) |
| 1976 | { |
| 1977 | /* write the special file ID first */ |
| 1978 | oswp4s(buf, ext->sfid); |
| 1979 | buf += 4; |
| 1980 | |
| 1981 | /* if it's not a special file, store the universal name */ |
| 1982 | if (ext->sfid == 0) |
| 1983 | { |
| 1984 | vmb_put_len(buf, unilen); |
| 1985 | memcpy(buf + VMB_LEN, uni, unilen); |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | err_finally |
| 1990 | { |
| 1991 | lib_free_str(uni); |
| 1992 | } |
| 1993 | err_end; |
| 1994 | |
| 1995 | /* return the length */ |
| 1996 | return needlen; |
| 1997 | } |
| 1998 | |
| 1999 | /* ------------------------------------------------------------------------ */ |
| 2000 | /* |
| 2001 | * Pattern metaclass |
| 2002 | */ |
| 2003 | |
| 2004 | /* |
| 2005 | * build an image file record for the object |
| 2006 | */ |
| 2007 | ulong CVmObjPattern::rebuild_image(VMG_ char *buf, ulong buflen) |
| 2008 | { |
| 2009 | size_t need_size; |
| 2010 | |
| 2011 | /* we need a DATAHOLDER to store the original pattern string reference */ |
| 2012 | need_size = VMB_DATAHOLDER; |
| 2013 | |
| 2014 | /* if we need more space, just return our size requirements */ |
| 2015 | if (need_size > buflen) |
| 2016 | return need_size; |
| 2017 | |
| 2018 | /* write our value */ |
| 2019 | vmb_put_dh(buf, get_orig_str()); |
| 2020 | |
| 2021 | /* return our size */ |
| 2022 | return need_size; |
| 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * Convert to constant data. |
| 2027 | */ |
| 2028 | void CVmObjPattern::convert_to_const_data(VMG_ CVmConstMapper *mapper, |
| 2029 | vm_obj_id_t /*self*/) |
| 2030 | { |
| 2031 | /* check our original source string value */ |
| 2032 | convert_val_to_const_data(vmg_ mapper, &get_ext()->str); |
| 2033 | } |
| 2034 | |
| 2035 | /* ------------------------------------------------------------------------ */ |
| 2036 | /* |
| 2037 | * StringComparator intrinsic class |
| 2038 | */ |
| 2039 | |
| 2040 | /* |
| 2041 | * build the image data |
| 2042 | */ |
| 2043 | ulong CVmObjStrComp::rebuild_image(VMG_ char *buf, ulong buflen) |
| 2044 | { |
| 2045 | /* set up a stream writer on the buffer, and write it out */ |
| 2046 | CVmMemoryStream str(buf, buflen); |
| 2047 | return write_to_stream(vmg_ &str, &buflen); |
| 2048 | } |
| 2049 | |
| 2050 | /* ------------------------------------------------------------------------ */ |
| 2051 | /* |
| 2052 | * DynamicFunc intrinsic class |
| 2053 | */ |
| 2054 | |
| 2055 | /* |
| 2056 | * build the image data |
| 2057 | */ |
| 2058 | ulong CVmDynamicFunc::rebuild_image(VMG_ char *buf, ulong buflen) |
| 2059 | { |
| 2060 | /* get my extension */ |
| 2061 | vm_dynfunc_ext *ext = get_ext(); |
| 2062 | |
| 2063 | /* figure the required size */ |
| 2064 | ulong needlen = 2 + 2 + VMB_DATAHOLDER + VMB_DATAHOLDER |
| 2065 | + (ext->obj_ref_cnt * 2) |
| 2066 | + ext->bytecode_len; |
| 2067 | |
| 2068 | /* make sure there's room */ |
| 2069 | if (needlen > buflen) |
| 2070 | return needlen; |
| 2071 | |
| 2072 | /* save the sizes: bytecode length, object reference count */ |
| 2073 | oswp2(buf, ext->bytecode_len); |
| 2074 | oswp2(buf, ext->obj_ref_cnt); |
| 2075 | buf += 4; |
| 2076 | |
| 2077 | /* save the method context object */ |
| 2078 | vmb_put_dh(buf, &ext->method_ctx); |
| 2079 | buf += VMB_DATAHOLDER; |
| 2080 | |
| 2081 | /* save the source object value */ |
| 2082 | vmb_put_dh(buf, &ext->src); |
| 2083 | buf += VMB_DATAHOLDER; |
| 2084 | |
| 2085 | /* save the byte code */ |
| 2086 | memcpy(buf, ext->get_bytecode_ptr(), ext->bytecode_len); |
| 2087 | buf += ext->bytecode_len; |
| 2088 | |
| 2089 | /* save the object reference list */ |
| 2090 | for (int i = 0 ; i < ext->obj_ref_cnt ; ++i, buf += 2) |
| 2091 | oswp2(buf, ext->obj_refs[i]); |
| 2092 | |
| 2093 | /* return the length */ |
| 2094 | return needlen; |
| 2095 | } |
| 2096 | |
| 2097 | /* ------------------------------------------------------------------------ */ |
| 2098 | /* |
| 2099 | * Stack Frame Descriptor intrinsic object |
| 2100 | */ |
| 2101 | ulong CVmObjFrameDesc::rebuild_image(VMG_ char *buf, ulong buflen) |
| 2102 | { |
| 2103 | /* get my extension */ |
| 2104 | vm_framedesc_ext *ext = get_ext(); |
| 2105 | |
| 2106 | /* figure the required size */ |
| 2107 | ulong needlen = VMB_OBJECT_ID + 2 + 2; |
| 2108 | |
| 2109 | /* make sure there's room */ |
| 2110 | if (needlen > buflen) |
| 2111 | return needlen; |
| 2112 | |
| 2113 | /* save the underlying StackFrameRef reference */ |
| 2114 | vmb_put_objid(buf, ext->fref); |
| 2115 | buf += VMB_OBJECT_ID; |
| 2116 | |
| 2117 | /* save the frame index and return address */ |
| 2118 | oswp2(buf, ext->frame_idx); |
| 2119 | oswp2(buf + 2, ext->ret_ofs); |
| 2120 | buf += 4; |
| 2121 | |
| 2122 | /* return the length */ |
| 2123 | return needlen; |
| 2124 | } |
| 2125 | |
| 2126 | |
| 2127 | /* ------------------------------------------------------------------------ */ |
| 2128 | /* |
| 2129 | * Stack Frame Reference intrinsic object |
| 2130 | */ |
| 2131 | ulong CVmObjFrameRef::rebuild_image(VMG_ char *buf, ulong buflen) |
| 2132 | { |
| 2133 | /* get my extension */ |
| 2134 | vm_frameref_ext *ext = get_ext(); |
| 2135 | |
| 2136 | /* figure the required size */ |
| 2137 | ulong needlen = 2 + 2 + VMB_DATAHOLDER |
| 2138 | + VMB_DATAHOLDER |
| 2139 | + VMB_OBJECT_ID |
| 2140 | + VMB_OBJECT_ID |
| 2141 | + VMB_PROP_ID |
| 2142 | + (ext->nlocals + ext->nparams)*VMB_DATAHOLDER; |
| 2143 | |
| 2144 | /* make sure there's room */ |
| 2145 | if (needlen > buflen) |
| 2146 | return needlen; |
| 2147 | |
| 2148 | /* if our frame is still active, build the local snapshot */ |
| 2149 | if (ext->fp != 0) |
| 2150 | make_snapshot(vmg0_); |
| 2151 | |
| 2152 | /* save the variable counts */ |
| 2153 | oswp2(buf, ext->nlocals); |
| 2154 | oswp2(buf+2, ext->nparams); |
| 2155 | buf += 4; |
| 2156 | |
| 2157 | /* write the entry pointer */ |
| 2158 | vmb_put_dh(buf, &ext->entry); |
| 2159 | buf += VMB_DATAHOLDER; |
| 2160 | |
| 2161 | /* write the method context variables */ |
| 2162 | vmb_put_dh(buf, &ext->self); |
| 2163 | buf += VMB_DATAHOLDER; |
| 2164 | |
| 2165 | vmb_put_objid(buf, ext->defobj); |
| 2166 | buf += VMB_OBJECT_ID; |
| 2167 | |
| 2168 | vmb_put_objid(buf, ext->targobj); |
| 2169 | buf += VMB_OBJECT_ID; |
| 2170 | |
| 2171 | vmb_put_propid(buf, ext->targprop); |
| 2172 | buf += VMB_PROP_ID; |
| 2173 | |
| 2174 | /* write the variable snapshot values */ |
| 2175 | int i; |
| 2176 | const vm_val_t *v; |
| 2177 | for (i = ext->nlocals + ext->nparams, v = ext->vars ; i > 0 ; --i, ++v) |
| 2178 | { |
| 2179 | vmb_put_dh(buf, v); |
| 2180 | buf += VMB_DATAHOLDER; |
| 2181 | } |
| 2182 | |
| 2183 | /* return the length */ |
| 2184 | return needlen; |
| 2185 | } |
| 2186 | |
| 2187 | |
| 2188 | /* ------------------------------------------------------------------------ */ |
| 2189 | /* |
| 2190 | * CVmObjDate intrinsic object |
| 2191 | */ |
| 2192 | ulong CVmObjDate::rebuild_image(VMG_ char *buf, ulong buflen) |
| 2193 | { |
| 2194 | /* get my extension */ |
| 2195 | vm_date_ext *ext = get_ext(); |
| 2196 | |
| 2197 | /* figure the required size */ |
| 2198 | ulong needlen = 8; |
| 2199 | |
| 2200 | /* make sure there's room */ |
| 2201 | if (needlen > buflen) |
| 2202 | return needlen; |
| 2203 | |
| 2204 | /* write our data */ |
| 2205 | oswp4s(buf, ext->dayno); |
| 2206 | oswp4(buf+4, ext->daytime); |
| 2207 | |
| 2208 | /* return the length */ |
| 2209 | return needlen; |
| 2210 | } |
| 2211 | |
| 2212 | /* ------------------------------------------------------------------------ */ |
| 2213 | /* |
| 2214 | * CVmObjTimeZone intrinsic object |
| 2215 | */ |
| 2216 | ulong CVmObjTimeZone::rebuild_image(VMG_ char *buf, ulong buflen) |
| 2217 | { |
| 2218 | /* get my timezone object and query its default settings */ |
| 2219 | CVmTimeZone *tz = get_ext()->tz; |
| 2220 | vmtzquery q; |
| 2221 | tz->query(&q); |
| 2222 | |
| 2223 | /* get the zone name */ |
| 2224 | size_t namelen; |
| 2225 | const char *name = tz->get_name(namelen); |
| 2226 | |
| 2227 | /* if this is the special local zone object, use ":local" instead */ |
| 2228 | if (G_tzcache->is_local_zone(tz)) |
| 2229 | name = ":local", namelen = 6; |
| 2230 | |
| 2231 | /* |
| 2232 | * figure the required size - int32 gmt offset, int32 dst offset, |
| 2233 | * abbreviation (with one-byte prefix), name (with one-byte prefix) |
| 2234 | */ |
| 2235 | size_t abbrlen = strlen(q.abbr); |
| 2236 | ulong needlen = 4 + 4 + 1 + namelen + 1 + abbrlen; |
| 2237 | |
| 2238 | /* make sure there's room */ |
| 2239 | if (needlen > buflen) |
| 2240 | return needlen; |
| 2241 | |
| 2242 | /* copy the data */ |
| 2243 | oswp4s(buf, q.gmtofs); |
| 2244 | oswp4s(buf+4, q.save); |
| 2245 | char *p = write_str_byte_prefix(buf+8, name, namelen); |
| 2246 | write_str_byte_prefix(p, q.abbr, abbrlen); |
| 2247 | |
| 2248 | /* return the length */ |
| 2249 | return needlen; |
| 2250 | } |
| 2251 | |
| 2252 | #ifdef TADSNET |
| 2253 | /* ------------------------------------------------------------------------ */ |
| 2254 | /* |
| 2255 | * HTTP Server intrinsic object |
| 2256 | */ |
| 2257 | ulong CVmObjHTTPServer::rebuild_image(VMG_ char *buf, ulong buflen) |
| 2258 | { |
| 2259 | /* we don't store anything in the image file */ |
| 2260 | ulong needlen = 0; |
| 2261 | |
| 2262 | /* make sure there's room */ |
| 2263 | if (needlen > buflen) |
| 2264 | return needlen; |
| 2265 | |
| 2266 | /* return the length */ |
| 2267 | return needlen; |
| 2268 | } |
| 2269 | |
| 2270 | /* ------------------------------------------------------------------------ */ |
| 2271 | /* |
| 2272 | * CVmObjHTTPRequest intrinsic object |
| 2273 | */ |
| 2274 | ulong CVmObjHTTPRequest::rebuild_image(VMG_ char *buf, ulong buflen) |
| 2275 | { |
| 2276 | /* we don't store anything in the image file */ |
| 2277 | ulong needlen = 0; |
| 2278 | |
| 2279 | /* make sure there's room */ |
| 2280 | if (needlen > buflen) |
| 2281 | return needlen; |
| 2282 | |
| 2283 | /* return the length */ |
| 2284 | return needlen; |
| 2285 | } |
| 2286 | |
| 2287 | #endif /* TADSNET */ |
| 2288 |