[futurebasic] Re: FB^3 Memory and Temp file questions

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2000 : Group Archive : Group : All Groups

From: Herbie Glunder <H.Gluender@...>
Date: Mon, 30 Oct 2000 13:08:51 +0200
Dear Robert Covington,

you wrote:

* Dynamic memory allocation for a GWorld for instance.
...

Here is a FBII Function from my UpDater Program that allocates Temporary
Memory for Copying of Files: (Take what you need...)

******************************************************
'Constants:
_minBufferSize = &H0200                 ' must be 512 bytes
_maxBufferSize =                       ' enter the megs you need
_memoryCushion = &H2000              ' you need some overhead

LOCAL MODE
CLEAR LOCAL FN GetCopyBufferAndCopy(srceFSS&,destFSS&)
  '———————————————————————————'
' largest continuous block outside heap
freeBytes& = FN TempMaxMem(dummy&)
' enough room in RAM? (not in heap)
  LONG IF freeBytes&>=(_minBufferSize<<1)
' less memory than desired?
    LONG IF freeBytes&<_maxBufferSize
' integer divide by 512 times 512 bytes
      bufferSize& = (freeBytes&>>9)*_minBufferSize
    XELSE
' more memory than desired
' never allocate more than maximum
      bufferSize& = _maxBufferSize
    END IF
' allocate memory block outside the heap
    tempHandle& = FN TempNewHandle(bufferSize&,err%)
' valid handle?; error: 0,-108
    LONG IF tempHandle& AND err%=_noErr
' lock handle and dereference it (next line)
      dc% = FN HLOCK(tempHandle&)
' Here comes the call for my Copy Routine
      err%=FN CopyFile(srceFSS&,destFSS&,[tempHandle&],bufferSize&)
' release memory block
      dc% = FN DISPOSHANDLE(tempHandle&)
' unable to allocate temporary RAM buffer
    XELSE
' create heap buffer; reset allocation error
      createHeapBuffer% = _true: err% = _noErr
    END IF
' not enough free memory outside heap
  XELSE
' create heap buffer
    createHeapBuffer% = _true
  END IF
' create heap buffer?
  LONG IF createHeapBuffer%
' largest heap block after compaction - cushion
    freeBytes& = FN MAXBLOCK - _memoryCushion
' enough room in heap?
    LONG IF freeBytes&>=_minBufferSize<<1
' integer divide by 512 times  512 bytes
      bufferSize&=(FN COMPACTMEM(freeBytes&)>>9)*_minBufferSize
' allocate max buffer (multiples of 512 bytes)
      bufferPtr& = FN NEWPTR(bufferSize&)
' not enough room in heap
    XELSE
' try the minimum approach
      bufferPtr& = _nil
    END IF
' empty pointer block?
    LONG IF bufferPtr&=_nil
' minimum heap buffer (512 bytes)
      bufferSize& = _minBufferSize
' allocate minimum heap buffer (512 bytes)
      bufferPtr& = FN NEWPTR(bufferSize&)
    END IF
' minimum heap buffer available
    LONG IF bufferPtr&<>_nil OR bufferSize&=0
' Here comes the call for my Copy Routine
      err%=FN CopyFile(srceFSS&,destFSS&,bufferPtr&,bufferSize&)
' release memory
      dc% = FN DISPOSPTR(bufferPtr&)
' no minimum heap buffer available
    XELSE
' set error (-108)
      err% = _memFullErr
    END IF
  END IF
END FN = err%
******************************************************

Furthermore you wrote:

* Item 2

> Anyone know how to find the Temporary Items folder, and  create and
then
> delete a file there? I am in the need of writing temporary PICT files,
and
> then deleting them when done. I want to be sure I am fine across OS
> versions and international too (folder finding/using). Creating just
> involves a Temp name and the vRefnum I imagine, and standard PICT
write.

In the Files volume of IM (1995) there is a nice Pascal example of how
to save a file and keep a copy in the Temp Folder. You will find all you
need. Be aware that there are some changes with Folder Manager for OS8
and newer (see Apple's Webpage).

Best,

Herbie
***************************************
H.Glu@...