flat assembler
Message board for the users of flat assembler.
Index
> Windows > Best API for memory allocation? Goto page 1, 2 Next |
Author |
|
OzzY 14 Feb 2008, 16:21
GlobalAlloc, VirtualAlloc or HeapAlloc?
|
|||
14 Feb 2008, 16:21 |
|
OzzY 14 Feb 2008, 16:50
I want simple allocation like malloc. For buffers.
Maybe malloc itself? From msvcrt.dll? |
|||
14 Feb 2008, 16:50 |
|
asmhack 14 Feb 2008, 16:56
OzzY wrote: GlobalAlloc, VirtualAlloc or HeapAlloc? 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 |
|||
14 Feb 2008, 16:56 |
|
OzzY 14 Feb 2008, 17:28
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? |
|||
14 Feb 2008, 17:28 |
|
revolution 14 Feb 2008, 17:32
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.
|
|||
14 Feb 2008, 17:32 |
|
asmhack 14 Feb 2008, 18:05
OzzY wrote: I wonder how this works: why not just Code:
my_buffer rb 2048
? |
|||
14 Feb 2008, 18:05 |
|
asmrox 14 Feb 2008, 19:10
i use virtualalloc or smtimes ntallocatevirtualmemory.
|
|||
14 Feb 2008, 19:10 |
|
itsnobody 14 Feb 2008, 22:28
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 |
|||
14 Feb 2008, 22:28 |
|
revolution 14 Feb 2008, 22:40
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. |
|||
14 Feb 2008, 22:40 |
|
itsnobody 14 Feb 2008, 22:42
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. 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 |
|||
14 Feb 2008, 22:42 |
|
revolution 14 Feb 2008, 22:47
It also says this:
MSDN webpage thingy wrote: Windows memory management does not provide a separate local heap and global heap. |
|||
14 Feb 2008, 22:47 |
|
itsnobody 14 Feb 2008, 22:54
revolution wrote: It also says this: Well we could do a speed test to find if any of them is actually faster |
|||
14 Feb 2008, 22:54 |
|
revolution 14 Feb 2008, 22:57
itsnobody wrote: Well we could do a speed test to find if any of them is actually faster |
|||
14 Feb 2008, 22:57 |
|
OzzY 15 Feb 2008, 00:37
After reading MSDN, I like LocalAlloc. It's easy (less args).
But MSDN says: Quote:
Let the benchmarking begin! |
|||
15 Feb 2008, 00:37 |
|
itsnobody 15 Feb 2008, 01:14
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 |
|||
15 Feb 2008, 01:14 |
|
revolution 15 Feb 2008, 01:20
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.
|
|||
15 Feb 2008, 01:20 |
|
itsnobody 15 Feb 2008, 01:30
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 |
|||
15 Feb 2008, 01:30 |
|
revolution 15 Feb 2008, 01:34
Good. And for LocalAlloc?
|
|||
15 Feb 2008, 01:34 |
|
revolution 15 Feb 2008, 01:37
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) |
|||
15 Feb 2008, 01:37 |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.