flat assembler
Message board for the users of flat assembler.

Index > Windows > Best API for memory allocation?

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
GlobalAlloc, VirtualAlloc or HeapAlloc? Confused
Post 14 Feb 2008, 16:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
It depends. Big chunks of memory or small? Do you want it to be relocatable? Do you need it to be non-pageable? Do you need it aligned in a particular way? Do you want to be able to resize it?
Post 14 Feb 2008, 16:41
View user's profile Send private message Visit poster's website Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
I want simple allocation like malloc. For buffers.
Maybe malloc itself? From msvcrt.dll?
Post 14 Feb 2008, 16:50
View user's profile Send private message Reply with quote
asmhack



Joined: 01 Feb 2008
Posts: 431
asmhack
OzzY wrote:
GlobalAlloc, VirtualAlloc or HeapAlloc? Confused


if you need just simple allocation for buffers use GlobalAlloc imho
and if you want to save one byte use LocalAlloc as in the linear win32 api environment, there is no difference between the local heap and the global heap
Post 14 Feb 2008, 16:56
View user's profile Send private message Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
I wonder how this works:
Code:
my_buffer: times 2048 db ?
    


If I don't use the buffer it will take 0 byte in the code. But if I use it I actually have the space to store bytes. How does it work? Confused
Post 14 Feb 2008, 17:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
The PE file format allows one to define a segment larger then the on-disc initialised data. The .exe loader will allocate the extra space at runtime.
Post 14 Feb 2008, 17:32
View user's profile Send private message Visit poster's website Reply with quote
asmhack



Joined: 01 Feb 2008
Posts: 431
asmhack
OzzY wrote:
I wonder how this works:
Code:
my_buffer: times 2048 db ?
    



why not just
Code:
my_buffer rb 2048
    

?
Post 14 Feb 2008, 18:05
View user's profile Send private message Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
i use virtualalloc or smtimes ntallocatevirtualmemory.
Post 14 Feb 2008, 19:10
View user's profile Send private message Reply with quote
itsnobody



Joined: 01 Feb 2008
Posts: 93
Location: Silver Spring, MD
itsnobody
Don't use GlobalAlloc, it's supposed to be slow according to the Windows SDK documentation:
"Note The global functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE, the clipboard functions, and OLE data objects."

I would use HeapAlloc for smaller allocations and VirtualAlloc for bigger ones, though I prefer HeapAlloc overall...

It's useful for strings and reading files...although rb is probably faster it'll bloat up the file size, and only works if you know the max size you want, heapAlloc with HEAP_ZERO_MEMORY will start off with zero memory and adjust as you need more...working for virtually any string size
Post 14 Feb 2008, 22:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
itsnobody: You must be reading some really old documentation. The Global/Local thing was only for Win16. In Win32 they are both the same and both use the heap to get memory, and in it's turn the heap uses VirtualAlloc.

In general, what asmhack said in the 3rd reply was good advice.
Post 14 Feb 2008, 22:40
View user's profile Send private message Visit poster's website Reply with quote
itsnobody



Joined: 01 Feb 2008
Posts: 93
Location: Silver Spring, MD
itsnobody
revolution wrote:
itsnobody: You must be reading some really old documentation. The Global/Local thing was only for Win16. In Win32 they are both the same and both use the heap to get memory, and in it's turn the heap uses VirtualAlloc.

In general, what asmhack said in the 3rd reply was good advice.



revolution,

The SDK isn't old, its coming from the latest Windows SDK Documentation....

It says it also here as well - http://msdn2.microsoft.com/en-us/library/aa366574.aspx

I wouldn't use GlobalAlloc, seems too slow, unless Microsoft is lying or something
Post 14 Feb 2008, 22:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
It also says this:
MSDN webpage thingy wrote:
Windows memory management does not provide a separate local heap and global heap.
Post 14 Feb 2008, 22:47
View user's profile Send private message Visit poster's website Reply with quote
itsnobody



Joined: 01 Feb 2008
Posts: 93
Location: Silver Spring, MD
itsnobody
revolution wrote:
It also says this:
MSDN webpage thingy wrote:
Windows memory management does not provide a separate local heap and global heap.


Well we could do a speed test to find if any of them is actually faster
Post 14 Feb 2008, 22:54
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
itsnobody wrote:
Well we could do a speed test to find if any of them is actually faster
Yes, that would be a good idea. I encourage you to do it to clear up this MSDN confusion.
Post 14 Feb 2008, 22:57
View user's profile Send private message Visit poster's website Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
After reading MSDN, I like LocalAlloc. It's easy (less args). Very Happy

But MSDN says:
Quote:

Note The local functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions.


Let the benchmarking begin! Laughing
Post 15 Feb 2008, 00:37
View user's profile Send private message Reply with quote
itsnobody



Joined: 01 Feb 2008
Posts: 93
Location: Silver Spring, MD
itsnobody
I did a speed test, and HeapAlloc is slightly faster than GlobalAlloc

But then I made the reserved bytes to be 0x7FFF8, and what happened was GlobalAlloc allocated up to around 17MB of space after 1000000 repetitions and stopped, but HeapAlloc seemed to allocate the actual space even with HEAP_ZERO_MEMORY set and it used up all my RAM

So I'm confused
Post 15 Feb 2008, 01:14
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
Paging! Turn off the disk paging when you do memory tests. You don't want Windows to swap it all to disk, that would just be silly.
Post 15 Feb 2008, 01:20
View user's profile Send private message Visit poster's website Reply with quote
itsnobody



Joined: 01 Feb 2008
Posts: 93
Location: Silver Spring, MD
itsnobody
Ah, thanks revolution, that was the problem, I accidentally had paging on in the heap

But with paging off it works a lot better, it also uses about 30k less memory than GlobalAlloc

As for the speed, allocating 0x7FFF8 bytes with GlobalAlloc takes around 9300 milliseconds and with HeapAlloc it takes around 7400 milliseconds after 100,000 repetitions
Post 15 Feb 2008, 01:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
Good. And for LocalAlloc?
Post 15 Feb 2008, 01:34
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
I found this in the SDK:
Quote:
There is no difference between memory allocated from a private heap and that allocated by using the standard allocation functions (GlobalAlloc, LocalAlloc, malloc, and so on)
The left hand is not sure what the right hand is doing in MS.
Post 15 Feb 2008, 01:37
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
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
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.