Page 1 of 2

Size of memory block

Posted: Fri Feb 18, 2005 8:18 am
by Lebostein
Hi,

i will calculate the size of an allocated memory block. I found "GlobalSize()" in API - under Windows 98 the function returns NULL :( . Under XP no problems:

Code: Select all

*adress_bas = AllocateMemory(20200)
*adress_api = GlobalAlloc_(#GMEM_FIXED, 20200)

Debug GlobalSize_(*adress_bas) ; = 20200 (under Win98 = 0 !!!)
Debug GlobalSize_(*adress_api) ; = 20200

Debug LocalSize_(*adress_bas) ; = 20200 (under Win98 = 0 !!!)
Debug LocalSize_(*adress_api) ; = 20200 
How i can calculate the size of an memory block (created with PB) in a different way?

Posted: Fri Feb 18, 2005 1:17 pm
by Fred
GlobalSize() shouldn't work even on XP.. Here is the right way to do it:

Code: Select all

a = AllocateMemory(1001)

PB_Memory_Heap.l = 0

!extrn _PB_Memory_Heap
!mov eax,[_PB_Memory_Heap]
!mov dword [v_PB_Memory_Heap],eax

Debug PB_Memory_Heap

Debug HeapSize_(PB_Memory_Heap, 0, a)

Posted: Fri Feb 18, 2005 3:06 pm
by Lebostein
Thank you!

Can I use "GetProcessHeap_()" instead of this small ASM-Routine?

Posted: Fri Feb 18, 2005 3:20 pm
by Fred
no, it's not the process heap, but a special one for the memory commands.

Posted: Fri Feb 25, 2005 11:28 am
by Lebostein
Ok! Thanks.

Can you implement this routine in PB alias "MemorySize(*MemoryID)", for example?

Posted: Fri Feb 25, 2005 12:09 pm
by Polo
Yes, this would be a good idea ;)

Posted: Fri Feb 25, 2005 12:54 pm
by GedB
I'd like this too.

Posted: Fri Feb 25, 2005 2:53 pm
by Rescator
That would definetly be great!
And since PureBasic keeps track internaly of the memory allocation sizes anyway,
a simple memory size command should be pretty easy to add (I hope).
It would certainly reduce the amount of variables since you don't have to keep track of the size yourself.

Posted: Sat Feb 26, 2005 8:55 am
by gnozal
Me too :wink:

Posted: Sat Feb 26, 2005 2:56 pm
by BinoX
*Jumps on the wagon*

Yep, me too :lol:

Posted: Fri Dec 09, 2005 10:23 am
by nco2k
dito. :wink:

c ya,
nco2k

Posted: Sat Dec 10, 2005 3:15 pm
by Rescator
Hmm odd! GetProcessHeap_() works here, (PB 3.94, WinXP)

i.e:

Code: Select all

Procedure.l MemorySize(*memptr)
 ProcedureReturn HeapSize_(GetProcessHeap_(),#Null,*memptr)
EndProcedure

*mem=AllocateMemory(2002)
If *mem=0
 Debug "ReAlloc Failed"
 End
EndIf

*tmp=ReAllocateMemory(*mem,3003)
If *tmp<>0
 *mem=*tmp
Else
 Debug "ReAlloc Failed"
 End
EndIf

Debug MemorySize(*mem)
Seem to work ok, at least here!

Posted: Sat Dec 10, 2005 3:28 pm
by Dr. Dri
doesn't work with win 98 se (PB 3.94)

Dri ;)

Posted: Sat Dec 10, 2005 4:03 pm
by Rescator
Hmm! Interesting. some difference in the WinAPI on different windows versions vs PB ?

Posted: Sat Dec 10, 2005 4:23 pm
by Fred
I don't know why it work at all, as it's definitely not the process heap.