IonicWind Software

IWBasic => General Questions => Topic started by: Hootie on October 26, 2008, 07:09:41 PM

Title: Pointers, Types and SETTYPE
Post by: Hootie on October 26, 2008, 07:09:41 PM
I have the following code that is throwing a compile error:


'=================================================================================
' Enums
'=================================================================================
ENUM PoolType
    POOL_PERMANENT
    POOL_TEMPORARY
ENDENUM


'=================================================================================
' User Defined Types
'=================================================================================
TYPE AllocBlock              ' Allocation within a memory pool
    DEF NextEntry:POINTER    ' Pointer to next AllocBlock
    DEF PrevEntry:POINTER    ' Pointer to previous AllocBlock
    DEF Start:POINTER        ' Pointer to the start of this allocation
    DEF FileId:UINT          ' File Id number of source file that requested this allocation
    DEF SourceLine:UINT      ' Line number within source file that requested this allocation
    DEF Size:UINT            ' Size of this allocation in bytes
ENDTYPE

TYPE FreeBlock               ' Free space within a memory pool
    DEF NextEntry:POINTER    ' Pointer to next FreeBlock
    DEF PrevEntry:POINTER    ' Pointer to previous FreeBlock
    DEF Start:POINTER        ' Pointer to the start of this free space
    DEF Size:UINT            ' Size of this free space in bytes
ENDTYPE

TYPE MemoryPool                 ' Memory pool
    DEF Pool:POINTER            ' Pointer to the pool space
    DEF AllocList:POINTER       ' Pointer to list of allocations
    DEF FreeList:POINTER        ' Pointer to list of free spaces
    DEF PoolId:UINT             ' Pool Id number
    DEF LargestFreeSpace:UINT ' Largest Freespace open in bytes
    DEF TypePool:PoolType       ' Type of pool
ENDTYPE


'=================================================================================
' MemoryPoolDestroy()
'
' This function destroys a memory pool.
'
' Parameters:  pool - Pointer to pool to destroy
'
' Returns: Nothing
'     
'=================================================================================
GLOBAL SUB MemoryPoolDestroy(POINTER pool)

    POINTER thisalloc = NULL
    POINTER nextalloc = NULL
    POINTER thisfree = NULL
    POINTER nextfree = NULL

SETTYPE pool, MemoryPool
SETTYPE thisalloc, AllocBlock
SETTYPE nextalloc, AllocBlock
SETTYPE thisfree, FreeBlock
SETTYPE nextfree, FreeBlock


' Release any left over allocation linked list entries
    IF #pool.AllocList <> NULL
        thisalloc = #pool.AllocList
        nextalloc = #thisalloc.NextEntry

        WHILE nextalloc <> NULL
            free(thisalloc)
            thisalloc = nextalloc
            nextalloc = #thisalloc.NextEntry
        ENDWHILE

        free(thisalloc)
    ENDIF

' Release any left over freespace linked list entries
    if #pool.FreeList <> NULL
        thisfree = #pool.FreeList
        nextfree = #thisfree.NextEntry

        WHILE nextfree <> NULL
            free(thisfree)
            thisfree = nextfree
            nextfree = #thisfree.NextEntry
        ENDWHILE

        free(thisfree)
    ENDIF
       
' Release the pool 
    IF #pool.Pool <> NULL
        free(#pool.Pool)
    ENDIF

    '? free(pool)
    #pool.AllocList = NULL
    #pool.FreeList = NULL
    #pool.Pool = NULL
    #pool.LargestFreeSpace = 0

ENDSUB



The line


        nextalloc = #thisalloc.NextEntry


is throwing a compile error


Compiling...
amMemoryPool.eba
File: D:\Animates\client\memory\amMemoryPool.eba (121) invalid use of dot operator, unknown type


Why am I getting this error when I have the type set for thisalloc?
Title: Re: Pointers, Types and SETTYPE
Post by: Ionic Wind Support Team on October 26, 2008, 07:26:42 PM
SETTYPE is only valid until the pointer is assigned again and is meant for pointer reading convenience, use typecasting and you'll save yourself the hassle.

nextalloc = #<AllocBlock>thisalloc.NextEntry

Thanks,
Paul.
Title: Re: Pointers, Types and SETTYPE
Post by: Hootie on October 26, 2008, 07:37:18 PM
OK, thanks!   ;D
Title: Re: Pointers, Types and SETTYPE
Post by: sapero on October 26, 2008, 07:39:05 PM
Paul was faster.
When you assign a pointer, its type will change to the assigned type.
In the line below #pool.AllocList is defined as "pointer", so your variable thisalloc changes type to "just a pointer"thisalloc = #pool.AllocList
You can use SETTYPE to change type of #pool.AllocList:

SETTYPE #thisalloc.NextEntry, AllocBlock /* please validate types */
SETTYPE #pool.AllocList, AllocBlock
SETTYPE #pool.FreeList, FreeBlock
SETTYPE #thisfree.NextEntry, FreeBlock