flat assembler
Message board for the users of flat assembler.

Index > Main > How to alloc heap space manually?

Author
Thread Post new topic Reply to topic
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 14 Jan 2010, 18:07
with no API (VirtualAlloc, HeapAlloc, etc).

_________________
Sorry if bad english.
Post 14 Jan 2010, 18:07
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 14 Jan 2010, 19:22
Be more specific on that. Where are you supposed to get memory to allocate from? Some uninitialized section + custom allocator?
Post 14 Jan 2010, 19:22
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 14 Jan 2010, 19:26
If you just want to do your own memory management you can use the end of your data section.
Code:
.data
...
MAX_HEAP_SIZE = (1024 * 1024)
MyHeap rb MAX_HEAP_SIZE
    

Then you could make your own allocate and free functions that will process and return addresses from your flat memory space. Rolling your own memory management routines will take some time and testing.
Post 14 Jan 2010, 19:26
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 14 Jan 2010, 19:41
But and in the runtime? Or my app must separete a space anyway (like you shown, r22)? (I dont think so, bc if you use a HeapAlloc you dont need separete space)

baldr, I need to read some strings (file paths (unknow quantity)), And I'd like to alloc some space to store them. But I'd like to alloc the exactly size of the strings to don't spend memory.
Post 14 Jan 2010, 19:41
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2465
Location: Bucharest, Romania
Borsuc 14 Jan 2010, 23:35
I think the stack is the only option you have without APIs. So you'll have to use the stack for it.

Define a big stack reserve (this does not allocate memory, it only reserves address space -- make it as big as you need to, you can't have larger buffer than this!), then everytime you "touch" a 4K memory page on the stack it allocates it -- this means if you put something or access that particular stack reserved page, it gets allocated.

so conclusion:

small stack commit size, big stack reserve size.

Other than that, what's wrong with HeapAlloc?
Post 14 Jan 2010, 23:35
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 15 Jan 2010, 00:16
Quote:

Other than that, what's wrong with HeapAlloc?

Or using VirtualAlloc and then implementing the heap over the returned memory (which can also reserve memory space without actually occupying it in memory nor in swap file).

Teehee, also note that Windows can't allocate less than 4KB (or the page size of the system) even with HeapAlloc. Of course, HeapAlloc will use those 4KB chunks to allocate as much as possible so a single HeapAlloc of, say, 16 bytes won't deplete the entire 4KB chunk.
Post 15 Jan 2010, 00:16
View user's profile Send private message Reply with quote
Teehee



Joined: 05 Aug 2009
Posts: 570
Location: Brazil
Teehee 15 Jan 2010, 09:08
Quote:
Other than that, what's wrong with HeapAlloc?

Nothing. I just want to know how to do that manually Smile

Thanks for tips guys.

PS: Everything that goes in 'data section' is in heap space, right?
Post 15 Jan 2010, 09:08
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 15 Jan 2010, 11:46
No. Everything that is reserved (rb, rw, rd, etc.) will not be part of the .exe, but everything that is defined (db, dw, dd, etc.) will be.


Also, from the manual:
Quote:
heap directive chooses the size of heap for Portable Executable, value of heap reserve size should follow, optionally value of heap commit separated with comma can follow. When no heap is defined, it is set by default to size of 65536 bytes, when size of heap commit is unspecified, it is by default set to zero.
Post 15 Jan 2010, 11:46
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 15 Jan 2010, 19:32
ManOfSteel,

Not exactly. Everything uninitialized (rb/db ?) before last initialized item in section still appears in PE .Exe file, only trailing uninitialized items don't (remember file alignment though).

_____
Teehee,

Windows creates standard heap for process (even if you specify 0/0 as heap commit/reserve); you may use GetProcessHeap() to obtain it's handle. This heap (and any you create yourself) resides outside any sections.

From where those paths are coming? HeapAlloc() / lstrlen() / HeapReAlloc() can be quite effective.

_____
Borsuc,

Stack allocation has one drawback: the technique Windows uses to commit stack pages (guard pages) requires them to be touched sequentially when allocated.
Post 15 Jan 2010, 19:32
View user's profile Send private message Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 15 Jan 2010, 21:18
baldr wrote:
only trailing uninitialized items don't

Yes, that's more precise and correct than what I said and it's exactly what I meant. I was thinking of r22's post when I posted my reply.
Post 15 Jan 2010, 21:18
View user's profile Send private message 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-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.