flat assembler
Message board for the users of flat assembler.
Index
> Windows > Allocate and Free memory dynamically in the Heap |
Author |
|
revolution 29 May 2008, 15:27
Erm, I think you are asking how to allocate and free memory?
Here is a list of the common memory functions, choose which are the ones you need: Code: CopyMemory FillMemory GetProcessHeap GetProcessHeaps GlobalAlloc GlobalDiscard GlobalFlags GlobalFree GlobalHandle GlobalLock GlobalMemoryStatus GlobalReAlloc GlobalSize GlobalUnlock HeapAlloc HeapCompact HeapCreate HeapDestroy HeapFree HeapLock HeapReAlloc HeapSize HeapUnlock HeapValidate HeapWalk IsBadCodePtr IsBadHugeReadPtr IsBadHugeWritePtr IsBadReadPtr IsBadStringPtr IsBadWritePtr LocalAlloc LocalDiscard LocalFlags LocalFree LocalHandle LocalLock LocalReAlloc LocalSize LocalUnlock MoveMemory VirtualAlloc VirtualAllocEx VirtualFree VirtualFreeEx VirtualLock VirtualProtect VirtualProtectEx VirtualQuery VirtualQueryEx VirtualUnlock ZeroMemory |
|||
29 May 2008, 15:27 |
|
jorge 29 May 2008, 16:52
Precisely i need to know wich of those function is the best for the contruccion that i show you (the array who you know the length and element in compiler time), and how I call it to allocate or free in FASM code.Please if you have any example, show me.
|
|||
29 May 2008, 16:52 |
|
revolution 29 May 2008, 16:57
HeapAlloc/HeapFree is designed for the purpose of allocating/freeing small amounts of memory.
But I recommend you check out the descriptions off all the allocation functions on MSDN to make sure you are using the proper APIs for your task. |
|||
29 May 2008, 16:57 |
|
AlexP 30 May 2008, 14:04
I use VirtualAlloc/VirtualFree for memory usage even in large amounts, and VirtualLock/VirtualUnlock may be useful.
ZeroMemory turns out to just be a macro in the C compiler I believe, maybe some others listed by Revolution too. |
|||
30 May 2008, 14:04 |
|
revolution 31 May 2008, 03:31
The Virtual* functions are good for large amounts of memory.
The Global* and Local* functions are aliases for each other in Win32. I've never used ZeroMemory, but it seems that under Win32 it is a macro for memset() and under Win64 is it an alias for RtlZeroMemory. |
|||
31 May 2008, 03:31 |
|
LocoDelAssembly 15 Jun 2008, 18:52
About the Is* functions see http://blogs.msdn.com/oldnewthing/archive/2006/09/27/773741.aspx
|
|||
15 Jun 2008, 18:52 |
|
barmentalisk 16 Jun 2008, 09:41
Could someone explain me, why MSDN say that GlobalUnlock Function must use an hMem param, which is "A handle to the global memory object. This handle is returned by either the GlobalAlloc or GlobalReAlloc function."
But in all examples (ICZELION's tuts and oth), I see usage of GlobalUnlock Function with param pMem (pointer to the first byte of the memory block), returned by GlobalLock. Which is correct? |
|||
16 Jun 2008, 09:41 |
|
Vasilev Vjacheslav 19 Jun 2008, 05:57
correct is to use HeapAlloc and HeapFree
|
|||
19 Jun 2008, 05:57 |
|
barmentalisk 19 Jun 2008, 11:46
Thank you, Vjacheslav.
Is using Heap Functions the best way for all purposes? At now I need just to copy text below 65535 bytes from/to editor (using WM_GETTEXT/WM_SETTEXT) to/from file. Should I use Heap Functions ? |
|||
19 Jun 2008, 11:46 |
|
AlexP_temp 19 Jun 2008, 20:43
If you're writing to/from a file, I would use the WriteFile/ReadFile API (I think those are the names).
|
|||
19 Jun 2008, 20:43 |
|
barmentalisk 19 Jun 2008, 21:38
Shure, I have to use theese API, but its param lpBuffer must be a pointer to a buffer with/for data. I thought I must allocate a buffer in memory for doing that, or am I mistaken?
for example to write file i need to do smth about that: invoke SendMessage,[hedit],WM_GETTEXT,MEMSIZE,lpBuffer invoke lstrlen,pmem invoke WriteFile,[hfile],pmem,eax,lpNumberOfBytesWritten,0 but before that I have to allocate memory for lpBuffer. |
|||
19 Jun 2008, 21:38 |
|
bitRAKE 19 Jun 2008, 21:57
Check out memory mapped files.
Code: invoke CreateFile,FILE.PATHNAME,GENERIC_READ or GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0 mov [FILE.HANDLE],eax invoke CreateFileMapping,[FILE.HANDLE],0,PAGE_READWRITE,0,0,0 mov [FILE.MEMMAP.HANDLE],eax invoke MapViewOfFile,[FILE.MEMMAP.HANDLE],FILE_MAP_WRITE,0,0,0 mov [FILE.DATA],eax test eax,eax je .bad _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
19 Jun 2008, 21:57 |
|
barmentalisk 20 Jun 2008, 12:31
bitRAKE, thank you, I appreciate your help!
|
|||
20 Jun 2008, 12:31 |
|
barmentalisk 03 Oct 2008, 11:14
Hi, people!
MSDN says: Quote: If the heap specified by the hHeap parameter is a "non-growable" heap, dwBytes must be less than 0x7FFF8. You create a non-growable heap by calling the HeapCreate function with a nonzero value. But I've made some experiments and got next: Max memory which I can allocate in non-growable heap is 0x7eff8. Trying to allocate 0x7eff9 and more returns error. Can someone confirm or deny that result? |
|||
03 Oct 2008, 11:14 |
|
barmentalisk 03 Oct 2008, 11:34
Oh, I found:
Quote:
Does this mean, size of max. block is not constant ? So I understand all, but how does it work. |
|||
03 Oct 2008, 11:34 |
|
bitRAKE 05 Oct 2008, 01:01
I've posted an example how to allocate as large a single block as windows will allow. Sorry, never worked with non-growable heaps.
_________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
05 Oct 2008, 01:01 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.