07-08-2022, 12:07 PM
Up next is the static memory manager:
Code: (Select All)
' MEM_STATIC memory manager
/'
mem_static uses a pointer called mem_static_pointer to allocate linear memory.
It can also change mem_static_pointer back to a previous location, effectively erasing
any memory after that point.
Because memory cannot be guaranteed to be allocated in exactly the same location
after realloc which QB64 requires to keep functionality of previous pointers when
the current block of memory is full QB64 creates an entirely new block, much larger
than the previous block (at least 2x), and "writes-off" the previous block as un-
reclaimable memory. This tradeoff is worth the speed it recovers.
This allocation strategy can be shown as follows: (X=1MB)
X
XX
XXXX
XXXXXXXX
XXXXXXXXXXXXXXXX
etc.
'/
proc SYSTEM_BUS_T.mem64_static_malloc(size as uinteger) as SYSTEM_TYPE ptr
size += 7
size -= (size and 7) ' align to 8 byte boundry
if ((mov(mem_static_pointer,mem_static_pointer add size)) < mem_static_limit) then
return mem_static_pointer - size
end if
mem_static_size = (mem_static_size shl 1) + size
mem_static = malloc(mem_static_size)
if (mem_static) then
mem_static_pointer = mem_static + size
mem_static_limit = mem_static + mem_static_size
return mem_static_pointer - size
else
error(504)
end if
end proc
def SYSTEM_BUS_T.mem64_static_restore(restore_point as SYSTEM_TYPE ptr)
if ((restore_point >= mem_static) and (restore_point <= mem_static_limit)) then
mem_static_pointer = restore_point
else
' if restore_point is not in the current block, use t=start of current block as a new base
mem_static_pointer = mem_static
end if
end def