[Implemented] FreeStructureStrings as normal PB FUNCTION

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: FreeStructureStrings as normal PB FUNCTION/COMMAND

Post by helpy »

traumatic wrote:...und wenn Du Deine Frage in Englisch stellen würdest, gäb's vielleicht auch
eine größere Chance auf eine Antwort...
Ja! Ich weiß ... man denkt einfach schneller in der Muttersprache.

Yes! I know ... it is faster to think in my native language. I posted in german by accident.

I have transleted it ... cu, helpy
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: FreeStructureStrings as normal PB FUNCTION/COMMAND

Post by traumatic »

helpy wrote:Ja! Ich weiß ... man denkt einfach schneller in der Muttersprache.

Yes! I know ... it is faster to think in my native language. I posted in german by accident.
To get this straight: I didn't mean to accuse you of anything, it was merely
meant to be an advice. Sorry if you were getting this wrong.

And about the "thinking-thingy": I don't know, I usually avoid to think. :D
Last edited by traumatic on Wed Mar 22, 2006 2:40 pm, edited 1 time in total.
Good programmers don't comment their code. It was hard to write, should be hard to read.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: FreeStructureStrings as normal PB FUNCTION/COMMAND

Post by helpy »

traumatic wrote:To get this straight: I didn't mean to accuse use of anything, it was merely
meant to be an advice. Sorry if you were getting this wrong.
I did understand it right! It was 100% OK how you wrote it!
traumatic wrote:And about the "thinking-thingy": I don't know, I usually avoid to think. :D
:D
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

:lol:
@}--`--,-- A rose by any other name ..
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

Hi all,

A workaround with a Macro:

Code: Select all

Macro PAUSE(msg)
  MessageRequester("Info",msg)
EndMacro

Macro SYS_FreeStructure(PointerToStructure,StructName)
  !PUSH   dword s_#StructName
  !PUSH   [PointerToStructure]
  !CALL  _SYS_FreeStructureStrings@8
EndMacro

Structure Struct2
  p.l
  s1.s
  s2.s
EndStructure

Structure StructAll
  p.l
  s1.s
  st.Struct2
  s2.s
EndStructure

; Without this Structure the offset data table
; "s_structall" is not created by the compiler!
Procedure Dummy_Proc()
  Protected x.StructAll
EndProcedure

#TestSize = 1024*1024

PAUSE("Start");===========================================> ~ 2.2 MByte

Dim pa.l(#TestSize)

PAUSE("Array is DIM'ed!");================================> ~ 2.3 MByte

For i = 0 To #TestSize
  pa(i) = AllocateMemory(SizeOf(StructAll))
Next i
PAUSE("Memory is allocated!");============================> ~ 39.2 MByte

*p.StructAll

For i = 0 To #TestSize
  *p = pa(i)
  With *p
    \s1=Space(32)
    \s2=Space(32)
    \st\s1=Space(32)
    \st\s1=Space(32)
  EndWith
Next i

PAUSE("Strings are assigned to structure elements!");=====> ~ 187 MByte

For i = 0 To #TestSize
  *p = pa(i)
  SYS_FreeStructure(p_p,structall) ; name of structure in LOWECASE !!!
Next i

PAUSE("strings are free again!");========================> ~ 39.6 MByte

For i = 0 To #TestSize
  *p = pa(i)
  FreeMemory(*p)
Next i

PAUSE("Memory is free again!");===========================> ~ 6.9 MByte

Dim pa(0)

PAUSE("Array is cleared");================================> ~ 2.8 MByte
The first tests work!

Disadvantages:
  • I have to crate a dummy procedure, where all the the structures are used. Otherwise the PBCompiler does not create the "offset data table" for the structures.
  • The name of the structure must be given in lower case!
  • pointer to the structure must be given like this:
    ==> *PointerVariable.StructName => p_PointerVariable
    ==> PointerInLongVariable.l => v_PointerInLongVariable
... for now I can live with this! Nevertheless a PB-Function like this would be very nice:

Code: Select all

FreeStructureStrings(*PointerToStructure)
The Compiler should know the structure name of *PointerToStructure!

cu, helpy
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

Fred wrote:Actually it could be a good idea for dynamic structures, i put it on the now famous TODO list.
I would like to request similar commands to copy (copies the contents of the strings, not the pointers), compare (compares the contents of the strings, not the pointers) and clear (if strings uninitialised creates a pointer to a null, if initialised resets them to empty strings) structured memory containing strings.

Or perhaps a native PB command that gives us access to the s_ data (along with documentation on its format) so that we can write our own routines? :)
Mat
Post Reply