Re: Feature Request 'SaveMemory' to Disk Drive
Posted: Tue Jun 28, 2022 3:29 pm
My test was with 1GB too (PB 6 32)
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
EnableExplicit
; The maximum value is: 2 GiB - 1 Byte = (2 * 1024 * 1024 * 1024 - 1) Bytes = (2^31 - 1) Bytes = 2147483647 Bytes
#MAX_CHUNK_SIZE = 2048 * 1024 * 1024 - 1
Procedure SaveMemory(*Memory, FileName.s, Size = #PB_Ignore)
Protected result.i, file.i
Protected chunkSize.i, writtenBytes.i
file = CreateFile(#PB_Any, FileName)
If Not file
DebuggerError("Could not open file for writing: " + FileName)
ProcedureReturn #False
EndIf
If Size = #PB_Ignore
Size = MemorySize(*Memory)
EndIf
While Size
If Size > #MAX_CHUNK_SIZE
chunkSize = #MAX_CHUNK_SIZE
Else
chunkSize = Size
EndIf
writtenBytes = WriteData(file, *Memory, chunkSize)
Debug writtenBytes
If Not writtenBytes
result = #False
DebuggerError("Could not write " + chunkSize + " bytes into file.")
Break
EndIf
Size - writtenBytes
*Memory + writtenBytes
Wend
CloseFile(file)
ProcedureReturn result
EndProcedure
Define *mem = AllocateMemory(4 * 1024 * 1024 * 1024)
SaveMemory(*mem, "/root/nicolas/tmp/purebasic_memory_test.dat")
FreeMemory(*mem)
Yes, It worked perfectly now with #MAX_CHUNK_SIZE = 2048 * 1024 * 1024 - 1
Code: Select all
2147483647
2147483647
2
Candidate to Tricks 'n' Tips forum?
Yes, try to change the chunks size and monitoring the disk efficiency with this app:
I don't think it can be changed easily because the underlying functions by itself uses 32 bit parameters. See fwrite which uses `size_t` which in turn usually is `unsigned int` and `int` usually only has 32 bits.Bisonte wrote: Wed Jun 29, 2022 7:10 am However, I now see that WriteData() has a 2GB limit.
It looks like the function works internally with longs (so a 32 bit limit).
That is maybe a bug report for the x64 version of pb ?
it's a 'bit' limiting/obsolete for today's times ...NicTheQuick wrote: Wed Jun 29, 2022 9:43 am I don't think it can be changed easily because the underlying functions by itself uses 32 bit parameters. See
But why? It's not limiting. It's quite unusual to write such big memory chunks into a file at once. Just split it up in small chunks and all is good.Simo_na wrote: Wed Jun 29, 2022 11:16 am it's a 'bit' limiting/obsolete for today's times ...
can be do with the Assembler ???
NicTheQuick wrote: Wed Jun 29, 2022 11:28 am But why? It's not limiting. It's quite unusual to write such big memory chunks into a file at once. Just split it up in small chunks and all is good.
No, no, not assembler. This will not work with the C compiler (at least for now). Also I don't see a problem to chunk it. The result is the same.Simo_na wrote: Wed Jun 29, 2022 11:16 amit's a 'bit' limiting/obsolete for today's times ...NicTheQuick wrote: Wed Jun 29, 2022 9:43 am I don't think it can be changed easily because the underlying functions by itself uses 32 bit parameters. See
can be do with the Assembler ???
Keep in mind that your HDD/SSD is way slower than your program code. Also in the background data gets not transferred to disk in the same moment you call WriteData(). Instead it goes into a memory cache and gets flushed to disk when your kernel thinks it is the time to do so.jacdelad wrote: Wed Jun 29, 2022 4:30 pm That's suspiciously close to two 2MB and somehow I doubt it, because this way the loop is run 1000 times for 2 GB, instead of once.