Size of memory block

Windows specific forum
Lebostein
Addict
Addict
Posts: 841
Joined: Fri Jun 11, 2004 7:07 am

Size of memory block

Post 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?
Fred
Administrator
Administrator
Posts: 18360
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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)
Lebostein
Addict
Addict
Posts: 841
Joined: Fri Jun 11, 2004 7:07 am

Post by Lebostein »

Thank you!

Can I use "GetProcessHeap_()" instead of this small ASM-Routine?
Fred
Administrator
Administrator
Posts: 18360
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

no, it's not the process heap, but a special one for the memory commands.
Lebostein
Addict
Addict
Posts: 841
Joined: Fri Jun 11, 2004 7:07 am

Post by Lebostein »

Ok! Thanks.

Can you implement this routine in PB alias "MemorySize(*MemoryID)", for example?
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Yes, this would be a good idea ;)
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post by GedB »

I'd like this too.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Me too :wink:
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
BinoX
User
User
Posts: 46
Joined: Mon Jan 31, 2005 11:57 am

Post by BinoX »

*Jumps on the wagon*

Yep, me too :lol:
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

dito. :wink:

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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!
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

doesn't work with win 98 se (PB 3.94)

Dri ;)
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Hmm! Interesting. some difference in the WinAPI on different windows versions vs PB ?
Fred
Administrator
Administrator
Posts: 18360
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

I don't know why it work at all, as it's definitely not the process heap.
Post Reply