示例#1
0
/**
 * If connection was broken, return NULL.  If the read timed out, return
 * (char *)-1.
 * @param buf Buffer to get
 * @param len Length
 * @param s Socket
 * @return buffer
 */
char *sgets(char *buf, int len, ano_socket_t s)
{
    int c = 0;
    struct timeval tv;
    fd_set fds;
    char *ptr = buf;

    flush_write_buffer(0);

    if (len == 0)
        return NULL;
    FD_SET(s, &fds);
    tv.tv_sec = ReadTimeout;
    tv.tv_usec = 0;
    while (read_buffer_len() == 0 &&
           (c = select(s + 1, &fds, NULL, NULL, &tv)) < 0) {
        if (ano_sockgeterr() != EINTR)
            break;
        flush_write_buffer(0);
    }
    if (read_buffer_len() == 0 && c == 0)
        return (char *) -1;
    c = sgetc(s);
    while (--len && (*ptr++ = c) != '\n' && (c = sgetc(s)) >= 0);
    if (c < 0)
        return NULL;
    *ptr = 0;
    return buf;
}
示例#2
0
文件: igzip.c 项目: ceph/isa-l
static void isal_deflate_pass(struct isal_zstream *stream)
{
	struct isal_zstate *state = &stream->internal_state;
	struct isal_hufftables *hufftables = stream->hufftables;
	uint8_t *start_in = stream->next_in;

	if (state->state == ZSTATE_NEW_HDR || state->state == ZSTATE_HDR) {
		if (state->count == 0)
			/* Assume the final header is being written since the header
			 * stored in hufftables is the final header. */
			state->has_eob_hdr = 1;
		write_header(stream, hufftables->deflate_hdr, hufftables->deflate_hdr_count,
			     hufftables->deflate_hdr_extra_bits, ZSTATE_BODY,
			     !stream->end_of_stream);
	}

	if (state->state == ZSTATE_BODY)
		isal_deflate_body(stream);

	if (state->state == ZSTATE_FLUSH_READ_BUFFER)
		isal_deflate_finish(stream);

	if (state->state == ZSTATE_SYNC_FLUSH)
		sync_flush(stream);

	if (state->state == ZSTATE_FLUSH_WRITE_BUFFER)
		flush_write_buffer(stream);

	if (stream->gzip_flag)
		state->crc = crc32_gzip(state->crc, start_in, stream->next_in - start_in);

	if (state->state == ZSTATE_TRL)
		write_trailer(stream);
}
示例#3
0
文件: cache.c 项目: 274914765/C
void invalidate_dcache_region(void *start, size_t size)
{
    unsigned long v, begin, end, linesz, mask;

    linesz = boot_cpu_data.dcache.linesz;
    mask = linesz - 1;

    /* when first and/or last cachelines are shared, flush them
     * instead of invalidating ... never discard valid data!
     */
    begin = (unsigned long)start;
    end = begin + size;

    if (begin & mask) {
        flush_dcache_line(start);
        begin += linesz;
    }
    if (end & mask) {
        flush_dcache_line((void *)end);
        end &= ~mask;
    }

    /* remaining cachelines only need invalidation */
    for (v = begin; v < end; v += linesz)
        invalidate_dcache_line((void *)v);
    flush_write_buffer();
}
/**
 * sysfs_write_file - write an attribute
 * @file: file pointer
 * @user_buf: data to write
 * @count: number of bytes
 * @ppos: starting offset
 *
 * Copy data in from userland and pass it to the matching
 * sysfs_ops->store() by invoking flush_write_buffer().
 *
 * There is no easy way for us to know if userspace is only doing a partial
 * write, so we don't support them. We expect the entire buffer to come on
 * the first write.  Hint: if you're writing a value, first read the file,
 * modify only the the value you're changing, then write entire buffer
 * back.
 */
static ssize_t sysfs_write_file(struct file *file, const char __user *user_buf,
				size_t count, loff_t *ppos)
{
	struct sysfs_open_file *of = sysfs_of(file);
	ssize_t len = min_t(size_t, count, PAGE_SIZE);
	loff_t size = file_inode(file)->i_size;
	char *buf;

	if (sysfs_is_bin(of->sd) && size) {
		if (size <= *ppos)
			return 0;
		len = min_t(ssize_t, len, size - *ppos);
	}

	if (!len)
		return 0;

	buf = kmalloc(len + 1, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	if (copy_from_user(buf, user_buf, len)) {
		len = -EFAULT;
		goto out_free;
	}
	buf[len] = '\0';	/* guarantee string termination */

	len = flush_write_buffer(of, buf, *ppos, len);
	if (len > 0)
		*ppos += len;
out_free:
	kfree(buf);
	return len;
}
示例#5
0
文件: cache.c 项目: 274914765/C
static inline void __flush_icache_range(unsigned long start, unsigned long end)
{
    unsigned long v, linesz;

    linesz = boot_cpu_data.dcache.linesz;
    for (v = start; v < end; v += linesz) {
        clean_dcache_line((void *)v);
        invalidate_icache_line((void *)v);
    }

    flush_write_buffer();
}
示例#6
0
文件: cache.c 项目: 274914765/C
void flush_dcache_region(void *start, size_t size)
{
    unsigned long v, begin, end, linesz;

    linesz = boot_cpu_data.dcache.linesz;
    begin = (unsigned long)start & ~(linesz - 1);
    end = ((unsigned long)start + size + linesz - 1) & ~(linesz - 1);

    for (v = begin; v < end; v += linesz)
        flush_dcache_line((void *)v);
    flush_write_buffer();
}
示例#7
0
文件: file.c 项目: xricson/knoppix
static ssize_t
sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
	struct sysfs_buffer * buffer = file->private_data;

	count = fill_write_buffer(buffer,buf,count);
	if (count > 0)
		count = flush_write_buffer(file,buffer,count);
	if (count > 0)
		*ppos += count;
	return count;
}
示例#8
0
static int buffered_write_one(int c, ano_socket_t fd)
{
    struct timeval tv = { 0, 0 };

    if (fd < 0) {
        ano_sockseterr(SOCKERR_EBADF);
        return -1;
    }
    write_fd = fd;

    /* Try to flush the buffer if it's full. */
    if (write_curpos == write_bufend + 1 ||
        (write_curpos == write_netbuf
         && write_bufend == write_buftop - 1)) {
        flush_write_buffer(1);
        if (write_curpos == write_bufend + 1 ||
            (write_curpos == write_netbuf
             && write_bufend == write_buftop - 1)) {
            /* Write failed */
            if (debug >= 4)
                alog("debug: buffered_write_one(%d) returning %d", fd,
                     EOF);
            return EOF;
        }
    }

    /* Write the character. */
    *write_bufend++ = c;
    if (write_bufend == write_buftop)
        write_bufend = write_netbuf;

    /* Move it to the socket if we can. */
    flush_write_buffer(0);

    if (debug >= 4)
        alog("debug: buffered_write_one(%d) returning %d", fd, c);
    return (int) c & 0xFF;
}
示例#9
0
文件: file.c 项目: 383530895/linux
static ssize_t
configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
	struct configfs_buffer * buffer = file->private_data;
	ssize_t len;

	mutex_lock(&buffer->mutex);
	len = fill_write_buffer(buffer, buf, count);
	if (len > 0)
		len = flush_write_buffer(file->f_path.dentry, buffer, len);
	if (len > 0)
		*ppos += len;
	mutex_unlock(&buffer->mutex);
	return len;
}
示例#10
0
文件: file.c 项目: cilynx/dd-wrt
static ssize_t
sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
	struct sysfs_buffer * buffer = file->private_data;
	ssize_t len;

	down(&buffer->sem);
	len = fill_write_buffer(buffer, buf, count);
	if (len > 0)
		len = flush_write_buffer(file->f_path.dentry, buffer, len);
	if (len > 0)
		*ppos += len;
	up(&buffer->sem);
	return len;
}
示例#11
0
bool SocketManager<B>::buffer_write_bytes(B &pkt_buf, size_t len, uchar type) {
  size_t window, pkt_buf_ptr = 0;
  int len_nb;  // length in network byte order

  // assuming wbuf is large enough to
  // fit type and size fields in one go
  if (type != 0) {
    // type shouldn't be ignored
    wbuf.buf[wbuf.buf_ptr++] = type;
  }

  // make len include its field size as well
  len_nb = htonl(len + sizeof(int32_t));

  std::copy(reinterpret_cast<uchar *>(&len_nb),
            reinterpret_cast<uchar *>(&len_nb) + 4,
            std::begin(wbuf.buf) + wbuf.buf_ptr);

  wbuf.buf_ptr += sizeof(int32_t);
  wbuf.buf_size = wbuf.buf_ptr;

  // fill the contents
  while (len) {
    window = wbuf.get_max_size() - wbuf.buf_ptr;

    if (len <= window) {
      // contents fit in window
      std::copy(std::begin(pkt_buf) + pkt_buf_ptr,
                std::begin(pkt_buf) + pkt_buf_ptr + len,
                std::begin(wbuf.buf) + wbuf.buf_ptr);
      wbuf.buf_ptr += len;
      wbuf.buf_size = wbuf.buf_ptr;
      return true;
    } else {
      // non-trivial window
      std::copy(std::begin(pkt_buf) + pkt_buf_ptr,
                std::begin(pkt_buf) + pkt_buf_ptr + window,
                std::begin(wbuf.buf) + wbuf.buf_ptr);
      pkt_buf_ptr += window;
      len -= window;

      wbuf.buf_size = wbuf.get_max_size();
      // write failure
      if (!flush_write_buffer()) return false;
    }
  }
  return true;
}
示例#12
0
文件: igzip.c 项目: ceph/isa-l
static void isal_deflate_icf_pass(struct isal_zstream *stream)
{
	uint8_t *start_in = stream->next_in;
	struct isal_zstate *state = &stream->internal_state;
	struct level_2_buf *level_buf = (struct level_2_buf *)stream->level_buf;

	do {
		if (state->state == ZSTATE_NEW_HDR)
			init_new_icf_block(stream);

		if (state->state == ZSTATE_BODY)
			isal_deflate_icf_body(stream);

		if (state->state == ZSTATE_FLUSH_READ_BUFFER)
			isal_deflate_icf_finish(stream);

		if (state->state == ZSTATE_CREATE_HDR)
			create_icf_block_hdr(stream);

		if (state->state == ZSTATE_HDR)
			/* Note that the header may be prepended by the
			 * remaining bits in the previous block, as such the
			 * toggle header flag cannot be used */
			write_header(stream, level_buf->deflate_hdr,
				     level_buf->deflate_hdr_count,
				     level_buf->deflate_hdr_extra_bits,
				     ZSTATE_FLUSH_ICF_BUFFER, 0);

		if (state->state == ZSTATE_FLUSH_ICF_BUFFER)
			flush_icf_block(stream);

	} while (state->state == ZSTATE_NEW_HDR);

	if (state->state == ZSTATE_SYNC_FLUSH)
		sync_flush(stream);

	if (state->state == ZSTATE_FLUSH_WRITE_BUFFER)
		flush_write_buffer(stream);

	if (stream->gzip_flag)
		state->crc = crc32_gzip(state->crc, start_in, stream->next_in - start_in);

	if (state->state == ZSTATE_TRL)
		write_trailer(stream);
}
示例#13
0
文件: igzip.c 项目: 01org/isa-l
static void isal_deflate_int(struct isal_zstream *stream)
{
	struct isal_zstate *state = &stream->internal_state;
	if (state->state == ZSTATE_NEW_HDR || state->state == ZSTATE_HDR)
		write_header(stream);

	if (state->state == ZSTATE_BODY)
		isal_deflate_body(stream);

	if (state->state == ZSTATE_FLUSH_READ_BUFFER)
		isal_deflate_finish(stream);

	if (state->state == ZSTATE_SYNC_FLUSH)
		sync_flush(stream);

	if (state->state == ZSTATE_FLUSH_WRITE_BUFFER)
		flush_write_buffer(stream);

	if (state->state == ZSTATE_TRL)
		write_trailer(stream);
}
示例#14
0
static ssize_t
configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
    struct configfs_buffer *buffer = file->private_data;
    struct configfs_dirent *sd = file->f_path.dentry->d_fsdata;
    ssize_t len;

    if (WARN_ON(sd == NULL))
        return -EINVAL;

    if (WARN_ON(!(sd->s_type & CONFIGFS_ITEM_ATTR)))
        return -EINVAL;

    mutex_lock(&buffer->mutex);
    len = fill_write_buffer(buffer, buf, count);
    if (len > 0)
        len = flush_write_buffer(file->f_path.dentry, buffer, len);
    if (len > 0)
        *ppos += len;
    mutex_unlock(&buffer->mutex);
    return len;
}
示例#15
0
/**
 * Write data.
 * @param fd File Pointer
 * @param buf Buffer to write
 * @param len Length to write
 * @return int
 */
static int buffered_write(ano_socket_t fd, char *buf, int len)
{
    int nwritten, left = len;
    int errno_save = ano_sockgeterr();

    if (fd < 0) {
        errno = EBADF;
        return -1;
    }
    write_fd = fd;

    while (left > 0) {

        /* Don't try putting anything in the buffer if it's full. */
        if (write_curpos != write_bufend + 1 &&
            (write_curpos != write_netbuf
             || write_bufend != write_buftop - 1)) {
            /* See if we need to write up to the end of the buffer. */
            if (write_bufend + left >= write_buftop
                && write_curpos <= write_bufend) {
                nwritten = write_buftop - write_bufend;
                memcpy(write_bufend, buf, nwritten);
                buf += nwritten;
                left -= nwritten;
                write_bufend = write_netbuf;
            }
            /* Now we can copy a single chunk to write_bufend. */
            if (write_curpos > write_bufend
                && write_curpos - write_bufend - 1 < left)
                nwritten = write_curpos - write_bufend - 1;
            else
                nwritten = left;
            if (nwritten) {
                memcpy(write_bufend, buf, nwritten);
                buf += nwritten;
                left -= nwritten;
                write_bufend += nwritten;
            }
        }

        /* Now write to the socket as much as we can. */
        if (write_curpos == write_bufend + 1 ||
            (write_curpos == write_netbuf
             && write_bufend == write_buftop - 1))
            flush_write_buffer(1);
        else
            flush_write_buffer(0);
        errno_save = errno;
        if (write_curpos == write_bufend + 1 ||
            (write_curpos == write_netbuf
             && write_bufend == write_buftop - 1)) {
            /* Write failed on full buffer */
            break;
        }
    }

    if (debug >= 4) {
        alog("debug: buffered_write(%d,%p,%d) returning %d",
             fd, buf, len, len - left);
    }
    ano_sockseterr(errno_save);
    return len - left;
}