flat assembler
Message board for the users of flat assembler.

Index > Windows > Allocate and Free memory dynamically in the Heap

Author
Thread Post new topic Reply to topic
jorge



Joined: 09 Apr 2008
Posts: 9
jorge 29 May 2008, 15:24
Hi,sorry for may english. I'm doing a compiler for Languaje that have this posibility contruccion in this gramar

String[] str_arr={"fasm","nasm","masm"};

I need to generate a PE format for any program in this Languaje and I need to allocate dinamic(maybe using the heap) memory for this array. The treatment of array is with pointer and his element are pointer that point to each content beteween the key of array, i will explain better:

str_arr->pointer1 [pointer1]="fasm"
pointer2 [pointer2]="nasm"
pointer3 [pointer3]="masm"

str_arr point to pointer1, pointer2 is behind pointer1 in memory, pointer3 is behind pointer2 in memory.In other words array of pointer.
Other things is I have to free this memory automatically(I have the AST of the program and whit this i know when free this memory).I dont Know what api use and how to use it in FASM. I'm student of Science of Computer and this is part of may thesis if any person can help me i'm would be very grateful
Post 29 May 2008, 15:24
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20339
Location: In your JS exploiting you and your system
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    
Post 29 May 2008, 15:27
View user's profile Send private message Visit poster's website Reply with quote
jorge



Joined: 09 Apr 2008
Posts: 9
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.
Post 29 May 2008, 16:52
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20339
Location: In your JS exploiting you and your system
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.
Post 29 May 2008, 16:57
View user's profile Send private message Visit poster's website Reply with quote
AlexP



Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
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.
Post 30 May 2008, 14:04
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: 20339
Location: In your JS exploiting you and your system
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.
Post 31 May 2008, 03:31
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 15 Jun 2008, 18:52
Post 15 Jun 2008, 18:52
View user's profile Send private message Reply with quote
barmentalisk



Joined: 06 Sep 2005
Posts: 36
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?
Post 16 Jun 2008, 09:41
View user's profile Send private message Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav 19 Jun 2008, 05:57
correct is to use HeapAlloc and HeapFree
Post 19 Jun 2008, 05:57
View user's profile Send private message Reply with quote
barmentalisk



Joined: 06 Sep 2005
Posts: 36
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 ?
Post 19 Jun 2008, 11:46
View user's profile Send private message Reply with quote
AlexP_temp



Joined: 19 Jun 2008
Posts: 2
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).
Post 19 Jun 2008, 20:43
View user's profile Send private message Reply with quote
barmentalisk



Joined: 06 Sep 2005
Posts: 36
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.
Post 19 Jun 2008, 21:38
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4042
Location: vpcmpistri
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
Post 19 Jun 2008, 21:57
View user's profile Send private message Visit poster's website Reply with quote
barmentalisk



Joined: 06 Sep 2005
Posts: 36
barmentalisk 20 Jun 2008, 12:31
bitRAKE, thank you, I appreciate your help!
Post 20 Jun 2008, 12:31
View user's profile Send private message Reply with quote
barmentalisk



Joined: 06 Sep 2005
Posts: 36
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?
Post 03 Oct 2008, 11:14
View user's profile Send private message Reply with quote
barmentalisk



Joined: 06 Sep 2005
Posts: 36
barmentalisk 03 Oct 2008, 11:34
Oh, I found:
Quote:

The largest memory block that can be allocated from the heap is slightly less than 0x7FFF8 bytes.

Does this mean, size of max. block is not constant ?
So I understand all, but how does it work.
Post 03 Oct 2008, 11:34
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4042
Location: vpcmpistri
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
Post 05 Oct 2008, 01:01
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:  


< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.