\ memory 05.1.21 NAB

needs core-ext

module memory

: inrange? ( a. -- flag )
  2dup  >rel >abs d= ;

: rel>Handle ( a -- h. )
  >abs MemPtrRecoverHandle ;

: Handle>rel ( h. -- a 0=success|ior )
  2dup d0= if  drop 1 exit  then
  MemHandleLock  2dup inrange?
  if  >rel 0  else  drop 1  then ;

public:

: ALLOCATE ( u -- a ior )
  0 ( u. ) MemHandleNew Handle>rel ;

: FREE ( a -- ior )
  rel>Handle MemHandleFree ;

: RESIZE ( a1 u -- a2 ior )
  >r  rel>Handle  r> 0 ( u. )
  2over MemHandleResize >r
  Handle>rel  r> or ;

: size ( a -- u )
  rel>Handle MemHandleSize d>s ;

end-module
