PDA

View Full Version : O2 - some basic questions



ReneMiner
21-07-2013, 08:23
A couple of questions, hopefully someone has an answer to one or another.

I think it's possible to run a few different O2-scripts from within one tB-script - at least I hope so. All examples I've studied use only one script once.

So the first question would be: Is there any limit?

When do I have to terminate an O2-script?

And I want to know if it's possible to pass the source-string "src" also as a pointer from tB. Instead


O2_Basic src

it would be great if it were possible to pass the pointer of - for example some dictionary-bucket so I could 'feed' O2 just by passing some dictionary-keyname alike this:


O2_BasicAt StrPtr(src)
'...
O2_BasicAt Peek(Dword, Dictionary_Exists(my02DrawingDict, "mandelbrot"))
O2_BasicAt Peek(Dword, Dictionary_Exists(my02DrawingDict, "pyramids"))

O2_BasicAt Peek(Dword, Dictionary_Exists(my02CollisionDict, "face2face"))
O2_BasicAt Peek(Dword, Dictionary_Exists(my02CollisionDict, "face2point"))
'...
O2_BasicAt myStrPtr ' whereever - as long as valid stringpointer
' over the top:
O2_BasicAt myPtr, SizeInBytes ' so could use Heap or any other memory to store different scripts

is there such method? Only clue I have is thinBasic_keywords.ini which lists:


O2_ASM
O2_ASMO
O2_ASMO_FILE
O2_ASM_FILE
O2_BASIC *
O2_BUF
O2_ERROR
O2_EVAL *
O2_EXEC
O2_GET
O2_LEN
O2_LINK
O2_PREP *
O2_PUT
O2_VIEW
O2_VIEW_FILE
OXYGEN_EVAL - suppose, has been replaced with O2_Eval ?


* = no documentation found

...or would it be better to have all those example functions in the code above (drawing, collision) in one big O2-script that stays permanently "alive" as long as my tB-script runs?

ReneMiner
21-07-2013, 13:21
How to Peek something from memory using oxygen?:confused:



Uses "oxygen", "console"
Alias Memory_Get As Get$
Alias Long As FunctionPtr

FunctionPtr P_O2End, P_GetPtr


If Not run_O2() Then
PrintL "error within O2-script - can not run this"
PrintL O2_ERROR
WaitKey
Stop
EndIf

Declare Sub finish() At P_O2End
Declare Function GetPtr(ByVal String, Optional ByVal Long, Optional ByVal Long) As DWord At P_GetPtr

' some testing:
Long foo = 123
DWord myPtr = GetPtr("foo")

PrintL "foo = " + CVL(Get$(myPtr, SizeOf(Long)))

Dim dog(123) As Byte
dog(42) = 42

myPtr = GetPtr("dog", 42, SizeOf(Byte))
Dim olk(77) As Double
olk(33) = CVBYT(Get$(myPtr, SizeOf(Byte))) + 0.125

PrintL "olk(33) = " + CVD(Get$(GetPtr("olk", 33, SizeOf(Double)), SizeOf(Double)))

String test = "I am an important string"
myPtr = GetPtr("test")
PrintL "memory found:" + Get$(myPtr, Peek(DWord, myPtr - 4))

WaitKey
finish()

'----------------------------------------------
Function run_O2() As Boolean

Local src As String = "
#basic
include "thincore.inc"

Function GetPtr(Byval sName as String, Optional ByVal Index as Long, Optional Byval lSize as Long) As Dword link #P_GetPtr
Long MainType, SubType, IsArray, DataPtr, nElements

thinBasic_VariableGetInfoEX(sName, MainType, SubType, IsArray, DataPtr, nElements)
If Index > 0 And Index <= nElements then
If MainType = 30 Then ' string
Function = *( DataPtr + (Index - 1) * 4)
Else
Function = DataPtr + (Index - 1) * lSize
EndIf
Else
If MainType = 30 Then ' string
Function = *DataPtr
Else
Function = DataPtr
EndIf
EndIf
End Function


'------------------------------------------
Sub finish() link #P_O2End
terminate
End Sub
"


O2_BASIC src
If O2_ERROR <> "" Then Return FALSE
O2_EXEC

Function = TRUE

End Function


Edit:Script corrected :) - returns pointers now for any thinBasic-variable-name passed as string

Charles Pegge
21-07-2013, 15:03
Peeking longs and dwords can be done using the '*' pointer operator

sys dat,adr
string s="12345678"


adr=strptr s
print hex *(adr+4) 'result 38373635

For more structured peeking, use an array overlay at...

string s="12345678"


long d at (strptr s)
print hex d[2] 'result 38373635


byte b at (strptr s)
print hex b[5] 'result 35

ReneMiner
21-07-2013, 15:21
thanks. the idea to use an overlay came to me already but I think it's good to know some "GetDataAt-method" as *
"sys" some alias for 'Dword' to store pointers?

PS. instead of reposting the correct script now I just changed the one above

Charles Pegge
21-07-2013, 15:55
Sys is a signed integer that is safe to use as a pointer. When compiling 64 bit binaries, it becomes 64 bits wide, whereas long, dword and int remain 32 bit integers.

ReneMiner
21-07-2013, 16:09
ah, ok. there seems something not to work as I wish it would... I repost the changed script here - the line which makes it crash is commented (line-number 42) - oh what coincidence... perhaps I have made something wrong in line 61 with that * ?

Problem seems to be that the pointers of a string-array are not there where I thought they would be. Straight in a row of Dwords starting at DataPtr...


Uses "oxygen", "console"

Alias Long As FunctionPtr

FunctionPtr P_O2End, P_GetPtr


If Not run_O2() Then
PrintL "error within O2-script - can not run this"
PrintL O2_ERROR
WaitKey
Stop
EndIf

Declare Sub finish() At P_O2End
Declare Function GetPtr(ByVal String, Optional ByVal Long, Optional ByVal Long) As DWord At P_GetPtr

' some testing:
Long foo = 123
DWord myPtr = GetPtr("foo")

PrintL "foo = " + CVL(Memory_Get(myPtr, SizeOf(Long)))

Dim dog(123) As Byte
dog(42) = 42

myPtr = GetPtr("dog", 42, SizeOf(Byte))
Dim olk(77) As Double
olk(33) = CVBYT(Memory_Get(myPtr, SizeOf(Byte))) + 0.125

PrintL "olk(33) = " + CVD(Memory_Get(GetPtr("olk", 33, SizeOf(Double)), SizeOf(Double)))

String test = "I am an important string"
myPtr = GetPtr("test")
PrintL "memory found:"
PrintL Memory_Get(myPtr, Peek(DWord, myPtr - 4))

Dim s2(5) As String = "wrong", "wrong", "right", "wrong", "wrong"

myPtr = GetPtr("s2", 3)
PrintL "memory found:"
' This crashes >>> PrintL Memory_Get(myPtr, Peek(DWord, myPtr - 4))

WaitKey
finish()

'----------------------------------------------
Function run_O2() As Boolean

Local src As String = "
#basic
include "thincore.inc"

Function GetPtr(Byval sName as String, Optional ByVal Index as Long, Optional Byval lSize as Long) As Dword link #P_GetPtr
long MainType, SubType, IsArray, DataPtr, nElements

thinBasic_VariableGetInfoEX(sName, MainType, SubType, IsArray, DataPtr, nElements)
If Index > 0 And Index <= nElements then
If MainType = 30 Then ' string
' print "String Array Check"
Function = *(DataPtr + (Index-1) * SizeOf(Dword))
Else
Function = DataPtr + (Index - 1) * lSize
EndIf
Else
If MainType = 30 Then ' string
Function = *DataPtr
Else
Function = DataPtr
EndIf
EndIf
End Function


'------------------------------------------
Sub finish() link #P_O2End
terminate
End Sub
"


O2_BASIC src
If O2_ERROR <> "" Then Return FALSE
O2_EXEC

Function = TRUE

End Function


and I still don't know how I could place some dword-array [nElements] at DataPtr - anyhow I try - error:badday:
dword sPtrs[nElements] at DataPtr - error invalid constant autodim,
the method in line 61 is not correct - I tested the result from thinBasic where I can find the Stringpointers in order as awaited and it returns wrong values.

zlatkoAB
21-07-2013, 17:37
anyhow I try - error
heh it looks that you trying to use wrong variable type but is not clear to me
because code is mix of oxygen & thinBasic...
Do you can use oxygen only code?

ReneMiner
21-07-2013, 17:57
...Do you can use oxygen only code?
Sorry - I wouldn't know how - even after downloading oxygen-basic now...
Problem is, I get on string-arrays the first elements pointer returned correct from O2 if I use just


Function = DataPtr

(line 61)

and peek the desired Index from thinBaisc side correct like


correctStrPtr = Peek(Dword, GetPtr("myvar",1) + (Index-1) * SizeOf(Dword))

but


Function = *( DataPtr + (Index-1) * SizeOf(Dword) )

does not return the expected value. Maybe I've misunderstood the use of * ?

zlatkoAB
21-07-2013, 18:29
Rene
I am confused with what you trying to do ...
what a heck is sizeOf(dWord) - i am not sure but i think that is not posible
get size of reserved variable type as is DWORD...:confused:

You say that you have oxygen - then try first directly in oxygen then if work
properly tray in thinBasic...
And what is not clear to you when you use Oxygen?

ReneMiner
21-07-2013, 18:42
I'm trying to use oxygen to manipulate thinBasic-variables.

SizeOf(Dword) should return the size of one element of variable type dword - can replace with 4 but won't help

Imagine a data-bound listview, that shall for example display a phone numbers list. The user tells the listview once: Hey, data for that list to find in an array-variable named "Phonelist", in first column display content of Phonelist().Name, in second column fill in the numbers found at PhoneList().Numbers. So no matter what user does with this array thereafter- the list "knows" always what and how much to display and where to find it - even if pointers change - when list gets drawn it "knows" what it has to fill in.So therefor I need a method to not just receive the correct pointers but also one to change dynamic strings without "loosing and orphaning" their pointers so my list becomes editable

ReneMiner
21-07-2013, 20:47
I see, I don't come any further this way. And just poking the right values to StrPtr/StrPtrLen-position... an idea I never dared to try. Now I will- even if my computer explodes and sets the cat on fire

zlatkoAB
21-07-2013, 21:35
Now I will- even if my computer explodes and sets the cat on fire
:D
Don't worry ...

ReneMiner
21-07-2013, 21:53
tommorow- in a sober condition- forgotten the troubles of the day before - with unstoppable powers released of thinCore an avalanche will go down the hill :)

Charles Pegge
22-07-2013, 00:04
Here is a simpler varptr test.





uses "oxygen"

'OXYGENBASIC SECTION
'===================

dim as string src
dim as long pGetVarPtr
dim as long pFinish

src="

'Derived from ThinCore header
! thinBasic_VariableGetInfoEX lib "thinCore.dll" (string SearchKey, sys *pMainType,*pSubType,*pIsArray,*pDataPtr,*pnElements,WhichLevel) as sys


'---Equates for variable Main Type
%MainType_IsNumber = 20&
%MainType_String = 30&
%MainType_IsString = %MainType_String
%MainType_Variant = 50&
%MainType_IsVariant = %MainType_Variant
%MainType_UDT = 60&
%MainType_IsUDT = %MainType_UDT
'---Equates for variable Sub Type
%SubType_Byte = 1&
%SubType_Integer = 2&
%SubType_Word = 3&
%SubType_DWord = 4&
%SubType_Long = 5&
%SubType_Quad = 6&
%SubType_Single = 7&
%SubType_Double = 8&
%SubType_Currency = 9&
%SubType_Ext = 10&
%SubType_AsciiZ = 25&


function GetVarPtr(bstring varname, optional sys n) as sys, link #pGetVarPtr
============================================================================
'
sys MainType, SubType, IsArray, DataPtr, nElements, WhichLevel
thinBasic_VariableGetInfoEX Varname, MainType, SubType, IsArray, DataPtr, nElements, WhichLevel
'print " MainType " MainType +
' " SubType " SubType +
' " IsArray " IsArray +
' " DataPtr " DataPtr +
' " nElements " nElements
if n=0 then n=1
if MainType=30 and n<=nElements then
bstring array at (DataPtr) 'thinBasic uses bstrings
return strptr array[n]
end if
end function


sub finish() link #pFinish
==========================
terminate
end sub
"


o2_basic src
if o2_error then
msgbox 0,o2_error
stop
else
o2_exec
end if

declare function GetVarPtr(byval v as string, optional byval n as dword ) as dword at pGetVarPtr
declare sub Finish() at pFinish

'MAIN THINBASIC SECTION
'======================

string a(3)
a(1)="Apples"
a(2)="Bananas"
dword p=GetVarPtr("a",2) 'pointer to Bananas
if p then msgbox 0,hex$(peek(byte,p)) '42
Finish

ReneMiner
30-07-2013, 09:21
I have another "basic" question regarding oxygen:

Is there a limit of dimensions for an array - and how many would that be? Does the element-order of 2- and 3-dimensional arrays in oxygen match the element-order in tB?

I wonder if it were possible to calculate the 1-dimensional index of some multidimensional element (http://www.thinbasic.com/community/showthread.php?t=12181&p=89429#post89429) a lot faster if the calculation was compiled. For full powers it should probably be done in assembler - but I don't even get the stuff in o2-basic together see below - mostly because I'm missing some simple methods as peek/poke and I don't know how to read out that Dword in front of a bstring (which tells the length of the string without creating a temporary one as "Len" does).

I'm convinced such low-level-often-needed calculations like the position/index of a variable should not be done by basic itself since it might be needed a few times per codeline.



Uses "console", "oxygen"

Long pGetIndex
Long pFinish

O2_Basic RawText

Function GetIndex( bString Element, bString Bounds) As DWord, link #pGetIndex

If Len(Element) <> Len(Bounds)
Return 0
Else
If Len(Element) < 8 Then Return 0
If Len(Bounds) < 8 Then Return 0
EndIf

DWord i, position, numElements
DWord numDims = Len(Element)/SizeOf(DWord)

Dim As DWord vElement[numDims] At StrPtr(Element)
Dim As DWord vBounds[numDims] At StrPtr(Bounds)

For i = 1 To numDims
If vElement[i] < 1 Or vElement[i] > vBounds[i] Then Return 0
If vBounds[i] < 2 Then Return 0
Next

numElements = vBounds[1]
For i = 2 To numDims
numElements = numElements * vBounds[i]
Next

For i = 1 To numDims - 1
numElements = numElements/vBounds[i]
position = position + (vElement[i]-1) * numElements
Next

Function = position + vElement[numDims]


End Function
'-------------------------
Sub Finish() link #pFinish
terminate
End Sub

End RawText

If O2_Error <> "" Then
PrintL "Can not run - Error within o2-script:" + $CRLF
PrintL O2_Error
WaitKey
Stop
Else
' can run
O2_Exec
End If
'---------------------------------------------------------------
' TB_section
Declare Function GetIndex( ByVal String, ByVal String) As DWord At pGetIndex

' test:
PrintL "search Element 3,4,5 in bounds of 4,5,6"
PrintL Str$( GetIndex MKDWD$(3,4,5), MKDWD$(4,5,6) )

WaitKey
Finish()

I think what I wrote here is a total mess - it does not run and I don't know why- no matter what I change- thereafter comes another Error and it's all blind trying out. I would not use a function as len here usually but I don't know any better. Script does not run anyway - it's like driving a fully loaded truck with broken mirrors backwards pushing the throttle down- no matter what comes.

ReneMiner
30-07-2013, 18:52
maybe I should add: the link posted above leads to a commented tB-version of this function (http://www.thinbasic.com/community/showthread.php?t=12181&p=89429#post89429) - just in case you don't understand what this is supposed to do if it would work - because it's a little rare described here

Charles Pegge
30-07-2013, 19:39
The order of dimensions for thinBasic is smallest-stride-first aka "column-major". This is the same as PowerBasic but the opposite to C and FreeBasic

OxygenBasic sidesteps this issue by supporting single dimension arrays only. So the multidimensional index has to be calculated. The downside is inconvenience, but the upside is flexibility.

Creating a pointer for an indexed element is pretty easy and certainly more efficient:

Example

uses "oxygen"


type pixelrgba
r as byte
g as byte
b as byte
a as byte
end type




dim pix(640,480) as pixelrgba
dim as long pFill,pFinish


dim as string src=rawtext


type pixel byte r,g,b,a


sub fill(pixel*p,sys nx,ny,margin) link #pFill
'filling pixel block with opaque red
nx-=margin
ny-=margin
indexbase 0
sys x,y
pixel *q
for y=margin to <ny
@q=@p[y*640+margin]
for x=margin to <nx
q.a=255
q.r=250
@q+=sizeof pixel
next
next
end sub


sub finish() link #pFinish
terminate
end sub


end rawtext


o2_basic src
if len(o2_error) then
msgbox 0,o2_error
stop
else
o2_exec
end if


declare sub fillpix(pix as pixelrgba, byval x as long, byval y as long, byval margin as long) at pFill
declare sub finish() at pFinish


fillpix(pix(1,1),640,480,100)
finish
msgbox 0,pix(200,200).r '250

ReneMiner
31-07-2013, 06:33
I don't understand the example. Is it supposed to mean Pixel is a replacement-keyword/substitute for two-dimensional arrays in oxygen? And why is it in oxygen part just fill while declared as fillpix from tB? Is it supposed to prove that name of the function can differ from oxygen and tb-version? mainly parameters and shared position matter?

And how does


For y=margin To <ny

work? it makes no sense to think "smaller than" since it would stop at 0 already - does it mean "For y = margin To (ny-1)" or to go the absolute value of y here = 480 steps?

Charles Pegge
31-07-2013, 10:16
My example is only an illustration.

Iteration:

for i=0 to <e

is more efficient than

for i=0 to e-1

Petr Schreiber
06-08-2013, 08:24
Hi John,

I think Charles it already neatly summed up in this post (http://www.thinbasic.com/community/showthread.php?t=12170&p=89338&viewfull=1#post89338). It is just that simple.


Petr

Charles Pegge
06-08-2013, 22:47
I think Petr is the only one using thinBasic Oxygen, in a live application so far. :)

Petr Schreiber
07-08-2013, 07:06
Hehe, I am afraid I am not very bright and it gets worse with every year :D. The Oxy <-> ThinBASIC interface is just simple to use, the most seamless embedding I have seen so far.

Oxygen is needed for ThinBASIC programmer whenever you need codepointer to function (mostly for callbacks), this is why I think it is not just something for my entertainment, but useful general purpose helper.
I am also happy Charles listened to my crazy ideas regarding OOP, I consider his final implementation the most easy one to use along with C#, but without need for those nasty semicolons.

In my opinion, even more people would use Oxygen (standalone or embedded) if it had a bit more structured help file and most importantly, more easy to read error messages. Which is ton of work of course.


Petr

Petr Schreiber
07-08-2013, 09:31
I do publish knowledge I consider interesting via articles (http://www.thinbasic.com/community/content.php) and samples. I take your saying that I "leave thinBasic users in the dust" as opinion, but I hope most of ThinBASIC users would not agree - I support TBGL, my primary creation, daily for 8 years in row. When user comes with general question on other sides of ThinBASIC, if I have time and know the answer, I always reply.

I can't offer this to Oxygen, because I have not enough time & most importantly knowledge for it. There are other interests that keep me busy, and I think author should be responsible person #1 for documenting and supporting their work.
That is why I am not asking anybody else to do support for TBGL for me. When Rene, Kent, Mike(s) or others do that voluntarily, I really appreciate it and it makes me really happy. But life is short and asking others for their time is always a thing to think about twice.


Petr

ReneMiner
08-08-2013, 08:25
I think I'm not leaving anyone in the dust and publish all my stuff here in plain thinBasic so everyone can look inside and check out what it does. And I do have to check tB-help over and over again- even if I used a method a hundred times already - I'll check help once more for parameters order, special behaviour etc.
The problem in using O2 is to me, that I don't have any asm- nor c-background and I only have a list of keywords in alphabetical order and no idea about what the keyword does - no idea what the "opposite method" is and I struggle already with some very basic functions as Peek & Poke - maybe there are ASM-methods but I don't know their names either...

It's like having a starship with built-in bistromathic drive parking in front of the house - ready for take off into another galaxy but the only clue I have how to operate it, is some advertising brochure in magrathean language. And John is the one who says: "Of course it can fly! Just place yourself in the pilots seat, push the ignition button and go!" - but after I've found the button to ignite I feel really stupid because I don't know how to control that thing...

But I did not give up yet: I got the function to calculate multidimensional indexes in one-dimensional array finally together in O2-basic. But I would like it faster and replace Len with something like StrPtrLen - but I don't know how to get these 4 bytes in front of that StrPtr-position :(- in tB I'd just Peek them...



Uses "console", "oxygen"

Long pGetIndex
Long pFinish

' this function calculates the one-dimensional array-index of any
' "multidimensional" stored element

' pass one string containing the elements position within multidimensional
' storage, for example myStorage(1,1,1) setup string like MKDWD$(1,1,1)
' second string has to contain the dimensions Ubounds,

' retrieve Index in one-dimensional array or 0 if invalid parameters

' to keep the function small there's a minimum of 2 dimensions
' and a minimum-Ubound of 2 elements per dimension

O2_Basic RawText

Function GetIndex( bString Element, bString Bounds) As DWord, link #pGetIndex

If Len(Element) <> Len(Bounds)
' both strings have to match in length
Return 0
Else
' both strings have to have at least 2 dimensions
If Len(Element) < 8 Then Return 0
' If Len(Bounds) < 8 Then Return 0 - not necessary if both match in size...
EndIf

DWord i, position, numElements
DWord numDims = Len(Element)/SizeOf(DWord)

DWord vElement At StrPtr(Element)
DWord vBounds At StrPtr(Bounds)

For i = 1 To numDims
' element must not be out of bounds
If vElement[i] < 1 Or vElement[i] > vBounds[i] Then Return 0
' a dimension needs at least 2 elements
If vBounds[i] < 2 Then Return 0
Next

numElements = vBounds[1]
For i = 2 To numDims
numElements = numElements * vBounds[i]
Next

For i = 1 To numDims - 1
numElements = numElements/vBounds[i]
position = position + (vElement[i]-1) * numElements
Next

Function = position + vElement[numDims]


End Function
'-------------------------
Sub Finish() link #pFinish
terminate
End Sub

End RawText

If O2_Error <> "" Then
PrintL "Can not run - Error within o2-script:" + $CRLF
PrintL O2_Error
WaitKey
Stop
Else
' can run
O2_Exec
End If
'---------------------------------------------------------------
' TB_section
Declare Function GetIndex( ByVal String, ByVal String) As DWord At pGetIndex
Declare Sub Finish() At pFinish
' test:
PrintL "search Element 2,3 in bounds of 2,3"
PrintL Str$( GetIndex MKDWD$(2,3), MKDWD$(2,3) )
PrintL

PrintL "search Element 3,4,5 in bounds of 4,5,6"
PrintL Str$( GetIndex MKDWD$(3,4,5), MKDWD$(4,5,6) )
PrintL

PrintL "search Element 2,3,4,5 in bounds of 4,6,8,10"
PrintL Str$( GetIndex MKDWD$(2,3,4,5), MKDWD$(4,6,8,10) )

PrintL "search Element 1,1,1,1,1,1 in bounds of 2,4,6,8,10,12"
PrintL Str$( GetIndex MKDWD$(1,1,1,1,1,1), MKDWD$(2,4,6,8,10,12) )

WaitKey
Finish()

Limits:
minimum 2 dimensions with at least 2 elements per dimension
maximum &H3FFFFFFE dimensions since there don't fit no more parameters into these strings
maximum &HFFFFFFFF elements altogether in all dimensions

more information (http://www.thinbasic.com/community/showthread.php?t=12181&p=89429#post89429)

ReneMiner
08-08-2013, 10:23
That's your guess. But how could I get anything together then?
It's just like having to browse 50 examples of unknown code to find a certain keyword or method of unknown name and structure not even knowing if any of these examples uses the desired method nor the ability to recognize it then because I don't know what it would look like.

ReneMiner
08-08-2013, 10:59
A good documentation is the alpha and omega to get good results out of a language. Users have the information about the famous abilities all bundled to some convincing package that leaves almost no open questions and encourage them to try out their ideas since it supports these ideas with some informational background.
I like the style of tB-documentation that not just lists those keywords in a tree sorted by purpose/subject area, but also describes their usage and covers for each keyword needed basic informations as

Description
Syntax
Returns
Parameters
Remarks
Restrictions
See also
Examples

It might be using up some time to write such documentation - but it's the key to success for a language: Add up all the time the author of the documentation needs: maybe hundreds of hours. Now add up the time all other users need to search for information - it's the time they need longer to get any projects done - so I think a good documentation with as much information as possible together at one place pays back because users are faster in developing their stuff - more stuff in less time and also faster learning users! - which will eventually lead the language to spread much faster and gain intelligence through more brains thinking of ways how to initiate it.

zlatkoAB
08-08-2013, 17:39
Rene
I really can't understand what is not clear to you about Oxygen Basic .
I think that you complicate things without reason.
And any example in oxygen package is very clean and simple to understand.

ReneMiner
08-08-2013, 20:21
In most cases all I need is an example that is in the ballpark of what I'm trying to do and I can figure out the rest...

problem is mostly to find the correct example that runs first. And the search for that fitting example always needs some time and includes reading through a dozen inappropriates and stumbling across unknown expressions and there's no help available that explains those - so have to find out by try'n error or search another example - good documentation is unpayable and pays off maugre

ReneMiner
09-08-2013, 07:42
too cumbersome. I don't like spending all my time on searching and browsing examples to get information together. As long as I don't find out how to poke some value to an absolute address and how to read it out again I feel unable to create something of use in O2.

I don't know why- maybe it has been overseen or nobody knows the answer - the problem is this:


bString test = "teststring"
' i think the length of test is stored at StrPtr(test)-4
' but if i do this:
Dword myLen at StrPtr(test)-4
' myLen holds 0 instead of 10!

so it's either a bug - or not as easy as this - or some exception?

And: does O2 have something like heap-memory in tB?

Charles Pegge
09-08-2013, 20:10
Hi ReneMiner

At expressions need to be enclosed by brackets, if they are not simple variables. I hope to make the compiler a bit smarter in this area.

Also better to use sys integers unless you intend to do 32 bit unsigned arithmetic with dwords.

bString test = "teststring"
print len(test) '10
sys myLen at (StrPtr(test)-4) 'put brackets around the 'at' expression
print mylen 'answer 10

ReneMiner
09-08-2013, 22:12
OK, thanks Charles, that explains it. Make a note for the help-file ;)

John, of course I got some plan and a goal in the end. It might take two years or more but I have no hurry - not with that nor with putting that shotgun into my mouth and pull the trigger :D
The way to get there needs a few steps and one of the most fundamentals is, that I need some very flexible, extremely dynamic, nodeable memory-management for the data i expect. The problem would be that I know how it will work but I have no clue how to manage this fast enough since these calls to calculate some pointer-position or index might occur twice or more often per codeline. So it has to go lightning speed to be of use. One key to get speed is to call functions with as few as even possible parameters byval and also few local variables - except statics of course.

I see no methods in oxygen-docs to alloc something and I don't know how to get the Heap_Size from tB to oxygen without passing it in each call. That would be no option.

Maybe you got some hint for me where the size of some allocated heap gets stored? Can oxygen identify some heap-memory that was allocated by tB somehow?
Does oxygen probably have own methods to allocate memory in any kind of blocks (but no ordinary strings) that would be accessable from tB then?
I'm definetely not interested if I would need any 3rd party software therefor.

Charles Pegge
11-08-2013, 10:01
The oxygen way of getting heap memory:

sys p=getmemory 1000 x sizeof double
double d at p
d(900)=4.2

Releasing heap memory:

freememory p

The memory space is always initialised with null bytes.


You can also work with any ThinBasic variable space by passing a member of any array variable byref. This will then be treated as the base member of a dynamic array on the Oxygen side. But you will still need to pass the number of elements or memory size you intend processing. If this is constant over meny calls, then you can use a separate function to set it.

ReneMiner
11-08-2013, 10:09
Isn't there a limit of 511/512 for memory? I thought I read something like this.
Also important to know: how can I retrieve the size of some allocated block of O2-memory when I just have it's pointer?

How to test if p is a valid pointer?

ReneMiner
12-08-2013, 07:45
So in tB


if Heap_Size(myHeap) then
' its for sure a pointer to some allocated heap
' and it's size is known
endif

and almost all of my functions need this size to calculate with. If I would have to pass it in each call would be very cumbersome. To be fast I can not afford to keep track on each memory-blocks size and this size is essential to all element-, node- and organizer-functions I wrote.
I think it would be much better to have it ALL done by O2 - the calculating of positions as well as the storing to memory and it probably would not make much sense to use tB-heap-functionalities and calculate in O2.
I would appreciate if GetMemory(1000) would instantly allocate 1004 bytes and return pointer to byte 5 as starting position. The other four bytes in front could tell the size. But probably for compatibility-reasons it's not possible to change the existing GetMemory-function and I would have to add one dword in front myself and write some wrappers to Get/FreeMemory...On the other hand: this size has been stored somewhere anyway - how else would FreeMemory know how much to deallocate? So a SizeOf( Memory p ) should be possible somehow...

And another question: is there something like SetAt(myVar, myAddress) in tB, to place some virtual overlay to another position or 0 to "neutralize" it?
Or would I need to use somewhat like Redim myVar At myAddress ?

Once again: I've read in oxyygen-helpfile about memory:


Remarks
Currently up to 1024 getmemory allocations are supported - thereafter the oldest get recycled. But if the strings are freed with freememory in the reverse order in which they were created, (like a stack) then this limitation does not apply.

is that still the case?

Charles Pegge
13-08-2013, 11:11
In Oxygen, heap memory allocation is done via olestrings/bstrings. Thus the length is always to be found in a long value immediately preceding the string bytes.

The theoretical upper limit in length for a olestring is 2 gig (2^31-1)

These snippets do the same thing:

sys m = getmemory 0x8000
double d at m
d[100]=1.23
print d[100]
...
print *(m-4) 'length
freememory m



bstring m = nuls 0x8000
double d at m
d[100]=1.23
print d[100]
...
print len m
frees m



thinBasic uses bstrings as its native string type, and a returned string from an oxygen function is always a bstring.

I hope this helps. The string / heapspace duality is very useful in practice.