When you want to use a block of RAM, what is the proper way to reserve memory for large blocks?

If I do this

BlockRAM DB Dup (20000) ;(not sure of the correct syntax)

where is this RAM located? And what kind of limit do you have?

Wondering if this would be considered stack RAM

I want to reserve a "dynamic" block of RAM. Originally it will start out at 1 DWORD and I would like it to grow and shrink depending on what I need possibly up to 10 Megs if need be

need something similar to the CArray of MFC if that helps


thanx
Posted on 2002-01-04 14:34:47 by Rockinronstar
Hi Rock,

If you declare it on the .data or .data? section the variable won't be allocated to the stack but in the process heap (correct me if i'm wrong).
LOCAL defined variables (after a new procedure) are allocated on the stack.

Considering your case, i think the best method would be to use HeapAlloc/HeapRealloc functions.

A simple and quick example from one of my prog :



Invoke GetProcessHeap
Invoke HeapAlloc, eax, HEAP_ZERO_MEMORY, BufferSize
mov dword ptr MemPointer, eax


it will allocate 'BufferSize' (an equate there, don't mind) bytes on the heap and initializes this memory range filled by zeroes.
It' just an example. I needed that the buffer to be zeroed, but you have others parameters.

The usage is very similar to the C malloc...

Quick reference : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/memman_6krn.asp

I hope this answer to your questions !
Posted on 2002-01-04 14:52:55 by JCP
You can dinamically allocate, resize and free memory with functions like:

GetProcessHeap
HeapAlloc
HeapReAlloc
HeapFree

or Global* couterparts, but slower and discouraged by MSDN help.

Edit:
Too bad Radiosys, I was chatting on IRC while writing this post. You was faster :).
Posted on 2002-01-04 15:00:37 by LuHa
thanx guys, that sounds clean, think I'll go with the HeapAlloc idea

also, this is prob a stupid question, but I have yet to use it

mov dword ptr MemPointer, eax

just want to clarify the exact meaning of the above statement

does this move the value in EAX into the memory location pointed to my MemPointer?


in Z80 you do this for var indirect addressing

ld , a

put value of A in address pointed to by memPointer

wondering this also means this ->

mov dword ptr MemPointer, eax is equal to ->
mov , eax


thanx
Posted on 2002-01-04 15:11:58 by Rockinronstar
The x86 does not do data indirection via memory. (Jump indirection is a different matter.)

So, in MASM and TASM, the following three instructions produce exactly the same code.

mov dword ptr MemPointer, eax
mov , eax
mov MemPointer,eax

They store the value in EAX in the DWORD that's labelled MemPointer.
Posted on 2002-01-04 17:20:12 by tank
LuHa, i was chatting too. :tongue:

But it is a good thing that we have given the same answer. That confirms that it is a good solution ;)
Posted on 2002-01-05 09:18:12 by JCP
A Rose is a rose is a ROse

I get it

lol
Posted on 2002-01-05 12:44:13 by Rockinronstar
You can use PTR to override the default "length" of an operand. For example if:

mystr db "byte string"

Masm will bitch if you

mov mystr,eax

since you are trying to move a dword to a byte size field. So if you really want to, use:

mov dword ptr mystr,eax

:)
Posted on 2002-01-05 17:29:29 by S/390
Hi all,


You can dinamically allocate, resize and free memory with functions like:

GetProcessHeap
HeapAlloc
HeapReAlloc
HeapFree

What disadvantages does VirtualAlloc & Co. have vs HeapAlloc & Co.?


or Global* couterparts, but slower and discouraged by MSDN help.

Doesn't GlobalAlloc allocate shared memory between processes? It should be used only if one needs to share memory between processes then, otherwise it will result in a waste of page directories ;)

Greets,
Maverick
Posted on 2002-01-21 03:18:19 by Maverick
I have changed my complete programm from GlobalAlloc to HeapAlloc.

But I only changed the API calls and now my programm quits immediately after starting.

Do I also have to change the memory calls?
Posted on 2002-01-21 04:43:53 by TCT
Did you get the process Heap ? Are you passing it to the HeapAlloc function ?
Posted on 2002-01-21 05:28:19 by JCP
Yes, I did.

But maybe is the HeapLoc call the problem

-Yep, now it works.

Now I've to test weather the programm goes laggy after a loong time.
Posted on 2002-01-21 06:05:54 by TCT

What disadvantages does VirtualAlloc & Co. have vs HeapAlloc & Co.?

VirtualAlloc works at the page level, thus all allocations are rounded
up to 4096 bytes. HeapAlloc and co uses VirtualAlloc to alloc chunks and
manage smaller allocations inside.

VirtualAlloc allows you control over the page protections and such. And
since it's at a lower level than the other memory management functions,
there is less overhead - which makes VirtualAlloc good if you deal primarily
with large blocks of memory.


Doesn't GlobalAlloc allocate shared memory between processes? It should be used only if one needs to share memory between processes then, otherwise it will result in a waste of page directories

Nope. That was in win3.x. Local/GlobalAlloc is exactly the same under win32
(both NT and 9x), and they both thunk down to HeapAlloc after a few checks
and parameter conversions. If you want to do process-shared memory, you'll
probably best off with memory mapped files.

As for the paging stuff... I believe each process has it's own set of page
tables (with CR3 reload) on NT kernels, while 9x works by patching a single
page table.
Posted on 2002-01-21 06:43:12 by f0dder


VirtualAlloc works at the page level, thus all allocations are rounded up to 4096 bytes. HeapAlloc and co uses VirtualAlloc to alloc chunks and manage smaller allocations inside.

VirtualAlloc allows you control over the page protections and such. And since it's at a lower level than the other memory management functions, there is less overhead - which makes VirtualAlloc good if you deal primarily with large blocks of memory.


Thx for the info.

Greets,
Maverick
Posted on 2002-01-21 08:17:18 by Maverick