neo4j_iostream_t *neo4j_buffering_iostream(neo4j_iostream_t *delegate,
        bool close, size_t rcvbuf_size, size_t sndbuf_size)
{
    REQUIRE(delegate != NULL, NULL);
    REQUIRE(rcvbuf_size > 0 || sndbuf_size > 0, NULL);

    struct buffering_iostream *ios =
        calloc(1, sizeof(struct buffering_iostream));
    if (ios == NULL)
    {
        return NULL;
    }

    ios->delegate = delegate;
    ios->close_delegate = close;

    if (rcvbuf_size > 0)
    {
        ios->rcvbuf = rb_alloc(rcvbuf_size);
        if (ios->rcvbuf == NULL)
        {
            goto failure;
        }
    }
    if (sndbuf_size > 0)
    {
        ios->sndbuf = rb_alloc(sndbuf_size);
        if (ios->sndbuf == NULL)
        {
            goto failure;
        }
    }

    neo4j_iostream_t *iostream = &(ios->_iostream);
    iostream->read = buffering_read;
    iostream->readv = buffering_readv;
    iostream->write = buffering_write;
    iostream->writev = buffering_writev;
    iostream->flush = buffering_flush;
    iostream->close = buffering_close;
    return iostream;

    int errsv;
failure:
    errsv = errno;
    if (ios->rcvbuf != NULL)
    {
        rb_free(ios->rcvbuf);
    }
    if (ios->sndbuf != NULL)
    {
        rb_free(ios->sndbuf);
    }
    free(ios);
    errno = errsv;
    return NULL;
}
示例#2
0
文件: main.c 项目: lythm/orb3d
int main(int argc, char* argv[])
{
	unsigned int left = 0;
	unsigned int size = 0;
	unsigned int i = 0;
	struct ring_buffer rb;
	char buffer[100];

	for(i = 0; i < 100; ++i)
	{
		buffer[i] = i;
	}
	
	rb_alloc(&rb, 100);

	for(i = 0; i < 100; ++i)
	{
		rb_write(&rb, buffer, 33);
		rb_read(&rb, buffer, 32);
	}
	

	printf("%d,%d  %d,%d\n", rb_length(&rb), rb_left(&rb), rb_is_full(&rb), rb_is_empty(&rb));

	rb_free(&rb);

	
	return 0;
}
static void setup(void)
{
    logger_provider = neo4j_std_logger_provider(stderr, NEO4J_LOG_ERROR, 0);
    in_rb = rb_alloc(1024);
    out_rb = rb_alloc(1024);
    client_ios = neo4j_memiostream(in_rb, out_rb);
    ios_close = client_ios->close;
    client_ios->close = ios_noop_close;

    stub_factory.tcp_connect = stub_connect;
    config = neo4j_new_config();
    neo4j_config_set_logger_provider(config, logger_provider);
    neo4j_config_set_connection_factory(config, &stub_factory);
    ck_assert_int_eq(neo4j_config_set_username(config, username), 0);
    ck_assert_int_eq(neo4j_config_set_password(config, password), 0);
}
static void setup(void)
{
    ck_assert_int_gt(sizeof(sample16), 16);
    rb = rb_alloc(16);
}
示例#5
0
/* Search for and if not found and insert is true, will add a new
** node in. Returns a pointer to the new node, or the node found
*/
int rbinsert(const void *key, const void * high, void *object,struct rbtree *rbinfo) {
  struct rbnode *x,*y,*z, *tmp;
  const void *max;
  int found=0;
  
  y=RBNULL; /* points to the parent of x */
  x=rbinfo->rb_root;
  
  /* walk x down the tree */
  while(x!=RBNULL && found==0) {
    y=x;
    /* printf("key=%s, x->key=%s\n", key, x->key); */

    if (key<x->key)
      x=x->left;
    else if (key>x->key)
      x=x->right;
    else
      found=1;
  }
  
  if (found)
    return RB_FALSE;
  
  if ((z=rb_alloc())==NULL) {
    /* Whoops, no memory */
    return RB_FALSE;
  }
  
  z->object=object;
  z->high=high;
  z->key=key;
  z->max=high;
  z->up=y;
  tmp=y;
  max=high;
  while(tmp!=RBNULL) {
    if (max>tmp->max)
      tmp->max=max;
    else
      max=tmp->max;
    tmp=tmp->up;
  }

  if (y==RBNULL) {
      rbinfo->rb_root=z;
  } else {
    if (z->key<y->key)
      y->left=z;
    else
      y->right=z;
  }

  z->left=RBNULL;
  z->right=RBNULL;
  
  /* colour this new node red */
  z->colour=RED;

  /* Having added a red node, we must now walk back up the tree balancing
  ** it, by a series of rotations and changing of colours
  */
  x=z;
  
  /* While we are not at the top and our parent node is red
  ** N.B. Since the root node is garanteed black, then we
  ** are also going to stop if we are the child of the root
  */
  
  while(x != rbinfo->rb_root && (x->up->colour == RED)) {
    /* if our parent is on the left side of our grandparent */
    if (x->up == x->up->up->left) {
      /* get the right side of our grandparent (uncle?) */
      y=x->up->up->right;
      if (y->colour == RED) {
	/* make our parent black */
	x->up->colour = BLACK;
	/* make our uncle black */
	y->colour = BLACK;
	/* make our grandparent red */
	x->up->up->colour = RED;
	
	/* now consider our grandparent */
	x=x->up->up;
      } else {
	/* if we are on the right side of our parent */
	if (x == x->up->right) {
	/* Move up to our parent */
	  x=x->up;
	  rb_left_rotate(&rbinfo->rb_root, x);
	}
	
	/* make our parent black */
	x->up->colour = BLACK;
	/* make our grandparent red */
	x->up->up->colour = RED;
	/* right rotate our grandparent */
	rb_right_rotate(&rbinfo->rb_root, x->up->up);
      }
    } else {
	/* everything here is the same as above, but
	** exchanging left for right
	*/

      y=x->up->up->left;
      if (y->colour == RED) {
	x->up->colour = BLACK;
	y->colour = BLACK;
	x->up->up->colour = RED;
	
	x=x->up->up;
      } else {
	if (x == x->up->left) {
	  x=x->up;
	  rb_right_rotate(&rbinfo->rb_root, x);
	}
	
	x->up->colour = BLACK;
	x->up->up->colour = RED;
	rb_left_rotate(&rbinfo->rb_root, x->up->up);
      }
    }
  }

  /* Set the root node black */
  (rbinfo->rb_root)->colour = BLACK;

  return RB_TRUE;
}