Archived

This topic is now archived and is closed to further replies.

Ethereal Darkness

GlobalAlloc || malloc

Recommended Posts

Mayrel    348
I don''t know about speed differences, but malloc is portable. You might also want to find out about the ''new'' operator if you''re using C++.

All your bases belong to us (I know. It''s irony.)

Share this post


Link to post
Share on other sites
DeltaVee    138
Okay boys and girls, gather round the camp fire and let me tell you a story that will scare you $hitless.

Once upon a time intel CPU's where segmented (in fact they still are). Each segment was 64kb in size. So to access memory you needed two registers the Segment register and an other who's name escapes me for now.

So if you wanted to traverse a 64K boundary you needed to increment the segment register 256. Why 256 well the bottom 8 bits of the segment register addressed the same pages as the top 8 bits of the addressing register. 86/186/286 memory addressing is 24 bits.

Now enter Windows 3 into the fray. It used memory banking (NT still does but it does so under wraps). So to allocated memory so that the CPU would force the memory to be swapped into real memory if it was currently banked onto disk, you used GLobalAlloc and GlobalFree. If you used alloc() the memory pointer would return a block in Local Memory block. This was a local heap of memory that was 64kb in size. In fact if you wanted to allocate anything greater than 64K you needed to allocate it in powers of 2, i.e 128K, 256K, etc... Did I also mention that memory allocated had to be divisable by 2, and any memory address access was the same, read 'No odd address access'.

The up side of this memory scheme was that if you used a ;wild' memory ptr and then tried to access the address a GPF (General Protection Fault) would ensue. Great for tracking memory errors.

The advent of 32 bit registers in the 386 increased segment sizes to about 4GB.

So today there is no 'Global Memory' it is all 'Local' i.e. the heap is around 1GB.


I make no apologies for any inaccuracies in this post. I have tried very hard to forget what I once had to go through.



D.V.

Edited by - DeltaVee on November 13, 2001 2:49:35 PM

Share this post


Link to post
Share on other sites
Mayrel    348
quote:
Original post by DeltaVee
Okay boys and girls, gather round the camp fire and let me tell you a story that will scare you $hitless.


Didn''t scare me.
quote:

Once upon a time intel CPU''s where segmented (in fact they still are). Each segment was 64kb in size. So to access memory you needed two registers the Segment register and an other who''s name escapes me for now.


There were Index registers for the specific purpose of indexing memory, but you could also use the general purpose registers for the same.
quote:

The advent of 32 bit registers in the 386 increased segment sizes to about 4GB.


In actuality, it wasn''t just the registers that caused the increase in segment sizes. By default, even your top-of-the-line Pentium4 2Ghz monster starts up in 16-bit segmented mode. The major turning-point was the addition of ''protected-mode'', which was the mode the CPU had to be in in order for the flat memory model to be available.

All your bases belong to us (I know. It''s irony.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Taking a gander throught the src for crtdll supplied with msvc - one sees that on windows malloc is a wrapper for HeapAlloc - using the prog''s 1 Mb default heap - don''t worry it will grow if it needs too.

I''m fairly certain that GlobalAlloc does the same. One way to check would be to use the ToolHelp API to examine the heap state after using each of the available mem allocators.

According to Jeffrey Richter''s "Advanced Windows" - HeapAlloc in turn wraps VirtualAlloc. I can''t confirm this because I don''t have the src for Kernel32 - (I wonder what they might be hiding...) - but it sounds reasonable. At any rate - for large memory allocations (16 Mb + ) use VirtualAlloc et al - or consider memory mapped files. For lots of little allocations, use malloc, GlobalAlloc, HeapAlloc as you want.


Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
All those function trace the development of computing history so to speak - you have DeltaVee''s explanation regarding segmented memory addresses to explain the difference betwee LocalAlloc and GlobalAlloc - they''re still around to provide backwards compatibility - we still want to give our old 16-bit windows progs a chance!

malloc is portable - so if you want to compile your src on Linux (etc) too - use malloc et al - including new and delete when appropriate.

On windows all memory is virtual - the kernel manages the hardware interface and dishes out memory as required. Even memory mapped files use virtual memory. My guess is that VirtualAlloc et al are the "true" memory funcs - but they are also more difficult to use - and thus the others come into play.

Say you''re just writing a quick console utility - having to write a full blown memory manager using VirtualAlloc etc would be quite a lot of work - so it''s much easier to use malloc, HeapAlloc or GlobalAlloc.
This is a general explanation - for more details find a copy of Richter''s book or check out comp.os.ms-windows.programmer.memory


Share this post


Link to post
Share on other sites
ok, let me give you some more info on what im doing,
im allocating image data and 3d mesh vertices,

so it''s pretty sizable data, that is allocated once in the program,

so should i use virtual alloc?

i want to use a function that doesnt end up calling a bunch of other functions that i could have called myself.

Share this post


Link to post
Share on other sites
DeltaVee    138
If it is performance you are worried about you need to ask yourself ''How many times am I allocating memory and freeing it?''.

If you are only doing infrequently, it really doesn''t matter. But if you are diung it in every frame you a) need to rework your code so that this does not happen (it is very inefficient IMHO) or use virtualalloc (it appears to be the root function).

I use malloc/free (well new/delete in reality), I don''t call it alot so it makes no difference to me.

D.V.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Since the operation is only performed once the performance cost of using a wrapper is negligible. I would suggest writing your routine using the easier to use functions first and then if that''s not satisfactory moving on to the more involved functions - VirtualAlloc etc. The definition of satisfactory is left to you.

Share this post


Link to post
Share on other sites
burp    122
I use VirtualAlloc for windows development for two reasons.

1. Its the lowest level memory allocation available in Windows, ie: all other memory allocations will eventually go throw this function.

2. I have control over the type of memory being allocated, ie: PAGE_READONLY, PAGE_READWRITE, PAGE_EXECUTE... This is very useful for debugging and optimization.

I allocate all of my memory at the start of the program, in separate banks depending on how I will be using it. Then I have my own memory allocation function that does the dirty work. Most of these calculations are done at compile time. In some cases the overhead for memory allocation is only 1 cycle. It just wouldn't make sense to use malloc in the inner loop on say a linked list.

Portability is easy because I use abstraction.

quote:

Windows SDK

The following functions are equivalent: GlobalAlloc, LocalAlloc, and HeapAlloc with the handle returned by the GetProcessHeap function.

The VirtualAlloc function allows you to specify additional options for memory allocation. However, its allocations use a page granularity, so using VirtualAlloc can result in higher memory usage.

The malloc function has the disadvantage of being run-time dependent. The new operator has the disadvantage of being compiler dependent and language dependent.




Edited by - burp on November 14, 2001 11:32:38 AM

Share this post


Link to post
Share on other sites
Black Marq    210
I had question like this a week before. I was wondering what
the mood was like on using extended memory management programs
like XMS and DPMI in DOS and Windows environment.

How do you rate these memory management programs? Your opinions
are greatly appreciated.

Thanks,
BM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
If the question is between using "GlobalAlloc" or "LocalAlloc" versus "malloc" or "new", you should pretty much always use "malloc" or "new". This is preferred, not only because it''s portable, but also because it can be a lot faster, and provides some opportunities for leak and access checking. The "malloc" implementation in Visual C++, for example, tends to be a lot faster than the API level allocators, because it uses the API level allocators smartly.

Share this post


Link to post
Share on other sites