How to have the memory size

Everything else that doesn't fall into one of the other PB categories.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

How to have the memory size

Post by Polo »

How can I have the memory size in another way than using this api command :
GlobalSize_(mem)
Because it doesn't seem to work perfectly with the new memory commands of the beta 1.89
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

Code: Select all

Procedure GetFreeRAM()
  Info.MEMORYSTATUS
  Info\dwLength = SizeOf(MEMORYSTATUS)
  GlobalMemoryStatus_(@Info)
  ProcedureReturn Info\dwAvailPhys
EndProcedure
Procedure GetTotalRAM()
  Info.MEMORYSTATUS
  Info\dwLength = SizeOf(MEMORYSTATUS)
  GlobalMemoryStatus_(@Info)
  ProcedureReturn Info\dwTotalPhys
EndProcedure
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

thanks, but I'm not searching for that (I certainly forgot to tell something, sorry :D).
What I want is to do that :

mem=AllocateMemory(12)
mem=ReallocateMemory(mem,16)

For example, and now :

size=functionwhichreturnthesize(mem)

size is here supposed to be 16.
And I don't want to use the Globalsize function.
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

IMHO it should be HeapSize_() as PB uses just that, a heap.
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

Quick and Dirty.....

Code: Select all

mem=AllocateMemory(12)
mem=ReAllocateMemory(mem,16) 

hndl=getprocessheap_()

Debug heapsize_(hndl,0,mem)
Paid up PB User !
freak
PureBasic Team
PureBasic Team
Posts: 5947
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Yes.. very dirty, because PB doesn't use the process heap. It creates it's own.
I wonder why this works though.

Timo
quidquid Latine dictum sit altum videtur
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

Maybe.... from the API doc...

The GetProcessHeap function obtains a handle to the heap of the calling process. This handle can then be used in subsequent calls to the heap functions. !!??
Paid up PB User !
Kris_a
User
User
Posts: 92
Joined: Sun Feb 15, 2004 8:04 pm
Location: Manchester, UK

Post by Kris_a »

would it be possible to use GlobalAlloc_ instead of PB's built in memory allocators?
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

You can use any part of the API :) I think 8O
Paid up PB User !
freak
PureBasic Team
PureBasic Team
Posts: 5947
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Yes I know the GetProcessHeap() function. But this function returns the heap, that
Windows has created for the process. PB however creates a seperate one.

GetProcesHeap() is intended to be used if you only need a few memory blocks,
and don't want to create an extra heap for your application.

See this:

Code: Select all

Procedure PB_Heap()  
  !mov eax, dword [_PB_MemoryBase]   ; this is the real PB memory heap
  ProcedureReturn
EndProcedure

Debug GetProcessHeap_()
Debug PB_Heap()
I guess this is an undocumented feature of windows. (that you can use 2 different heap handles and both work)
But i wouldn't count on it, who knows how different windows versions handle this.

Better use the above PB_Heap() procedure.

Timo
quidquid Latine dictum sit altum videtur
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

Well this produces the same end-result.......

Code: Select all

Procedure PB_Heap() 
  !MOV eax, dword [_PB_MemoryBase]   ; this is the real PB memory heap
  ProcedureReturn
EndProcedure

mem=AllocateMemory(12)
mem=ReAllocateMemory(mem,16) 

PB_hndl=PB_Heap()
hndl=getprocessheap_()

Debug heapsize_(hndl,0,mem)
Debug heapsize_(PB_hndl,0,mem)
Maybe PB requests memory from Windoze, reserves some for itself..... and then allocates from that point onwards....... so we are talking about the same memory at the end of the day..... and two different ways of acessing it..... through PB's 'stack' or through API.....

??!! Maybe Fred can answer this ??
Paid up PB User !
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Fred should do himself a function that return memory size, because, I don't know why, but in some cases, the api command doesn't work and return -1
freak
PureBasic Team
PureBasic Team
Posts: 5947
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Well this produces the same end-result.......
That's what i am telling you. It produces the same end result.
But this is a not documented behaviour. (of Windows), so you can't
be 100% sure of it.
Polo wrote:Fred should do himself a function that return memory size, because, I don't know why, but in some cases, the api command doesn't work and return -1
See, there you have it. It doesn't always work.
Maybe PB requests memory from Windoze, reserves some for itself..... and then allocates from that point onwards....... so we are talking about the same memory at the end of the day..... and two different ways of acessing it..... through PB's 'stack' or through API.....
Maybe you should read a little documentation on memory management... what you say makes no sense :wink:

PB's memory management is a direct wrapper for the windows API here,
there is nothing misterious going on.

It's just a M$ *feature*, nothing more.
(I'd still stick with _PB_MemoryBase though.. just to be sure)


Timo
quidquid Latine dictum sit altum videtur
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

In fact it works every time, it's just me who is stupid :) I was doing that :

mem=allocatememory(4)
pokel(mem,4)
pokel(mem+4,1)

I was using more memory than the allowed one, and it's why it was returning -1 :D
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Post by dontmailme »

freak wrote:
Polo wrote:Fred should do himself a function that return memory size, because, I don't know why, but in some cases, the api command doesn't work and return -1
freak wrote: See, there you have it. It doesn't always work.
It does, see polo's corrected statement ;)
freak wrote: Maybe you should read a little documentation on memory management... what you say makes no sense :wink:
I see no point, you obviously have read this and you don't understand this behaviour..... 8O
freak wrote: PB's memory management is a direct wrapper for the windows API here,
there is nothing misterious going on.
That must be why it works then !

I think maybe Fred should comment on this as he obviously knows what he's doing :)
Paid up PB User !
Post Reply