File: FullSet.C

package info (click to toggle)
maria 1.3.5-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,980 kB
  • ctags: 5,458
  • sloc: cpp: 43,402; yacc: 8,080; ansic: 436; sh: 404; lisp: 395; makefile: 291; perl: 21
file content (423 lines) | stat: -rw-r--r-- 9,750 bytes parent folder | download | duplicates (6)
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
// Lossless reachability set storage -*- c++ -*-

/** @file FullSet.C
 * Transient, lossless reachability set storage
 */

/* Copyright  2002-2003 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. */

#ifdef __GNUC__
# pragma implementation
# ifdef __sgi
#  define _XOPEN_SOURCE 1
#  define _XOPEN_SOURCE_EXTENDED 1
# endif // __sgi
#endif // __GNUC__

#include "FullSet.h"
#include "BTree.h"
#include "ByteBuffer.h"

#include <stdlib.h> // abort(3)

#if defined __digital__
# define fileno(f) ((f)->_file)
#endif // __digital__

#if defined __CYGWIN__
# define fileno(f) __sfileno(f)
#endif // __CYGWIN__

#ifdef NO_MMAP
/** an empty file structure */
# define NULL_F { 0, 0, 0 }
#else // NO_MMAP
# ifdef USE_MMAP
/** an empty file structure */
#  define NULL_F { -1, 0, 0, 0 }
# else // USE_MMAP
/** an empty file structure */
#  define NULL_F 0
# endif // USE_MMAP
#endif // NO_MMAP

/** 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
#ifdef NO_MMAP
  return bool (file.addr);
# else // NO_MMAP
  return file.fd >= 0;
# endif // NO_MMAP
#else // USE_MMAP
  return bool (file);
#endif // USE_MMAP
}

/** an unopened file */
static const file_t nofile = NULL_F;

/** Open a temporary file
 * @return	an opened file, or something on which isOpen() does not hold
 */
static file_t
tempFile (void)
{
#ifdef USE_MMAP
# ifdef NO_MMAP
  file_t f = { 0, 16384, malloc (16384) };
  if (!f.addr) {
    perror ("malloc");
    abort ();
  }
# else // NO_MMAP
  FILE* file = tmpfile ();
  if (!file) {
    perror ("FullSet: tmpfile");
    return nofile;
  }
  file_t f;
  f.fd = dup (fileno (file));
  fclose (file);
  if (f.fd < 0) {
    perror ("FullSet: dup");
    return nofile;
  }
  f.len = 0;
  f.alloc = 16384;
  if (ftruncate (f.fd, f.alloc)) {
    perror ("FullSet: ftruncate");
    close (f.fd);
    return nofile;
  }
  f.addr = mmap (0, f.alloc, PROT_READ | PROT_WRITE, MAP_SHARED, f.fd, 0);
  if (f.addr == reinterpret_cast<void*>(MAP_FAILED)) {
    close (f.fd);
    perror ("FullSet: mmap");
    return nofile;
  }
# endif // NO_MMAP
  return f;
#else // USE_MMAP
  FILE* file = tmpfile ();
  if (!file) {
    perror ("FullSet: tmpfile");
    return nofile;
  }
  return 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 ("FullSet: 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 ("FullSet: 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 ("FullSet: extend: mmap");
    abort ();
  }
# endif // NO_MMAP
}
#endif // USE_MMAP

/** Close a temporary file
 * @param f	the file to be closed
 */
static void
closeFile (file_t& f)
{
#ifdef USE_MMAP
# ifdef NO_MMAP
  if (f.addr)
    free (f.addr);
# else // NO_MMAP
  munmap (f.addr, f.alloc);
  close (f.fd);
# endif // NO_MMAP
#else // USE_MMAP
  fclose (f);
#endif // USE_MMAP
}

FullSet::FullSet () : StateSet (),
  myHash (0), myStates (nofile)
{
}

FullSet::~FullSet ()
{
#ifdef USE_MMAP
  myHash->cleanup ();
#endif // USE_MMAP
  delete myHash;
  if (isOpen (myStates))
    closeFile (myStates);
  mySearch.clear_allocated ();
}

bool
FullSet::init (bool path)
{
#ifndef NO_MMAP
  assert (!isOpen (myStates));
#endif // !NO_MMAP
  if (path && !openFile ())
    return false;
  file_t f;
  if (!isOpen (myStates = tempFile ()) ||
      !isOpen (f = tempFile ())) {
    if (isOpen (myStates))
      closeFile (myStates);
    return false;
  }
  myHash = new class BTree (f);
  return true;
}

void
FullSet::clear ()
{
  assert (isOpen (myStates));
  assert (!!myHash);
  myHash->clear ();
#ifdef USE_MMAP
  myStates.len = 0;
#else // USE_MMAP
  fclose (myStates);
  myStates = tempFile ();
#endif // USE_MMAP
  mySearch.clear_allocated ();
  if (myPathFile)
    fseek (myPathFile, myPathFileLength = myOffset = 0, SEEK_SET);
}

/** 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;
}

/** Read an encoded state from the file
 * @param f		the state storage file
 * @param offset	offset to the file (bytes from the start of the file)
 * @param len		number of bytes to read
 */
static word_t*
getState (const file_t& f,
	  long offset,
	  unsigned len)
{
  word_t* state =
    new word_t[(len + (sizeof (word_t) - 1)) / sizeof (word_t)];
#ifdef USE_MMAP
  assert (!len || ssize_t (offset + len) <= f.len);
  memcpy (state, static_cast<char*>(f.addr) + offset, len);
#else // USE_MMAP
  if (fseek (f, offset, SEEK_SET)) {
    perror ("FullSet: fseek");
    abort ();
  }
  if (fread (state, 1, len, f) != len) {
    perror ("FullSet: fread");
    abort ();
  }
#endif // USE_MMAP
  return state;
}

/** Find an encoded state in the set
 * @param f		the state file
 * @param offset	offset to the state file
 * @param buf		compare this state against the one at offset
 * @param tmp		work storage for encoded states
 * @param bytes		length of the encoded state in bytes
 * @return		whether buf matches the state
 */
static bool
cmpfetch (const file_t& f,
	  unsigned offset,
	  const void* buf,
#ifndef USE_MMAP
	  void* tmp,
#endif // !USE_MMAP
	  size_t bytes)
{
#ifdef USE_MMAP
  return !bytes ||
    (ssize_t (offset + bytes) <= f.len &&
     !memcmp (static_cast<char*>(f.addr) + offset, buf, bytes));
#else // USE_MMAP
  if (fseek (f, offset, SEEK_SET)) {
    perror ("FullSet: fseek");
    abort ();
  }
  return
    fread (tmp, 1, bytes, f) == bytes &&
    !memcmp (tmp, buf, bytes);
#endif // USE_MMAP
}

bool
FullSet::do_add (const void* buf,
		 size_t size)
{
  size_t h = getHashValue (reinterpret_cast<const unsigned char*>(buf), size);
  if (unsigned* offsets = myHash->search (h)) {
#ifndef USE_MMAP
    word_t* tmp =
      new word_t[(size + (sizeof (word_t) - 1)) / sizeof (word_t)];
#endif // !USE_MMAP

    for (unsigned i = *offsets; i; i--) {
      if (::cmpfetch (myStates, offsets[i], buf,
#ifndef USE_MMAP
		      tmp,
#endif // !USE_MMAP
		      size)) {
#ifndef USE_MMAP
	delete[] tmp;
#endif // !USE_MMAP
	delete[] offsets;
	return false;
      }
    }

#ifndef USE_MMAP
    delete[] tmp;
#endif // !USE_MMAP
    delete[] offsets;
  }

  // a new state: add it to disk
#ifdef USE_MMAP
  const long len = myStates.len;
  myStates.len += size;
  extend (myStates);
  memcpy (static_cast<char*>(myStates.addr) + len, buf, size);
#else // USE_MMAP
  if (fseek (myStates, 0, SEEK_END)) {
    perror ("FullSet: fseek");
    abort ();
  }
  const long len = ftell (myStates);
  if (fwrite (buf, 1, size, myStates) != size) {
    perror ("FullSet: fwrite");
    abort ();
  }
#endif // USE_MMAP

  myHash->insert (h, len);
  newState ();

  assert (!myPathFile || myPathFileLength == ftell (myPathFile));
  assert (!myOffset || myOffset < myPathFileLength);

  mySearch.push_allocated (reinterpret_cast<word_t*>(len),
			   size, myPathFileLength);
  if (myPathFile) {
    class BytePacker p;
    p.append (myOffset), p.append (len), p.append (size);
    fwrite (p.getBuf (), 1, p.getLength (), myPathFile);
    myPathFileLength += p.getLength ();
  }
  return true;
}

word_t*
FullSet::getState (long pos, size_t* size) const
{
  unsigned char rbuf[12];
  class ByteUnpacker u (rbuf);
  assert (!!myPathFile);
  assert (pos < myPathFileLength);
  fseek (myPathFile, pos, SEEK_SET);
  fread (rbuf, sizeof rbuf, 1, myPathFile);
  u.extract ();
  unsigned offset = u.extract (), len = u.extract ();
  word_t* state = new word_t[len + (sizeof (word_t) - 1) / sizeof (word_t)];
#ifdef USE_MMAP
  assert (!len || ssize_t (offset + len) <= myStates.len);
  memcpy (state, static_cast<char*>(myStates.addr) + offset, len);
#else // USE_MMAP
  if (fseek (myStates, offset, SEEK_SET)) {
    perror ("FullSet: fseek");
    abort ();
  }
  fread (state, 1, len, myStates);
#endif // USE_MMAP
  *size = len;
  return state;
}

word_t*
FullSet::pop (bool tail, size_t& size)
{
  if (mySearch.empty ())
    return 0;
  else {
    unsigned long ofs = reinterpret_cast<unsigned long>
      (mySearch.pop (tail, myOffset, &size));
    return ::getState (myStates, ofs, size);
  }
}