static uint8_t manage_block_overflow(cir_storage_flash_t *storage, uint32_t size, pointer_type pointer_type)
{
	uint32_t pointer;

	if (pointer_type == POINTER_READ) {
		pointer = READ_PTR(storage);
	} else {
		pointer = WRITE_PTR(storage);
	}

	/* If there is not enough place on the current block, we read at the beginning
	   of the next block */
	if (storage->block_size - (pointer%storage->block_size) < size) {
		pointer = storage->block_size*(pointer/storage->block_size+1);
		/* If we are at the end of the circular storage, we move the read pointer
		   on the first data block */
		if (pointer + size > (storage->address_start + storage->parent.size)) {
			pointer = storage->address_start+storage->block_size;
		}
		if (pointer_type == POINTER_READ) {
			READ_PTR(storage) = pointer;
		} else {
			WRITE_PTR(storage) = pointer;
		}

		return 1;
	}
	return 0;
}
Example #2
0
/**
 * Get a cstring from the buffer.
 */
VALUE rb_bson_byte_buffer_get_cstring(VALUE self)
{
  byte_buffer_t *b;
  VALUE string;
  int length;

  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
  length = (int)strlen(READ_PTR(b));
  ENSURE_BSON_READ(b, length);
  string = rb_enc_str_new(READ_PTR(b), length, rb_utf8_encoding());
  b->read_position += length + 1;
  return string;
}
Example #3
0
/**
 * Get a string from the buffer.
 */
VALUE rb_bson_byte_buffer_get_string(VALUE self)
{
  byte_buffer_t *b;
  int32_t length;
  int32_t length_le;
  VALUE string;

  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
  ENSURE_BSON_READ(b, 4);
  memcpy(&length, READ_PTR(b), 4);
  length_le = BSON_UINT32_FROM_LE(length);
  b->read_position += 4;
  ENSURE_BSON_READ(b, length_le);
  string = rb_enc_str_new(READ_PTR(b), length_le - 1, rb_utf8_encoding());
  b->read_position += length_le;
  return string;
}
cir_storage_err_t cir_storage_clear(cir_storage_t * self, uint32_t size)
{
	cir_storage_flash_t *storage = (cir_storage_flash_t *)self;

	if (size == 0) {
		/* Put the read pointer at same location as the write pointer */
		READ_PTR(storage) = WRITE_PTR(storage);
	} else {
		/* Move the read pointer of "size" */
		READ_PTR(storage) += size;

		manage_block_overflow(storage, size, POINTER_READ);
	}

	if (set_pointers(storage, &storage->pointers) == 0)
		return CBUFFER_STORAGE_SUCCESS;
	else
		return CBUFFER_STORAGE_ERROR;
}
Example #5
0
/**
 * Get the 16 bytes representing the decimal128 from the buffer.
 */
VALUE rb_bson_byte_buffer_get_decimal128_bytes(VALUE self)
{
  byte_buffer_t *b;
  VALUE bytes;

  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
  ENSURE_BSON_READ(b, 16);
  bytes = rb_str_new(READ_PTR(b), 16);
  b->read_position += 16;
  return bytes;
}
Example #6
0
/**
 * Get a single byte from the buffer.
 */
VALUE rb_bson_byte_buffer_get_byte(VALUE self)
{
  byte_buffer_t *b;
  VALUE byte;

  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
  ENSURE_BSON_READ(b, 1);
  byte = rb_str_new(READ_PTR(b), 1);
  b->read_position += 1;
  return byte;
}
Example #7
0
/**
 * Get a int32 from the buffer.
 */
VALUE rb_bson_byte_buffer_get_int32(VALUE self)
{
  byte_buffer_t *b;
  int32_t i32;

  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
  ENSURE_BSON_READ(b, 4);
  memcpy(&i32, READ_PTR(b), 4);
  b->read_position += 4;
  return INT2NUM(BSON_UINT32_FROM_LE(i32));
}
Example #8
0
/**
 * Get a int64 from the buffer.
 */
VALUE rb_bson_byte_buffer_get_int64(VALUE self)
{
  byte_buffer_t *b;
  int64_t i64;

  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
  ENSURE_BSON_READ(b, 8);
  memcpy(&i64, READ_PTR(b), 8);
  b->read_position += 8;
  return LL2NUM(BSON_UINT64_FROM_LE(i64));
}
Example #9
0
/**
 * Get a double from the buffer.
 */
VALUE rb_bson_byte_buffer_get_double(VALUE self)
{
  byte_buffer_t *b;
  double d;

  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
  ENSURE_BSON_READ(b, 8);
  memcpy(&d, READ_PTR(b), 8);
  b->read_position += 8;
  return DBL2NUM(BSON_DOUBLE_FROM_LE(d));
}
cir_storage_err_t cir_storage_pop(cir_storage_t *self,
                                  uint8_t *buf,
                                  uint32_t buf_size)
{
	cir_storage_flash_t *storage = (cir_storage_flash_t *)self;
	if ( (buf_size > storage->block_size) || (buf_size <= 0) || (buf == NULL) ) {
		return CBUFFER_STORAGE_ERROR;
	}

	/* nothing to read */
	if (READ_PTR(storage) == WRITE_PTR(storage)) {
		return CBUFFER_STORAGE_EMPTY_ERROR;
	}

	if (storage->read(READ_PTR(storage), buf_size, buf) != 0) {
		return CBUFFER_STORAGE_ERROR;
	}

	/* We increase data pointer read and copy it in pointers block */
	return cir_storage_clear(self, buf_size);
}
Example #11
0
/**
 * Get bytes from the buffer.
 */
VALUE rb_bson_byte_buffer_get_bytes(VALUE self, VALUE i)
{
  byte_buffer_t *b;
  VALUE bytes;
  const uint32_t length = FIX2LONG(i);

  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
  ENSURE_BSON_READ(b, length);
  bytes = rb_str_new(READ_PTR(b), length);
  b->read_position += length;
  return bytes;
}
Example #12
0
/**
 * Expand the byte buffer linearly.
 */
void rb_bson_expand_buffer(byte_buffer_t* buffer_ptr, size_t length)
{
  const size_t required_size = buffer_ptr->write_position - buffer_ptr->read_position + length;
  if (required_size <= buffer_ptr->size) {
    memmove(buffer_ptr->b_ptr, READ_PTR(buffer_ptr), READ_SIZE(buffer_ptr));
    buffer_ptr->write_position -= buffer_ptr->read_position;
    buffer_ptr->read_position = 0;
  } else {
    char *new_b_ptr;
    const size_t new_size = required_size * 2;
    new_b_ptr = ALLOC_N(char, new_size);
    memcpy(new_b_ptr, READ_PTR(buffer_ptr), READ_SIZE(buffer_ptr));
    if (buffer_ptr->b_ptr != buffer_ptr->buffer) {
      xfree(buffer_ptr->b_ptr);
    }
    buffer_ptr->b_ptr = new_b_ptr;
    buffer_ptr->size = new_size;
    buffer_ptr->write_position -= buffer_ptr->read_position;
    buffer_ptr->read_position = 0;
  }
}
Example #13
0
/**
 * Replace a 32 bit integer int the byte buffer.
 */
VALUE rb_bson_byte_buffer_replace_int32(VALUE self, VALUE index, VALUE i)
{
  byte_buffer_t *b;
  const int32_t position = NUM2LONG(index);
  const int32_t i32 = BSON_UINT32_TO_LE(NUM2LONG(i));

  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);

  memcpy(READ_PTR(b) + position, &i32, 4);

  return self;
}
int32_t cir_storage_flash_init(cir_storage_flash_t *storage)
{
	pointers_block_t local_pointers;

	if (storage->parent.size%storage->block_size != 0) {
		return -1;
	}

	if (storage->read(storage->address_start,
			  sizeof(pointers_block_t),
			  (uint8_t *)&local_pointers) != 0) {
		return -1;
	}

	/* If an init as already been done */
	if (local_pointers.data_pointer_read != 0xFFFFFFFF) {
		/* Initialize pointers with previous value */
		if (get_pointers(storage, &storage->pointers) != 0){
			return -1;
		}
		return 0;
	}

	/* Storage first init */
	READ_PTR(storage) = storage->address_start + storage->block_size;
	WRITE_PTR(storage) = READ_PTR(storage);
	storage->pointers_index = storage->address_start;

	/* Erase the two firsts blocks (pointers block and first data block) */
	if (storage->erase(storage->block_start, 2) != 0) {
		return -1;
	}

	/* Store our pointers at the beginning of the first block */
	return storage->write(storage->address_start,
			      sizeof(pointers_block_t),
			      (uint8_t *)&storage->pointers);
}
cir_storage_err_t cir_storage_peek(cir_storage_t * self,
                                   uint32_t offset,
                                   uint8_t *buf,
                                   uint32_t size)
{
	int32_t ret;
	cir_storage_flash_t *storage = (cir_storage_flash_t *)self;
	if ( (size > storage->block_size) || (size <= 0) || (buf == NULL) ) {
		return CBUFFER_STORAGE_ERROR;
	}

	/* nothing to read */
	if (READ_PTR(storage) == WRITE_PTR(storage)) {
		return CBUFFER_STORAGE_EMPTY_ERROR;
	}

	ret = storage->read(READ_PTR(storage) + offset, size, buf);
	if (ret == 0) {
		return CBUFFER_STORAGE_SUCCESS;
	} else {
		return CBUFFER_STORAGE_ERROR;
	}
}
cir_storage_err_t cir_storage_push(cir_storage_t *self,
                                   uint8_t *buf,
                                   uint32_t len)
{
	cir_storage_flash_t *storage = (cir_storage_flash_t *)self;

	if ( (len > storage->block_size) || (len <= 0) || (buf == NULL) ) {
		return CBUFFER_STORAGE_ERROR;
	}

	if (manage_block_overflow(storage, len, POINTER_WRITE)) {
		if (storage->erase(WRITE_PTR(storage)/storage->block_size, 1) !=0) {
			return CBUFFER_STORAGE_ERROR;
		}

		/* If read pointer is in the erased block, we move it on the next one */
		if ((WRITE_PTR(storage) / storage->block_size) ==
		    (READ_PTR(storage) / storage->block_size)) {
			READ_PTR(storage) = storage->address_start+storage->block_size+
					(WRITE_PTR(storage) - storage->address_start)%
					(storage->parent.size - storage->block_size);
		}
	}

	if (storage->write(WRITE_PTR(storage), len, buf) != 0) {
		return CBUFFER_STORAGE_ERROR;
	}

	/* We increase data pointer write and copy it in pointers block */
	WRITE_PTR(storage) += len;

	if (set_pointers(storage, &storage->pointers) != 0) {
		return CBUFFER_STORAGE_ERROR;
	}

	return CBUFFER_STORAGE_SUCCESS;
}
Quantizer* LshKBM::load(FILE *f)
{
  cout<<"LshKBM::load: loading"<<endl;
  unsigned int signature=0;
  unsigned char ver=99;

  READ_VAR(signature); assert(signature==0xFAADBEEB);
  READ_VAR(ver); assert(ver<=mVer);
  mVer=ver;
  READ_VAR(type); assert(type==CV_8UC1);
  READ_VAR(dims); assert(dims==BriefDesc::BRIEF_K);
  READ_VAR(num_descriptors);
  READ_VAR(num_images);
  unsigned int word_count;
  READ_VAR(word_count);
  // Create forest of kd-trees for searching
  switch(mVer) {
  case 0:
    mJfinal=256.0;
    mDtKBinMeans=0.0;
    for(uint32_t i=0;i<guidLen+1;++i)
      m_guid[i]=0;
    break;
  case 1:
    READ_VAR(mJfinal);
    READ_VAR(mDtKBinMeans);
    for(uint32_t i=0;i<guidLen+1;++i)
      m_guid[i]=0;
    break;
  case 2:
    READ_VAR(mJfinal);
    READ_VAR(mDtKBinMeans);
    READ_VAR(m_guid);
    break;
  case 3:
    READ_VAR(mJfinal);
    READ_VAR(mDtKBinMeans);
    READ_VAR(m_guid);
    uint32_t m,l;
    READ_VAR(m);
    READ_VAR(l);
    mLshKbmP=LshKBMParams(word_count,l,m,mLshKbmP.getThreadMax());
    break;
  default:
    Log::error("Invalid file version: %d!\n", ver);
    return NULL;
  }

//  m_num_clusters = word_count;
  cout<<"sig="<<signature<<" ver="<<ver<<" type="<<type<<" dims="<<dims<<" #descriptors="<<num_descriptors<<" #images="<<num_images<<" #words="<<word_count<<endl;
  cout<<" mJfinal="<<mJfinal<<" mDtKBinMeans="<<mDtKBinMeans<<endl;

  mLshKbmP=LshKBMParams(word_count,mLshKbmP.getL(),mLshKbmP.getM(),mLshKbmP.getThreadMax());
  mCentroids.resize(mLshKbmP.getK(), NULL);
  for(uint32_t i=0; i<mLshKbmP.getK(); ++i)
  { // create all centroids
    mCentroids[i]=new BriefDescS();
    READ_PTR(&(mCentroids[i]->bd[0]), dims*sizeof(mCentroids[i]->bd[0]));
  }

  // Create forest of kd-trees for searching
  switch(ver) {
  case 0:
  case 1:
  case 2:
  case 3:
    Log::info("Quantizer file had NO prebuilt LSH\n");
    // build hash tables - the class will realize,
    // that the centroids have been loaded already!
    prepareFromCentroids(mCentroids);
    cout<<"LshKBM::load: loaded successfully"<<endl;
    break;
  default:
    Log::error("Invalid file version: %d!\n", ver);
    return NULL;
  }
  return this;
}
Example #18
0
/**
 * Convert the buffer to a string.
 */
VALUE rb_bson_byte_buffer_to_s(VALUE self)
{
  byte_buffer_t *b;
  TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
  return rb_str_new(READ_PTR(b), READ_SIZE(b));
}