1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
|
// Persistent reachability graph storage -*- c++ -*-
#ifdef __GNUC__
# pragma implementation
#endif // __GNUC__
#include "Graph.h"
#include "LSTS.h"
#include "BTree.h"
#include "Net.h"
#include "Place.h"
#include "GlobalMarking.h"
#include "BitVector.h"
#include "util.h"
#include <stdlib.h> // abort(3)
#ifndef __WIN32
# include <sys/errno.h>
#endif // !__WIN32
#include <errno.h>
#include <list>
#ifdef USE_MMAP
# include <fcntl.h>
# ifdef __DECCXX
/** A qualifier for pointers to unaligned data */
# define UNALIGNED __unaligned
# else // __DECCXX
/** A qualifier for pointers to unaligned data */
# define UNALIGNED /*nothing*/
# endif // __DECCXX
# if defined __digital__
# define fileno(f) ((f)->_file)
# endif // __digital__
# ifdef NO_MMAP
/** an empty file structure */
# define NULL_F { 0, 0, 0 }
# else // NO_MMAP
/** an empty file structure */
# define NULL_F { -1, 0, 0, 0 }
# endif // NO_MMAP
/** an empty file pointer */
static const file_t nofile = NULL_F;
/** empty file pointers */
static const struct Graph::files nofiles = { NULL_F, NULL_F, 0, NULL_F };
#else // USE_MMAP
# define UNALIGNED /*nothing*/
/** an empty file pointer */
static const file_t nofile = 0;
/** empty file pointers */
static const struct Graph::files nofiles = { 0, 0, 0, 0 };
#endif // USE_MMAP
/** @file Graph.C
* Persistent, lossless reachability graph storage
*/
/* Copyright 1999-2003,2005 Marko Mkel (msmakela@tcs.hut.fi).
This file is part of MARIA, a reachability analyzer and model checker
for high-level Petri nets.
MARIA is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
MARIA is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
The GNU General Public License is often shipped with GNU software, and
is generally kept in a file called COPYING or LICENSE. If you do not
have a copy of the license, write to the Free Software Foundation,
59 Temple Place, Suite 330, Boston, MA 02111 USA. */
/** file offset to state directory slot
* @param s number of the state
* @return file offset of the corresponding directory slot
*/
#ifdef USE_MMAP
# define DIRSLOT1(s) \
(reinterpret_cast<UNALIGNED Graph::fpos_t*> \
(static_cast<char*>(myFiles.directory.addr) + \
myDirectoryOffset) + 3 * (s))
#else // USE_MMAP
# define DIRSLOT1(s) (myDirectoryOffset + (s) * (3 * sizeof (Graph::fpos_t)))
#endif // USE_MMAP
/** file offset to the second component of a state directory slot
* @param s number of the state
* @return file offset to the second component of the slot
*/
#ifdef USE_MMAP
# define DIRSLOT2(s) (&DIRSLOT1 (s)[1])
#else // USE_MMAP
# define DIRSLOT2(s) (DIRSLOT1 (s) + sizeof (Graph::fpos_t))
#endif // USE_MMAP
/** file offset to the third component of a state directory slot
* @param s number of the state
* @return file offset to the second component of the slot
*/
#ifdef USE_MMAP
# define DIRSLOT3(s) (&DIRSLOT1 (s)[2])
#else // USE_MMAP
# define DIRSLOT3(s) (DIRSLOT1 (s) + 2 * sizeof (Graph::fpos_t))
#endif // USE_MMAP
#ifdef USE_MMAP
/** number of states */
# define myNumStates \
(reinterpret_cast<UNALIGNED unsigned*> \
(static_cast<char*>(myFiles.directory.addr) + myDirectoryOffset)[-2])
/** number of arcs */
# define myNumArcs \
(reinterpret_cast<UNALIGNED unsigned*> \
(static_cast<char*>(myFiles.directory.addr) + myDirectoryOffset)[-1])
#endif // USE_MMAP
/** File offset flag */
#define OFFSET_FLAG LONG_MIN
/** Determine whether an offset has been flagged
* @param offset offset to be checked
* @return whether the offset has the error bit set
*/
inline static bool
isFlagged (Graph::fpos_t offset)
{
return offset & OFFSET_FLAG && true;
}
/** Flag an offset
* @param state offset to be flagged
*/
inline static void
flag (UNALIGNED Graph::fpos_t& offset)
{
assert (!::isFlagged (offset));
offset |= OFFSET_FLAG;
}
inline static Graph::fpos_t
getOffset (Graph::fpos_t offset)
{
return offset & ~OFFSET_FLAG;
}
#undef OFFSET_FLAG
#ifdef NO_MMAP
/** pretend that mmapped files are always open */
# define isOpen(file) true
/** pretend to open a file */
# define openFile(name, cr) nofile
#else
/** Check if a file is open
* @param file the file
* @return true if the file has been opened
*/
static bool
isOpen (const file_t& file)
{
# ifdef USE_MMAP
return file.fd >= 0;
# else // USE_MMAP
return bool (file);
# endif // USE_MMAP
}
/** Open a file
* @param name the file name (optional)
* @param cr flag: create or truncate the file
* @return an opened file, or something on which isOpen() does not hold
*/
static file_t
openFile (const char* name, bool cr)
{
# ifdef USE_MMAP
file_t f = nofile;
if (name)
f.fd = open (name, cr ? O_RDWR | O_CREAT | O_TRUNC : O_RDWR, 0666);
else {
FILE* file = tmpfile ();
if (!file) {
perror ("tmpfile");
return nofile;
}
f.fd = dup (fileno (file));
fclose (file);
name = "(temporary)";
}
if (f.fd >= 0) {
f.len = lseek (f.fd, 0, SEEK_END);
for (f.alloc = 4096; f.alloc < f.len; ) {
if (!(f.alloc *= 2)) {
fputs (name, stderr), fputs (": file size overflow\n", stderr);
abort ();
}
}
if (ftruncate (f.fd, f.alloc)) {
fputs (name, stderr), perror (": ftruncate");
abort ();
}
f.addr =
# ifdef __sun
(caddr_t)
# endif // __sun
mmap (0, f.alloc, PROT_READ | PROT_WRITE, MAP_SHARED, f.fd, 0);
if (f.addr == reinterpret_cast<void*>(MAP_FAILED)) {
fputs (name, stderr), perror (": mmap");
close (f.fd);
return nofile;
}
}
return f;
# else // USE_MMAP
return name ? fopen (name, cr ? "wb+" : "rb+") : tmpfile ();
# endif // USE_MMAP
}
#endif // NO_MMAP
#ifdef USE_MMAP
size_t
Graph::getNumStates () const
{
return myNumStates;
}
unsigned
Graph::getNumArcs () const
{
return myNumArcs;
}
#endif // USE_MMAP
/** Close a file
* @param file the file
*/
static void
closeFile (file_t& file)
{
assert (isOpen (file));
#ifdef USE_MMAP
# ifdef NO_MMAP
if (file.addr)
free (file.addr);
# else // NO_MMAP
if (file.addr)
munmap (file.addr, file.alloc);
ftruncate (file.fd, file.len);
close (file.fd);
# endif // NO_MMAP
#else // USE_MMAP
fclose (file);
#endif // USE_MMAP
}
#ifdef USE_MMAP
/** Extend a file
* @param file the file
*/
static void
extend (file_t& file)
{
assert (isOpen (file) && file.addr);
if (file.len < file.alloc)
return;
# ifndef NO_MMAP
if (file.addr)
munmap (file.addr, file.alloc);
# endif // !NO_MMAP
while (file.alloc < file.len) {
if (!(file.alloc *= 2)) {
fputs ("extend: file size overflow\n", stderr);
abort ();
}
}
# ifdef NO_MMAP
if (!(file.addr = realloc (file.addr, file.alloc))) {
perror ("extend: realloc");
abort ();
}
# else // NO_MMAP
if (ftruncate (file.fd, file.alloc)) {
perror ("extend: ftruncate");
abort ();
}
file.addr =
# ifdef __sun
(caddr_t)
# endif // __sun
mmap (0, file.alloc, PROT_READ | PROT_WRITE, MAP_SHARED, file.fd, 0);
if (file.addr == reinterpret_cast<void*>(MAP_FAILED)) {
perror ("extend: mmap");
abort ();
}
# endif // NO_MMAP
}
#endif // USE_MMAP
/** Find and update an encoded state in the graph
* @param myFiles the graph files
* @param myDirectoryOffset offset to the state directory
* @param buf the encoded state to find or update
* @param tmp work storage for encoded states
* @param bytes length of the encoded state in bytes
* @param number number of the state to be searched
* @return whether the state was found
*/
static bool
update (const struct Graph::files& myFiles,
Graph::fpos_t myDirectoryOffset,
const void* buf,
#ifndef USE_MMAP
void* tmp,
#endif // !USE_MMAP
size_t bytes, card_t number)
{
Graph::fpos_t num;
#ifdef USE_MMAP
num = *DIRSLOT1 (number);
assert (!bytes || getOffset (num) < myFiles.states.len);
return !bytes ||
(getOffset (num) + Graph::fpos_t (bytes) <= myFiles.states.len &&
!memcmp (static_cast<char*>(myFiles.states.addr) + getOffset (num),
buf, bytes));
#else // USE_MMAP
fseek (myFiles.directory, DIRSLOT1 (number), SEEK_SET);
if (fread (&num, sizeof num, 1, myFiles.directory) != 1)
assert (false);
if (fseek (myFiles.states, getOffset (num), SEEK_SET)) {
perror ("update (): fseek");
abort ();
}
return
fread (tmp, 1, bytes, myFiles.states) == bytes &&
!memcmp (tmp, buf, bytes);
#endif // USE_MMAP
}
Graph::Graph (const class Net& net) :
myNet (net),
mySucc (new card_t[1]),
#ifndef USE_MMAP
myNumArcs (0), myNumStates (0),
#endif // !USE_MMAP
myStates (0), myFiles (nofiles),
myDirectoryOffset (0), myFilename (0), myFilebase (0), myLSTS (0)
{
*mySucc = 0;
}
Graph::Graph (const class Net& net,
const char* filename,
const char* filebase) :
myNet (net),
mySucc (new card_t[1]),
#ifndef USE_MMAP
myNumArcs (0), myNumStates (0),
#endif // !USE_MMAP
myStates (0),
myFiles (nofiles),
myDirectoryOffset (0), myFilename (0), myFilebase (0), myLSTS (0)
{
*mySucc = 0;
unsigned i;
assert (filename || filebase);
if (filename)
myFilename = newString (filename);
if (filebase)
myFilebase = newString (filebase);
else {
// remove directory component
for (i = 0; filename[i]; i++);
while (i > 0 && filename[i - 1] != '/') i--;
filebase = filename + i;
// remove the last suffix starting with '.'
for (i = 0; filebase[i]; i++);
while (i > 0 && filebase[i] != '.') i--;
if (!i) // no suffix
for (i = 0; filebase[i]; i++);
char* name = new char[i + 1];
memcpy (name, filebase, i);
name[i] = 0;
myFilebase = name;
}
}
Graph::~Graph ()
{
if (isOpen (myFiles.directory)) {
closeFile (myFiles.directory);
closeFile (myFiles.states);
fclose (myFiles.arcs);
closeFile (myFiles.preds);
}
delete myStates;
delete[] mySucc;
delete[] myFilename;
delete[] myFilebase;
delete myLSTS;
}
bool
Graph::openFiles (bool regenerate)
{
assert (!isOpen (myFiles.directory) && !isOpen (myFiles.states) &&
!myFiles.arcs && !isOpen (myFiles.preds));
unsigned i = 0;
if (myFilebase) while (myFilebase[i]) i++;
/** flag: create or truncate the graph files */
bool cr = !myFilebase || regenerate;
{
char* base = i ? new char[i + 5] : 0;
memcpy (base, myFilebase, i);
if (i) memcpy (base + i, ".rgh", 5);
file_t f = openFile (base, cr);
#ifndef NO_MMAP
if (isOpen (f))
myStates = new class BTree (f);
else if (!cr && myFilename && errno == ENOENT) {
f = openFile (base, cr = true);
if (isOpen (f))
goto failRGH;
myStates = new class BTree (f);
}
else {
failRGH:
perror (base);
delete[] base;
return false;
}
#endif // !NO_MMAP
if (i) memcpy (base + i, ".rgd", 5);
myFiles.directory = openFile (base, cr);
if (!isOpen (myFiles.directory)) {
perror (base);
delete myStates; myStates = 0;
delete[] base;
return false;
}
if (i) memcpy (base + i, ".rgs", 5);
myFiles.states = openFile (base, cr);
if (!isOpen (myFiles.states)) {
perror (base);
delete myStates; myStates = 0;
closeFile (myFiles.directory); myFiles.directory = nofile;
delete[] base;
return false;
}
if (i) memcpy (base + i, ".rga", 5);
if (!(myFiles.arcs = i ? fopen (base, cr ? "wb+" : "rb+") : tmpfile ())) {
perror (base);
delete myStates; myStates = 0;
closeFile (myFiles.directory); myFiles.directory = nofile;
closeFile (myFiles.states); myFiles.states = nofile;
delete[] base;
return false;
}
if (i) memcpy (base + i, ".rgp", 5);
myFiles.preds = openFile (base, cr);
if (!isOpen (myFiles.preds)) {
perror (base);
delete myStates; myStates = 0;
closeFile (myFiles.directory); myFiles.directory = nofile;
closeFile (myFiles.states); myFiles.states = nofile;
fclose (myFiles.arcs); myFiles.arcs = 0;
delete[] base;
return false;
}
delete[] base;
}
static const char magic[] = "Maria-generated Reachability Graph of ";
const size_t magiclen = (sizeof magic) - 1;
if (!cr) {
// check the magic cookie
#ifdef USE_MMAP
if (myFiles.directory.len < fpos_t (magiclen)) {
wrongFormatEOF:
fprintf (stderr, "%s: premature EOF of graph directory\n",
myFilebase);
goto wrongFormatNoEOF;
}
if (memcmp (myFiles.directory.addr, magic, magiclen)) {
wrongFormatNoEOF:
delete myStates; myStates = 0;
closeFile (myFiles.directory); myFiles.directory = nofile;
closeFile (myFiles.states); myFiles.states = nofile;
fclose (myFiles.arcs); myFiles.arcs = 0;
closeFile (myFiles.preds); myFiles.preds = nofile;
return false;
}
#else // USE_MMAP
const size_t capacity = 4096;
char* buf = new char[capacity];
assert (capacity >= magiclen);
if ((fread (buf, 1, magiclen, myFiles.directory) != magiclen) ||
memcmp (buf, magic, magiclen)) {
fprintf (stderr, "%s: graph: wrong magic cookie\n",
myFilebase);
wrongFormat:
if (feof (myFiles.directory))
fprintf (stderr, "%s: premature EOF of graph directory\n",
myFilebase);
wrongFormatNoEOF:
delete myStates; myStates = 0;
closeFile (myFiles.directory); myFiles.directory = nofile;
closeFile (myFiles.states); myFiles.states = nofile;
fclose (myFiles.arcs); myFiles.arcs = 0;
closeFile (myFiles.preds); myFiles.preds = nofile;
delete[] buf;
return false;
}
#endif // USE_MMAP
#ifdef USE_MMAP
size_t got = myFiles.directory.len - magiclen;
const char* buf = static_cast<char*>(myFiles.directory.addr) + magiclen;
#else // USE_MMAP
size_t got = fread (buf, 1, capacity, myFiles.directory);
#endif // USE_MMAP
size_t len;
for (len = 0; len < got && buf[len]; len++);
if (!len) {
fprintf (stderr, "%s: graph: empty model name\n", myFilebase);
argError:
goto wrongFormatNoEOF;
}
else if (len == got) {
fprintf (stderr, "%s: graph: model name is too long\n", myFilebase);
goto argError;
}
#ifndef USE_MMAP
if (fseek (myFiles.directory, fpos_t (len) - got + 1, SEEK_CUR)) {
fprintf (stderr, "%s: ", myFilebase);
perror ("fseek");
goto wrongFormatNoEOF;
}
#endif // !USE_MMAP
if (myFilename) {
if (strcmp (buf, myFilename)) {
fprintf (stderr, "%s: model names differ: \"%s\" \"%s\"\n",
myFilebase, buf, myFilename);
goto argError;
}
}
else
myFilename = newString (buf);
#ifndef USE_MMAP
delete[] buf; buf = 0;
#endif // !USE_MMAP
#ifdef USE_MMAP
myDirectoryOffset = len + (magiclen + sizeof (fpos_t));
#else // USE_MMAP
myDirectoryOffset = ftell (myFiles.directory) + (sizeof (fpos_t) - 1);
#endif // USE_MMAP
myDirectoryOffset /= sizeof (fpos_t);
myDirectoryOffset *= sizeof (fpos_t);
#ifdef USE_MMAP
myDirectoryOffset += (sizeof myNumStates) + (sizeof myNumArcs);
if (myDirectoryOffset >= myFiles.directory.len)
goto wrongFormatEOF;
fpos_t endpos = myFiles.directory.len;
#else // USE_MMAP
fseek (myFiles.directory, myDirectoryOffset, SEEK_SET);
myDirectoryOffset += (sizeof myNumStates) + (sizeof myNumArcs);
if (fread (&myNumStates, sizeof myNumStates, 1, myFiles.directory) != 1 ||
fread (&myNumArcs, sizeof myNumArcs, 1, myFiles.directory) != 1)
goto wrongFormat;
fseek (myFiles.directory, 0, SEEK_END);
fpos_t endpos = ftell (myFiles.directory);
#endif // USE_MMAP
if ((endpos - myDirectoryOffset) % (3 * sizeof (fpos_t)) ||
(endpos - myDirectoryOffset) / (3 * sizeof (fpos_t)) != myNumStates) {
fprintf (stderr, "%s.rgd: trying to ignore extra bytes at end of file\n",
myFilebase);
#ifdef USE_MMAP
myFiles.directory.len = myDirectoryOffset +
3 * sizeof (fpos_t) * myNumStates;
#endif // USE_MMAP
}
if (!myNumStates) {
fprintf (stderr, "%s.rgd: no states\n", myFilebase);
goto wrongFormatNoEOF;
}
}
else { // create the directory afresh
#ifdef USE_MMAP
assert (myFiles.directory.len == 0);
const size_t len = i ? strlen (myFilename) + 1 : 0;
myDirectoryOffset = len + (magiclen + sizeof (fpos_t) - 1 +
(sizeof myNumStates) + (sizeof myNumArcs));
myDirectoryOffset /= sizeof (fpos_t);
myDirectoryOffset *= sizeof (fpos_t);
myFiles.directory.len = myDirectoryOffset;
extend (myFiles.directory);
memcpy (myFiles.directory.addr, magic, magiclen);
memcpy (static_cast<char*>(myFiles.directory.addr) + magiclen,
myFilename, len);
myNumStates = 0;
myNumArcs = 0;
#else // USE_MMAP
// magic cookie
fwrite (magic, magiclen, 1, myFiles.directory);
fwrite (myFilename, 1, i ? 1 + strlen (myFilename) : 0, myFiles.directory);
myDirectoryOffset = ftell (myFiles.directory) + (sizeof (fpos_t) - 1);
myDirectoryOffset /= sizeof (fpos_t);
myDirectoryOffset *= sizeof (fpos_t);
fseek (myFiles.directory, myDirectoryOffset, SEEK_SET);
myDirectoryOffset += sizeof myNumStates;
fwrite (&myNumStates, sizeof myNumStates, 1, myFiles.directory);
myDirectoryOffset += sizeof myNumArcs;
fwrite (&myNumArcs, sizeof myNumArcs, 1, myFiles.directory);
assert (myDirectoryOffset == ftell (myFiles.directory));
fflush (myFiles.directory);
#endif // USE_MMAP
}
return true;
}
void
Graph::setLSTS (class LSTS* lsts)
{
delete myLSTS;
myLSTS = lsts;
}
/** Compute a hash value for a sequence of bytes
* @param buf the sequence
* @param bytes number of bytes in the sequence
*/
inline static size_t
getHashValue (const unsigned char* buf, size_t bytes)
{
size_t h = 0;
while (bytes--)
h ^= (h << 3 | h >> (CHAR_BIT - 3)) ^ buf[bytes];
return h;
}
const class Graph::AddStatus
Graph::add (const void* buf,
size_t bytes)
{
size_t h = getHashValue (reinterpret_cast<const unsigned char*>(buf), bytes);
if (card_t* states = myStates->search (h)) {
#ifndef USE_MMAP
word_t* tmp =
new word_t[(bytes + (sizeof (word_t) - 1)) / sizeof (word_t)];
#endif // !USE_MMAP
for (card_t s = *states; s; s--) {
if (::update (myFiles, myDirectoryOffset, buf,
#ifndef USE_MMAP
tmp,
#endif // !USE_MMAP
bytes, states[s])) {
s = states[s];
#ifndef USE_MMAP
delete[] tmp;
#endif // !USE_MMAP
delete[] states;
return AddStatus (false, s);
}
}
#ifndef USE_MMAP
delete[] tmp;
#endif // !USE_MMAP
delete[] states;
}
// a new state: add it to disk
#ifdef USE_MMAP
myFiles.directory.len += 3 * sizeof (fpos_t);
extend (myFiles.directory);
UNALIGNED fpos_t* const num = DIRSLOT1 (myNumStates);
const fpos_t len = myFiles.states.len;
num[0] = len;
num[1] = num[2] = FPOS_NONE;
myFiles.states.len += bytes;
extend (myFiles.states);
memcpy (static_cast<char*>(myFiles.states.addr) + len, buf, bytes);
myStates->insert (h, myNumStates);
return AddStatus (true, myNumStates++);
#else // USE_MMAP
{
fseek (myFiles.directory, DIRSLOT1 (myNumStates), SEEK_SET);
if (fseek (myFiles.states, 0, SEEK_END))
assert (false);
fpos_t num[3];
num[0] = ftell (myFiles.states);
num[1] = num[2] = FPOS_NONE;
if ((fwrite (num, sizeof num, 1, myFiles.directory) != 1) ||
(fwrite (buf, 1, bytes, myFiles.states) != bytes)) {
error:
perror ("Graph::add (state): fwrite");
abort ();
}
myStates->insert (h, myNumStates++);
fseek (myFiles.directory, myDirectoryOffset -
(sizeof (myNumStates) + sizeof (myNumArcs)), SEEK_SET);
if (1 != fwrite (&myNumStates, sizeof myNumStates, 1, myFiles.directory))
goto error;
return AddStatus (true, myNumStates - 1);
}
#endif // USE_MMAP
}
card_t
Graph::lookup (const void* buf,
size_t bytes) const
{
size_t h = getHashValue (reinterpret_cast<const unsigned char*>(buf),
bytes);
if (card_t* states = myStates->search (h)) {
#ifndef USE_MMAP
word_t* tmp =
new word_t[(bytes + (sizeof (word_t) - 1)) / sizeof (word_t)];
#endif // !USE_MMAP
for (card_t s = *states; s; s--) {
if (::update (myFiles, myDirectoryOffset, buf,
#ifndef USE_MMAP
tmp,
#endif // !USE_MMAP
bytes, states[s])) {
s = states[s];
#ifndef USE_MMAP
delete[] tmp;
#endif // !USE_MMAP
delete[] states;
return s;
}
}
#ifndef USE_MMAP
delete[] tmp;
#endif // !USE_MMAP
delete[] states;
}
return CARD_T_MAX;
}
void
Graph::add (card_t source, card_t target)
{
assert (!!mySucc);
assert (source != CARD_T_MAX);
assert (target != CARD_T_MAX);
if (!(*mySucc & (*mySucc + 1))) {
card_t* succ = new card_t[(*mySucc + 1) << 1];
memcpy (succ, mySucc, (*mySucc + 1) * sizeof *succ);
delete[] mySucc;
mySucc = succ;
}
mySucc[++(*mySucc)] = target;
addReverse (source, target);
}
/** Encode a number to a file using a variable-length code
* @param file the output file
* @param num the number to be encoded
* @return true if the operation succeeded
*/
static bool
encodeNumber (FILE* file, card_t num)
{
char buf[4];
unsigned len;
// variable-length code:
// 0..127 0xxxxxxx
// 128..16511 10xxxxxxxxxxxxxx
// 16512..1073758335 11xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if (num < 128)
buf[0] = num, len = 1;
else if ((num -= 128) < (1 << 14))
buf[0] = 0x80 | (num >> 8), buf[1] = num, len = 2;
else if ((num -= (1 << 14)) < (1 << 30)) {
buf[3] = num;
buf[2] = num >>= 8;
buf[1] = num >>= 8;
buf[0] = (num >> 8) | 0xc0;
len = 4;
}
else
return false;
return len == fwrite (buf, 1, len, file);
}
/** Decode a number from a file using a variable-length code
* @param file the output file
* @return the decoded number
*/
static card_t
decodeNumber (FILE* file)
{
unsigned char buf[4];
if (1 != fread (buf, 1, 1, file)) {
err:
perror ("decodeNumber: fread");
assert (false);
return 0;
}
switch (*buf & 0xc0) {
default:
// 0..127
return *buf;
case 0x80:
// 128..16511
if (1 != fread (buf + 1, 1, 1, file))
goto err;
return 128 + ((static_cast<unsigned>(*buf & 0x3f) << 8) | buf[1]);
case 0xc0:
// 16512..1073758335
if (3 != fread (buf + 1, 1, 3, file))
goto err;
return 16512 + ((static_cast<unsigned>(*buf & 0x3f) << 24) |
(static_cast<unsigned>(buf[1]) << 16) |
(static_cast<unsigned>(buf[2]) << 8) |
buf[3]);
}
}
void
Graph::addEvents (card_t source,
const void* buf,
size_t bytes)
{
assert (!isProcessed (source) && mySucc);
#ifdef USE_MMAP
/** offset to the arc file */
UNALIGNED fpos_t& arcoffset = *DIRSLOT2 (source);
assert (arcoffset == FPOS_NONE);
#else // USE_MMAP
/** offset to the arc file */
fpos_t arcoffset;
#endif // USE_MMAP
if (*mySucc) {
if (myLSTS) {
word_t* data = const_cast<word_t*>(static_cast<const word_t*>(buf));
BitPacker::inflate (data[(bytes - 1) / sizeof (word_t)],
(-bytes) % sizeof (word_t));
myLSTS->outputArcs (*this, source, mySucc, data);
BitPacker::deflate (data[(bytes - 1) / sizeof (word_t)],
(-bytes) % sizeof (word_t));
}
fseek (myFiles.arcs, 0, SEEK_END);
arcoffset = ftell (myFiles.arcs);
if (!encodeNumber (myFiles.arcs, bytes) ||
!encodeNumber (myFiles.arcs, *mySucc) ||
(*mySucc != fwrite (mySucc + 1, sizeof *mySucc,
*mySucc, myFiles.arcs)) ||
(bytes != fwrite (buf, 1, bytes, myFiles.arcs))) {
perror ("Graph::add (event): fwrite");
abort ();
}
*mySucc = 0;
}
else
arcoffset = FPOS_DEAD;
// update the directory file
#ifndef USE_MMAP
fseek (myFiles.directory, DIRSLOT2 (source), SEEK_SET);
# ifndef NDEBUG
{
fpos_t arcpos;
assert (1 == fread (&arcpos, sizeof arcpos, 1, myFiles.directory) &&
arcpos == FPOS_NONE);
fseek (myFiles.directory, DIRSLOT2 (source), SEEK_SET);
}
# endif // !NDEBUG
fwrite (&arcoffset, sizeof arcoffset, 1, myFiles.directory);
fseek (myFiles.directory, myDirectoryOffset - sizeof myNumArcs, SEEK_SET);
fwrite (&myNumArcs, sizeof myNumArcs, 1, myFiles.directory);
#endif // !USE_MMAP
}
word_t*
Graph::fetchState (card_t number, size_t& length, bool* erroneous) const
{
assert (number < myNumStates);
#ifdef USE_MMAP
fpos_t num = *DIRSLOT1 (number);
#else // USE_MMAP
fseek (myFiles.directory, DIRSLOT1 (number), SEEK_SET);
fpos_t num;
if (fread (&num, sizeof num, 1, myFiles.directory) != 1) {
readError:
perror ("Graph::fetchState (): fread");
return 0;
}
#endif // USE_MMAP
if (erroneous)
*erroneous = ::isFlagged (num);
num = ::getOffset (num);
#ifdef USE_MMAP
length = (number == myNumStates - 1
? myFiles.states.len
: ::getOffset (*DIRSLOT1 (number + 1))) - num;
#else // USE_MMAP
if (number == myNumStates - 1) {
fseek (myFiles.states, 0, SEEK_END);
length = ftell (myFiles.states) - num;
}
else {
fpos_t pos;
fseek (myFiles.directory, DIRSLOT1 (number + 1), SEEK_SET);
if (fread (&pos, sizeof pos, 1, myFiles.directory) != 1)
goto readError;
length = ::getOffset (pos) - num;
}
if (fseek (myFiles.states, num, SEEK_SET)) {
perror ("Graph::fetchState (): fseek");
return 0;
}
#endif // USE_MMAP
unsigned numWords = (length + sizeof (word_t) - 1) / sizeof (word_t);
word_t* buf = new word_t[numWords];
#ifdef USE_MMAP
memcpy (buf, static_cast<char*>(myFiles.states.addr) + num, length);
#else // USE_MMAP
if (fread (buf, 1, length, myFiles.states) != length) {
delete[] buf;
goto readError;
}
#endif // USE_MMAP
return buf;
}
class GlobalMarking*
Graph::fetchState (card_t number) const
{
size_t length;
bool erroneous;
word_t* buf = fetchState (number, length, &erroneous);
if (!buf)
return 0;
BitPacker::inflate (buf[(length - 1) / sizeof (word_t)],
(-length) % sizeof (word_t));
// Decode the marking
class GlobalMarking* marking = new class GlobalMarking (myNet);
marking->decode (*myNet.getInitMarking (), buf);
// Set the error bit
if (erroneous)
marking->flagErroneous ();
delete[] buf;
return marking;
}
card_t
Graph::getNumPredecessors (card_t state) const
{
card_t num = 0;
for (fpos_t pos = getPredecessors (state); pos != FPOS_NONE;
getPredecessor (pos))
num++;
return num;
}
Graph::fpos_t
Graph::getPredecessors (card_t state) const
{
assert (state < myNumStates);
#ifdef USE_MMAP
return *DIRSLOT3 (state);
#else // USE_MMAP
fseek (myFiles.directory, DIRSLOT3 (state), SEEK_SET);
fpos_t pos;
if (fread (&pos, sizeof pos, 1, myFiles.directory) != 1) {
assert (false);
return FPOS_NONE;
}
return pos;
#endif // USE_MMAP
}
card_t
Graph::getPredecessor (Graph::fpos_t& pos) const
{
card_t state;
assert (pos != FPOS_NONE);
#ifdef USE_MMAP
assert (pos >= 0);
assert (pos + fpos_t ((sizeof state) + (sizeof pos)) <= myFiles.preds.len);
const char* buf = static_cast<char*>(myFiles.preds.addr);
state = *reinterpret_cast<UNALIGNED const card_t*>(buf + pos);
pos = *reinterpret_cast<UNALIGNED const fpos_t*>(buf + pos + sizeof state);
#else // USE_MMAP
fseek (myFiles.preds, pos, SEEK_SET);
if (fread (&state, sizeof state, 1, myFiles.preds) != 1 ||
fread (&pos, sizeof pos, 1, myFiles.preds) != 1) {
assert (false);
pos = FPOS_NONE;
return CARD_T_MAX;
}
#endif // USE_MMAP
assert (state < myNumStates);
return state;
}
card_t*
Graph::getSuccessors (card_t state, word_t** data) const
{
fpos_t pos;
assert (state < myNumStates);
#ifdef USE_MMAP
pos = *DIRSLOT2 (state);
#else // USE_MMAP
fseek (myFiles.directory, DIRSLOT2 (state), SEEK_SET);
if (fread (&pos, sizeof pos, 1, myFiles.directory) != 1) {
assert (false);
return 0;
}
#endif // USE_MMAP
if (pos == FPOS_DEAD || pos == FPOS_NONE)
return 0;
fseek (myFiles.arcs, pos, SEEK_SET);
/** length of the encoded data */
const size_t length = decodeNumber (myFiles.arcs);
/** number of successor states */
const card_t numSucc = decodeNumber (myFiles.arcs);
assert (numSucc > 0);
card_t* succ = new card_t[numSucc + 1];
*succ = numSucc;
if (fread (succ + 1, sizeof *succ, *succ, myFiles.arcs) != *succ) {
assert (false);
delete[] succ;
return 0;
}
if (data) {
if (!length)
*data = 0;
else {
const unsigned numWords =
(length + sizeof (word_t) - 1) / sizeof (word_t);
word_t* buf = new word_t[numWords];
if (fread (buf, 1, length, myFiles.arcs) != length) {
assert (false);
delete[] succ;
delete[] buf;
return 0;
}
BitPacker::inflate (buf[numWords - 1], (-length) % sizeof (word_t));
*data = buf;
}
}
return succ;
}
bool
Graph::eval (card_t state,
const class Expression* cond) const
{
if (!cond)
return true;
class GlobalMarking* m = fetchState (state);
bool accept = m->eval (*cond);
delete m;
return accept;
}
card_t*
Graph::toPath (const PathMap& tree,
card_t dest)
{
card_t* p = new card_t[1];
*p = 0;
for (;;) {
card_t* np = new card_t[++(*p) + 1];
memcpy (np, p, *p * sizeof *p);
delete[] p; p = np;
p[*p] = dest;
PathMap::const_iterator i = tree.find (dest);
if (i == tree.end ()) break;
dest = i->second;
}
return p;
}
card_t*
Graph::path (card_t state, card_t target,
const class Expression* pathc) const
{
if (!eval (state, pathc) || !eval (target, pathc))
return 0;
/** Visited states */
class BitVector visited (myNumStates);
visited.assign (state, true);
/** Search queue */
std::list<card_t> sq;
sq.push_front (CARD_T_MAX); /* mark for the next level */
sq.push_back (state);
/** Arcs in the explored graph (target, source) */
PathMap p;
for (;;) {
if ((state = *sq.begin ()) == CARD_T_MAX) {
sq.pop_front ();
if (sq.empty ())
return 0;
sq.push_back (CARD_T_MAX);
state = *sq.begin ();
}
sq.pop_front ();
assert (!sq.empty ());
assert (visited[state]);
if (state == target)
return toPath (p, target);
if (card_t* succ = getSuccessors (state)) {
for (assert (*succ > 0); *succ; (*succ)--) {
card_t s = succ[*succ];
if (!visited.tset (s) && eval (s, pathc)) {
sq.push_back (s);
p.insert (PathMap::value_type (s, state));
}
}
delete[] succ;
}
}
}
card_t*
Graph::path (card_t state, const class Expression& cond,
const class Expression* pathc) const
{
if (!eval (state, pathc))
return 0;
/** Visited states */
class BitVector visited (myNumStates);
visited.assign (state, true);
/** Search queue */
std::list<card_t> sq;
sq.push_front (CARD_T_MAX); /* mark for the next level */
sq.push_back (state);
/** Arcs in the explored graph (target, source) */
PathMap p;
for (;;) {
if ((state = *sq.begin ()) == CARD_T_MAX) {
sq.pop_front ();
if (sq.empty ())
return 0;
sq.push_back (CARD_T_MAX);
state = *sq.begin ();
}
sq.pop_front ();
assert (!sq.empty ());
assert (visited[state]);
if (eval (state, &cond))
return toPath (p, state);
if (card_t* succ = getSuccessors (state)) {
for (assert (*succ > 0); *succ; (*succ)--) {
card_t s = succ[*succ];
if (!visited.tset (s) && eval (s, pathc)) {
sq.push_back (s);
p.insert (PathMap::value_type (s, state));
}
}
delete[] succ;
}
}
}
card_t*
Graph::rpath (card_t state, const class Expression& cond,
const class Expression* pathc) const
{
if (!eval (state, pathc))
return 0;
/** Visited states */
class BitVector visited (myNumStates);
visited.assign (state, true);
/** Search queue */
std::list<card_t> sq;
sq.push_front (CARD_T_MAX); /* mark for the next level */
sq.push_back (state);
/** Arcs in the explored graph (target, source) */
PathMap p;
for (;;) {
if ((state = *sq.begin ()) == CARD_T_MAX) {
sq.pop_front ();
if (sq.empty ())
return 0;
sq.push_back (CARD_T_MAX);
state = *sq.begin ();
}
sq.pop_front ();
assert (!sq.empty ());
assert (visited[state]);
if (eval (state, &cond))
return toPath (p, state);
for (fpos_t pos = getPredecessors (state); pos != FPOS_NONE;) {
card_t s = getPredecessor (pos);
if (!visited.tset (s) && eval (s, pathc)) {
sq.push_back (s);
p.insert (PathMap::value_type (s, state));
}
}
}
}
card_t*
Graph::path (card_t state,
const card_t* loop,
const class Expression* pathc) const
{
if (!eval (state, pathc))
return 0;
/** Visited states */
class BitVector visited (myNumStates);
visited.assign (state, true);
/** Search queue */
std::list<card_t> sq;
sq.push_front (CARD_T_MAX); /* mark for the next level */
sq.push_back (state);
/** Arcs in the explored graph (target, source) */
PathMap p;
for (;;) {
if ((state = *sq.begin ()) == CARD_T_MAX) {
sq.pop_front ();
if (sq.empty ())
return 0;
sq.push_back (CARD_T_MAX);
state = *sq.begin ();
}
sq.pop_front ();
assert (!sq.empty ());
assert (visited[state]);
for (const card_t* l = loop + *loop; l > loop; l--) {
if (state == *l) {
card_t* q = toPath (p, state);
// append the loop to the p
card_t* r = new card_t[*q + *loop];
*r = *q + *loop - 1;
memcpy (r + *loop, q + 1, *q * sizeof *q);
delete[] q;
q = r + *loop;
const card_t* k = l;
for (; l++ < loop + *loop; *--q = *l);
for (l = loop + 1; l++ < k; *--q = *l);
assert (q == r + 1);
return r;
}
}
if (card_t* succ = getSuccessors (state)) {
for (assert (*succ > 0); *succ; (*succ)--) {
card_t s = succ[*succ];
if (!visited.tset (s) && eval (s, pathc)) {
sq.push_back (s);
p.insert (PathMap::value_type (s, state));
}
}
delete[] succ;
}
}
}
bool
Graph::flagErroneous (card_t number)
{
assert (number < myNumStates);
#ifdef USE_MMAP
UNALIGNED fpos_t& num = *DIRSLOT1 (number);
#else // USE_MMAP
fseek (myFiles.directory, DIRSLOT1 (number), SEEK_SET);
fpos_t num;
if (fread (&num, sizeof num, 1, myFiles.directory) != 1)
assert (false);
#endif // USE_MMAP
if (::isFlagged (num))
return true;
::flag (num);
#ifndef USE_MMAP
fseek (myFiles.directory, -sizeof num, SEEK_CUR);
if (fwrite (&num, sizeof num, 1, myFiles.directory) != 1)
assert (false);
#endif // !USE_MMAP
return false;
}
bool
Graph::isErroneous (card_t number) const
{
assert (number < myNumStates);
#ifdef USE_MMAP
return ::isFlagged (*DIRSLOT1 (number));
#else // USE_MMAP
fseek (myFiles.directory, DIRSLOT1 (number), SEEK_SET);
fpos_t num;
if (fread (&num, sizeof num, 1, myFiles.directory) != 1)
assert (false);
return ::isFlagged (num);
#endif // USE_MMAP
}
bool
Graph::isProcessed (card_t number) const
{
fpos_t num;
assert (number < myNumStates);
#ifdef USE_MMAP
num = *DIRSLOT2 (number);
#else // USE_MMAP
fseek (myFiles.directory, DIRSLOT2 (number), SEEK_SET);
if (fread (&num, sizeof num, 1, myFiles.directory) != 1)
assert (false);
#endif // USE_MMAP
return num != FPOS_NONE;
}
void
Graph::addReverse (card_t source, card_t target)
{
myNumArcs++;
#ifdef USE_MMAP
UNALIGNED fpos_t& arcend = *DIRSLOT3 (target);
const fpos_t arcoffset = arcend;
const fpos_t len = myFiles.preds.len;
arcend = myFiles.preds.len;
myFiles.preds.len += (sizeof arcoffset) + (sizeof source);
extend (myFiles.preds);
char* buf = static_cast<char*>(myFiles.preds.addr) + len;
*reinterpret_cast<UNALIGNED card_t*>(buf) = source;
*reinterpret_cast<UNALIGNED fpos_t*>(buf + sizeof source) = arcoffset;
#else // USE_MMAP
fpos_t arcoffset;
fseek (myFiles.directory, DIRSLOT3 (target), SEEK_SET);
if (1 != fread (&arcoffset, sizeof arcoffset, 1, myFiles.directory)) {
perror ("Graph::addReverse (): fread");
abort ();
}
fseek (myFiles.directory, -sizeof arcoffset, SEEK_CUR);
fseek (myFiles.preds, 0, SEEK_END);
fpos_t arcend = ftell (myFiles.preds);
fwrite (&arcend, sizeof arcend, 1, myFiles.directory);
fwrite (&source, sizeof source, 1, myFiles.preds);
fwrite (&arcoffset, sizeof arcoffset, 1, myFiles.preds);
#endif // USE_MMAP
}
|