void buffer_stream_initialize( stream_buffer_t* stream, void* buffer, unsigned int mode, uint64_t size, uint64_t capacity, bool adopt, bool grow ) { memset( stream, 0, sizeof( stream_buffer_t ) ); stream_initialize( (stream_t*)stream, system_byteorder() ); if( !FOUNDATION_VALIDATE_MSG( adopt || !grow, "Cannot grow buffer streams that are not adopted" ) ) grow = false; if( !buffer ) { size = 0; capacity = 0; } if( size > capacity ) size = capacity; stream->type = STREAMTYPE_MEMORY; stream->path = string_format( "buffer://0x%" PRIfixPTR, stream ); stream->mode = mode & ( STREAM_OUT | STREAM_IN | STREAM_BINARY ); stream->buffer = buffer; stream->size = size; stream->capacity = capacity; stream->own = adopt; stream->grow = ( adopt && grow ); if( mode & STREAM_TRUNCATE ) stream->size = 0; if( mode & STREAM_ATEND ) stream->current = stream->size; stream->vtable = &_buffer_stream_vtable; }
void pipe_initialize( stream_pipe_t* pipestream ) { stream_t* stream = (stream_t*)pipestream; memset( stream, 0, sizeof( stream_pipe_t ) ); stream_initialize( stream, system_byteorder() ); pipestream->type = STREAMTYPE_PIPE; pipestream->path = string_format( "pipe://0x" PRIfixPTR, pipestream ); pipestream->mode = STREAM_OUT | STREAM_IN | STREAM_BINARY; pipestream->sequential = true; #if FOUNDATION_PLATFORM_WINDOWS { //Inheritable by default so process can use for stdstreams SECURITY_ATTRIBUTES security_attribs = {0}; security_attribs.nLength = sizeof( SECURITY_ATTRIBUTES ); security_attribs.bInheritHandle = TRUE; security_attribs.lpSecurityDescriptor = 0; if( !CreatePipe( &pipestream->handle_read, &pipestream->handle_write, &security_attribs, 0 ) ) log_warnf( 0, WARNING_SYSTEM_CALL_FAIL, "Unable to create unnamed pipe: %s", system_error_message( GetLastError() ) ); } #elif FOUNDATION_PLATFORM_POSIX || FOUNDATION_PLATFORM_PNACL int fds[2] = { 0, 0 }; if( pipe( fds ) < 0 ) log_warnf( 0, WARNING_SYSTEM_CALL_FAIL, "Unable to create unnamed pipe: %s", system_error_message( 0 ) ); pipestream->fd_read = fds[0]; pipestream->fd_write = fds[1]; #endif pipestream->vtable = &_pipe_stream_vtable; }
void buffer_stream_initialize(stream_buffer_t* stream, void* buffer, unsigned int mode, size_t size, size_t capacity, bool adopt, bool grow) { memset(stream, 0, sizeof(stream_buffer_t)); stream_initialize((stream_t*)stream, system_byteorder()); if (!adopt && grow) { log_warn(0, WARNING_INVALID_VALUE, STRING_CONST("Cannot grow buffer streams that are not adopted")); grow = false; } if (!buffer) { size = 0; capacity = 0; } if (size > capacity) size = capacity; stream->type = STREAMTYPE_MEMORY; stream->path = string_allocate_format(STRING_CONST("buffer://0x%" PRIfixPTR), (uintptr_t)stream); stream->mode = mode & (STREAM_OUT | STREAM_IN | STREAM_BINARY); stream->buffer = buffer; stream->size = size; stream->capacity = capacity; stream->own = adopt; stream->grow = (adopt && grow); stream->lastmod = time_current(); if ((mode & STREAM_OUT) && (mode & STREAM_TRUNCATE)) stream->size = 0; if (mode & STREAM_ATEND) stream->current = stream->size; stream->vtable = &_buffer_stream_vtable; }
void _stream_initialize( stream_t* stream, byteorder_t order ) { stream->byteorder = order; stream->sequential = 0; stream->reliable = 1; stream->inorder = 1; stream->swap = ( stream->byteorder != system_byteorder() ) ? 1 : 0; stream->mode = STREAM_BINARY; stream->path = 0; }
void stream_initialize(stream_t* stream, byteorder_t order) { stream->byteorder = (unsigned int)order; stream->sequential = 0; stream->reliable = 1; stream->inorder = 1; stream->swap = (stream->byteorder != system_byteorder()) ? 1 : 0; stream->mode = STREAM_BINARY; stream->path = (string_t) { 0, 0 }; }
stream_t* stream_open_stdin( void ) { stream_std_t* stream = memory_allocate_zero_context( MEMORYCONTEXT_STREAM, sizeof( stream_std_t ), 8, MEMORY_PERSISTENT ); _stream_initialize( (stream_t*)stream, system_byteorder() ); stream->sequential = 1; stream->mode = STREAM_IN; stream->type = STREAMTYPE_STDSTREAM; stream->vtable = &_stream_stdin_vtable; stream->path = string_clone( "stdin://" ); stream->std = stdin; return (stream_t*)stream; }
void ringbuffer_stream_initialize( stream_ringbuffer_t* stream, unsigned int buffer_size, uint64_t total_size ) { memset( stream, 0, sizeof( stream_ringbuffer_t ) ); stream_initialize( (stream_t*)stream, system_byteorder() ); stream->type = STREAMTYPE_RINGBUFFER; stream->sequential = 1; stream->path = string_format( "ringbuffer://0x%" PRIfixPTR, stream ); stream->mode = STREAM_OUT | STREAM_IN | STREAM_BINARY; ringbuffer_initialize( RINGBUFFER_FROM_STREAM( stream ), buffer_size ); semaphore_initialize( &stream->signal_read, 0 ); semaphore_initialize( &stream->signal_write, 0 ); stream->total_size = total_size; stream->vtable = &_ringbuffer_stream_vtable; }
stream_t* pipe_allocate( void ) { stream_pipe_t* pipestream = memory_allocate_zero_context( MEMORYCONTEXT_STREAM, sizeof( stream_pipe_t ), 0, MEMORY_PERSISTENT ); stream_t* stream = (stream_t*)pipestream; _stream_initialize( stream, system_byteorder() ); pipestream->type = STREAMTYPE_PIPE; pipestream->path = string_format( "pipe://" STRING_FORMAT_POINTER, pipestream ); pipestream->mode = STREAM_OUT | STREAM_IN | STREAM_BINARY; pipestream->sequential = true; #if FOUNDATION_PLATFORM_WINDOWS { //Inheritable by default so process can use for stdstreams SECURITY_ATTRIBUTES security_attribs = {0}; security_attribs.nLength = sizeof( SECURITY_ATTRIBUTES ); security_attribs.bInheritHandle = TRUE; security_attribs.lpSecurityDescriptor = 0; if( !CreatePipe( &pipestream->handle_read, &pipestream->handle_write, &security_attribs, 0 ) ) log_warnf( 0, WARNING_SYSTEM_CALL_FAIL, "Unable to create unnamed pipe: %s", system_error_message( GetLastError() ) ); } #elif FOUNDATION_PLATFORM_POSIX int fds[2] = { 0, 0 }; if( pipe( fds ) < 0 ) log_warnf( 0, WARNING_SYSTEM_CALL_FAIL, "Unable to create unnamed pipe: %s", system_error_message( 0 ) ); else { pipestream->fd_read = fds[0]; pipestream->fd_write = fds[1]; } #endif pipestream->vtable = &_pipe_stream_vtable; return stream; }
stream_t* ringbuffer_stream_allocate( unsigned int buffer_size, uint64_t total_size ) { ringbuffer_stream_t* bufferstream = memory_allocate_zero( sizeof( ringbuffer_stream_t ) + buffer_size, 0, MEMORY_PERSISTENT ); stream_t* stream = (stream_t*)bufferstream; _stream_initialize( stream, system_byteorder() ); stream->type = STREAMTYPE_RINGBUFFER; stream->sequential = 1; stream->path = string_format( "ringbuffer://" STRING_FORMAT_POINTER, stream ); stream->mode = STREAM_OUT | STREAM_IN | STREAM_BINARY; semaphore_initialize( &bufferstream->signal_read, 0 ); semaphore_initialize( &bufferstream->signal_write, 0 ); bufferstream->pending_read = 0; bufferstream->pending_write = 0; bufferstream->total_size = total_size; bufferstream->buffer_size = buffer_size; stream->vtable = &_ringbuffer_stream_vtable; return stream; }
void stream_set_byteorder( stream_t* stream, byteorder_t byteorder ) { FOUNDATION_ASSERT( stream ); stream->byteorder = byteorder; stream->swap = ( byteorder != system_byteorder() ) ? 1 : 0; }
void stream_set_byteorder(stream_t* stream, byteorder_t byteorder) { stream->byteorder = (unsigned int)byteorder; stream->swap = (byteorder != system_byteorder()) ? 1 : 0; }