BBC BASIC for Windows
General >> General Board >> [BB4WFORTH] Memory Allocation and thoughts...
http://bb4w.conforums.com/index.cgi?board=general&action=display&num=1253837139
[BB4WFORTH] Memory Allocation and thoughts...
Post by afarlie on Sep 25th, 2009, 12:05am
At the end of this message is some sample code for doing memory allocations in BB4W.
It would be appreciated if someone was able to provide
a hint as to how COMPILE, and POSTPONE could be added
as there's some interesting code here
http://www.jwdt.com/~paysan/mini-oof.html
The code for Mini OOF could with translation might also
be of interest to anyone wanting to attempt OO in
BBCBASIC using structures and indirect PROC/FN calls
Alex Farlie
\ Memory Allocation words for BB4WFORTH
\ Allocation words...
\ These are wrappers around the equivlant Win32 API Functions
\ FIXME: ior is the Win32 error code at present - Somone might want to convert these to BB4W type error codes.
Z "KERNEL32.DLL" LoadLibrary
DUP Z" GetLastError" GetProcAddress CONSTANT 'GetLastError'
DUP Z" GlobalAlloc" GetProcAddress CONSTANT 'GlobalAlloc'
DUP Z" GlobalRealloc" GetProcAddress CONSTANT 'GlobalRealloc'
DUP Z" GlobalFree" GetProcAddress CONSTANT 'GlobalFree'
DROP
HEX
: _CHECK_HANDLE (handle -- handle ior)
DUP \Duplicate return value
=0 \ IS Null?
IF
'GetLastError' \ Get the last error value from Windows
SYSCALL
ELSE
0 \ Otherwise the IOR code is 0= Success!
THEN
;
: ALLOCATE (u -- handle ior) \ Allocate fixed memory
40 LITERAL \ GPTR Constant
SWAP \ Reorder for API call
'GlobalAlloc'
SYSCALL \Global alloc call
_CHECK_HANDLE
;
: FREE (a-addr -- ior)
'GlobalFree'
SYSCALL
_CHECK_HANDLE
SWAP DROP \ Don't want handle only want I/O code.
;
: RESIZE (a-addr u -- addr ior)
40 LITERAL \GMEM_ZEROINIT
'GlobalRealloc' \
SYSCALL
_CHECK HANDLE
;
DECIMAL
\ Hide WIN32 calls in favour of 'standard' words
HIDE 'GlobalAlloc'
HIDE 'GlobalRealloc'
HIDE 'GlobalFree'