spot/iface/nips/nips_vm/split.c
Guillaume Sadegh bc5f13bb4e NIPS VM added to the SPOT distribution.
2008-05-29  Guillaume SADEGH  <sadegh@lrde.epita.fr>

	* iface/nips/nips.cc, iface/nips/nips.hh, iface/nips/common.cc,
	iface/nips/common.hh, iface/nips/Makefile.am: TGBA implementation
	with the NIPS library.
	* iface/nips/emptiness_check.cc: Emptiness check on a Promela
	interface.
	* iface/nips/dottynips.cc: Dot printer on the NIPS interface.
	* iface/nips/compile.sh: Add. Wrapper around nips compiler to
	compile Promela to NIPS bytecode.
	* iface/nips/nips_vm,iface/nips/nips_vm/bytecode.h,
	iface/nips/nips_vm/ChangeLog, iface/nips/nips_vm/COPYING,
	iface/nips/nips_vm/hashtab.c, iface/nips/nips_vm/hashtab.h,
	iface/nips/nips_vm/INSTALL, iface/nips/nips_vm/instr.c,
	iface/nips/nips_vm/instr.h, iface/nips/nips_vm/instr_step.c,
	iface/nips/nips_vm/instr_step.h,
	iface/nips/nips_vm/instr_tools.c,
	iface/nips/nips_vm/instr_tools.h,
	iface/nips/nips_vm/instr_wrap.c,
	iface/nips/nips_vm/instr_wrap.h,
	iface/nips/nips_vm/interactive.c,
	iface/nips/nips_vm/interactive.h, iface/nips/nips_vm/main.c,
	iface/nips/nips_vm/Makefile, iface/nips/nips_vm/Makefile.am,
	iface/nips/nips_vm/nips_asm_help.pl,
	iface/nips/nips_vm/nips_asm_instr.pl,
	iface/nips/nips_vm/nips_asm.pl,
	iface/nips/nips_vm/nips_disasm.pl, iface/nips/nips_vm/nipsvm.c,
	iface/nips/nips_vm/nipsvm.h, iface/nips/nips_vm/README,
	iface/nips/nips_vm/rt_err.c, iface/nips/nips_vm/rt_err.h,
	iface/nips/nips_vm/search.c, iface/nips/nips_vm/search.h,
	iface/nips/nips_vm/split.c, iface/nips/nips_vm/split.h,
	iface/nips/nips_vm/state.c, iface/nips/nips_vm/state.h,
	iface/nips/nips_vm/state_inline.h,
	iface/nips/nips_vm/state_parts.c,
	iface/nips/nips_vm/state_parts.h, iface/nips/nips_vm/timeval.h,
	iface/nips/nips_vm/tools.h: NIPS VM added to the SPOT
	distribution.
	* configure.ac, iface/Makefile.am: Build system updated for the
	NIPS front-end.
2008-05-30 13:22:00 +02:00

105 lines
3.4 KiB
C

/* NIPS VM - New Implementation of Promela Semantics Virtual Machine
* Copyright (C) 2005: Stefan Schuermans <stefan@schuermans.info>
* Michael Weber <michaelw@i2.informatik.rwth-aachen.de>
* Lehrstuhl fuer Informatik II, RWTH Aachen
* Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
*/
#include <stdio.h>
#include <string.h>
#include "split.h"
#include "state_parts.h"
#include "tools.h"
#include "nipsvm.h"
// out context for splitting up the state and reassembling ist
typedef struct t_split_ctx
{
char *parts[512];
unsigned int part_cnt;
char *p_buf;
unsigned int buf_len;
unsigned int parts_size;
unsigned int restore_cnt;
} st_split_ctx;
// callback to save a part
void part_cb( char *p_part, unsigned int part_size, void *vp_ctx )
{
st_split_ctx *p_ctx = (st_split_ctx *)vp_ctx;
if( p_ctx->part_cnt < count( p_ctx->parts ) && // free pointer storage
part_size <= p_ctx->buf_len ) // free buffer space
{
memcpy( p_ctx->p_buf, p_part, part_size ); // copy part
p_ctx->parts[p_ctx->part_cnt] = p_ctx->p_buf; // save pointer
p_ctx->part_cnt++; // count pointers
p_ctx->p_buf += part_size; // reduce buffer space
p_ctx->buf_len -= part_size;
p_ctx->parts_size += part_size; // count total size
}
}
// callbacks to deliver parts for restoring state
unsigned int restore_cb( char *p_buf, unsigned int buf_len, void *vp_ctx, unsigned int (*part_size)( char * ) )
{
st_split_ctx *p_ctx = (st_split_ctx *)vp_ctx;
if( p_ctx->restore_cnt >= p_ctx->part_cnt ) // no more parts
return 0;
unsigned int sz = part_size( p_ctx->parts[p_ctx->restore_cnt] ); // get size
if( buf_len < sz ) // no more buffer space
return 0;
memcpy( p_buf, p_ctx->parts[p_ctx->restore_cnt], sz ); // copy part
p_ctx->restore_cnt++; // next part
return sz;
}
unsigned int glob_cb( char *p_buf, unsigned int buf_len, void *vp_ctx )
{
return restore_cb( p_buf, buf_len, vp_ctx, glob_part_size );
}
unsigned int proc_cb( char *p_buf, unsigned int buf_len, void *vp_ctx )
{
return restore_cb( p_buf, buf_len, vp_ctx, proc_part_size );
}
unsigned int chan_cb( char *p_buf, unsigned int buf_len, void *vp_ctx )
{
return restore_cb( p_buf, buf_len, vp_ctx, chan_part_size );
}
// split a state and recreate it from its parts - then compare it
// - used to test state_parts
// - outputs some info if stream != NULL
// - returns boolean flag if success
int split_test( st_global_state_header * p_glob, FILE * stream ) // extern
{
char buffer[65536]; // some memory to store the parts of the state
st_split_ctx ctx;
ctx.part_cnt = 0;
ctx.p_buf = buffer;
ctx.buf_len = sizeof( buffer );
ctx.parts_size = 0;
ctx.restore_cnt = 0;
// split up state
state_parts( p_glob, part_cb, part_cb, part_cb, &ctx );
if( stream != NULL )
fprintf( stream, "splitted state into %d parts with total size %d\n",
ctx.part_cnt, ctx.parts_size );
// restore state
char restore[65536];
st_global_state_header *p_restored = state_restore( restore, sizeof( restore ), glob_cb, proc_cb, chan_cb, &ctx );
if( p_restored == NULL )
return 0;
// compare
int ok = nipsvm_state_compare( p_glob, p_restored, nipsvm_state_size (p_glob) ) == 0;
if( stream != NULL )
fprintf( stream, "comparing restored state with original one: %s\n",
ok ? "identical" : "DIFFERENT" );
return ok;
}