Example #1
0
File: test.c Project: nsbradford/OS
void testMultithreaded(){
	int NUMTHREADS = 2;

	pthread_t threads[NUMTHREADS];
	vAddr indexes[100];
	int i;

	// indexes
	for (i = 0; i < 100; ++i) {
		if (DEBUG) 
			printf("Page %d is being created\n",i);
		
		indexes[i] = create_page();
		uint32_t *val = get_value(indexes[i]);
		if (val){
			uint32_t myrand = (*val + rand()) %1000+1000;
			store_value(indexes[i], &myrand);
		}
	}
	
	// threads
	for (i=0; i<NUMTHREADS; i++){
		uint32_t *thread_id = malloc(sizeof(int));
		*thread_id = i;
		pthread_create (&(threads[i]),NULL, (void *) &multithreadedHelper, thread_id);
	}
	for (i=0; i<NUMTHREADS; i++){
		pthread_join (threads[i], NULL);
	}
}
Example #2
0
 void register_buildin_func(
     var_key_type const& key,
     F const& f
     )
 {
     auto fp = allocate_object<object::builtin_function<F>>( f );
     store_value( key, fp );
 }
void memoryTester() {
  vAddr indexes[1000];
  uint32_t i = 0;
  for( ; i < 1000; i++ )
    indexes[i] = create_page();
  for( i = 0; i < 1000; ++i ) {
    uint32_t value = i;
    store_value(indexes[i], &value);
  }
  for( i = 0; i < 1000; i++ )
    free_page(indexes[i]);
}
Example #4
0
gpg_error_t
_ksba_der_store_octet_string (AsnNode node, const char *buf, size_t len)
{
  if (node->type == TYPE_ANY)
    node->type = TYPE_OCTET_STRING;

  if (node->type == TYPE_OCTET_STRING)
    {
      return store_value (node, buf, len);
    }
  else
    return gpg_error (GPG_ERR_INV_VALUE);
}
Example #5
0
File: test.c Project: nsbradford/OS
/**
 * multithreadedHelper()
 *
 */
void multithreadedHelper(int *threadID){
	int i;

	for (i=0; i<300; i++){
		
		int address = rand()%1000;
		uint32_t *val = get_value(address);
		if (val){
			uint32_t myrand = (*val + rand()) %1000+1000;
			store_value(address, &myrand);
		}	
	}
}
Example #6
0
Double_t KVPartitionFunction::sneppen_Nclass(int A, int Z, int M, int B)
{
    if(A>0&&Z>=0&&M>0&&B>=0){
            Double_t snc = get_value(A,Z,M,B);
            if(snc<0){
                snc = calc_sneppen_Nclass(A,Z,M,B);
                store_value(snc,A,Z,M,B);
                maxvalueNclass = TMath::Max(snc,maxvalueNclass);
            }
            return snc;
    }
    return 0;
}
Example #7
0
/* Store the integer VALUE in NODE.  VALUE is assumed to be a DER
   encoded integer prefixed with 4 bytes given its length in network
   byte order. */
gpg_error_t
_ksba_der_store_integer (AsnNode node, const unsigned char *value)
{
  if (node->type == TYPE_INTEGER)
    {
      size_t len;

      len = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
      return store_value (node, value+4, len);
    }
  else
    return gpg_error (GPG_ERR_INV_VALUE);
}
Example #8
0
gpg_error_t
_ksba_der_store_sequence (AsnNode node, const unsigned char *buf, size_t len)
{
  if (node->type == TYPE_ANY)
    node->type = TYPE_PRE_SEQUENCE;

  if (node->type == TYPE_SEQUENCE || node->type == TYPE_PRE_SEQUENCE)
    {
      return store_value (node, buf, len);
    }
  else
    return gpg_error (GPG_ERR_INV_VALUE);
}
void memoryMaxer() {
	vAddr indexes[1000];
	int i = 0;
	for (i = 0; i < 128; ++i) {
		indexes[i] = create_page();
		int *value = get_value(indexes[i]);
		*value = (i * 3);
		store_value(indexes[i], value);
	}
	
	for (i = 0; i < 128; ++i) {
		free_page(indexes[i]);
	}
}
Example #10
0
Double_t KVPartitionFunction::sneppen_Np(int A, int Z, int M)
{
    if(A>0&&Z>=0&&M>0){
        if(Z>A-Z) Z=A-Z; // symmetry
            Double_t snc = get_value(A,Z,M);
            if(snc<0){
                snc = calc_sneppen_Np(A,Z,M);
                store_value(snc,A,Z,M);
                maxvalueNp = TMath::Max(snc,maxvalueNp);
            }
            return snc;
    }
    return 0;
}
Example #11
0
File: test.c Project: nsbradford/OS
/**
 * Test function. Used in a multithreaded manner during stress testing.
 */
void memoryMaxer(){
	printf("\n------------------------------\nTest memoryMaxer()...\n");
	vAddr indexes[1000];
	int i;
	for (i = 0; i < 1000; i++){
		indexes[i] = create_page();
		uint32_t *value = get_value(indexes[i]);
		*value = (i * 3);
		store_value (indexes[i], value);
	}
	for (i = 0; i < 1000; i++){
		free_page(indexes[i]);
	}
	printf("\n------------------------------\nTest memoryMaxer() SUCCESS!\n");
}
// Testing functions and some helper functions
void memoryMaxer() {
  vAddr indexes[1000];
  uint32_t i = 0;
  for ( ; i < 1000; ++i) {
    indexes[i] = create_page();
    int valid = 0; 
    uint32_t value = get_value(indexes[i], &valid);
    if(!valid) return; // Address not found
    value = (i * 3);
    store_value(indexes[i], &value);
  }
  for (i = 0; i < 1000; ++i) {
    free_page(indexes[i]);
  }
}
Example #13
0
static config_t process_scalar(
                   yaml_event_t event,
                   const char * key,
                   config_t config){
    
    config_t conf = config;
    char * val = (char *)event.data.scalar.value;
               
    conf = store_value(
                    get_key(key, conf), 
                    val, 
                    conf);
                 
    return conf;                       
}
Example #14
0
File: test.c Project: nsbradford/OS
/**
 * testRAM();
 * This function puts everything into RAM and then successfully takes it out.
 */
void testRAM(){
	printf("\n------------------------------\nTest testRAM()...\n");
	vAddr indexes[10];
	int i;
	for (i = 0; i < 10; i++){
		indexes[i] = create_page();
		uint32_t *val = get_value(indexes[i]);
		*val = (i * 3);
		store_value (indexes[i], val);
	}
	for (i = 0; i < 10; i++){
		free_page(indexes[i]);
	}
	printf("\n------------------------------\nTest testRAM() SUCCESS!\n");
}
Example #15
0
    int main(int argc, char const *argv[])
    {
        

int i,j,initValues[NUMTHREADPAGES],returnValues[NUMTHREADPAGES];
struct timeval start_time, end_time;

    gettimeofday(&start_time, NULL);


//create
    for (i = 0; i < NUMTHREADPAGES; ++i) {
        memory[i] = create_page();
    }
    
    printf("%d %d\n", memory[10], memory[100]);


//store
    for (i = 0; i < NUMTHREADPAGES; ++i) {
        j = i*3;
        store_value(memory[i], &j);
        initValues[i]=j;
    }


//get
    for (i = 0; i < NUMTHREADPAGES; ++i) {
        j= *get_value(memory[i]);

        if(j != initValues[i])
            printf("ErrorInMemory! expected: %d, actual: %d\n",  initValues[i],j);
    }

//free
    for (i = 0; i < NUMTHREADPAGES; ++i) {
        free_page(memory[i]);
    }


    gettimeofday(&end_time, NULL);


    double msec = (end_time.tv_sec - start_time.tv_sec) * 1000 +
                  (end_time.tv_usec - start_time.tv_usec) / 1000.;

    printf("Thread: %d time = %f\n", 1, msec);
}
Example #16
0
/* Store the utf-8 STRING in NODE. */
gpg_error_t
_ksba_der_store_string (AsnNode node, const char *string)
{
  if (node->type == TYPE_CHOICE)
    {
      /* find a suitable choice to store the value */
    }


  if (node->type == TYPE_PRINTABLE_STRING)
    {
      return store_value (node, string, strlen (string));
    }
  else
    return gpg_error (GPG_ERR_INV_VALUE);
}
Example #17
0
gpg_error_t
_ksba_der_store_time (AsnNode node, const ksba_isotime_t atime)
{
  char buf[50], *p;
  int need_gen;
  gpg_error_t err;

  /* First check that ATIME is indeed as formatted as expected. */
  err = _ksba_assert_time_format (atime);
  if (err)
    return err;

  memcpy (buf, atime, 8);
  memcpy (buf+8, atime+9, 6);
  strcpy (buf+14, "Z");

  /* We need to use generalized time beginning with the year 2050. */
  need_gen = (_ksba_cmp_time (atime, "20500101T000000") >= 0);

  if (node->type == TYPE_ANY)
    node->type = need_gen? TYPE_GENERALIZED_TIME : TYPE_UTC_TIME;
  else if (node->type == TYPE_CHOICE)
    { /* find a suitable choice to store the value */
      AsnNode n;

      for (n=node->down; n; n=n->right)
        {
          if ( (need_gen && n->type == TYPE_GENERALIZED_TIME)
               || (!need_gen && n->type == TYPE_UTC_TIME))
            {
              node = n;
              break;
            }
        }
    }

  if (node->type == TYPE_GENERALIZED_TIME
      || node->type == TYPE_UTC_TIME)
    {
      p = node->type == TYPE_UTC_TIME? (buf+2):buf;
      return store_value (node, p, strlen (p));
    }
  else
    return gpg_error (GPG_ERR_INV_VALUE);
}
Example #18
0
int LCG64::pack_rng( std::string &buffer ) const
{
  // Clear the buffer
  buffer.clear();

  // All member data will be stored in a sub buffer and appended to main buffer
  std::string partial_buffer;

  // Store the generator type
  store_value( generatorTypeToInt( d_rng_type ), partial_buffer );
  buffer += partial_buffer;

  // Store the stream number
  store_value( d_stream_number, partial_buffer );
  buffer += partial_buffer;
  
  // Store the initial seed
  store_value( d_init_seed, partial_buffer );
  buffer += partial_buffer;
  
  // Store the parameter
  store_value( d_parameter, partial_buffer );
  buffer += partial_buffer;
  
  // Store the spawn offset
  store_value( d_spawn_offset, partial_buffer );
  buffer += partial_buffer;
  
  // Store the prime
  store_value( d_prime, partial_buffer );
  buffer += partial_buffer;
  
  // Store the state
  store_value( d_state, partial_buffer );
  buffer += partial_buffer;
  
  // Store the multiplier
  store_value( d_multiplier, partial_buffer );
  buffer += partial_buffer;

  return buffer.size();
}
Example #19
0
int do_word(
    STACK *stack,
    TEXT_RLD *tr,
    char *cp,
    int size)
{
    int comma;

    if (size == 2 && (DOT & 1)) {
        report(stack->top, ".WORD on odd boundary\n");
        store_word(stack->top, tr, 1, 0);       /* Align it */
    }

    cp = skipwhite(cp);

    do {
        if (cp[0] == ',') {
            /* Empty expressions count as 0 */
            store_word(stack->top, tr, size, 0);
        } else {
            EX_TREE        *value = parse_expr(cp, 0);

            if (value->cp > cp) {
                store_value(stack, tr, size, value);

                cp = value->cp;
            } else {
                report(stack->top, "Invalid expression in .WORD\n");
                cp = "";                /* force loop to end */
            }

            free_tree(value);
        }
    } while (cp = skipdelim_comma(cp, &comma), !EOL(*cp));

    if (comma) {
        /* Trailing empty expressions count as 0 */
        store_word(stack->top, tr, size, 0);
    }

    return 1;
}
Example #20
0
/* Copy all values from the tree SRC (with values store in SRCIMAGE)
   to the tree DST */
gpg_error_t
_ksba_der_copy_tree (AsnNode dst_root,
                     AsnNode src_root, const unsigned char *src_image)
{
  AsnNode s, d;

  s = src_root;
  d = dst_root;
  /* note: we use the is_any flags becuase an inserted copy may have
     already changed the any tag to the actual type */
  while (s && d && (s->type == d->type || d->flags.is_any))
    {
      if (d->flags.is_any)
        d->type = s->type;

      if (s->flags.in_array && s->right)
        {
          if (!_ksba_asn_insert_copy (d))
            return gpg_error (GPG_ERR_ENOMEM);
        }

      if ( !_ksba_asn_is_primitive (s->type) )
        ;
      else if (s->off == -1)
        clear_value (d);
      else
        store_value (d, src_image + s->off + s->nhdr, s->len);

      s = _ksba_asn_walk_tree (src_root, s);
      d = _ksba_asn_walk_tree (dst_root, d);
    }

  if (s || d)
    {
/*        fputs ("ksba_der_copy_tree: trees don't match\nSOURCE TREE:\n", stderr); */
/*        _ksba_asn_node_dump_all (src_root, stderr); */
/*        fputs ("DESTINATION TREE:\n", stderr); */
/*        _ksba_asn_node_dump_all (dst_root, stderr); */
      return gpg_error (GPG_ERR_ENCODING_PROBLEM);
    }
  return 0;
}
Example #21
0
File: test.c Project: nsbradford/OS
/**
 * testFullMemory()
 * This function tries to add more than 1000 pages, and shows failure to do so
 */
void testFullMemory(){
	printf("\n------------------------------\nTest testFullMemory()...\n");	
	vAddr indexes[1015];
	int i;

	for (i = 0; i < 1000; i++){
		indexes[i] = create_page();
		uint32_t *value = get_value(indexes[i]);
		*value = (i * 3);
		store_value (indexes[i], value);
	}

	for (i = 0; i < 1000; i++){
		indexes[i] = create_page();
		// this should fail, and return -1
		if (indexes[i] == -1){
			//printf(" Failed to allocate new page in memory. 1000 pages already in memory. \n");
		}
	}

	printf("\n------------------------------\nTest testFullMemory() SUCCESS!\n");
}
void memoryTester() {
  vAddr indexes[1000];
  uint32_t i = 0;
  for( ; i < 1000; i++ )
    indexes[i] = create_page();
  for( i = 0; i < 1000; ++i ) {
    uint32_t value = i;
    store_value(indexes[i], &value);
  }
  for( i = 0; i < 1000; i++ ) {
    uint32_t expectedValue = i;
    int valid = 0;
    uint32_t value = get_value(indexes[i], &valid);
    if(!valid) return; // Address not found
    if(value != expectedValue) {
      printf("Failure to prove correctness of the algorithm! Exitting...\n");
      exit(1);
    }
  }
  for( i = 0; i < 1000; i++ )
    free_page(indexes[i]);
}
Example #23
0
gpg_error_t
_ksba_der_store_oid (AsnNode node, const char *oid)
{
  gpg_error_t err;

  if (node->type == TYPE_ANY)
    node->type = TYPE_OBJECT_ID;

  if (node->type == TYPE_OBJECT_ID)
    {
      unsigned char *buf;
      size_t len;

      err = ksba_oid_from_str (oid, &buf, &len);
      if (err)
        return err;
      err = store_value (node, buf, len);
      xfree (buf);
      return err;
    }
  else
    return gpg_error (GPG_ERR_INV_VALUE);
}
Example #24
0
static int decode_csv_row (CSV_Type *csv, int flags)
{
   char *line;
   size_t line_ofs;
   char *value;
   size_t value_size, value_ofs;
   char delimchar, quotechar;
   int return_status;
   Values_Array_Type av;
   int do_read, in_quote;
   int blank_line_seen;
   int is_quoted;

   if (NULL == csv->read_callback)
     {
	SLang_verror (SL_InvalidParm_Error, "CSV decoder object has no read callback function");
	return -1;
     }
	
   if (-1 == init_values_array_type (&av))
     return -1;

   delimchar = csv->delimchar;
   quotechar = csv->quotechar;
   value_ofs = line_ofs = 0;
   value_size = 0;
   value = NULL;
   line = NULL;
   do_read = 1;

   in_quote = 0;
   return_status = -1;
   blank_line_seen = 0;
   is_quoted = 0;
   while (1)
     {
	int status;
	char ch;

	if (value_ofs == value_size)
	  {
	     char *new_value;

	     if (value_size < 64)
	       value_size += 32;
	     else if (value_size < 8192)
	       value_size *= 2;
	     else value_size += 8192;

	     new_value = SLrealloc (value, value_size);
	     if (new_value == NULL)
	       goto return_error;
	     value = new_value;
	  }

	NEXT_CHAR(ch)

	if ((ch == quotechar) && quotechar)
	  {
	     if (in_quote)
	       {
		  NEXT_CHAR(ch)
		  if (ch == quotechar)
		    {
		       value[value_ofs++] = ch;
		       continue;
		    }

		  if ((ch != ',') && (ch != 0) && (ch != '\n'))
		    {
		       SLang_verror (SL_Data_Error, "Expecting a delimiter after an end-quote character");
		       goto return_error;
		    }
		  in_quote = 0;
		  /* drop */
	       }
	     else if (value_ofs != 0)
	       {
		  SLang_verror (SL_Data_Error, "Misplaced quote character inside a csv field");
		  goto return_error;
	       }
	     else 
	       {
		  in_quote = 1;
		  is_quoted = 1;
		  continue;
	       }
	  }

	if (ch == delimchar)
	  {
	     if (in_quote)
	       {
		  value[value_ofs++] = ch;
		  continue;
	       }
	     value[value_ofs] = 0;
	     if (-1 == store_value (&av, value))
	       goto return_error;
	     value_ofs = 0;
	     continue;
	  }
	if ((ch == 0) || (ch == '\n'))
	  {
	     if (in_quote)
	       {
		  if (ch == '\n')
		    {
		       value[value_ofs++] = ch;
		       do_read = 1;
		       continue;
		    }
		  SLang_verror (SL_Data_Error, "No closing quote seen parsing CSV data");
		  goto return_error;
	       }

	     if ((ch == '\n') || (av.num != 0) || (value_ofs > 0))
	       {
		  if ((is_quoted == 0)
		      && (ch == '\n') && (av.num == 0) && (value_ofs == 0))
		    {
		       /* blank line */
		       int blank_line_behavior = (flags & BLANK_ROW_BEHAVIOR);
		       if (blank_line_behavior == CSV_SKIP_BLANK_ROWS)
			 {
			    do_read = 1;
			    continue;
			 }
		       if (blank_line_behavior == CSV_STOP_ON_BLANK_ROWS)
			 {
			    blank_line_seen = 1;
			    break;
			 }
		    }
		  value[value_ofs] = 0;
		  if (-1 == store_value (&av, value))
		    goto return_error;
	       }
	     break;		       /* done */
	  }

	value[value_ofs++] = ch;
     }
/**
 * This function parses received UART configuration requests from telnet
 * client, validates the requests and executes respective commands.
 * Various configuration commands supported are:
 * (a) UART configuration fetch (GET) - for a specified UART
 * (b) UART configuration apply (SET) -   for a specified UART
 * (c) UART configuration save (SAVE) - apply working UART configurations
 * for all UARTs repectively
 * (d) UART configuration retrieve (FETCH) - fetch UART configuration stored
 * in flash, for all UARTs
 * @param    c_xtcp       Channel-end between XTCP and TCP handler thread
 * @param    c_uart_configChannel-end to communicate UART configuration data
 *            TCP handler and UART handler thread
 * @param    c_flash_data Channel-end to communicate UART configuration data
 *            stored in flash to TCP handler thread
 * @param   conn          Reference to structure holding IP configuration info
 * @param   buf           Buffer containing received UART command
 * @param   len           length of buffer
 * @param   st            Reference to structure holding Telnet configuration
 *            state information
 * @return              None
 */
static void parse_config(chanend c_xtcp,
                         chanend c_uart_config,
                         chanend c_flash_data,
                         xtcp_connection_t *conn,
                         char *buf,
                         int len,
                         connection_state_t *st)
{
  char *end = buf + len;
  while (buf < end) {
    if (st->config_parsing_state0 == PARSING_SEP) {
      if (*buf=='~') {
        buf++;
        continue;
      }
      else {
        st->config_parsing_state0 = PARSING_VALUE;
        st->buf_len = 0;
        continue;
      }
    }
    switch (*buf)
      {
      case '~':
        if (st->config_parsing_state1 == PARSING_TERM) {
          st->err = invalid_cmd_msg;
          xtcp_init_send(c_xtcp, conn);
          reset_parsing_state(st);
        } else {
          store_value(st);
          st->config_parsing_state0 = PARSING_SEP;
          buf++;
        }
        break;
      case '@':
        if (st->config_parsing_state1 == PARSING_TERM) {
          execute_command(c_xtcp, c_uart_config, c_flash_data, conn, st);
          reset_parsing_state(st);
        }
        else {
          st->err = invalid_cmd_msg;
          xtcp_init_send(c_xtcp, conn);
          reset_parsing_state(st);
        }
        buf++;
        break;
      case 10:
      case 13:
        // Newline resets everything
        reset_parsing_state(st);
        buf++;
        break;
      default:
        if (st->buf_len < MAX_VALUE_LEN) {
          st->buf[st->buf_len] = *buf;
          st->buf_len++;
        }
        buf++;
        break;
      }

  }
}
Example #26
0
/*
 *   Parse a file 
 */
int CTadsGameInfo::parse_file(osfildef *fp, unsigned long res_seek_pos,
                              unsigned long res_size)
{
    /* find the tail of the existing list */
    tads_valinfo *last_val;
    for (last_val = first_val_ ; last_val != 0 && last_val->nxt != 0 ;
         last_val = last_val->nxt) ;
    
    /* we found the resource - seek to it in the file */
    if (osfseek(fp, res_seek_pos, OSFSK_SET))
        return FALSE;

    /* 
     *   Allocate a block of memory for loading the game information.  The
     *   game information resource is typically fairly small, so for
     *   simplicity we'll just allocate a single block of memory and load
     *   the whole resource into the block.  Allocate space for one extra
     *   byte, so that we can ensure we have a newline at the end of the
     *   buffer.  
     */
    buf_ = (char *)osmalloc(res_size + 1);
    if (buf_ == 0)
        return FALSE;

    /* read the data */
    if (osfrb(fp, buf_, res_size))
        return FALSE;

    /* 
     *   store an extra newline at the end of the buffer, so that we can be
     *   certain that the last line ends in a newline - at worst, this will
     *   add an extra blank line to the end, but since we ignore blank lines
     *   this will do no harm 
     */
    buf_[res_size++] = '\n';

    /* parse the data */
    utf8_ptr p(buf_);
    for (size_t rem = res_size ; rem != 0 ; )
    {
        /* skip any leading whitespace */
        while (rem != 0 && is_space(p.getch()))
            p.inc(&rem);

        /* if the line starts with '#', it's a comment, so skip it */
        if (rem != 0 && p.getch() == '#')
        {
            /* skip the entire line, and go back for the next one */
            skip_to_next_line(&p, &rem);
            continue;
        }

        /* we must have the start of a name - note it */
        utf8_ptr name_start = p;

        /* skip ahead to a space or colon */
        while (rem != 0 && p.getch() != ':' && !is_hspace(p.getch()))
            p.inc(&rem);

        /* note the length of the name */
        size_t name_len = p.getptr() - name_start.getptr();

        /* skip any whitespace before the presumed colon */
        while (rem != 0 && is_hspace(p.getch()))
            p.inc(&rem);

        /* if we're not at a colon, the line is ill-formed, so skip it */
        if (rem == 0 || p.getch() != ':')
        {
            /* skip the entire line, and go back for the next one */
            skip_to_next_line(&p, &rem);
            continue;
        }

        /* skip the colon and any whitespace immediately after it */
        for (p.inc(&rem) ; rem != 0 && is_hspace(p.getch()) ; p.inc(&rem)) ;

        /* 
         *   Whatever terminated the name, replace it with a null character
         *   - this is safe, since we at least have a colon character to
         *   replace, and we've already skipped it so we can overwrite it
         *   now.  A null character in utf-8 is simply a single zero byte,
         *   so we can store our byte directly without worrying about any
         *   utf-8 multi-byte strangeness.  
         */
        *(name_start.getptr() + name_len) = '\0';

        /* note where the value starts */
        utf8_ptr val_start = p;

        /* set up to write to the buffer at the current point */
        utf8_ptr dst = p;

        /*
         *   Now find the end of the value.  The value can span multiple
         *   lines; if we find a newline followed by a space, it means that
         *   the next line is a continuation of the current value, and that
         *   the entire sequence of newlines and immediately following
         *   whitespace should be converted to a single space in the value.
         *   
         *   Note that we copy the transformed value directly over the old
         *   version of the value in the buffer.  This is safe because the
         *   transformation can only remove characters - we merely collapse
         *   each newline-whitespace sequence into a single space.  
         */
        while (rem != 0)
        {
            /* get this character */
            wchar_t ch = p.getch();
            
            /* check for a newline */
            if (is_vspace(ch))
            {
                /* 
                 *   it's a newline - skip it (and any other characters in
                 *   the newline sequence) 
                 */
                skip_newline(&p, &rem);

                /* 
                 *   if there's no leading whitespace on the next line,
                 *   we've reached the end of this value
                 */
                if (rem == 0 || !is_hspace(p.getch()))
                {
                    /* 
                     *   no whitespace -> end of the value - stop scanning
                     *   the value 
                     */
                    break;
                }

                /* skip leading whitespace on the line */
                while (rem != 0 && is_hspace(p.getch()))
                    p.inc(&rem);

                /* 
                 *   add a single whitespace character to the output for the
                 *   entire sequence of the newline plus the leading
                 *   whitespace on the line 
                 */
                dst.setch(' ');
            }
            else if (ch == 0)
            {
                /* change null bytes to spaces */
                dst.setch(' ');

                /* skip this character */
                p.inc(&rem);
            }
            else
            {
                /* it's not a newline - simply copy it out and keep going */
                dst.setch(ch);

                /* skip this character */
                p.inc(&rem);
            }
        }

        /* 
         *   Store a null terminator at the end of the value (it's safe to
         *   write this to the buffer because at worst it'll overwrite the
         *   newline at the end of the last line, which we've already
         *   skipped in the source).  Note that we obtain the length of the
         *   value string before storing the null terminator, because we
         *   don't want to count the null byte in the value's length.  
         */
        dst.setch('\0');

        /* 
         *   Create a new value list entry.  Point the entry directly into
         *   our buffer, since we're keeping the buffer around as long as
         *   the value list is around. 
         */
        tads_valinfo *val_info =
            (tads_valinfo *)osmalloc(sizeof(tads_valinfo));
        val_info->name = name_start.getptr();
        val_info->name_len = name_len;
        val_info->val = store_value(val_start.getptr(),
                                    dst.getptr() - val_start.getptr());
        val_info->nxt = 0;

        /* link the new value at the end of our list */
        if (last_val != 0)
            last_val->nxt = val_info;
        else
            first_val_ = val_info;
        last_val = val_info;
    }

    /* success */
    return TRUE;
}
Example #27
0
int start_parser(struct p_parser *p, char *f) {
  
  FILE *file;
  int i,fd,pos;
  char c;
  char *buffer, *temp;
  struct stat file_stats;

  temp   = NULL;
  buffer = NULL;
  file   = fopen(f,"r");

  if (file == NULL){
    fprintf(stderr,"Error Reading File: %s\n",f);
    return errno;
  }
  fd     = fileno(file);
  if (stat(f,&file_stats) != 0)
    return errno;
  
  p->open_time = file_stats.st_atime;
  p->fsize     = file_stats.st_size; 

  buffer = mmap(NULL,p->fsize,PROT_READ,MAP_SHARED,fd,0);

  if (buffer == MAP_FAILED){
#ifdef DEBUG
    fprintf(stderr,"mmap failed: %s\n",strerror(errno));
#endif
    fclose(file);
    return EIO;
  }

#ifdef DEBUG
  fprintf(stderr,"fd: %d st_atime:%d st_size:%d mmap:%p\n",fd,(int)p->open_time,(int)p->fsize,buffer);
#endif

  p->state = S_START; 
  pos=0;

  for (i=0;i<p->fsize;i++){
    c = buffer[i];
    
    switch (p->state){
      
      case S_COMMENT:
        switch(c) {
          case '\n':
          case '\r':
            if (!store_comment(p,buffer,pos,i))
              return KATCP_RESULT_FAIL;
            p->state = S_START;
            pos = i+1;
            break;
        }
        break;

      case S_LABEL:
        switch(c){
          case CLABEL:
            if (!store_label(p,buffer,pos,i))
              return KATCP_RESULT_FAIL;
            p->state = S_START;
            pos = i;
            break;
        }
        break;

      case S_SETTING:
        if (!store_setting(p,buffer,pos,i-1))
          return KATCP_RESULT_FAIL;
        pos = i;
        p->state = S_VALUE;
        break;
      
      case S_VALUE:
        switch(c){
          case OLABEL:
            p->state = S_MLVALUE;
            pos = i;
            break;
          case VALUE:
            if (!store_value(p,buffer,pos,i))
              return KATCP_RESULT_FAIL;
            p->state = S_VALUE;
            pos = i+1;
            break;
          case '\n':
          case '\r':
            if (!store_value(p,buffer,pos,i))
              return KATCP_RESULT_FAIL;
            p->state = S_START;
            pos = i+1;
            break;
        }
        break;

      case S_MLVALUE:
          switch(c){
            case CLABEL:
              if (!store_value(p,buffer,pos,i+1))
                return KATCP_RESULT_FAIL;
              p->state = S_START;
              break;
          }
        break;

      default:
        switch (c){
          case COMMENT:
            p->state = S_COMMENT;
            pos = i;
            break;
          case OLABEL:
            p->state = S_LABEL;
            pos = i+1;
            break;
          case SETTING:
            p->state = S_SETTING;
            break;
          case '\n':
          case '\r':
            p->state = S_START;
            pos = i+1;
            break;
        }
    }
  }

  munmap(buffer,p->fsize);
  fclose(file);
  
  return EX_OK;
}
Example #28
0
/*@
 * @deftypefun void _jit_gen_insn (jit_gencode_t @var{gen}, jit_function_t @var{func}, jit_block_t @var{block}, jit_insn_t @var{insn})
 * Generate native code for the specified @var{insn}.  This function should
 * call the appropriate register allocation routines, output the instruction,
 * and then arrange for the result to be placed in an appropriate register
 * or memory destination.
 * @end deftypefun
@*/
void _jit_gen_insn(jit_gencode_t gen, jit_function_t func,
				   jit_block_t block, jit_insn_t insn)
{
	jit_label_t label;
	void **pc;
	jit_nint offset;
	jit_nint size;

	switch(insn->opcode)
	{
	case JIT_OP_BR_IEQ:
	case JIT_OP_BR_INE:
	case JIT_OP_BR_ILT:
	case JIT_OP_BR_ILT_UN:
	case JIT_OP_BR_ILE:
	case JIT_OP_BR_ILE_UN:
	case JIT_OP_BR_IGT:
	case JIT_OP_BR_IGT_UN:
	case JIT_OP_BR_IGE:
	case JIT_OP_BR_IGE_UN:
	case JIT_OP_BR_LEQ:
	case JIT_OP_BR_LNE:
	case JIT_OP_BR_LLT:
	case JIT_OP_BR_LLT_UN:
	case JIT_OP_BR_LLE:
	case JIT_OP_BR_LLE_UN:
	case JIT_OP_BR_LGT:
	case JIT_OP_BR_LGT_UN:
	case JIT_OP_BR_LGE:
	case JIT_OP_BR_LGE_UN:
	case JIT_OP_BR_FEQ:
	case JIT_OP_BR_FNE:
	case JIT_OP_BR_FLT:
	case JIT_OP_BR_FLE:
	case JIT_OP_BR_FGT:
	case JIT_OP_BR_FGE:
	case JIT_OP_BR_FLT_INV:
	case JIT_OP_BR_FLE_INV:
	case JIT_OP_BR_FGT_INV:
	case JIT_OP_BR_FGE_INV:
	case JIT_OP_BR_DEQ:
	case JIT_OP_BR_DNE:
	case JIT_OP_BR_DLT:
	case JIT_OP_BR_DLE:
	case JIT_OP_BR_DGT:
	case JIT_OP_BR_DGE:
	case JIT_OP_BR_DLT_INV:
	case JIT_OP_BR_DLE_INV:
	case JIT_OP_BR_DGT_INV:
	case JIT_OP_BR_DGE_INV:
	case JIT_OP_BR_NFEQ:
	case JIT_OP_BR_NFNE:
	case JIT_OP_BR_NFLT:
	case JIT_OP_BR_NFLE:
	case JIT_OP_BR_NFGT:
	case JIT_OP_BR_NFGE:
	case JIT_OP_BR_NFLT_INV:
	case JIT_OP_BR_NFLE_INV:
	case JIT_OP_BR_NFGT_INV:
	case JIT_OP_BR_NFGE_INV:
		/* Binary branch */
		load_value(gen, insn->value2, 2);
		/* Fall through */

	case JIT_OP_BR_IFALSE:
	case JIT_OP_BR_ITRUE:
	case JIT_OP_BR_LFALSE:
	case JIT_OP_BR_LTRUE:
		/* Unary branch */
		load_value(gen, insn->value1, 1);
		/* Fall through */

	case JIT_OP_BR:
	case JIT_OP_CALL_FINALLY:
		/* Unconditional branch */
	branch:
		label = (jit_label_t)(insn->dest);
		pc = (void **)(gen->ptr);
		jit_cache_opcode(gen, insn->opcode);
		block = jit_block_from_label(func, label);
		if(!block)
		{
			break;
		}
		if(block->address)
		{
			/* We already know the address of the block */
			jit_cache_native(gen, ((void **)(block->address)) - pc);
		}
		else
		{
			/* Record this position on the block's fixup list */
			jit_cache_native(gen, block->fixup_list);
			block->fixup_list = (void *)pc;
		}
		break;

	case JIT_OP_CALL_FILTER:
		/* Branch to a filter subroutine, load the filter
		   parameter to the r0 register */
		load_value(gen, insn->value1, 0);
		goto branch;

	case JIT_OP_JUMP_TABLE:
	{
		jit_label_t *labels;
		jit_nint num_labels;
		jit_nint index;

		load_value(gen, insn->dest, 0);

		labels = (jit_label_t *) insn->value1->address;
		num_labels = insn->value2->address;

		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, num_labels);
		for(index = 0; index < num_labels; index++)
		{
			block = jit_block_from_label(func, labels[index]);
			if(!block)
			{
				return;
			}
			if(block->address)
			{
				/* We already know the address of the block */
				jit_cache_native(gen, block->address);
			}
			else
			{
				/* Record this position on the block's fixup list */
				pc = (void **)(gen->ptr);
				jit_cache_native(gen, block->fixup_absolute_list);
				block->fixup_absolute_list = pc;
			}
		}
		break;
	}

	case JIT_OP_ADDRESS_OF_LABEL:
		/* Get the address of a particular label */
		label = (jit_label_t)(insn->value1);
		block = jit_block_from_label(func, label);
		if(!block)
		{
			break;
		}
		pc = (void **)(gen->ptr);
		jit_cache_opcode(gen, insn->opcode);
		if(block->address)
		{
			/* We already know the address of the block */
			jit_cache_native(gen, ((void **)(block->address)) - pc);
		}
		else
		{
			/* Record this position on the block's fixup list */
			jit_cache_native(gen, block->fixup_list);
			block->fixup_list = (void *)pc;
		}
		store_value(gen, insn->dest);
		break;

	case JIT_OP_CALL:
	case JIT_OP_CALL_TAIL:
		/* Call a function, whose pointer is supplied explicitly */
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, (jit_nint)(insn->dest));
		break;

	case JIT_OP_CALL_INDIRECT:
	case JIT_OP_CALL_INDIRECT_TAIL:
		/* Call a function, whose pointer is supplied in the register */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, (jit_nint)(insn->value2));
		jit_cache_native(gen, (jit_nint)
				 (jit_type_num_params((jit_type_t)(insn->value2))));
		break;

	case JIT_OP_CALL_VTABLE_PTR:
	case JIT_OP_CALL_VTABLE_PTR_TAIL:
		/* Call a function, whose vtable pointer is supplied in the register */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		break;

	case JIT_OP_CALL_EXTERNAL:
	case JIT_OP_CALL_EXTERNAL_TAIL:
		/* Call a native function, whose pointer is supplied explicitly */
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, (jit_nint)(insn->value2));
		jit_cache_native(gen, (jit_nint)(insn->dest));
		jit_cache_native(gen, (jit_nint)
				 (jit_type_num_params((jit_type_t)(insn->value2))));
		break;

	case JIT_OP_RETURN:
		/* Return from the current function with no result */
		jit_cache_opcode(gen, JIT_OP_RETURN);
		break;

	case JIT_OP_RETURN_INT:
	case JIT_OP_RETURN_LONG:
	case JIT_OP_RETURN_FLOAT32:
	case JIT_OP_RETURN_FLOAT64:
	case JIT_OP_RETURN_NFLOAT:
		/* Return from the current function with a specific result */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		break;

	case JIT_OP_RETURN_SMALL_STRUCT:
		/* Return from current function with a small structure result */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, jit_value_get_nint_constant(insn->value2));
		break;

	case JIT_OP_SETUP_FOR_NESTED:
		/* TODO!!! */
		/* Set up to call a nested child */
		jit_cache_opcode(gen, insn->opcode);
		adjust_working(gen, 2);
		break;

	case JIT_OP_SETUP_FOR_SIBLING:
		/* TODO!!! */
		/* Set up to call a nested sibling */
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, jit_value_get_nint_constant(insn->value1));
		adjust_working(gen, 2);
		break;

	case JIT_OP_IMPORT:
		/* Import a local variable from an outer nested scope */
		_jit_gen_fix_value(insn->value1);
		if(insn->value1->frame_offset >= 0)
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_IMPORT_LOCAL);
			jit_cache_native(gen, insn->value1->frame_offset);
			jit_cache_native(gen, jit_value_get_nint_constant(insn->value2));
		}
		else
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_IMPORT_ARG);
			jit_cache_native(gen, -(insn->value1->frame_offset + 1));
			jit_cache_native(gen, jit_value_get_nint_constant(insn->value2));
		}
		store_value(gen, insn->dest);
		break;

	case JIT_OP_THROW:
		/* Throw an exception */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		break;

	case JIT_OP_LOAD_PC:
	case JIT_OP_LOAD_EXCEPTION_PC:
		/* Load the current program counter onto the stack */
		jit_cache_opcode(gen, insn->opcode);
		store_value(gen, insn->dest);
		break;

	case JIT_OP_CALL_FILTER_RETURN:
		/* The r0 register currently contains "dest" */
		store_value(gen, insn->dest);
		break;

	case JIT_OP_ENTER_FINALLY:
		/* Record that the finally return address is on the stack */
		++(gen->extra_working_space);
		break;

	case JIT_OP_LEAVE_FINALLY:
		/* Leave a finally clause */
		jit_cache_opcode(gen, insn->opcode);
		break;

	case JIT_OP_ENTER_FILTER:
		/* The top of the stack contains the return address,
		   the r0 register contains the "dest" (filter parameter). */
		++(gen->extra_working_space);
		store_value(gen, insn->dest);
		break;

	case JIT_OP_LEAVE_FILTER:
		/* Leave a filter clause, returning a particular value */
		load_value(gen, insn->value1, 0);
		jit_cache_opcode(gen, insn->opcode);
		break;

	case JIT_OP_INCOMING_REG:
		/* Store incoming value (in interpreter this is used to
		   pass an exception object to the catcher) */
		store_value(gen, insn->value1);
		break;

	case JIT_OP_RETURN_REG:
		/* Push a function return value back onto the stack */
		switch(jit_type_normalize(insn->value1->type)->kind)
		{
		case JIT_TYPE_SBYTE:
		case JIT_TYPE_UBYTE:
		case JIT_TYPE_SHORT:
		case JIT_TYPE_USHORT:
		case JIT_TYPE_INT:
		case JIT_TYPE_UINT:
			jit_cache_opcode(gen, JIT_INTERP_OP_LDR_0_INT);
			store_value(gen, insn->value1);
			break;

		case JIT_TYPE_LONG:
		case JIT_TYPE_ULONG:
			jit_cache_opcode(gen, JIT_INTERP_OP_LDR_0_LONG);
			store_value(gen, insn->value1);
			break;

		case JIT_TYPE_FLOAT32:
			jit_cache_opcode(gen, JIT_INTERP_OP_LDR_0_FLOAT32);
			store_value(gen, insn->value1);
			break;

		case JIT_TYPE_FLOAT64:
			jit_cache_opcode(gen, JIT_INTERP_OP_LDR_0_FLOAT64);
			store_value(gen, insn->value1);
			break;

		case JIT_TYPE_NFLOAT:
			jit_cache_opcode(gen, JIT_INTERP_OP_LDR_0_NFLOAT);
			store_value(gen, insn->value1);
			break;
		}
		break;

	case JIT_OP_COPY_LOAD_SBYTE:
	case JIT_OP_COPY_LOAD_UBYTE:
	case JIT_OP_COPY_LOAD_SHORT:
	case JIT_OP_COPY_LOAD_USHORT:
	case JIT_OP_COPY_INT:
	case JIT_OP_COPY_LONG:
	case JIT_OP_COPY_FLOAT32:
	case JIT_OP_COPY_FLOAT64:
	case JIT_OP_COPY_NFLOAT:
	case JIT_OP_COPY_STORE_BYTE:
	case JIT_OP_COPY_STORE_SHORT:
		/* Copy a value from one temporary variable to another */
		load_value(gen, insn->value1, 0);
		store_value(gen, insn->dest);
		break;

	case JIT_OP_COPY_STRUCT:
		/* Copy a struct from one address to another */
		load_value(gen, insn->dest, 0);
		load_value(gen, insn->value1, 1);
		size = (jit_nint)jit_type_get_size(jit_value_get_type(insn->dest));
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, size);
		break;

	case JIT_OP_ADDRESS_OF:
		/* Get the address of a local variable */
		_jit_gen_fix_value(insn->value1);
		if(insn->value1->frame_offset >= 0)
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_LDLA_0);
			jit_cache_native(gen, insn->value1->frame_offset);
		}
		else
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_LDAA_0);
			jit_cache_native(gen, -(insn->value1->frame_offset + 1));
		}
		store_value(gen, insn->dest);
		break;

	case JIT_OP_PUSH_INT:
	case JIT_OP_PUSH_LONG:
	case JIT_OP_PUSH_FLOAT32:
	case JIT_OP_PUSH_FLOAT64:
	case JIT_OP_PUSH_NFLOAT:
		/* Push an item onto the stack, ready for a function call */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		adjust_working(gen, 1);
		break;

	case JIT_OP_PUSH_STRUCT:
		/* Load the pointer value */
		load_value(gen, insn->value1, 1);
		/* Push the structure at the designated pointer */
		size = jit_value_get_nint_constant(insn->value2);
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, size);
		adjust_working(gen, JIT_NUM_ITEMS_IN_STRUCT(size));
		break;

	case JIT_OP_PUSH_RETURN_AREA_PTR:
		/* Push the address of the interpreter's return area */
		jit_cache_opcode(gen, insn->opcode);
		adjust_working(gen, 1);
		break;

	case JIT_OP_POP_STACK:
		/* Pop parameter values from the stack after a function returns */
		size = jit_value_get_nint_constant(insn->value1);
		if(size == 1)
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_POP);
		}
		else if(size == 2)
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_POP_2);
		}
		else if(size == 3)
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_POP_3);
		}
		else if(size != 0)
		{
			jit_cache_opcode(gen, JIT_OP_POP_STACK);
			jit_cache_native(gen, size);
		}
		break;

	case JIT_OP_FLUSH_SMALL_STRUCT:
		/* Flush a small structure return value back into the frame */
		load_value(gen, insn->value1, 0);
		size = (jit_nint)jit_type_get_size(jit_value_get_type(insn->value1));
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, size);
		break;

	case JIT_OP_LOAD_RELATIVE_SBYTE:
	case JIT_OP_LOAD_RELATIVE_UBYTE:
	case JIT_OP_LOAD_RELATIVE_SHORT:
	case JIT_OP_LOAD_RELATIVE_USHORT:
	case JIT_OP_LOAD_RELATIVE_INT:
	case JIT_OP_LOAD_RELATIVE_LONG:
	case JIT_OP_LOAD_RELATIVE_FLOAT32:
	case JIT_OP_LOAD_RELATIVE_FLOAT64:
	case JIT_OP_LOAD_RELATIVE_NFLOAT:
		/* Load a value from a relative pointer */
		load_value(gen, insn->value1, 1);
		offset = jit_value_get_nint_constant(insn->value2);
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, offset);
		store_value(gen, insn->dest);
		break;

	case JIT_OP_LOAD_RELATIVE_STRUCT:
		/* Load a structured value from a relative pointer */
		load_value(gen, insn->dest, 0);
		load_value(gen, insn->value1, 1);
		offset = jit_value_get_nint_constant(insn->value2);
		size = (jit_nint)jit_type_get_size(jit_value_get_type(insn->dest));
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, offset);
		jit_cache_native(gen, size);
		break;

	case JIT_OP_STORE_RELATIVE_BYTE:
	case JIT_OP_STORE_RELATIVE_SHORT:
	case JIT_OP_STORE_RELATIVE_INT:
	case JIT_OP_STORE_RELATIVE_LONG:
	case JIT_OP_STORE_RELATIVE_FLOAT32:
	case JIT_OP_STORE_RELATIVE_FLOAT64:
	case JIT_OP_STORE_RELATIVE_NFLOAT:
		/* Store a value to a relative pointer */
		load_value(gen, insn->dest, 0);
		load_value(gen, insn->value1, 1);
		offset = jit_value_get_nint_constant(insn->value2);
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, offset);
		break;

	case JIT_OP_STORE_RELATIVE_STRUCT:
		/* Store a structured value to a relative pointer */
		load_value(gen, insn->dest, 0);
		load_value(gen, insn->value1, 1);
		offset = jit_value_get_nint_constant(insn->value2);
		size = (jit_nint)jit_type_get_size(jit_value_get_type(insn->value1));
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, offset);
		jit_cache_native(gen, size);
		break;

	case JIT_OP_ADD_RELATIVE:
		/* Add a relative offset to a pointer */
		offset = jit_value_get_nint_constant(insn->value2);
		if(offset != 0)
		{
			load_value(gen, insn->value1, 1);
			jit_cache_opcode(gen, insn->opcode);
			jit_cache_native(gen, offset);
			store_value(gen, insn->dest);
		}
		else
		{
			load_value(gen, insn->value1, 0);
			store_value(gen, insn->dest);
		}
		break;

	case JIT_OP_MARK_BREAKPOINT:
		/* Mark the current location as a potential breakpoint */
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, insn->value1->address);
		jit_cache_native(gen, insn->value2->address);
		break;

	default:
		if(insn->dest && (insn->flags & JIT_INSN_DEST_IS_VALUE) != 0)
		{
			load_value(gen, insn->dest, 0);
		}
		if(insn->value1)
		{
			load_value(gen, insn->value1, 1);
		}
		if(insn->value2)
		{
			load_value(gen, insn->value2, 2);
		}
		jit_cache_opcode(gen, insn->opcode);
		if(insn->dest && (insn->flags & JIT_INSN_DEST_IS_VALUE) == 0)
		{
			store_value(gen, insn->dest);
		}
		break;
	}
}
Example #29
0
int main(int argc, char** argv) {
	
	int i, eviction_algorithm, seed;
	
	//Parse input
	if(argc == 2){
		eviction_algorithm = atoi(argv[1]);
		seed = time(NULL);
	}else if(argc == 3) {
		eviction_algorithm = atoi(argv[1]);
		seed = atoi(argv[2]);
	}else{
		printf("1st argument: eviction algorithm (0-Random; 1-Clock; 2-Clock2ndChance)\n2nd argument: seed for random algorithm\n");
		return -1;
	}
	
	init_memory(eviction_algorithm, seed);
	
	printf("STORE VALUES IN ORDER value = vAddr+1 (press enter to continue):\n");
	memoryMaxer(PAGE_TABLE_SIZE+2);//beyond the limit (the last 2 page creations are ignored)
	print_memory_state();
	getchar();
	
	printf("GET_VALUE FROM ALL ADDRESSES (press enter to continue):\n");
	for(i = 0; i < PAGE_TABLE_SIZE; i++){
		if(get_value(i, NULL) != i+1){
			printf("wrong answer for page %d\n", i);
			return -1;//not supposed to happen
		}
	}
	print_memory_state();
	getchar();
	
	printf("FREE RANDOM pages AND TRY TO GET_VALUE. AND CREATE SOME PAGES (press enter to continue):\n");
	for (i = 0; i < 20; i++){
		int addr = uniform_rand(0, PAGE_TABLE_SIZE);
		printf("free %d\n", addr);
		free_page(addr);
		int valid;
		get_value(addr, &valid);
		if(valid == 0) printf("Couldn't read from addr %d\n", addr);
	}
	for (i = 0; i < 10; i++) {
		vAddr addr = create_page();
		uint32_t v = uniform_rand(0, PAGE_TABLE_SIZE);
		store_value(addr, &v);
		printf("created page %d with value %d\n", addr, v);
	}
	
	
	print_memory_state();
	getchar();
	
	printf("ALTERNATE STORE AND GET (press enter to continue)\n");
	for (i = 0; i < 20; i++){
		int addr = uniform_rand(0, PAGE_TABLE_SIZE);
		uint32_t v = uniform_rand(0, 100);
		store_value(addr, &v);
		printf("store %d in vAddr %d\n", v, addr);
		int valid;
		addr = uniform_rand(0, PAGE_TABLE_SIZE);
		v = get_value(addr, &valid);
		if(valid) printf("got %d from addr %d\n", v, addr);
		else printf("Couldn't read from addr %d\n", addr);
	}
	
	
	print_memory_state();
	getchar();
	
	destroy_memory();
	
	return 0;
}