BigString

Share your advanced PureBasic knowledge/code with the community.
User avatar
Deeem2031
Enthusiast
Enthusiast
Posts: 216
Joined: Sat Sep 20, 2003 3:57 pm
Location: Germany
Contact:

BigString

Post by Deeem2031 »

You want bigger string than 64000 B? Or just lower memory-usage whether you use small strings? Use this: (but don't use the big strings with "Debug", the Debugger still have a limit to 64000B)

Code: Select all

;BigString by Deeém2031 and PureFan

Structure EXCEPTIONREPORTRECORD 
  ExceptionCode.l 
  fHandlerFlags.l 
  *NestedExceptionReportRecord.EXCEPTIONREPORTRECORD 
  *ExceptionAddress.l 
  cParameters.l 
  ExceptionInfo.l[#EXCEPTION_MAXIMUM_PARAMETERS] 
EndStructure 

;{ BigString 
Global BigString_Base, BigString_BaseSize, BigString_OldExceptionHandler 

DisableDebugger 
Procedure StringExceptionHandler(*ExceptionInfo.EXCEPTION_POINTERS) 
  Protected BigString_BaseEnd, tmp 
  *ExceptionRecord.EXCEPTIONREPORTRECORD = *ExceptionInfo\pExceptionRecord 
  If *ExceptionRecord\ExceptionCode = #EXCEPTION_ACCESS_VIOLATION 
    If *ExceptionRecord\cParameters = 2 
      BigString_BaseEnd = BigString_Base+BigString_BaseSize 
      If *ExceptionRecord\ExceptionInfo[1]&$FFFFF000 = BigString_BaseEnd-$1000 
        VirtualProtect_(BigString_BaseEnd-$1000,$1000,#PAGE_READWRITE,@tmp) 
        If VirtualAlloc_(BigString_BaseEnd,$1000,#MEM_COMMIT,#PAGE_NOACCESS) 
          BigString_BaseSize+$1000 
          ProcedureReturn #EXCEPTION_CONTINUE_EXECUTION 
        EndIf 
        MessageRequester("Error","Kein Speicher mehr da, der String ist zu groß.",16) 
      EndIf 
    EndIf 
  EndIf 
  ProcedureReturn CallFunctionFast(BigString_OldExceptionHandler,*ExceptionInfo) 
EndProcedure 
EnableDebugger 

Procedure InitBigString() 
  If BigString_Base 
    VirtualFree_(BigString_Base, BigString_BaseSize, #MEM_RELEASE) 
  EndIf 
  
  BigString_Base = VirtualAlloc_(#Null,$10000000,#MEM_RESERVE,#PAGE_READWRITE) 
  If BigString_Base 
    BigString_BaseSize = $2000 
    !PUSH dword[PB_StringBase] 
    !PUSH 0 
    !PUSH dword[PB_MemoryBase] 
    !EXTRN _HeapFree@12 
    !CALL _HeapFree@12 
    If VirtualAlloc_(BigString_Base,BigString_BaseSize,#MEM_COMMIT,#PAGE_READWRITE) 
      !MOV dword[PB_StringBase], Eax 
      VirtualProtect_(BigString_Base+(BigString_BaseSize-$1000),$1000,#PAGE_NOACCESS,@tmp) 
      BigString_OldExceptionHandler = SetUnhandledExceptionFilter_(@StringExceptionHandler()) 
      ProcedureReturn #True 
    EndIf 
  EndIf 
  ProcedureReturn #False 
EndProcedure 

Procedure FreeBigString() 
  VirtualFree_(BigString_Base, BigString_BaseSize, #MEM_RELEASE) 
  SetUnhandledExceptionFilter_(BigString_OldExceptionHandler) 
  !PUSH 64000 
  !PUSH 8 ;HEAP_ZERO_MEMORY 
  !PUSH dword[PB_MemoryBase] 
  !EXTRN _HeapAlloc@12 
  !CALL _HeapAlloc@12 
  !MOV dword[PB_StringBase], Eax 
EndProcedure 

;} 

If InitBigString() 
  
  For i2 = 1 To 10 
    x.s = ""
    For i = 1 To i2 
      x + Space(65536) 
    Next 
    MessageRequester("",Str(i2)+"*64KB = "+Str(Len(x))) 
  Next 
  
  FreeBigString() 
Else 
  MessageRequester("","Alloc-Prob",16) 
EndIf
Last edited by Deeem2031 on Thu Sep 22, 2005 8:37 pm, edited 1 time in total.
irc://irc.freenode.org/#purebasic
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Very nice 8)
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

It seems to me like a hack. Is it save to use this one? I do not understand rigtht what is happening there. Why is there an ExceptionHandler?

I have an Idea but not sure about it.

Mike
Tranquil
Phoenix
Enthusiast
Enthusiast
Posts: 141
Joined: Sun Sep 04, 2005 2:25 am

Re: BigString

Post by Phoenix »

There is a tip at viewtopic.php?p=45054 which seems to do the same thing, which is better to use?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: BigString

Post by ts-soft »

Phoenix wrote:There is a tip at viewtopic.php?p=45054 which seems to do the same thing, which is better to use?
It is not the same, the code of DEEM2031 allocate the memory automatically of 2 KB to the available storage
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: BigString

Post by DarkDragon »

Good job Deeem2031 and purefan! :)
ts-soft wrote:
Phoenix wrote:There is a tip at viewtopic.php?p=45054 which seems to do the same thing, which is better to use?
It is not the same, the code of DEEM2031 allocate the memory automatically of 2 KB to the available storage
Learned English, huh? :roll: :lol:
bye,
Daniel
Post Reply