Esempio n. 1
0
    static void construct(PyObject* obj, converter::rvalue_from_python_stage1_data* data )
    {
        //get the storage
        typedef converter::rvalue_from_python_storage< multi_array_t > storage_t;
        storage_t * the_storage = reinterpret_cast< storage_t * >( data );
        void * memory_chunk = the_storage->storage.bytes;
        //new (memory_chunk) multi_array_t(obj);

        //new placement
        object py_obj( handle<>( borrowed( obj ) ) );
        shape_t shape;
        get_shape( py_obj, shape );
        multi_array_t * a = new (memory_chunk) multi_array_t( shape );

        //extract each element from numpy array and put in c array
        index i( a->num_dimensions(), 0 );
        do {
            boost::python::list numpy_index;
            for( unsigned dim = 0; a->num_dimensions() != dim; ++dim ) {
                numpy_index.append( i[ dim ] );
            }
            (*a)(i) = extract<typename multi_array_t::element>(py_obj[ boost::python::tuple( numpy_index ) ]);
        }
        while( increment_index( i, *a ) );

        data->convertible = memory_chunk;
    }
Esempio n. 2
0
    static PyObject* convert( const multi_array_t & c_array )
    {
        object numpy = object( handle<>(::PyImport_Import(object("numpy").ptr())));
        if( !numpy  ) {
            throw std::logic_error( "Could not import numpy" );
        }
        object array_function = numpy.attr("empty");
        if( !array_function  ) {
            throw std::logic_error( "Could not find array function" );
        }

        //create a numpy array to put it in
        boost::python::list extents;
        for( unsigned dim = 0; c_array.num_dimensions() != dim; ++dim ) {
            extents.append( c_array.shape()[ dim ] );
        }
        object result(array_function( extents,numpy.attr( "dtype" ) ( mydetail::get_dtype<typename multi_array_t::element>::name())));

        //copy the elements
        index i( c_array.num_dimensions(), 0 );
        do {
            boost::python::list numpy_index;
            for( unsigned dim = 0; c_array.num_dimensions() != dim; ++dim ) {
                numpy_index.append(i[dim]);
            }
            result[ tuple( numpy_index ) ] = c_array( i );
        } while( increment_index( i, c_array ) );

        return incref( result.ptr() );
    }
Esempio n. 3
0
bool GenericTaskQueue<E, F, N>::push_slow(E t, uint dirty_n_elems) {
  if (dirty_n_elems == N - 1) {
    // Actually means 0, so do the push.
    uint localBot = _bottom;
    // g++ complains if the volatile result of the assignment is
    // unused, so we cast the volatile away.  We cannot cast directly
    // to void, because gcc treats that as not using the result of the
    // assignment.  However, casting to E& means that we trigger an
    // unused-value warning.  So, we cast the E& to void.
    (void)const_cast<E&>(_elems[localBot] = t);
    OrderAccess::release_store(&_bottom, increment_index(localBot));
    TASKQUEUE_STATS_ONLY(stats.record_push());
    return true;
  }
  return false;
}
Esempio n. 4
0
template<class E, MEMFLAGS F, unsigned int N> inline bool
GenericTaskQueue<E, F, N>::push(E t) {
  uint localBot = _bottom;
  assert(localBot < N, "_bottom out of range.");
  idx_t top = _age.top();
  uint dirty_n_elems = dirty_size(localBot, top);
  assert(dirty_n_elems < N, "n_elems out of range.");
  if (dirty_n_elems < max_elems()) {
    // g++ complains if the volatile result of the assignment is
    // unused, so we cast the volatile away.  We cannot cast directly
    // to void, because gcc treats that as not using the result of the
    // assignment.  However, casting to E& means that we trigger an
    // unused-value warning.  So, we cast the E& to void.
    (void) const_cast<E&>(_elems[localBot] = t);
    OrderAccess::release_store(&_bottom, increment_index(localBot));
    TASKQUEUE_STATS_ONLY(stats.record_push());
    return true;
  } else {
    return push_slow(t, dirty_n_elems);
  }
}
Esempio n. 5
0
static void serial_readbyte_isr(USARTInstance* instance, uchar byte)
{
  SerialInstance* serial = Instances[instance->PeripheralMapping];

  if (serial == NULL)
    throw(MissingPointerException, "Could not locate the SerialInstance from UsartInstance");

  // If the last byte we received was a SYNC byte and the current byte is
  // an STX, we have found ourselves a start-of-frame. We can reposition the index to 0.
  if (serial->_LastByteSync && byte == STX)
    serial->_BufferIndex = 0;

  serial->_ReceiveBuffer[serial->_BufferIndex] = byte;
  increment_index(&serial->_BufferIndex);

  // Set the flag that this byte is a SYNC byte for alignment of the data
  serial->_LastByteSync = (byte == SYN);

  // Only attempt to parse the buffer if we have reached the protocols minimum size requirements
  if (serial->_BufferIndex >= RX_MIN_SIZE)
  {
    serial_parse_buffer(serial);
  }
}
Esempio n. 6
0
int getopt(int argc, char** argv, const char* optstr)
{
	int c = 0;

	/* If we have new argv, reinitialize */
	if(prev_argv != argv || prev_argc != argc)
	{
		/* Initialize variables */
		prev_argv = argv;
		prev_argc = argc;
		argv_index = 1;
		argv_index2 = 1;
		opt_offset = 1;
		dashdash = 0;
		nonopt = 0;
	}

	/* Jump point in case we want to ignore the current argv_index */
	getopt_top:

	/* Misc. initializations */
	optarg = NULL;

	/* Dash-dash check */
	if(argv[argv_index] && !strcmp(argv[argv_index], "--"))
	{
		dashdash = 1;
		increment_index();
	}

	/* If we're at the end of argv, that's it. */
	if(argv[argv_index] == NULL)
	{
		c = -1;
	}
	/* Are we looking at a string? Single dash is also a string */
	else if(dashdash || argv[argv_index][0] != '-' || !strcmp(argv[argv_index], "-"))
	{
		/* If we want a string... */
		if(optstr[0] == '-')
		{
			c = 1;
			optarg = argv[argv_index];
			increment_index();
		}
		/* If we really don't want it (we're in POSIX mode), we're done */
		else if(optstr[0] == '+' || getenv("POSIXLY_CORRECT"))
		{
			c = -1;

			/* Everything else is a non-opt argument */
			nonopt = argc - argv_index;
		}
		/* If we mildly don't want it, then move it back */
		else
		{
			if(!permute_argv_once()) goto getopt_top;
			else c = -1;
		}
	}
	/* Otherwise we're looking at an option */
	else
	{
		char* opt_ptr = NULL;

		/* Grab the option */
		c = argv[argv_index][opt_offset++];

		/* Is the option in the optstr? */
		if(optstr[0] == '-') opt_ptr = strchr(optstr+1, c);
		else opt_ptr = strchr(optstr, c);
		/* Invalid argument */
		if(!opt_ptr)
		{
			if(opterr)
			{
				fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
			}

			optopt = c;
			c = '?';

			/* Move onto the next option */
			increment_index();
		}
		/* Option takes argument */
		else if(opt_ptr[1] == ':')
		{
			/* ie, -oARGUMENT, -xxxoARGUMENT, etc. */
			if(argv[argv_index][opt_offset] != '\0')
			{
				optarg = &argv[argv_index][opt_offset];
				increment_index();
			}
			/* ie, -o ARGUMENT (only if it's a required argument) */
			else if(opt_ptr[2] != ':')
			{
				/* One of those "you're not expected to understand this" moment */
				if(argv_index2 < argv_index) argv_index2 = argv_index;
				while(argv[++argv_index2] && argv[argv_index2][0] == '-');
				optarg = argv[argv_index2];

				/* Don't cross into the non-option argument list */
				if(argv_index2 + nonopt >= prev_argc) optarg = NULL;

				/* Move onto the next option */
				increment_index();
			}
			else
			{
				/* Move onto the next option */
				increment_index();
			}

			/* In case we got no argument for an option with required argument */
			if(optarg == NULL && opt_ptr[2] != ':')
			{
				optopt = c;
				c = '?';

				if(opterr)
				{
					fprintf(stderr,"%s: option requires an argument -- %c\n",
							argv[0], optopt);
				}
			}
		}
		/* Option does not take argument */
		else
		{
			/* Next argv_index */
			if(argv[argv_index][opt_offset] == '\0')
			{
				increment_index();
			}
		}
	}

	/* Calculate optind */
	if(c == -1)
	{
		optind = argc - nonopt;
	}
	else
	{
		optind = argv_index;
	}

	return c;
}
Esempio n. 7
0
static void serial_parse_buffer(SerialInstance* instance)
{
  uint index = 0;

  if (instance->_ReceiveBuffer[index] != STX)
    return;

  increment_index(&index);

  InputCommand command = (InputCommand)instance->_ReceiveBuffer[index];
  increment_index(&index);

  // Extract out the sub-command
  uchar subcommand = instance->_ReceiveBuffer[index];
  increment_index(&index);

  // Extract out the length 1 (LSB)
  uchar length1 = instance->_ReceiveBuffer[index];
  increment_index(&index);

  // Extract out the length 2 (MSB)
  uchar length2 = instance->_ReceiveBuffer[index];
  increment_index(&index);

  ushort length = length1 | (length2 << 8);

  // Only continue if our Buffer is large enough
  if (instance->_BufferIndex - RX_MIN_SIZE < length)
    return;

  // Extract out the data-frame
  uchar dataframe[length];
  for (int i = 0; i < length; i++)
  {
    dataframe[i] = instance->_ReceiveBuffer[index];
    increment_index(&index);
  }

  // Extract out the checksum
  uchar crc1 = instance->_ReceiveBuffer[index];
  increment_index(&index);
  uchar crc2 = instance->_ReceiveBuffer[index];
  increment_index(&index);

  ushort checksum = crc1 | (crc2 << 8);

  if (!validate_checksum(checksum, command, subcommand, length, dataframe))
  {
    // Unexpected / bad data
    serial_instance_write(instance, NAK);
    return;
  }
  // Finally, we should have an ETX
  if (instance->_ReceiveBuffer[index] != ETX)
  {
    serial_instance_write(instance, NAK);
    return;
  }

  // Build output
  SysInput input = {0};
  input.Type = SysInput_Serial;
  input.Command = command;
  input.Subcommand = subcommand;
  input.Length = length;

  for (int i = 0; i < length; i++)
    input.Dataframe[i] = dataframe[i];

  // Send the acknowledgment byte back
  serial_instance_write(instance, ACK);

  // Scramble the frame
  instance->_ReceiveBuffer[0] = 0x00;
  instance->_ReceiveBuffer[index] = 0x00;

  // Handle the input. TODO: Could put this in the application domain
  if (InputHandler != NULL)
    InputHandler((void*)instance, input);
}
Esempio n. 8
0
 // Increment top; if it wraps, increment tag also.
 void increment() {
   _fields._top = increment_index(_fields._top);
   if (_fields._top == 0) ++_fields._tag;
 }
Esempio n. 9
0
int getopt(int argc, char** argv, char* optstr)
{
	int c = 0;


	if(prev_argv != argv || prev_argc != argc)
	{

		prev_argv = argv;
		prev_argc = argc;
		argv_index = 1;
		argv_index2 = 1;
		opt_offset = 1;
		dashdash = 0;
		nonopt = 0;
	}


	getopt_top:


	optarg = NULL;


	if(argv[argv_index] && !strcmp(argv[argv_index], "--"))
	{
		dashdash = 1;
		increment_index();
	}


	if(argv[argv_index] == NULL)
	{
		c = -1;
	}

	else if(dashdash || argv[argv_index][0] != '-' || !strcmp(argv[argv_index], "-"))
	{

		if(optstr[0] == '-')
		{
			c = 1;
			optarg = argv[argv_index];
			increment_index();
		}

		else if(optstr[0] == '+' || getenv("POSIXLY_CORRECT"))
		{
			c = -1;


			nonopt = argc - argv_index;
		}

		else
		{
			if(!permute_argv_once()) goto getopt_top;
			else c = -1;
		}
	}

	else
	{
		char* opt_ptr = NULL;


		c = argv[argv_index][opt_offset++];


		if(optstr[0] == '-') opt_ptr = strchr(optstr+1, c);
		else opt_ptr = strchr(optstr, c);

		if(!opt_ptr)
		{
			if(opterr)
			{
				fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
			}

			optopt = c;
			c = '?';


			increment_index();
		}

		else if(opt_ptr[1] == ':')
		{

			if(argv[argv_index][opt_offset] != '\0')
			{
				optarg = &argv[argv_index][opt_offset];
				increment_index();
			}

			else if(opt_ptr[2] != ':')
			{

				if(argv_index2 < argv_index) argv_index2 = argv_index;
				while(argv[++argv_index2] && argv[argv_index2][0] == '-');
				optarg = argv[argv_index2];


				if(argv_index2 + nonopt >= prev_argc) optarg = NULL;


				increment_index();
			}
			else
			{

				increment_index();
			}


			if(optarg == NULL && opt_ptr[2] != ':')
			{
				optopt = c;
				c = '?';

				if(opterr)
				{
					fprintf(stderr,"%s: option requires an argument -- %c\n",
						argv[0], optopt);
				}
			}
		}

		else
		{

			if(argv[argv_index][opt_offset] == '\0')
			{
				increment_index();
			}
		}
	}


	if(c == -1)
	{
		optind = argc - nonopt;
	}
	else
	{
		optind = argv_index;
	}

	return c;
}