It is currently Wed Jun 19, 2013 2:01 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Merging Memory
PostPosted: Sat May 12, 2012 8:08 pm 
Offline
User
User

Joined: Sat Oct 09, 2010 3:47 am
Posts: 74
Hi,

I am trying to merge the contents from an unknown number of memory locations into a single location...

As you can see from the Debug report below I have the memory locations and sizes.

NB*: file can be spanned accross 2 or more memory locations - number unkown

I can save the file directly to disk using CreateFile with multiple WriteData calls, but I would like to keep it in memory.

File1
Memory: 31096512
Size: 65536
------------
File1
Memory: 31096512
Size: 65536
------------
File1
Memory: 31096512
Size: 27837

------------
File2
Memory: 31096512
Size: 65536
------------
File2
Memory: 31096512
Size: 12030

------------

Thank you in advance for your help,


Top
 Profile  
 
 Post subject: Re: Merging Memory
PostPosted: Sat May 12, 2012 10:19 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1468
Location: Germany
Hi,

I think it is no problem:
Code:
Procedure.i AppendMemory(*Src, *Dest, Size.i)
  Protected *New, OldSize.i
 
  OldSize = MemorySize(*Dest)
  *New = ReAllocateMemory(*Dest, OldSize + Size)
  If *New
    CopyMemory(*Src, *New + OldSize, Size)
  EndIf
 
  ProcedureReturn *New
 
EndProcedure


*Buffer = AllocateMemory(10)
If *Buffer
  FillMemory(*Buffer, 10, 1)
  *Buffer1 = AllocateMemory(5)
  If *Buffer1
    FillMemory(*Buffer1, 5, 2)
    *New = AppendMemory(*Buffer1, *Buffer, 5)
    If *New
      For i = 0 To MemorySize(*New) - 1
        Debug Str(i) + ": " + Str(PeekA(*New + i))
      Next i
      FreeMemory(*New)
    Else
      FreeMemory(*Buffer)
    EndIf
    FreeMemory(*Buffer1)
  Else
    FreeMemory(*Buffer)
  EndIf
EndIf
Bernd


Top
 Profile  
 
 Post subject: Re: Merging Memory
PostPosted: Sun May 13, 2012 1:11 am 
Offline
User
User

Joined: Sat Oct 09, 2010 3:47 am
Posts: 74
infratec thank you for the quick response... and hopefully continued support :)

I'm having trouble applying your code to my script. I think because I'm in a Callback the returned memory size is not being updated.
See the following Debug report:

YEAH!!!
65536

65536
27837


Image was returned during the first pass, but not during the other two. If the image is returned after only the first pass - its pixelated.
Same result is returned using: CatchImage(0, P1, P2) without your AppendMemory procedure.

Code:
Procedure.i AppendMemory(*Src, *Dest, Size.i)
  Protected *New, OldSize.i
 
  If *Dest
    OldSize = MemorySize(*Dest)
  EndIf
  *New = ReAllocateMemory(*Dest, OldSize + Size)
 
  If *New
    CopyMemory(*Src, *New + OldSize, Size)
  EndIf
  ProcedureReturn *New
EndProcedure

Procedure RARCallback(msg, UserData, P1, P2)
  #UCM_PROCESSDATA = 1
  #RAR_CONTINUE = 1
 
  Select msg
    Case #UCM_PROCESSDATA
      *New = AppendMemory(P1, *Buffer, P2)
      *Buffer = AllocateMemory(P2)
      CatchImage(0, *New, MemorySize(*New))
     
      If IsImage(0)
        Debug "YEAH!!!"
      EndIf
      Debug MemorySize(*New)
      ProcedureReturn #RAR_CONTINUE
  EndSelect
EndProcedure


Thank you again,


Top
 Profile  
 
 Post subject: Re: Merging Memory
PostPosted: Sun May 13, 2012 8:01 am 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1468
Location: Germany
Hi JHP JHP,

It was only to show how it works.
For your programm use something like this:
Code:
Procedure RARCallback(msg, UserData, P1, P2)
  #UCM_PROCESSDATA = 1
  #RAR_CONTINUE = 1
 
  Static *Buffer = #Null
  Protected *Tmp, OldSize.i
 
  Select msg
    Case #UCM_PROCESSDATA
     
      If *Buffer = #Null
        *Buffer = AllocateMemory(P2)
        If *Buffer = #Null
          ProcedureReturn 0
        EndIf
        OldSize = 0
      Else
        OldSize = MemorySize(*Buffer)
        *Tmp = ReAllocateMemory(*Buffer, OldSize + P2)
        If *Tmp = #Null
          FreeMemory(*Buffer)
          *Buffer = #Null
          ProcedureReturn 0
        EndIf
        *Buffer = *Tmp
      EndIf
     
      CopyMemory(P1, *Buffer + OldSize, P2)
     
      Debug MemorySize(*Buffer)
      If CatchImage(0, *Buffer, MemorySize(*Buffer))
        If IsImage(0)
          Debug "YEAH!!!"
          FreeMemory(*Buffer)
          *Buffer = #Null
        EndIf
      EndIf
     
      ProcedureReturn #RAR_CONTINUE
  EndSelect
 
EndProcedure


Bernd


Top
 Profile  
 
 Post subject: Re: Merging Memory
PostPosted: Sun May 13, 2012 5:06 pm 
Offline
User
User

Joined: Sat Oct 09, 2010 3:47 am
Posts: 74
infratec,

I want to know what you know...

I study, read everythihng I can - take apart code written by people of your calibre, and I still have so much too learn - but it's a fun road :)

Thank you for taking your time to (help doesn't seem to cover it) give me the answer. I'm inspired to work harder!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: BASICBasher, Exabot [Bot], Fred and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye