Пример #1
0
void *RcMemMalloc( size_t size )
/******************************/
{
    void *  ptr;

#ifdef RC_USE_TRMEM
    ptr = _trmem_alloc( size, _trmem_guess_who(), RcMemHandle );
#else
    ptr = RCMemLayer1Malloc( size );
#endif

    if (ptr == NULL) {
        RcFatalError( ERR_OUT_OF_MEMORY );
    }

    return( ptr );
}
Пример #2
0
extern void *RCMemLayer1Realloc( void *mem, size_t size )
/*******************************************************/
{
    char             *blockptr;
    BigMemList       *newbigptr;
    void             *newnode;
    HeapId           *heapid;
    HeapId           *idptr;
    BigMemList       *reallocptr;
    unsigned long     reallocsize;
    unsigned short    headersize;
#ifdef RCMEM_DEBUG
    DebugMemInfo     *debugmem;
#endif

    if( mem == NULL ) {     // emulate realloc() behaviour
        return( RCMemLayer1Malloc( size ) );
    }

    blockptr = (char *)mem - sizeof( HeapId );
    heapid = (HeapId *)blockptr;
    if( heapid->id == BIGLIST_ID ) {
        reallocptr = (BigMemList *)( (char *)heapid - sizeof( BigMemList ) );
        if( reallocptr->size < size ) {
            FreeBigListNode( mem, FALSE );
            headersize = sizeof( BigMemList ) + sizeof( HeapId );
            reallocsize = size + headersize;
#ifdef RCMEM_DEBUG
            reallocsize++;
#endif
            newnode = realloc( reallocptr, reallocsize );
            if( newnode == NULL ) {
                RcFatalError( ERR_OUT_OF_MEMORY );
            }
            newbigptr = (BigMemList *)newnode;
            newbigptr->next = BigList;
            newbigptr->size = size;
            idptr = (HeapId *)( (char *)newbigptr + sizeof( BigMemList ) );
            idptr->id = BIGLIST_ID;
            BigList = newbigptr;
#ifdef RCMEM_DEBUG
            *((unsigned char *)newbigptr + headersize + size ) = RCMEM_ENDBYTE;
#endif
            return( (char *)newbigptr + headersize );
        }
    } else if( heapid->id < NUM_HEAPS ) {
        if( size + sizeof( HeapId ) > HeapSizes[ heapid->id ] ) {
            newnode = RCMemLayer1Malloc( size );
            memcpy( newnode, mem, HeapSizes[ heapid->id ] - sizeof( HeapId ) );
            RCMemLayer1Free( mem );
            return( newnode );
        }
#ifdef RCMEM_DEBUG
        else {
            debugmem = (DebugMemInfo *)((char *)blockptr -
                                        sizeof( DebugMemInfo ) );
            debugmem->size = size + sizeof( HeapId );
            *((unsigned char *)mem + size) = RCMEM_ENDBYTE;
        }
#endif
    } else {
        RcFatalError( ERR_INTERNAL, INTERR_MEM_REALLOC_FAILED );
    }
    return( mem );
}