示例#1
0
文件: daemon-io.c 项目: tasleson/lvm2
/*
 * Read a single message from a (socket) filedescriptor. Messages are delimited
 * by blank lines. This call will block until all of a message is received. The
 * memory will be allocated from heap. Upon error, all memory is freed and the
 * buffer pointer is set to NULL.
 *
 * See also write_buffer about blocking (read_buffer has identical behaviour).
 */
int buffer_read(int fd, struct buffer *buffer) {
	int result;

	if (!buffer_realloc(buffer, 32)) /* ensure we have some space */
		return 0;

	while (1) {
		result = read(fd, buffer->mem + buffer->used, buffer->allocated - buffer->used);
		if (result > 0) {
			buffer->used += result;
			if (buffer->used >= 4 && !strncmp((buffer->mem) + buffer->used - 4, "\n##\n", 4)) {
				buffer->used -= 4;
				buffer->mem[buffer->used] = 0;
				break; /* success, we have the full message now */
			}
			if ((buffer->allocated - buffer->used < 32) &&
			    !buffer_realloc(buffer, 1024))
				return 0;
		} else if (result == 0) {
			errno = ECONNRESET;
			return 0; /* we should never encounter EOF here */
		} else if (result < 0 && (errno == EAGAIN ||
					  (EWOULDBLOCK != EAGAIN && errno == EWOULDBLOCK) ||
					  errno == EINTR || errno == EIO)) {
			fd_set in;
			FD_ZERO(&in);
			FD_SET(fd, &in);
			/* ignore the result, this is just a glorified sleep */
			select(FD_SETSIZE, &in, NULL, NULL, NULL);
		} else if (result < 0)
			return 0;
	}

	return 1;
}
示例#2
0
文件: buffer.c 项目: mity/mctrl
void
buffer_shrink(BUFFER* buf)
{
    /* Avoid realloc() if the potential memory gain is negligible. */
    if(buf->alloc / 4 > buf->size / 3)
        buffer_realloc(buf, buf->size);
}
示例#3
0
void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count)
{
	if (UNLIKELY(new_count > buffer->alloc_count)) {
		if (buffer->flag & BLI_BUFFER_USE_STATIC) {
			void *orig = buffer->data;

			buffer->data = buffer_alloc(buffer, new_count);
			memcpy(buffer->data, orig, buffer->elem_size * buffer->count);
			buffer->alloc_count = new_count;
			buffer->flag &= ~BLI_BUFFER_USE_STATIC;
		}
		else {
			if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
				buffer->alloc_count *= 2;
			}
			else {
				buffer->alloc_count = new_count;
			}

			buffer->data = buffer_realloc(buffer, buffer->alloc_count);
		}
	}

	buffer->count = new_count;
}
示例#4
0
static VALUE
buffer_initialize_copy(VALUE copy, VALUE origin)
{
  buffer_t *ptr_copy, *ptr_origin;
  size_t buffer_len;

  if (copy == origin) return copy;

  rb_check_frozen(copy);

  Data_Get_Struct(copy, buffer_t, ptr_copy);
  Data_Get_Struct(origin, buffer_t, ptr_origin);

  buffer_len = buffer_buffer_length(ptr_origin);

  if (ptr_copy->memsize < ptr_origin->memsize) {
    buffer_realloc(ptr_copy, sizeof(char) * ptr_origin->memsize);
  }

  memcpy(ptr_copy->buffer, ptr_origin->buffer, buffer_len);
  ptr_copy->p = ptr_copy->buffer + buffer_len;
  ptr_copy->memsize = ptr_origin->memsize;

  return copy;
}
示例#5
0
static VALUE
buffer_update(VALUE self, VALUE str)
{
  size_t buffer_len, str_len, require, newsize;
  const char* str_p;
  buffer_t *ptr;
  Data_Get_Struct(self, buffer_t, ptr);

  StringValue(str);
  str_p = RSTRING_PTR(str);
  str_len = RSTRING_LEN(str);
  buffer_len = buffer_buffer_length(ptr);
  require = buffer_len + str_len;
  if (ptr->memsize < require) {
    newsize = ptr->memsize;
    while (newsize < require) {
      newsize *= 2;
    }
    buffer_realloc(ptr, sizeof(char) * newsize);
    ptr->p = ptr->buffer + buffer_len;
    ptr->memsize = newsize;
  }
  memcpy(ptr->p, str_p, str_len);
  ptr->p += str_len;

  return self;
}
示例#6
0
文件: server.c 项目: kaye64/gem
/**
 * Initializes a client
 *  - fd: The file descriptor of the client
 *  - addr: The address of the client
 */
client_t* server_client_init(server_t* server, int fd, struct in_addr addr)
{
	client_t* client = server->accept_cb(fd, addr, server);
	if (client == NULL) {
		return NULL;
	}
	client->fd = fd;
	client->addr = addr;
	client->server = server;
	client->handshake_stage = HANDSHAKE_PENDING;
	client->client_drop = false;
	object_init(buffer, &client->read_buffer);
	object_init(buffer, &client->write_buffer);
	buffer_realloc(&client->read_buffer, server->buf_size);
	buffer_realloc(&client->write_buffer, server->buf_size);
	return client;
}
示例#7
0
文件: buffer.c 项目: mity/mctrl
int
buffer_reserve(BUFFER* buf, size_t extra_alloc)
{
    if(buf->size + extra_alloc > buf->alloc)
        return buffer_realloc(buf, buf->size + extra_alloc);
    else
        return 0;
}
示例#8
0
void buffer_string_set_length(buffer *b, size_t len) {
	force_assert(NULL != b);
	force_assert(len + 1 > len);

	buffer_realloc(b, len + 1);

	b->used = len + 1;
	b->ptr[len] = '\0';
}
示例#9
0
/*
 * Add a char to the bottom of a buffer
 */
void buffer_add(buffer * buf, char c)
{
  assert(buf);

  if ((buf->use + 1) >= buf->size) buffer_realloc(buf);

  buf->buf[buf->use] = c;
  buf->buf[buf->use + 1] = '\0';
  buf->use++;
}
示例#10
0
/*
 * Return a buffer from a double
 */
buffer *buffer_ftoa(double f)
{
  buffer *res;

  res = buffer_init();
  while (res->size < 100) buffer_realloc(res);
  snprintf(res->buf, 99, "%f", f);
  res->use = strlen(res->buf);

  return res;
}
示例#11
0
/*
 * Add a string to a buffer
 */
void buffer_add_str(buffer * buf, const char *str)
{
  assert(buf);
  assert(str);

  if ((strlen(str) + buf->use) >= buf->size)
    while ((strlen(str) + buf->use) >= buf->size)
      buffer_realloc(buf);

  strcat(buf->buf, str);
  buf->use = buf->use + strlen(str);
}
示例#12
0
/*
 * Convert an integer to a buffer (base 10 only)
 */
buffer *buffer_itoa(int i)
{
  buffer *res;

  res = buffer_init();
  /* FIXME use snprintf instead ! */
  while (res->size < 100) buffer_realloc(res);
  sprintf(res->buf, "%i", i);
  res->use = strlen(res->buf);

  return res;
}
示例#13
0
文件: buffer.c 项目: sdikiy/tinyows
/*
 * Return a buffer from a double
 */
buffer *buffer_ftoa(double f)
{
    buffer *res;

    res = buffer_init();
    /* FIXME use snprintf instead ! */
    while (res->size < 100) buffer_realloc(res);
    sprintf(res->buf, "%f", f);
    res->use = strlen(res->buf);

    return res;
}
/*
 * Función data_buffer_write.
 * Recibe por parámetro la dirección de memoria de una estructura data_buffer_t,
 * la dirección de memoria del dato a escribir, y el tamaño del dato.
*/
void data_buffer_write(data_buffer_t * d, void * dato, uint32_t tam)
{
    if (d != NULL)
    {
        if (d->bufferUsado == d->tamBuffer)
            buffer_realloc(d);

        memcpy(&d->buffer[d->bufferUsado], dato, tam);
        d->bufferUsado += tam;
    }
    else
        printf("Error! La función data_buffer_write() recibió un puntero nulo!\n");
}
示例#15
0
static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *data)
{
    viprinet_status_t *status;
    size_t realsize = size * nmemb;

    status = (viprinet_status_t *)data;
    buffer_realloc(status, realsize + 1);
    if (status->buffer) {
        memcpy(&(status->buffer[0]), ptr, realsize);
        status->buffer[realsize] = 0;
        handle_messages(status);
    }
    return realsize;
}
示例#16
0
文件: buffer.c 项目: mity/mctrl
void*
buffer_insert_(BUFFER* buf, size_t pos, size_t n)
{
    if(buf->size + n > buf->alloc) {
        if(buffer_realloc(buf, (buf->size + n) * 2) != 0)
            return NULL;
    }

    if(buf->size > pos)
        memmove((uint8_t*)buf->data + pos + n, (uint8_t*)buf->data + pos, buf->size - pos);

    buf->size += n;
    return buffer_data_at(buf, pos);
}
示例#17
0
/*
 * Add n char from string to a buffer
 */
void buffer_add_nstr(buffer * buf, const char *str, size_t n)
{
  assert(buf);
  assert(str);
  assert(n > 0);
  assert(n <= strlen(str));

  if ((n + buf->use) >= buf->size) 
    while ((n + buf->use) >= buf->size)
      buffer_realloc(buf);

  strncat(buf->buf, str, n);
  buf->use = buf->use + n;
}
示例#18
0
void unix_socket_client_recv(usc_t *usc,
                             size_t sz,
                             usc_reader_t reader, void *ctx) {
    assert(usc);

    usc->read_task.currently_read = 0;
    usc->read_task.b.offset = usc->read_task.b.user_size;
    buffer_realloc(&usc->read_task.b, usc->read_task.b.offset + sz);
    usc->read_task.reader = reader;
    usc->read_task.ctx = ctx;

    io_service_post_job(
        usc->iosvc, usc->fd, IO_SVC_OP_READ, !IOSVC_JOB_ONESHOT,
        (iosvc_job_function_t)data_may_be_read,
        usc
    );
}
示例#19
0
/*
 * Add a char to the top of a buffer
 */
void buffer_add_head(buffer * buf, char c)
{
  size_t i;

  assert(buf);

  if ((buf->use + 2) >= buf->size)
    buffer_realloc(buf);

  if (buf->use > 0)
    for (i = buf->use; i > 0; i--)
      buf->buf[i] = buf->buf[i - 1];

  buf->buf[0] = c;
  buf->buf[buf->use + 1] = '\0';
  buf->use++;
}
示例#20
0
void unix_socket_client_send(usc_t *usc,
                             const void *d, size_t sz,
                             usc_writer_t writer, void *ctx) {
    assert(usc);

    usc->write_task.currently_wrote = 0;
    buffer_realloc(&usc->write_task.b, sz);
    memcpy(usc->write_task.b.data, d, sz);
    usc->write_task.writer = writer;
    usc->write_task.ctx = ctx;

    io_service_post_job(
        usc->iosvc, usc->fd, IO_SVC_OP_WRITE, !IOSVC_JOB_ONESHOT,
        (iosvc_job_function_t)data_may_be_sent,
        usc
    );
}
示例#21
0
char* buffer_string_prepare_append(buffer *b, size_t size) {
	force_assert(NULL !=  b);

	if (buffer_string_is_empty(b)) {
		return buffer_string_prepare_copy(b, size);
	} else {
		size_t req_size = b->used + size;

		/* not empty, b->used already includes a terminating 0 */
		force_assert(req_size >= b->used);

		/* check for overflow: unsigned overflow is defined to wrap around */
		force_assert(req_size >= b->used);

		buffer_realloc(b, req_size);

		return b->ptr + b->used - 1;
	}
}
示例#22
0
void writer(uss_t *srv, uss_connection_t *conn,
            int error, driver_core_t *core) {
    driver_core_connection_state_t *s = conn->priv;

    if (error) {
        LOG(LOG_LEVEL_WARN, "Couldn't send buffer to client: %s\n",
            strerror(errno));
        avl_tree_remove(&core->connection_state, conn->fd);
        unix_socket_server_close_connection(srv, conn);
        return;
    }

    s->argc = s->args_received = 0;
    s->last_argument_offset = 0;

    buffer_realloc(&conn->read_task.b, 0);

    unix_socket_server_recv(srv, conn, sizeof(pr_signature_t),
                            (uss_reader_t)reader,
                            core);
}