flat assembler
Message board for the users of flat assembler.

Index > Windows > Memory allocation problem

Author
Thread Post new topic Reply to topic
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
Hello,

I have made a program that outputs words, shows them in a list box and writes every one of them separately to a file, which is messy, wastes time and prevents me from doing more processing on the words, unless I read the output file back, but that would add even more complications.

My problem is that the words have no fixed length and there can be dozens, hundreds or thousands of them, so there is no way to know the final size of the output before the whole process is completely done.

What I want is to write the words somewhere in an allocated portion of the memory so that I can work on them easily without messing with files (at first) and then when the whole process is finished I will write the allocated portion to a file.

So my question is: how can I allocate a piece of memory and then grow it every time a word comes (what are the API I should use)?

Thank you for any help.
Post 16 Sep 2006, 09:22
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
You could use the functions in 'crtdll.dll', 'alloc, realloc, free', just don't forget to use 'cinvoke' for each of them.

perimeter's required for the above mentioned functions:

malloc memsize
realloc memblock, newsize
free memblock
Post 16 Sep 2006, 09:36
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
GetProcessHeap / HeapAlloc / HeapRealloc / HeapFree Wink
Post 16 Sep 2006, 10:08
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
You can use the Heap or Virtual functions, or the crtdll functions which are just wrappers for the former.

_________________
redghost.ca
Post 16 Sep 2006, 22:47
View user's profile Send private message AIM Address MSN Messenger Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
Hello,
the functions of crtdll.dll you are talking about, are they the same as GlobalAlloc, GlobalLock, GlobalReAlloc, etc...
What is the difference between these Global* Functions and the Heap functions?
Post 17 Sep 2006, 07:30
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/memory_management_functions.asp

copy&pasted from msdn:
Quote:
The following are the global and local functions. These 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 and the clipboard functions.
Post 17 Sep 2006, 09:53
View user's profile Send private message MSN Messenger Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
The global functions are a bit more involved to use, you have to: allocate, lock, USE, unlock, free. Virtual***** functions the same way, also many more parameters. For simple memory uses the CRT functions are the way to go. However, if you need more advanced control and protection over your memory then use the Virtual***** functions.
Post 17 Sep 2006, 13:29
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
yup, for simple malloc(), realloc(), free() use Heap* functions
Post 18 Sep 2006, 07:57
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel
okasvi,
Quote:
These functions are slower than other memory management functions and do not provide as many features.

The program could work on three files containing more than 8.400 words, that it processed one by one, letter by letter, in less than one second with an old 333MHz celeron. And I still have not optimized any code for speed. So speed-wise, I think it is more than satisfying.
I do not really need many features (if any). All I need is to allocate some memory for words, keep the words there for a few milliseconds, process them, write them to a file and free all the memory. That's it.


madmatt,
Quote:
The global functions are a bit more involved to use, you have to: allocate, lock, USE, unlock, free.

Basically, allocate, USE and free are enough. AFAIK, lock/unlock pair is not necessary if you use fixed memory chunks (GMEM_FIXED) since doing so will directly return a pointer to the allocated memory instead of a handle. It works perfectly like that, so far.
The USE part is not very cycle-consuming, since you can do it with two mov instructions (one to get the address of the pointer and one to read/write from/to the address) and when using heap or crt functions you still have to read from and/or write to your re/allocated memory, don't you?


And is it normal that a c library like crtdll.dll be faster than a system DLL like kernel32.dll? I always hear c libraries are slow, unoptimized and sometimes even buggy.
Post 19 Sep 2006, 13:12
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt
I haven't done any speed tests, so I don't know which is faster. Maybe you could do some tests and post the results? As far as the c library (crtdll.dll) being buggy, I've used the [malloc, free] functions on occasion and haven't had any problems yet.
Post 19 Sep 2006, 16:35
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Post 19 Sep 2006, 20:47
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number 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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.