Memory Buffer Functionsv3BufferCreateSyntax#include "buffer.h" s32 v3BufferCreate( v3_BUFFER * buffer, u64 size, u32 mode, u64 grow, const void * data, u64 length ); DescriptionCreate a new buffer in buffer, initially assigning size bytes to its memory buffer. Once the buffer fills up it will allocate more memory in chunks of grow bytes. If grow is zero the buffer will never grow and may become full. If data is not NULL then length bytes will be copied from data and into the new buffer. mode determines which kind of buffer should be created. If you intend to access a buffer from more then one thread, be sure to wrap any calls in a mutex. Modes
Return ValuesV3_PASS on success, or an error code on failure. Errors
Examplev3_BUFFER * buf; u64 size, grow; if ( ( buf = v3MemAlloc( v3u64u32( sizeof( v3_BUFFER ) ), V3_MEM_NORMAL ) ) == NULL ) { /* Error in memory allocation. */ return( V3_FAIL ); } _V3_SET64( size, 00000000, 00000400 ); _V3_SET64( grow, 00000000, 00000200 ); if ( v3BufferCreate( buf, size, V3_BUFFER_MODE_QUEUE, grow, NULL, v3u64u32( 0 ) ) == V3_FAIL ) { /* Failed */ return( V3_FAIL ); } v3BufferLengthSyntax#include "buffer.h" u64 v3BufferLength( v3_BUFFER * buffer ); DescriptionGives the length of the data currently in buffer. Return ValuesThe length of the data currently in buffer. ErrorsNone. Examplev3_BUFFER * buf; u64 size, grow; u64 length; if ( ( buf = v3MemAlloc( v3u64u32( sizeof( v3_BUFFER ) ), V3_MEM_NORMAL ) ) == NULL ) { /* Error in memory allocation. */ return( V3_FAIL ); } _V3_SET64( size, 00000000, 00000400 ); _V3_SET64( grow, 00000000, 00000200 ); if ( v3BufferCreate( buf, size, V3_BUFFER_MODE_QUEUE, grow, NULL, v3u64u32( 0 ) ) == V3_FAIL ) { /* Failed */ return( V3_FAIL ); } length = v3BufferLength( buf ); /* Should be zero. */ v3BufferClearSyntax#include "buffer.h" s32 v3BufferClear( v3_BUFFER * buffer ); DescriptionSets the length of the buffer to 0 without freeing it. Return ValuesV3_PASS on success, or an error code on failure. Errors
Examplev3_BUFFER * buf; u64 length; /* ... */ v3BufferClear( buf ); length = v3BufferLength( buf ); /* Should be zero. */ v3BufferPutSyntax#include "buffer.h" s32 v3BufferPut( v3_BUFFER * buffer, const void * data, u64 length ); DescriptionInserts length bytes of data into buffer. Return ValuesV3_PASS on success, or an error code on failure. Errors
Examplev3_BUFFER * buf; u64 size, grow; u32 data, counter, data; if ( ( buf = v3MemAlloc( v3u64u32( sizeof( v3_BUFFER ) ), V3_MEM_NORMAL ) ) == NULL ) { /* Error in memory allocation. */ return( V3_FAIL ); } _V3_SET64( size, 00000000, 00000400 ); _V3_SET64( grow, 00000000, 00000200 ); if ( v3BufferCreate( buf, size, V3_BUFFER_MODE_QUEUE, grow, NULL, v3u64u32( 0 ) ) == V3_FAIL ) { /* Failed */ return( V3_FAIL ); } /* Lets insert some u32's into the buffer. They are 4 bytes each. */ for ( counter = 0; counter < 20; counter++ ) { data = counter * counter; if ( v3BufferPut( buf, &data, sizeof( u32 ) ) != V3_PASS ) { /* Error. */ return( V3_FAIL ); } } v3BufferGetSyntax#include "buffer.h" u64 v3BufferGet( void * data, u64 length, v3_BUFFER * buffer ); DescriptionGets as many bytes as possible, up to a maximum of length from buffer placing them into data. Note that while a stack will return your data-items in reverse order it will consider the size of a data-item to be length bytes and keep the internal byte-order. Return Valueslength on success, otherwise bytes read out of the buffer. ErrorsNone. Examplev3_BUFFER * buf; u64 size, grow; u32 data, counter, data; if ( ( buf = v3MemAlloc( v3u64u32( sizeof( v3_BUFFER ) ), V3_MEM_NORMAL ) ) == NULL ) { /* Error in memory allocation. */ return( V3_FAIL ); } _V3_SET64( size, 00000000, 00000400 ); _V3_SET64( grow, 00000000, 00000200 ); if ( v3BufferCreate( buf, size, V3_BUFFER_MODE_STACK, grow, NULL, v3u64u32( 0 ) ) == V3_FAIL ) { /* Failed */ return( V3_FAIL ); } /* Lets insert some u32's into the buffer. They are four bytes each. */ for ( counter = 0; counter < 20; counter++ ) { data = counter * counter; if ( v3BufferPut( buf, &data, sizeof(u32) ) != V3_PASS ) { /* Error. */ return( V3_FAIL ); } } /* Lets retrieve the data. We should be getting the squares of the numbers from 0 to 19 in reverse order. */ for ( counter = 0; counter < 20; counter ++ ) { if ( v3BufferGet( &data, sizeof( u32 ), buf ) != sizeof( u32 ) ) { /* An error.*/ return( V3_FAIL ); } /* do something with data. */ } v3BufferUngetSyntax#include "buffer.h" s32 v3BufferUnget( v3_BUFFER * buffer, const void * data, u64 length ); DescriptionInserts length bytes of data into buffer. The difference from v3BufferPut is that the data is placed so it will be the first data retrieved by a v3BufferGet. Note: be careful when using this function in a multithreaded environment. Return ValuesV3_PASS on success, or an error code on failure. Errors
Examplev3_BUFFER * buf; u64 size, grow; u32 data, counter, data; if ( ( buf = v3MemAlloc( v3u64u32( sizeof( v3_BUFFER ) ), V3_MEM_NORMAL ) ) == NULL ) { /* Error in memory allocation. */ return( V3_FAIL ); } _V3_SET64( size, 00000000, 00000400 ); _V3_SET64( grow, 00000000, 00000200 ); if ( v3BufferCreate( buf, size, V3_BUFFER_MODE_STACK, grow, NULL, v3u64u32( 0 ) ) == V3_FAIL ) { /* Failed */ return( V3_FAIL ); } /* Lets insert some u32's into the buffer. They are four bytes each. */ for ( counter = 0; counter < 20; counter++ ) { data = counter * counter; if ( v3BufferPut( buf, &data, sizeof( u32 ) ) != V3_PASS ) { /* Error. */ return( V3_FAIL ); } } /* Lets retrieve the data. */ for ( counter = 0; counter < 20; counter ++ ) { if ( v3BufferGet( &data, sizeof( u32 ), buf ) != sizeof( u32 ) ) { /* An error.*/ return( V3_FAIL ); } /* We keep pushing the data back on the stack, this means we will be getting the same data each time */ if ( v3BufferUnget( buf, &data, sizeof( u32 ) ) != V3_PASS ) { /* An error. */ return( V3_FAIL ); } } v3BufferFreeSyntax#include "buffer.h" void v3BufferFree( v3_BUFFER * buffer); DescriptionFree all data in the buffer and return it to an uninitialized state. Return ValuesNone. ErrorsNone. Examplev3_BUFFER * buf; /* Use of the buffer...*/ v3BufferFree( buf );
© Copyright Mithral Communications & Design Inc.
1995-2003.
All rights reserved.
Mithral® and Cosm® are trademarks of
Mithral Communications & Design Inc.
|