Memory Functions
v3MemAllocSyntax#include "cosmmem.h" void * v3MemAlloc( u64 bytes, u32 secure ); DescriptionAllocate a block of zeroed memory. Will allocate 16*floor((bytes+15)/16)) bytes, i.e. blocks of 16 bytes, and only on 16 byte boundaries. If secure is V3_MEM_NORMAL no special requirements are needed. If secure is V3_MEM_SECURE the program will expect the following:
If the secure flag cannot be enforced, v3MemWarning will be called and the function will return NULL. The user will then be warned any time that the system is insecure and the program attempts to do secure operations. Return ValuesAn aligned pointer to the RAM block, or NULL on failure. ErrorsNone. Exampleu64 size; void * mem; _V3_SET64( size, 00000000, 00000400 ); mem = v3MemAlloc( size, V3_MEM_NORMAL ); if ( mem == NULL ) { /* Error */ return( V3_FAIL ); } v3MemReallocSyntax#include "cosmmem.h" void * v3MemRealloc( void * memory, u64 bytes, u32 secure ); DescriptionAdjust the size of the ram block memory to bytes length, copying any old data. If memory is NULL, this function acts like v3MemAlloc. The secure flag will be treated as it is in v3MemAlloc. If bytes is zero, then free the memory, and return NULL. Any expanded memory space will have unknown values. Return ValuesA pointer to the new memory space on success, or NULL if unable to change the size - leaving the old memory unmodified. ErrorsNone. Exampleu64 size; void * mem; void * small; _V3_SET64( size, 00000000, 00000400 ); mem = v3MemAlloc( size, V3_MEM_NORMAL ); if ( mem == NULL ) { /* Error */ return( V3_FAIL ); } _V3_SET64( size, 00000000, 00000200 ); small = v3MemRealloc( mem, size, 0 ); if ( small == NULL ) { /* Error */ return( V3_FAIL ); } v3MemCopySyntax#include "cosmmem.h" s32 v3MemCopy( void * dest, const void * src, u64 length ); DescriptionCopy length bytes of memory from src to dest. Return ValuesV3_PASS on success, or V3_FAIL on failure. ErrorsNone. Examplevoid * old; void * new; u64 size; _V3_SET64( size, 00000000, 00000400 ); old = v3MemAlloc( size, V3_MEM_NORMAL ); if ( old == NULL ) { /* Error */ return( V3_FAIL ); } new = v3MemAlloc( size, V3_MEM_NORMAL ); if ( new == NULL ) { /* Error */ return( V3_FAIL ); } if ( v3MemCopy( new, mem, size ) == V3_FAIL ) { /* Error */ return( V3_FAIL ); } v3MemSetSyntax#include "cosmmem.h" s32 v3MemSet( void * memory, u64 length, u8 value ); DescriptionSet length bytes of memory to value. Return ValuesV3_PASS on success, or V3_FAIL on failure. ErrorsNone. Exampleu64 size; void * mem; void * small; _V3_SET64( size, 00000000, 00000400 ); mem = v3MemAlloc( size, V3_MEM_NORMAL ); if ( mem == NULL ) { /* Error */ return( V3_FAIL ); } if ( v3MemSet( mem, size, 0xFF ) == V3_FAIL ) { /* Error */ return( V3_FAIL ); } v3MemCmpSyntax#include "cosmmem.h" s32 v3MemCmp( const void * blockA, const void * blockB, u64 max_bytes ); DescriptionCompare the memory blocks blockA and blockB, up to max_bytes will be compared. Return Values0 if the blocks are identical. If they are different then a positive or negative based on blockA[x] - blockB[x]. Due to endian issues the +/- result may or may not be relivant. If any parameter is NULL/0 then it returns -1 unless blockA == blockB. ErrorsNone. Exampleu64 size; void memA[256]; void memB[256]; s32 diff; _V3_SET64( size, 00000000, 00000100 ); if ( ( diff = v3MemCmp( memA, memB, size ) ) == 0 ) { /* memory blocks are the same */ } v3MemOffsetSyntax#include "cosmmem.h" void * v3MemOffset( const void * memory, u64 offset ); DescriptionCalculates the address of memory + offset. Return ValuesA pointer to the data location, or NULL if address space is exceeded. ErrorsNone. Exampleu64 offset; void * memory; void * ptr; _V3_SET64( offset, 00000000, 00000100 ); ptr = v3MemOffset( memory, offset ); /* ptr is a pointer to 256 bytes past memory */ v3MemFreeSyntax#include "cosmmem.h" void v3MemFree( void * memory ); DescriptionFree the requested memory block memory. memory may safely be NULL. Return ValuesNone. ErrorsNone. Exampleu64 size; void * mem; _V3_SET64( size, 00000000, 00000400 ); mem = v3MemAlloc( size, V3_MEM_NORMAL ); if ( mem == NULL ) { /* Error */ return( V3_FAIL ); } v3MemFree( mem ); v3MemSystemSyntax#include "cosmmem.h" s32 v3MemSystem( u64 * amount ); DescriptionSet amount to the number of bytes of physical RAM in the system. Return ValuesV3_PASS on success, or V3_FAIL on failure. ErrorsPossible causes of an failure:
Exampleu64 memory_size; void * mem; if ( v3MemSystem( &memory_size ) != V3_PASS ) { /* couldn't get memory total */ } v3MemWarningSyntax#include "cosmmem.h" void * v3MemWarning( void ); DescriptionForce display of a warning message to the user, when secure memory is requested but not available. Return ValuesNone. ErrorsNone. Examplev3MemWarning(); v3MemDumpLeaksSyntax#include "cosmmem.h" s32 v3MemDumpLeaks( const ascii * filename ); DescriptionWrite out a list of memory that has not been v3MemFeee'd yet to filename. Due to the overhead of this function you must define MEM_LEAK_FIND when compiling the cosm libraries and your code, otherwise it will not exist. Return ValuesV3_PASS on success, or V3_FAIL on failure. ErrorsAny errors will be related to opening the file.. Examplev3MemDumpLeaks( (ascii *) "leaks.txt" );
© Copyright Mithral Communications & Design Inc.
1995-2003.
All rights reserved.
Mithral® and Cosm® are trademarks of
Mithral Communications & Design Inc.
|