// Consume the specified number of bytes from the buffers. void consume(std::size_t size) { // Remove buffers from the start until the specified size is reached. while (size > 0 && !at_end_) { if (buffer_size(first_) <= size) { size -= buffer_size(first_); if (begin_remainder_ == buffers_.end()) at_end_ = true; else first_ = *begin_remainder_++; } else { first_ = first_ + size; size = 0; } } // Remove any more empty buffers at the start. while (!at_end_ && buffer_size(first_) == 0) { if (begin_remainder_ == buffers_.end()) at_end_ = true; else first_ = *begin_remainder_++; } }
/// Create a new non-modifiable buffer from an existing buffer. inline const_buffer_container_1 buffer(const const_buffer& b, std::size_t max_size_in_bytes) { return const_buffer_container_1( const_buffer(buffer_cast<const void*>(b), buffer_size(b) < max_size_in_bytes ? buffer_size(b) : max_size_in_bytes)); }
/// Create a new modifiable buffer from an existing buffer. inline mutable_buffer_container_1 buffer(const mutable_buffer& b, std::size_t max_size_in_bytes) { return mutable_buffer_container_1( mutable_buffer(buffer_cast<void*>(b), buffer_size(b) < max_size_in_bytes ? buffer_size(b) : max_size_in_bytes)); }
/** * @relates const_buffer */ inline const_buffer operator+(std::size_t start, const const_buffer& b) { if (start > buffer_size(b)) return const_buffer(); const char* new_data = buffer_cast<const char*>(b) + start; std::size_t new_size = buffer_size(b) - start; return const_buffer(new_data, new_size); }
/** * @relates mutable_buffer */ inline mutable_buffer operator+(const mutable_buffer& b, std::size_t start) { if (start > buffer_size(b)) return mutable_buffer(); char* new_data = buffer_cast<char*>(b) + start; std::size_t new_size = buffer_size(b) - start; return mutable_buffer(new_data, new_size); }
static bool need_wrap(struct buffer * buffer) { if (buffer == &primary_buf) return buffer_size(buffer) + indent_size() == max_columns; else return (buffer_size(&primary_buf) + buffer_size(&tag_buf) + buffer_size(buffer) + indent_size()) >= max_columns; }
void Attribute::resize(Mesh *mesh, AttributePrimitive prim, bool reserve_only) { if(reserve_only) { buffer.reserve(buffer_size(mesh, prim)); } else { buffer.resize(buffer_size(mesh, prim), 0); } }
mutable_buffer save(mutable_buffer& buffer, const mutable_buffer& v) { if(buffer_size(buffer) < buffer_size(v)) { throw serialization_error("not enough buffer"); } memcpy(buffer_cast<uint8*>(buffer), buffer_cast<uint8*>(v), buffer_size(v)); return buffer + buffer_size(v); }
/* * pconn is proxy --> server, and has no buffer. */ int write_server_handle( conn_t *pconn ) { log_message( LOG_DEBUG, "in write_server_handle" ); //如果client_conn已经关闭,还是需要把剩余数据发送给server端 if ( NULL == pconn->write_buffer ) { log_message( LOG_ERROR, "write_server_handle: write_buffer is NULL." ); return -1; } if ( 1 == pconn->write_closed ) { log_message( LOG_WARNING, "try writing to server[%s] fd[%d], but it closed write", inet_ntoa(pconn->addr.sin_addr), pconn->fd ); return -1; } int bytes; bytes = write_buffer( pconn->fd, pconn->write_buffer ); log_message( LOG_DEBUG, "send to server[%s:%d] %d bytes.\n", inet_ntoa(pconn->addr.sin_addr), ntohs(pconn->addr.sin_port), bytes ); if ( g_errno < 0 ) { //写出错 CONN_CLOSE_WRITE( pconn ); if ( 1 == pconn->read_closed ) return -1; } else { if ( 0==buffer_size(pconn->write_buffer) && //发送完缓冲区中的数据 ( NULL==pconn->server_conn || //并且客户端不会再有数据过来 1 == pconn->server_conn->read_closed ) ) { CONN_CLOSE_WRITE( pconn ); if ( 1 == pconn->read_closed ) return -1; } } /* * it's very important */ uint32_t clientf = 0, serverf = 0; //写缓存中还有数据 if ( 0 == pconn->write_closed && buffer_size( pconn->write_buffer ) > 0 ) serverf |= EPOLLOUT; //读缓存未满并且未关闭读 if ( 0 == pconn->read_closed && buffer_size( pconn->read_buffer ) < MAXBUFFSIZE ) serverf |= EPOLLIN; //继续侦听读事件 epoll_mod_connection( pconn, serverf ); conn_t *pclie_conn; if ( NULL != ( pclie_conn = pconn->server_conn ) ) { if ( 0 == pclie_conn->write_closed && buffer_size( pclie_conn->write_buffer ) > 0 ) clientf |= EPOLLOUT; if ( 0 == pclie_conn->read_closed && buffer_size( pclie_conn->read_buffer ) < MAXBUFFSIZE ) clientf |= EPOLLIN; epoll_mod_connection( pclie_conn, clientf ); } return TRUE; }
bool Lexer::skipComments() { // We need to ensure that we have enough characters in the buffer. switch (buffer_size()) { case 0: buffer_put(m_input->get()); case 1: buffer_put(m_input->get()); case 2: buffer_put(m_input->get()); case 3: buffer_put(m_input->get()); } // We have a comment iff it begins with '<!--' sequence. if (!(buffer_at(0) == '<' && buffer_at(1) == '!' && buffer_at(2) == '-' && buffer_at(3) == '-')) { return false; } buffer_pop(); buffer_pop(); buffer_pop(); buffer_pop(); for (;;) { // TODO: Handle unclosed comments. // As above, we enusre that we have enough characters available. switch (buffer_size()) { case 0: buffer_put(m_input->get()); case 1: buffer_put(m_input->get()); case 2: buffer_put(m_input->get()); } // The comment ends only with the '-->' sequence. if (buffer_at(0) == '-' && buffer_at(1) == '-' && buffer_at(2) == '>') { buffer_pop(); buffer_pop(); buffer_pop(); break; } buffer_pop(); } return true; }
static void content(void) { char current; /* * We should get one character at a time. */ assert(strlen(yytext) == 1); current = yytext[0]; if (current == EOF) return; if (is_newline(current)) { newline(); return; } buffer_push_char(current_buf, current); /* * Forcing newline changes 'text' so lets do it after we've pushed * it to the buffer. */ if (wrap_long_lines && need_wrap(current_buf)) { struct buffer tmp; buffer_init(&tmp, buffer_size(current_buf)); /* * Find last character that was not whitespace */ for (;;) { int c; if (buffer_size(current_buf) == 0) break; c = buffer_pop_char(current_buf); if (is_whitespace(c)) { /* * Do not push whitespace because it would appear * after the newline. */ break; } /* * Characters are put in tmp buffer in reverse order. */ buffer_push_char(&tmp, c); } force_newline_for_wrap(current_buf); /* * Restore non-wrapped text into buffer. */ while (buffer_size(&tmp) > 0) { buffer_push_char(current_buf, buffer_pop_char(&tmp)); } buffer_release(&tmp); } }
void Attribute::reserve(int numverts, int numtris, int numsteps, int numcurves, int numkeys, bool resize) { if (resize) { buffer.resize(buffer_size(numverts, numtris, numsteps, numcurves, numkeys), 0); } else { buffer.reserve(buffer_size(numverts, numtris, numsteps, numcurves, numkeys)); } }
const_buffer load(const_buffer& buffer, const_buffer& v) { if(buffer_size(buffer) < buffer_size(v)) { throw serialization_error("not enough buffer"); } memcpy(const_cast<uint8*>(buffer_cast<const uint8*>(v)) , buffer_cast<const uint8*>(buffer), buffer_size(v)); return buffer + buffer_size(v); }
bool equal(const consuming_buffers_iterator& other) const { if (at_end_ && other.at_end_) return true; return !at_end_ && !other.at_end_ && buffer_cast<const void*>(first_) == buffer_cast<const void*>(other.first_) && buffer_size(first_) == buffer_size(other.first_) && begin_remainder_ == other.begin_remainder_ && end_remainder_ == other.end_remainder_; }
/** * VMNative: string_prealloc( workshop , newsize ) * Accepts a workshop argument and a number. The newsize is the size to * reallocate the workshop buffer to. Preallocate buffer for best performance. */ static bool vmn_str_prealloc(VM * vm, VMArg * arg, int argc) { VMLibData * data; Buffer * buffer; int newSize; /* check for proper number of arguments */ if(argc != 2) { vm_set_err(vm, VMERR_INCORRECT_NUMARGS); return false; } /* check argument 1 major type */ if(vmarg_type(arg[0]) != TYPE_LIBDATA) { vm_set_err(vm, VMERR_INVALID_TYPE_ARGUMENT); return false; } /* extract the libdata from the argument */ data = vmarg_libdata(arg[0]); /* check libdata type */ if(!vmlibdata_is_type(data, LIBSTR_STRING_TYPE, LIBSTR_STRING_TYPE_LEN)) { vm_set_err(vm, VMERR_INVALID_TYPE_ARGUMENT); return false; } /* check argument 2 type */ if(vmarg_type(arg[1]) != TYPE_NUMBER) { vm_set_err(vm, VMERR_INVALID_TYPE_ARGUMENT); return false; } newSize = (int)vmarg_number(arg[1], NULL); /* check buffer size range */ if(newSize < 1) { vm_set_err(vm, VMERR_ARGUMENT_OUT_OF_RANGE); return false; } /* extract the workshop */ buffer = vmlibdata_data(data); /* can't make it smaller, only bigger */ buffer_resize(buffer, newSize >= buffer_size(buffer) ? newSize : buffer_size(buffer)); /* push null result */ vmarg_push_null(vm); /* this function does return a value */ return true; }
void stream_check_test(Buffer_Set *buffers, void *scratch, int scratch_size){ int i, page_size, size; size = buffer_size(&buffers->buffer); { int size2; size2 = buffer_size(&buffers->gap_buffer); hard_assert_4tech(size == size2); size2 = buffer_size(&buffers->multi_gap_buffer); hard_assert_4tech(size == size2); size2 = buffer_size(&buffers->rope_buffer); hard_assert_4tech(size == size2); } page_size = 1 << 10; char *page_1 = (char*)scratch; char *page_2 = page_1 + page_size; scratch_size -= page_size*2; hard_assert_4tech(scratch_size > 0); for (i = 0; i < size; i += page_size){ int end = i + page_size; if (end > size) end = size; buffer_stringify(&buffers->buffer, i, end, page_1); buffer_stringify(&buffers->gap_buffer, i, end, page_2); page_compare(page_1, page_2, page_size); buffer_stringify(&buffers->multi_gap_buffer, i, end, page_2); page_compare(page_1, page_2, page_size); buffer_stringify(&buffers->rope_buffer, i, end, page_2); page_compare(page_1, page_2, page_size); } for (i = size-1; i > 0; i -= page_size){ int end = i - page_size; if (end < 0) end = 0; buffer_backify(&buffers->buffer, i, end, page_1); buffer_backify(&buffers->gap_buffer, i, end, page_2); page_compare(page_1, page_2, page_size); buffer_backify(&buffers->multi_gap_buffer, i, end, page_2); page_compare(page_1, page_2, page_size); buffer_backify(&buffers->rope_buffer, i, end, page_2); page_compare(page_1, page_2, page_size); } }
int qmux_fill(u16 cid, struct buffer *buf) { struct qmux *qmux = buffer_data(buf); if (buffer_size(buf) < sizeof(*qmux)) return -ENOMEM; qmux->tf = 1; qmux->len = buffer_size(buf) - 1; qmux->ctrl = 0; qmux->service = cid & 0xff; qmux->qmicid = cid >> 8; return 0; }
static int check_size(const struct buffer *b, size_t offset, size_t size, const char *desc) { if (size == 0) return 0; if (offset >= buffer_size(b) || (offset + size) > buffer_size(b)) { ERROR("The file is not large enough for the '%s'. " "%zu bytes @ offset %zu, input %zu bytes.\n", desc, size, offset, buffer_size(b)); return -1; } return 0; }
void data_received(endpoint_t ep, int err, size_t bytes, size_t more_bytes, buffer_t *buffer, void *ctx) { context_t *context = ctx; fprintf(stdout, "Receival error: %d: %s\n", err, strerror(err)); if(err) { io_service_stop(context->service, true); return; } fprintf(stdout, "Data received (size: %llu): %.*s\n", buffer_size(buffer), buffer_size(buffer), buffer_data(buffer)); io_service_stop(context->service, true); }
/* * pconn: client --> proxy, its buffer isn't NULL */ int write_client_handle( conn_t *pconn ) { log_message( LOG_DEBUG, "in write_client_handle" ); if ( 1 == pconn->write_closed ) { log_message( LOG_WARNING, "try writing to client[%s:%d], but it closed write", inet_ntoa(pconn->addr.sin_addr), ntohs(pconn->addr.sin_port) ); return -1; } int bytes; bytes = write_buffer( pconn->fd, pconn->write_buffer ); log_message( LOG_DEBUG, "write %d bytes to client[%s:%d].", bytes, inet_ntoa(pconn->addr.sin_addr), ntohs(pconn->addr.sin_port) ); if ( g_errno < 0 ) { //写出错 CONN_CLOSE_WRITE( pconn ); if ( 1 == pconn->read_closed ) return -1; } else { if ( 0==buffer_size(pconn->write_buffer) && //发送完缓冲区中的数据 ( NULL==pconn->server_conn || //并且server已经不再写 ( NULL!=pconn->server_conn && 1 == pconn->server_conn->read_closed )) ) { CONN_CLOSE_WRITE( pconn ); if ( 1 == pconn->read_closed ) return -1; } } /* * it's very important */ uint32_t clientf = 0, serverf = 0; if ( NULL != pconn->server_conn ) { conn_t *pserv_conn = pconn->server_conn; if ( 0 == pserv_conn->write_closed && buffer_size( pserv_conn->write_buffer ) > 0 ) serverf |= EPOLLOUT; if ( 0 == pserv_conn->read_closed && buffer_size( pserv_conn->read_buffer ) < MAXBUFFSIZE ) serverf |= EPOLLIN; epoll_mod_connection( pserv_conn, serverf ); } if ( 0 == pconn->write_closed && buffer_size( pconn->write_buffer ) > 0 ) clientf |= EPOLLOUT; if ( 0 == pconn->read_closed && buffer_size( pconn->read_buffer ) < MAXBUFFSIZE ) clientf |= EPOLLIN; epoll_mod_connection( pconn, clientf ); return 0; }
//实现随机存取,迭代器可以直接跳跃n个距离 self& operator+=(difference_type n) { difference_type offset = n + (cur - first); if (offset >= 0 && offset < difference_type(buffer_size())) cur += n; //目标位置在同一缓冲区内 else { //目标位置不在同一缓冲区内 difference_type node_offset = offset > 0 ? offset / difference_type(buffer_size()) : -difference_type((-offset - 1) / buffer_size()) - 1; set_node(node + node_offset); //切换到正确的节点(缓冲区) //切换到正确的元素 cur = first + (offset - node_offset * difference_type(buffer_size())); } return *this; }
void ramp_request_proxy (void* state, void* param, bid_t bid) { ramp_t* ramp = state; assert (ramp != NULL); assert (buffer_size (bid) == sizeof (proxy_request_t)); const proxy_request_t* request = buffer_read_ptr (bid); ramp_proxy_t* ramp_proxy = malloc (sizeof (ramp_proxy_t)); ramp_proxy->request = *request; assert (automan_declare (ramp->automan, &ramp_proxy->composed, ramp_proxy, ramp_declared, ramp_proxy) == 0); assert (automan_create (ramp->automan, &ramp_proxy->aid, &ramp_proxy_descriptor, NULL, ramp_proxy_created, ramp_proxy) == 0); assert (automan_compose (ramp->automan, &ramp_proxy->composed, &ramp->self, ramp_integer_out, ramp_proxy, &ramp_proxy->aid, ramp_proxy_integer_in, NULL, NULL, NULL) == 0); assert (schedule_system_output () == 0); }
/** file_read : 'file -> s:string -> p:int -> l:int -> int <doc> Read up to [l] chars into the string [s] starting at position [p]. Returns the number of chars readed which is > 0 (or 0 if l == 0). </doc> **/ static value file_read( value o, value s, value pp, value n ) { fio *f; int p; int len; int buf_len; val_check_kind(o,k_file); val_check(s,buffer); buffer buf = val_to_buffer(s); buf_len = buffer_size(buf); val_check(pp,int); val_check(n,int); f = val_file(o); p = val_int(pp); len = val_int(n); if( p < 0 || len < 0 || p > buf_len || p + len > buf_len ) return alloc_null(); gc_enter_blocking(); while( len > 0 ) { int d; POSIX_LABEL(file_read_again); d = (int)fread(buffer_data(buf)+p,1,len,f->io); if( d <= 0 ) { int size = val_int(n) - len; HANDLE_FINTR(f->io,file_read_again); if( size == 0 ) file_error("file_read",f); return alloc_int(size); } p += d; len -= d; } gc_exit_blocking(); return n; }
void increment() { if (!at_end_) { if (begin_remainder_ == end_remainder_ || offset_ + buffer_size(first_) >= max_size_) { at_end_ = true; } else { offset_ += buffer_size(first_); first_ = buffer(*begin_remainder_++, max_size_ - offset_); } } }
size_t octstr_len(const octstr_t *octstr) { assert(octstr != NULL); return buffer_size(octstr->buf); }
static value socket_recv_from( value o, value dataBuf, value pos, value len, value addr ) { int p,l,ret; int retry = 0; struct sockaddr_in saddr; SockLen slen = sizeof(saddr); val_check_kind(o,k_socket); val_check(dataBuf,buffer); buffer buf = val_to_buffer(dataBuf); char *data = buffer_data(buf); int dlen = buffer_size(buf); val_check(pos,int); val_check(len,int); val_check(addr,object); p = val_int(pos); l = val_int(len); if( p < 0 || l < 0 || p > dlen || p + l > dlen ) neko_error(); SOCKET sock = val_sock(o); gc_enter_blocking(); POSIX_LABEL(recv_from_again); if( retry++ > NRETRYS ) { ret = recv(sock,data+p,l,MSG_NOSIGNAL); } else ret = recvfrom(sock, data + p , l, MSG_NOSIGNAL, (struct sockaddr*)&saddr, &slen); if( ret == SOCKET_ERROR ) { HANDLE_EINTR(recv_from_again); return block_error(); } gc_exit_blocking(); alloc_field(addr,f_host,alloc_int32(*(int*)&saddr.sin_addr)); alloc_field(addr,f_port,alloc_int(ntohs(saddr.sin_port))); return alloc_int(ret); }
/** socket_send_to : 'socket -> buf:string -> pos:int -> length:int -> addr:{host:'int32,port:int} -> int <doc> Send data from an unconnected UDP socket to the given address. </doc> **/ static value socket_send_to( value o, value dataBuf, value pos, value len, value vaddr ) { int p,l; value host, port; struct sockaddr_in addr; val_check_kind(o,k_socket); buffer buf = val_to_buffer(dataBuf); const char *cdata = buffer_data(buf); int dlen = buffer_size(buf); val_check(pos,int); val_check(len,int); val_check(vaddr,object); host = val_field(vaddr, f_host); port = val_field(vaddr, f_port); val_check(host,int); val_check(port,int); p = val_int(pos); l = val_int(len); memset(&addr,0,sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(val_int(port)); *(int*)&addr.sin_addr.s_addr = val_int(host); if( p < 0 || l < 0 || p > dlen || p + l > dlen ) neko_error(); SOCKET sock = val_sock(o); gc_enter_blocking(); POSIX_LABEL(send_again); dlen = sendto(sock, cdata + p , l, MSG_NOSIGNAL, (struct sockaddr*)&addr, sizeof(addr)); if( dlen == SOCKET_ERROR ) { HANDLE_EINTR(send_again); return block_error(); } gc_exit_blocking(); return alloc_int(dlen); }
DEFINE_FUNC_1(webp_decode_argb, data_buffer_value) { if (!val_is_buffer(data_buffer_value)) { val_throw(alloc_string("webp_decode_argb: Expected to be a buffer")); return alloc_null(); } buffer data_buffer = val_to_buffer(data_buffer_value); int data_len = buffer_size(data_buffer); char *data_ptr = buffer_data(data_buffer); int webp_width = -1, webp_height = -1; char *webp_data_ptr = (char *)WebPDecodeARGB((const unsigned char *)data_ptr, data_len, &webp_width, &webp_height); int webp_data_len = webp_width * webp_height * 4; if (webp_data_ptr == NULL) { val_throw(alloc_string("webp_decode_argb: Invalid webp data")); return alloc_null(); } buffer webp_buffer = alloc_buffer_len(0); buffer_append_sub(webp_buffer, webp_data_ptr, webp_data_len); buffer_set_size(webp_buffer, webp_data_len); value array = alloc_array(3); val_array_set_i(array, 0, alloc_int(webp_width)); val_array_set_i(array, 1, alloc_int(webp_height)); val_array_set_i(array, 2, buffer_val(webp_buffer)); if (webp_data_ptr != NULL) free(webp_data_ptr); return array; }
void BufferedFile::refill_buffer() { if (at_eof()) { return; } int bytes_to_read = buffer_size(); int max = file_size() - file_pos(); if (bytes_to_read > max) { bytes_to_read = max; } int count_read; { // The file_pointer() may be shared with a FileDecoder // object. This may cause BufferedFile::file_pos() to become out // of date. A call to OsFile_seek() makes everything consistent again. AllocationDisabler raw_pointers_used_in_this_block; Buffer::Raw fb = data_buffer(); OsFile_seek(file_pointer(), file_pos(), SEEK_SET); count_read = OsFile_read(file_pointer(), fb().base_address(), 1, bytes_to_read); } set_count(count_read); set_file_pos(long(file_pos() + count_read)); set_index(0); if (count_read <= 0) { set_at_eof(true); } return; }
int fix_message_parse(struct fix_message *self, struct buffer *buffer) { int ret = FIX_MSG_STATE_PARTIAL; unsigned long size; const char *start; self->head_buf = buffer; retry: start = buffer_start(buffer); size = buffer_size(buffer); if (!size) goto fail; ret = first_three_fields(self); if (ret) goto fail; ret = checksum(self, buffer); if (ret) goto fail; rest_of_message(self, buffer); return 0; fail: if (ret != FIX_MSG_STATE_PARTIAL) goto retry; buffer_advance(buffer, start - buffer_start(buffer)); return -1; }