Memory Buffer Functions
CosmBufferInitSyntax#include "cosm/buffer.h" s32 CosmBufferInit( cosm_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 ValuesCOSM_PASS on success, or an error code on failure. Errors
Examplecosm_BUFFER * buf; u64 size, grow; if ( ( buf = CosmMemAlloc( sizeof( cosm_BUFFER ) ) ) == NULL ) { /* Error in memory allocation. */ return COSM_FAIL; } size = 0x0000000000000400LL; grow = 0x0000000000000200LL; if ( CosmBufferInit( buf, size, COSM_BUFFER_MODE_QUEUE, grow, NULL, 0 ) == COSM_FAIL ) { /* Failed */ return COSM_FAIL; } CosmBufferLengthSyntax#include "cosm/buffer.h" u64 CosmBufferLength( cosm_BUFFER * buffer ); DescriptionGives the length of the data currently in buffer. Return ValuesThe length of the data currently in buffer. ErrorsNone. Examplecosm_BUFFER * buf; u64 size, grow; u64 length; if ( ( buf = CosmMemAlloc( sizeof( cosm_BUFFER ) ) ) == NULL ) { /* Error in memory allocation. */ return COSM_FAIL; } size = 0x0000000000000400LL; grow = 0x0000000000000200LL; if ( CosmBufferInit( buf, size, COSM_BUFFER_MODE_QUEUE, grow, NULL, 0 ) == COSM_FAIL ) { /* Failed */ return COSM_FAIL; } length = CosmBufferLength( buf ); /* Should be zero. */ CosmBufferClearSyntax#include "cosm/buffer.h" s32 CosmBufferClear( cosm_BUFFER * buffer ); DescriptionSets the length of the buffer to 0 without freeing it. Return ValuesCOSM_PASS on success, or an error code on failure. Errors
Examplecosm_BUFFER * buf; u64 length; /* ... */ CosmBufferClear( buf ); length = CosmBufferLength( buf ); /* Should be zero. */ CosmBufferPutSyntax#include "cosm/buffer.h" s32 CosmBufferPut( cosm_BUFFER * buffer, const void * data, u64 length ); DescriptionInserts length bytes of data into buffer. Return ValuesCOSM_PASS on success, or an error code on failure. Errors
Examplecosm_BUFFER * buf; u64 size, grow; u32 data, counter, data; if ( ( buf = CosmMemAlloc( sizeof( cosm_BUFFER ) ) ) == NULL ) { /* Error in memory allocation. */ return COSM_FAIL; } size = 0x0000000000000400LL; grow = 0x0000000000000200LL; if ( CosmBufferInit( buf, size, COSM_BUFFER_MODE_QUEUE, grow, NULL, 0 ) == COSM_FAIL ) { /* Failed */ return COSM_FAIL; } /* Lets insert some u32's into the buffer. They are 4 bytes each. */ for ( counter = 0; counter < 20; counter++ ) { data = counter * counter; if ( CosmBufferPut( buf, &data, sizeof( u32 ) ) != COSM_PASS ) { /* Error. */ return COSM_FAIL; } } CosmBufferGetSyntax#include "cosm/buffer.h" u64 CosmBufferGet( void * data, u64 length, cosm_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. Examplecosm_BUFFER * buf; u64 size, grow; u32 data, counter, data; if ( ( buf = CosmMemAlloc( sizeof( cosm_BUFFER ) ) ) == NULL ) { /* Error in memory allocation. */ return COSM_FAIL; } size = 0x0000000000000400LL; grow = 0x0000000000000200LL; if ( CosmBufferInit( buf, size, COSM_BUFFER_MODE_STACK, grow, NULL, 0 ) == COSM_FAIL ) { /* Failed */ return COSM_FAIL; } /* Lets insert some u32's into the buffer. They are four bytes each. */ for ( counter = 0; counter < 20; counter++ ) { data = counter * counter; if ( CosmBufferPut( buf, &data, sizeof(u32) ) != COSM_PASS ) { /* Error. */ return COSM_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 ( CosmBufferGet( &data, sizeof( u32 ), buf ) != sizeof( u32 ) ) { /* An error.*/ return COSM_FAIL; } /* do something with data. */ } CosmBufferUngetSyntax#include "cosm/buffer.h" s32 CosmBufferUnget( cosm_BUFFER * buffer, const void * data, u64 length ); DescriptionInserts length bytes of data into buffer. The difference from CosmBufferPut is that the data is placed so it will be the first data retrieved by a CosmBufferGet. Note: be careful when using this function in a multithreaded environment. Return ValuesCOSM_PASS on success, or an error code on failure. Errors
Examplecosm_BUFFER * buf; u64 size, grow; u32 data, counter, data; if ( ( buf = CosmMemAlloc( sizeof( cosm_BUFFER ) ) ) == NULL ) { /* Error in memory allocation. */ return COSM_FAIL; } size = 0x0000000000000400LL; grow = 0x0000000000000200LL; if ( CosmBufferInit( buf, size, COSM_BUFFER_MODE_STACK, grow, NULL, 0 ) == COSM_FAIL ) { /* Failed */ return COSM_FAIL; } /* Lets insert some u32's into the buffer. They are four bytes each. */ for ( counter = 0; counter < 20; counter++ ) { data = counter * counter; if ( CosmBufferPut( buf, &data, sizeof( u32 ) ) != COSM_PASS ) { /* Error. */ return COSM_FAIL; } } /* Lets retrieve the data. */ for ( counter = 0; counter < 20; counter ++ ) { if ( CosmBufferGet( &data, sizeof( u32 ), buf ) != sizeof( u32 ) ) { /* An error.*/ return COSM_FAIL; } /* We keep pushing the data back on the stack, this means we will be getting the same data each time */ if ( CosmBufferUnget( buf, &data, sizeof( u32 ) ) != COSM_PASS ) { /* An error. */ return COSM_FAIL; } } CosmBufferFreeSyntax#include "cosm/buffer.h" void CosmBufferFree( cosm_BUFFER * buffer); DescriptionFree all data in the buffer and return it to an uninitialized state. Return ValuesNone. ErrorsNone. Examplecosm_BUFFER * buf; /* Use of the buffer...*/ CosmBufferFree( buf );
© Copyright Mithral Communications & Design Inc.
1995-2024.
All rights reserved.
Mithral® and Cosm® are trademarks of
Mithral Communications & Design Inc.
|