static void test4() { struct Buffer *buffer; int read_fd, write_fd; buffer = new_buffer(4096, EV_DEFAULT); read_fd = open("/dev/zero", O_RDONLY); if (read_fd < 0) { perror("open:"); exit(1); } write_fd = open("/dev/null", O_WRONLY); if (write_fd < 0) { perror("open:"); exit(1); } while (buffer->tx_bytes < 65536) { buffer_read(buffer, read_fd); buffer_write(buffer, write_fd); } free_buffer(buffer); }
struct BufferedSocket *new_buffered_socket(struct ev_loop *loop, const char *address, int port, void (*connect_callback)(struct BufferedSocket *buffsock, void *arg), void (*close_callback)(struct BufferedSocket *buffsock, void *arg), void (*read_callback)(struct BufferedSocket *buffsock, struct Buffer *buf, void *arg), void (*write_callback)(struct BufferedSocket *buffsock, void *arg), void (*error_callback)(struct BufferedSocket *buffsock, void *arg), void *cbarg) { struct BufferedSocket *buffsock; buffsock = malloc(sizeof(struct BufferedSocket)); buffsock->address = strdup(address); buffsock->port = port; buffsock->read_buf = new_buffer(1024 * 16, 1024 * 16); buffsock->write_buf = new_buffer(1024 * 16, 1024 * 16); buffsock->fd = -1; buffsock->state = BS_INIT; buffsock->connect_callback = connect_callback; buffsock->close_callback = close_callback; buffsock->read_callback = read_callback; buffsock->write_callback = write_callback; buffsock->error_callback = error_callback; buffsock->cbarg = cbarg; buffsock->read_bytes_n = 0; buffsock->read_bytes_callback = NULL; buffsock->read_bytes_arg = NULL; buffsock->loop = loop; ev_init(&buffsock->read_bytes_timer_ev, buffered_socket_read_bytes_cb); buffsock->read_bytes_timer_ev.data = buffsock; ev_timer_set(&buffsock->read_bytes_timer_ev, 0., 0.); return buffsock; }
header * new_header(size_t fsize, size_t flimit, size_t vsize, size_t vlimit) { header *h; h = ruby_xmalloc(sizeof(header)); h->field = new_buffer(fsize, flimit); h->value = new_buffer(vsize, vlimit); return h; }
int query_string_cb (http_parser *p, const char *buf, size_t len, char partial) { client_t *client = get_client(p); request *req = client->req; buffer_result ret = MEMORY_ERROR; if(req->query_string){ ret = write2buf(req->query_string, buf, len); }else{ req->query_string = new_buffer(1024*2, LIMIT_QUERY_STRING); ret = write2buf(req->query_string, buf, len); } switch(ret){ case MEMORY_ERROR: client->bad_request_code = 500; return -1; case LIMIT_OVER: client->bad_request_code = 400; return -1; default: break; } return 0; }
void test1() { struct Buffer *buffer; char input[] = "This is a test."; char output[sizeof(input)]; int len, i; buffer = new_buffer(); len = buffer_push(buffer, input, sizeof(input)); assert(len == sizeof(input)); len = buffer_peek(buffer, output, sizeof(output)); assert(len == sizeof(input)); for (i = 0; i < len; i++) assert(input[i] == output[i]); len = buffer_peek(buffer, output, sizeof(output)); assert(len == sizeof(input)); for (i = 0; i < len; i++) assert(input[i] == output[i]); len = buffer_pop(buffer, output, sizeof(output)); assert(len == sizeof(input)); for (i = 0; i < len; i++) assert(input[i] == output[i]); len = buffer_pop(buffer, output, sizeof(output)); assert(len == 0); free_buffer(buffer); }
int fragment_cb (http_parser *p, const char *buf, size_t len, char partial) { client_t *client = get_client(p); register request *req = client->req; buffer_result ret = MEMORY_ERROR; if(req->fragment){ ret = write2buf(req->fragment, buf, len); }else{ req->fragment = new_buffer(1024, LIMIT_FRAGMENT); ret = write2buf(req->fragment, buf, len); } switch(ret){ case MEMORY_ERROR: client->bad_request_code = 500; return -1; case LIMIT_OVER: client->bad_request_code = 400; return -1; default: break; } return 0; }
struct buffer_head *blockdirty(struct buffer_head *buffer, unsigned newdelta) { map_t *map = buffer->map; assert(buffer->state < BUFFER_STATES); buftrace("---- before: fork buffer %p ----", buffer); if (buffer_dirty(buffer)) { if (buffer_already_dirty(buffer, newdelta)) return buffer; /* Buffer can't modify already, we have to fork buffer */ buftrace("---- fork buffer %p ----", buffer); struct buffer_head *clone = new_buffer(map); if (IS_ERR(clone)) return clone; /* Create the cloned buffer */ memcpy(bufdata(clone), bufdata(buffer), bufsize(buffer)); clone->index = buffer->index; /* Replace the buffer by cloned buffer. */ remove_buffer_hash(buffer); insert_buffer_hash(clone); /* * The refcount of buffer is used for backend. So, the * backend has to free this buffer (blockput(buffer)) */ buffer = clone; } __tux3_mark_buffer_dirty(buffer, newdelta); return buffer; }
static DWORD add_dispatcher_to_pefile(pefile_t *pefile, struct call_table *ctable, DWORD nElements) { buffer_t *section_data_buf; char *dispatcher; //char *new_ctable; assert(pefile != NULL); assert(ctable != NULL); section_data_buf = new_buffer(sizeof (DWORD) + sizeof dispatcher_stub + nElements * sizeof (struct call_table)); memcpy(§ion_data_buf->buf[sizeof (DWORD)], dispatcher_stub, sizeof dispatcher_stub); memcpy(§ion_data_buf->buf[sizeof (DWORD) + sizeof dispatcher_stub], ctable, nElements * sizeof (struct call_table)); //last_calltable = (struct call_table *)new_ctable; dispatcher = add_section_to_pefile(pefile, ".disp", section_data_buf, 0xE0000020); if (dispatcher != NULL) { DWORD *lpdwIndirectPtr = (DWORD *) dispatcher; *lpdwIndirectPtr = ptr_to_va(pefile, dispatcher + sizeof (DWORD)); last_calltable = (struct call_table *)(dispatcher + sizeof (DWORD) + sizeof dispatcher_stub); return *lpdwIndirectPtr - sizeof (DWORD); } else { return -1; } }
static void test3() { struct Buffer *buffer; char input[] = "Test buffer resizing."; char output[sizeof(input)]; int len, i; buffer = new_buffer(200, EV_DEFAULT); assert(buffer != NULL); len = buffer_push(buffer, input, sizeof(input)); assert(len == sizeof(input)); /* Test resizing to too small of a buffer size */ len = buffer_resize(buffer, 5); assert(len == -1); buffer_resize(buffer, 40); assert(buffer_room(buffer) == 40 - sizeof(input)); len = buffer_peek(buffer, output, sizeof(output)); assert(len == sizeof(input)); for (i = 0; i < len; i++) assert(input[i] == output[i]); free_buffer(buffer); }
static int url_cb(http_parser *p, const char *buf, size_t len) { request *req = get_current_request(p); buffer_result ret = MEMORY_ERROR; if(unlikely(req->path)){ ret = write2buf(req->path, buf, len); }else{ req->path = new_buffer(1024, LIMIT_PATH); ret = write2buf(req->path, buf, len); } switch(ret){ case MEMORY_ERROR: req->bad_request_code = 500; return -1; case LIMIT_OVER: req->bad_request_code = 400; return -1; default: break; } return 0; }
TextContent::TextContent() : WindowContent(), _leading(0.06), _fontScale(0.04), _fontAspect(1.0), _fontWeight(1), _windowLinePos(0), _windowLineSpan(0), _magicCharPos(0), _title("buffer:"), _canvasDirty(true), _parsed(NULL), _parse_tree(NULL), _parse_edit_point(0), _viewportDirty(true) { EM_log( CK_LOG_FINER, "(audicle) TextContent ctor..." ); _loc.line = 0; _loc.chr = 0; _markLoc = _loc; _wSpace = bufferFont->length("m"); _wTab = _wSpace * 4; _viewport.setScale( 1.1, 1.1 ); _fontColor = Color4D ( 0,0,0,0.95 ); _buf = new_buffer(); //should be in CodeRevision }
static file_buffer* find_buffer(buffered_file_handle* h, off_t offset) { offset = offset - offset % READ_BUFFER_CAPACITY; // Find a buffer for this offset, or use the last one: file_buffer* buffer = h->first_buffer; while (buffer->offset != offset && buffer->next != NULL) buffer = buffer->next; if (buffer->offset != offset) { if (h->nbuffers < MAX_READ_BUFFERS) { // Didn't find a matching one, but we can still create another: file_buffer* buffer2 = new_buffer(h, READ_BUFFER_CAPACITY); if (buffer2) { buffer = buffer2; ++h->nbuffers; } } else { #if LOG_BUFFER fprintf(stderr, "BUFFER: %p recycled, from %zd to %zd\n", buffer, buffer->offset, offset); #endif } } if (buffer != h->first_buffer) { // Move the buffer to the start of the list: if (buffer->prev) buffer->prev->next = buffer->next; if (buffer->next) buffer->next->prev = buffer->prev; buffer->prev = NULL; h->first_buffer->prev = buffer; buffer->next = h->first_buffer; h->first_buffer = buffer; } return buffer; }
static PyObject* create_status(PyObject *bytes, int bytelen, int http_minor) { buffer_result r; buffer_t *b = new_buffer(256, 0); if(b == NULL){ return NULL; } if(http_minor == 1){ r = write2buf(b, "HTTP/1.1 ", 9); }else{ r = write2buf(b, "HTTP/1.0 ", 9); } if(r != WRITE_OK){ goto error; } r = write2buf(b, PyBytes_AS_STRING(bytes), bytelen); if(r != WRITE_OK){ goto error; } r = write2buf(b, "\r\n", 2); if(r != WRITE_OK){ goto error; } return getPyString(b); error: free_buffer(b); return NULL; }
variable flux_new_buffer(opts o,interpreter *i) { int buf=0; char *str; char *filename; variable v=init_variable(); if (i->cast_string(&str,o,0)) { if (i->cast_string(&filename,o,1)) v=i->int_variable((buf=new_buffer(str,filename))); else v=i->int_variable((buf=new_buffer(str))); } else v=i->int_variable((buf=new_buffer())); return v; }
static couch_file_handle buffered_constructor_with_raw_ops(const couch_file_ops* raw_ops) { buffered_file_handle *h = malloc(sizeof(buffered_file_handle)); if (h) { h->raw_ops = raw_ops; h->raw_ops_handle = raw_ops->constructor(raw_ops->cookie); h->nbuffers = 1; h->write_buffer = new_buffer(h, WRITE_BUFFER_CAPACITY); h->first_buffer = new_buffer(h, READ_BUFFER_CAPACITY); if (!h->write_buffer || !h->first_buffer) { buffered_destructor((couch_file_handle)h); h = NULL; } } return (couch_file_handle) h; }
Buffer * filtered_buffer(filtre_t *F) { Buffer *b = new_buffer(); init_filtre(F, b); pari_stack_pushp(&s_bufstack, (void*)b); return b; }
file *rl_decode(const char *data, long size) { int i; byte_buffer *buff = new_buffer(size); memcpy(buff->buffer, data, size); // 1. Get size of each run. uchar bytes[2]; for (i = 0; i < 2; ++i) { bytes[i] = get_next_byte(buff); } const ushort NUM_BITS = *((ushort *)bytes); // 2. Decode data int zeros = 1; int remaining = 8 * size; int max_len = (4 * size)/3; byte_buffer *output = new_buffer(max_len); i = 0; while (remaining > NUM_BITS) { int bits = read_length(buff, NUM_BITS); for (i = 0; i < bits; ++i) { if (zeros) { append_bit_to_buffer(output, 0); } else { append_bit_to_buffer(output, 1); } } zeros = (zeros == 0)? 1 : 0; remaining -= NUM_BITS; } // 3. Convert output buffer to file file *f = (file *)malloc(sizeof(file)); f->size = (long)output->current_byte; f->data = (char *)malloc(f->size); memcpy(f->data, output->buffer, f->size); // 4. Free resources free_buffer(output); free_buffer(buff); return f; }
void String::ConvertToUnicode() { if ( unicode ) return; unicode = true; Buffer new_buffer( GetTotalLengthInBytes() ); CharToWChar( buffer.ptr.As< char >(), new_buffer.ptr.As< wchar_t >(), GetTotalLength() ); buffer = new_buffer; }
void String::ConvertToAscii() { if ( !unicode ) return; unicode = false; Buffer new_buffer( GetTotalLengthInBytes() ); WCharToChar( buffer.ptr.As< wchar_t >(), new_buffer.ptr.As< char >(), GetTotalLength() ); buffer = new_buffer; }
RexxObject *SystemInterpreter::buildEnvlist() { BufferClass *newBuffer; /* Buffer object to hold env */ char **Environment; /* environment pointer */ size_t size = 0; /* size of the new buffer */ char *curr_dir; /* current directory */ char *New; /* starting address of buffer */ Environment = getEnvironment(); /* get the ptr to the environ */ for (;*Environment != NULL;Environment++) { size += strlen(*Environment); /* calculate the size for all */ size++; /* environment variables+'\0' */ } /* now get current dir */ if (!size) { return OREF_NULL; /* no envrionment ! */ } if (!(curr_dir=(char *)malloc(PATH_MAX + 3)))/* malloc storage for cwd*/ { reportException(Error_System_service); } // start with a copy of the current working directory SystemInterpreter::getCurrentWorkingDirectory(curr_dir); size += strlen(curr_dir); /* add the space for curr dir */ size++; /* and its terminating '\0' */ size += sizeof(size_t); /* this is for the size itself*/ /* Now we have the size for */ /* allocating the new buffer */ newBuffer = new_buffer(size); /* let's do it */ /* Get starting address of buf*/ New = newBuffer->getData(); ((ENVENTRY*)New)->size = size; /* first write the size */ New += sizeof(size_t); /* update the pointer */ /* now write the curr dir */ memcpy(New,curr_dir,strlen(curr_dir)); New += strlen(curr_dir); /* update the pointer */ memcpy(New,"\0",1); /* write the terminator */ New++; /* update the pointer */ Environment = getEnvironment(); /* reset to begin of environ */ /* Loop through environment */ /* and copy all entries to the*/ /* buffer, each terminating */ /* with '\0' */ for (;*Environment != NULL;Environment++) { /* copy the entry */ memcpy(New,*Environment,strlen(*Environment)); New += strlen(*Environment); /* update the pointer */ memcpy(New,"\0",1); /* write the terminator */ New++; /* update the pointer */ } free(curr_dir); /* free curr dir buffer */ return newBuffer; /* return the pointer */ }
void String::NullTerminate() { if ( null_terminated ) return; null_terminated = true; Buffer new_buffer( GetTotalLengthInBytes() ); if ( unicode ) ((wchar_t*)new_buffer.GetPtr( buffer.size ))[0] = 0; else (new_buffer.GetPtr( buffer.size ))[0] = 0; buffer = new_buffer; }
byte_buffer *rl_compress(char *data, long size) { byte_buffer *buff = new_buffer((3 * size)/4); long i; int k; // 1. Determine the longest sequence of 1s or 0s. int longest_seq = 1, current_seq = 0; uchar current = is_bit_set(data[0], 0); for (i = 0; i < size; i++) { for (k = 0; k < 8; ++k) { if (is_bit_set(data[i], k) == current) { current_seq++; } else { if (current_seq > longest_seq) longest_seq = current_seq; current_seq = 1; current = (current == 0)? 1 : 0; } } } // 2. Determine number of bits required to represent that sequence. const ushort NUM_BITS = (const ushort)ceil(log((double)longest_seq)/log(2.0)); // 3. First 16 bits used to store length of the run lengths in bits. uchar *byte_representation = (uchar *)(&NUM_BITS); for (i = 0; i < sizeof(NUM_BITS); ++i) { append_byte_to_buffer(buff, byte_representation[i]); } // 4. Start with 0s, write length, then write 1s length, etc. if (is_bit_set(data[0], 0)) { write_length(buff, 0, NUM_BITS); } current_seq = 0; current = is_bit_set(data[0], 0); for (i = 0; i < size; i++) { for (k = 0; k < 8; ++k) { if (is_bit_set(data[i], k) == current) { current_seq++; } else { write_length(buff, current_seq, NUM_BITS); current_seq = 1; current = is_bit_set(data[i], k); } } } if (current_seq > 0) { write_length(buff, current_seq, NUM_BITS); } return buff; }
int sa_stream_create_pcm( sa_stream_t ** _s, const char * client_name, sa_mode_t mode, sa_pcm_format_t format, unsigned int rate, unsigned int n_channels ) { /* * Make sure we return a NULL stream pointer on failure. */ if (_s == NULL) { return SA_ERROR_INVALID; } *_s = NULL; if (mode != SA_MODE_WRONLY) { return SA_ERROR_NOT_SUPPORTED; } if (format != SA_PCM_FORMAT_S16_NE) { return SA_ERROR_NOT_SUPPORTED; } /* * Allocate the instance and required resources. */ sa_stream_t * s; if ((s = malloc(sizeof(sa_stream_t))) == NULL) { return SA_ERROR_OOM; } if ((s->bl_head = new_buffer()) == NULL) { free(s); return SA_ERROR_OOM; } if (pthread_mutex_init(&s->mutex, NULL) != 0) { free(s->bl_head); free(s); return SA_ERROR_SYSTEM; } s->output_unit = NULL; s->playing = FALSE; s->bytes_played = 0; s->bytes_played_last = 0; s->rate = rate; s->n_channels = n_channels; s->bytes_per_ch = 2; s->bl_tail = s->bl_head; s->n_bufs = 1; *_s = s; return SA_SUCCESS; }
PYOPENCL_USE_RESULT buffer* buffer::get_sub_region(size_t orig, size_t size, cl_mem_flags flags) const { cl_buffer_region reg = {orig, size}; auto mem = retry_mem_error([&] { return pyopencl_call_guarded(clCreateSubBuffer, PYOPENCL_CL_CASTABLE_THIS, flags, CL_BUFFER_CREATE_TYPE_REGION, ®); }); return new_buffer(mem); }
// Buffer error* create_buffer(clobj_t *buffer, clobj_t _ctx, cl_mem_flags flags, size_t size, void *hostbuf) { auto ctx = static_cast<context*>(_ctx); return c_handle_retry_mem_error([&] { auto mem = pyopencl_call_guarded(clCreateBuffer, ctx, flags, size, hostbuf); *buffer = new_buffer(mem); }); }
lexeme *get_lex(lexer_info *linfo) { lexeme *lex = NULL; buffer buf; new_buffer(&buf); do { if (linfo->get_next_char) { linfo->get_next_char = 0; get_char(linfo); } switch (linfo->state) { case ST_START: lex = st_start(linfo, &buf); break; case ST_ONE_SYM_LEX: lex = st_one_sym_lex(linfo, &buf); break; case ST_ONE_TWO_SYM_LEX: lex = st_one_two_sym_lex(linfo, &buf); break; case ST_BACKSLASH: lex = st_backslash(linfo, &buf); break; case ST_BACKSLASH_IN_QUOTES: lex = st_backslash_in_quotes(linfo, &buf); break; case ST_IN_QUOTES: lex = st_in_quotes(linfo, &buf); break; case ST_WORD: lex = st_word(linfo, &buf); break; case ST_ERROR: lex = st_error(linfo, &buf); break; case ST_EOLN_EOF: lex = st_eoln_eof(linfo, &buf); break; } /* switch */ } while (lex == NULL); return lex; }
/** * @brief ensures that the stream has requested size * * @param size requested size */ void MemoryStreamOutput::EnsureSize(ULong size) { // ensure that m_buffer has length at least 'size' if (size > m_actual_size) { m_actual_size = std::max<ULong>(size, UInt(1.5* m_actual_size)); boost::shared_array<Byte> new_buffer(new Byte[static_cast<unsigned int>(m_actual_size)]); memcpy(new_buffer.get(), m_buffer.get(), static_cast<unsigned int>(m_bytes_written)); m_buffer.swap(new_buffer); } }
static Buffer *extractText(htmlDocPtr doc) { char * term = "\0"; Buffer *buf = new_buffer(256); xmlTextReaderPtr reader = xmlReaderWalker(doc); while(xmlTextReaderRead(reader)){ processNode(reader, buf); } xmlFreeTextReader(reader); buffer_in(buf, term, 1); return buf; }
/* * handle accept event */ int accept_handle( conn_t *pconn ) { int fd; struct sockaddr_in addr; socklen_t addrlen; conn_t *client_conn; bzero( &addr, sizeof(struct sockaddr_in) ); addrlen = sizeof(struct sockaddr_in); fd = accept( pconn->fd, (struct sockaddr*)&addr, &addrlen ); epoll_mod_connection( pconn, EPOLLIN ); //重新把监听套接子添加到epoll中 if ( fd < 0 ) { log_message( LOG_WARNING, "accept error:%s.", strerror(errno) ); return -1; //惊群现象 } client_conn = get_conns_slot(); if ( NULL == client_conn ) { log_message( LOG_WARNING, "get_conns_slot NULL." ); return -2; } client_conn->fd = fd; client_conn->read_handle = read_client_handle; client_conn->write_handle = write_client_handle; client_conn->read_buffer = new_buffer(); //这里有内存泄漏 client_conn->write_buffer = new_buffer(); //这里有内存泄漏 client_conn->server_conn = NULL; client_conn->addr = addr; client_conn->read_closed = client_conn->write_closed = 0; client_conn->type = C_CLIENT; char *ip = NULL; ip = inet_ntoa( addr.sin_addr ); log_message( LOG_CONN, "create a new conn[fd:%d] client[%s:%d]--proxy.", client_conn->fd, ip, ntohs(addr.sin_port) ); epoll_add_connection( client_conn, EPOLLIN ); return TRUE; }
static char * build_auth_header(char * access_id, char * signature) { Buffer *auth_header = new_buffer(64); buffer_in(auth_header, "Authorization: AuthHMAC ", strlen("Authorization: AuthHMAC ")); buffer_in(auth_header, access_id, strlen(access_id)); buffer_in(auth_header, ":", 1); buffer_in(auth_header, signature, strlen(signature)); buffer_in(auth_header, "\0", 1); char * return_header = auth_header->buf; free(auth_header); return return_header; }