// Dynamic memory blocks for CoolBasic
 
Function createMemoryBlock(size = 0)
	Return MakeMEMBlock(4+size)
EndFunction
 
Function deleteMemoryBlock(mem)
	DeleteMEMBlock mem
EndFunction
 
Function seekMem(mem, offset)
	PokeInt mem, 0, offset
EndFunction
 
Function memOffset(mem)
	Return PeekInt(mem, 0)
EndFunction
 
Function memReSize(mem, size)
	ResizeMEMBlock mem, size + 4
EndFunction
 
Function memSize(mem)
	Return MEMBlockSize(mem) - 4
EndFunction
 
Function putByte(mem, byte_, off_ = -1)
	If off_ > -1 Then
		If off_ + 1 > memSize(mem) Then memReSize(mem, memSize(mem) + 1)
		PokeByte mem, off_, byte_
	Else
		If memOffset(mem) + 1 > memSize(mem) Then memReSize(mem, memSize(mem) + 1)
		PokeByte mem, memOffset(mem) + 4, byte_
		seekMem(mem, memOffset(mem) + 1)
	EndIf
EndFunction
 
Function putShort(mem, short_, off_ = -1)
	If off_ > -1 Then
		If off_ + 2 >= memSize(mem) Then memReSize(mem, memSize(mem) + 2)
		PokeShort mem, off_, short_
	Else
		If memOffset(mem) + 2 >= memSize(mem) Then memReSize(mem, memSize(mem) + 2)
		PokeShort mem, memOffset(mem) + 4, short_
		seekMem(mem, memOffset(mem) + 2)
	EndIf
EndFunction
 
Function putFloat(mem, float_, off_ = -1)
	If off_ > -1 Then
		If off_ + 4 >= memSize(mem) Then memReSize(mem, memSize(mem) + 4)
		PokeFloat mem, off_, float_
	Else
		If memOffset(mem) + 4 >= memSize(mem) Then memReSize(mem, memSize(mem) + 4)
		PokeFloat mem, memOffset(mem) + 4, float_
		seekMem(mem, memOffset(mem) + 4)
	EndIf
EndFunction
 
Function putInt(mem, int_, off_ = -1)
	If off_ > -1 Then
		If off_ + 4 >= memSize(mem) Then memReSize(mem, memSize(mem) + 4)
		PokeInt mem, off_, int_
	Else
		If memOffset(mem) + 4 >= memSize(mem) Then memReSize(mem, memSize(mem) + 4)
		PokeInt mem, memOffset(mem) + 4, int_
		seekMem(mem, memOffset(mem) + 4)
	EndIf
EndFunction
 
Function putString(mem, string_$, off_ = -1, length = 0)
	If length = 0 Then length = Len(string_$)
	If off_ > -1 Then
		putByte(mem, length, off_)
		For i = 1 To length
			putByte(mem, Asc(Mid(string_$, i, 1)), off_ + i)
		Next i
	Else
		putByte(mem, length)
		For i = 1 To length
			putByte(mem, Asc(Mid(string_$, i, 1)))
		Next i
	EndIf
EndFunction
 
Function getByte(mem, off_ = -1)
	If off_ > -1 Then
		If off_ < memSize(mem) Then
			Return PeekByte(mem, 4 + off_)
		EndIf
	Else
		If memOffset(mem) < memSize(mem) Then
			seekMem(mem, memOffset(mem) + 1)
			Return PeekByte(mem, 4 + memOffset(mem) - 1)
		EndIf
	EndIf
EndFunction
 
Function getShort(mem, off_ = -1)
	If off_ > -1 Then
		If off_ + 2 <= memSize(mem) Then
			Return PeekShort(mem, 4 + off_)
		EndIf
	Else
		If memOffset(mem) + 2 <= memSize(mem) Then
			seekMem(mem, memOffset(mem) + 2)
			Return PeekShort(mem, 4 + memOffset(mem) - 2)
		EndIf
	EndIf
EndFunction
 
Function getFloat(mem, off_ = -1)
	If off_ > -1 Then
		If off_ + 4 <= memSize(mem) Then
			Return PeekFloat(mem, 4 + off_)
		EndIf
	Else
		If memOffset(mem) + 4 <= memSize(mem) Then
			seekMem(mem, memOffset(mem) + 4)
			Return PeekFloat(mem, 4 + memOffset(mem) - 4)
		EndIf
	EndIf
EndFunction
 
Function getInt(mem, off_ = -1)
	If off_ > -1 Then
		If off_ + 4 <= memSize(mem) Then
			Return PeekInt(mem, 4 + off_)
		EndIf
	Else
		If memOffset(mem) + 4 <= memSize(mem) Then
			seekMem(mem, memOffset(mem) + 4)
			Return PeekInt(mem, 4 + memOffset(mem) - 4)
		EndIf
	EndIf
EndFunction
 
Function getString(mem, off_ = -1, length = 0)
	If off_ > -1 Then
		If length = 0 Then length = getByte(mem, off_) Else off_ + 1
		For i = 1 To length
			ret$ = ret$ + Chr(getByte(mem, off_ + i))
		Next i
	Else
		If length = 0 Then length = getByte(mem)
		For i = 1 To length
			ret$ = ret$ + Chr(getByte(mem))
		Next i
	EndIf
	Return ret
EndFunction