Memory Functions
CosmMemAllocSyntax#include "cosm/os_mem.h" void * CosmMemAlloc( 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 COSM_MEM_NORMAL no special requirements are needed. If secure is COSM_MEM_SECURE the program will expect the following:
If the secure flag cannot be enforced, CosmMemWarning 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; size = 0x0000000000000400LL; mem = CosmMemAlloc( size ); if ( mem == NULL ) { /* Error */ return COSM_FAIL; } CosmMemReallocSyntax#include "cosm/os_mem.h" void * CosmMemRealloc( 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 CosmMemAlloc. The secure flag will be treated as it is in CosmMemAlloc. 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; size = 0x0000000000000400LL; mem = CosmMemAlloc( size ); if ( mem == NULL ) { /* Error */ return COSM_FAIL; } size = 0x0000000000000200LL; small = CosmMemRealloc( mem, size, 0 ); if ( small == NULL ) { /* Error */ return COSM_FAIL; } CosmMemCopySyntax#include "cosm/os_mem.h" s32 CosmMemCopy( void * dest, const void * src, u64 length ); DescriptionCopy length bytes of memory from src to dest. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsNone. Examplevoid * old; void * new; u64 size; size = 0x0000000000000400LL; old = CosmMemAlloc( size ); if ( old == NULL ) { /* Error */ return COSM_FAIL; } new = CosmMemAlloc( size ); if ( new == NULL ) { /* Error */ return COSM_FAIL; } if ( CosmMemCopy( new, mem, size ) == COSM_FAIL ) { /* Error */ return COSM_FAIL; } CosmMemSetSyntax#include "cosm/os_mem.h" s32 CosmMemSet( void * memory, u64 length, u8 value ); DescriptionSet length bytes of memory to value. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPassing a NULL parameter or wrapping around the top of memory. Exampleu64 size; void * mem; void * small; size = 0x0000000000000400LL; mem = CosmMemAlloc( size ); if ( mem == NULL ) { /* Error */ return COSM_FAIL; } if ( CosmMemSet( mem, size, 0xFF ) == COSM_FAIL ) { /* Error */ return COSM_FAIL; } CosmMemCmpSyntax#include "cosm/os_mem.h" s32 CosmMemCmp( 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; size = 0x0000000000000100LL; if ( ( diff = CosmMemCmp( memA, memB, size ) ) == 0 ) { /* memory blocks are the same */ } CosmMemOffsetSyntax#include "cosm/os_mem.h" void * CosmMemOffset( 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; offset = 0x0000000000000100LL; ptr = CosmMemOffset( memory, offset ); /* ptr is a pointer to 256 bytes past memory */ CosmMemFreeSyntax#include "cosm/os_mem.h" void CosmMemFree( void * memory ); DescriptionFree the requested memory block memory. memory may safely be NULL. Return ValuesNone. ErrorsNone. Exampleu64 size; void * mem; size = 0x0000000000000400LL; mem = CosmMemAlloc( size ); if ( mem == NULL ) { /* Error */ return COSM_FAIL; } CosmMemFree( mem ); CosmMemSystemSyntax#include "cosm/os_mem.h" s32 CosmMemSystem( u64 * amount ); DescriptionSet amount to the number of bytes of physical RAM in the system. Return ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsPossible causes of an failure:
Exampleu64 memory_size; void * mem; if ( CosmMemSystem( &memory_size ) != COSM_PASS ) { /* couldn't get memory total */ } CosmMemWarningSyntax#include "cosm/os_mem.h" void * CosmMemWarning( void ); DescriptionForce display of a warning message to the user, when secure memory is requested but not available. Return ValuesNone. ErrorsNone. ExampleCosmMemWarning(); CosmMemDumpLeaksSyntax#include "cosm/os_mem.h" s32 CosmMemDumpLeaks( const ascii * filename ); DescriptionWrite out a list of memory that has not been CosmMemFeee'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 ValuesCOSM_PASS on success, or COSM_FAIL on failure. ErrorsAny errors will be related to opening the file.. ExampleCosmMemDumpLeaks( "leaks.txt" );
© Copyright Mithral Communications & Design Inc.
1995-2024.
All rights reserved.
Mithral® and Cosm® are trademarks of
Mithral Communications & Design Inc.
|