Re: Static memory allocation

From: Bob Masta (NoSpam_at_daqarta.com)
Date: 07/04/04


Date: Sun, 4 Jul 2004 18:15:21 +0000 (UTC)

On Sat, 3 Jul 2004 07:34:26 +0000 (UTC), "Matt Taylor"
<spamtrap@crayne.org> wrote:

>"Bob Masta" <NoSpam@daqarta.com> wrote in message
>news:40e54d15.490659@news.itd.umich.edu...
>> I am wondering about the differences between
>> allocating uninitialized memory in a .data? (BSS)
>> section versus Windows GlobalAlloc. The former
>> seems better in every way I can think of, but my
>> understanding of protected mode memory management
>> is lacking. In particular, allocating several large
>> blocks via BSS makes it appear they require
>> contiguous memory, whereas GlobalAlloc can
>> use any memory it can find. If that were true,
>> there might be some (unusual) fragmented memory
>> circumstance where the BSS program couldn't load,
>
>This seems unlikely unless you are dealing with a very large program. By
>very large I am referring to something larger than 1.5 GB when mapped into
>virtual memory.
>
>When a process is created, at least on NT systems, a file mapping object is
>allocated backed by the initial executable file. Initially the process is
>just an empty PEB structure with roughly 2 GB of free user address space and
>an associated file mapping object. The initial file mapping object is, I
>believe, the first executable that gets mapped into the newly created
>process. If not, then it is right after critical system DLLs are mapped. At
>this point in time the address space is unfragmented, so if a .exe file with
>BSS cannot load, then surely it GlobalAlloc will simply result in a runtime
>failure.
>
>> but the GlobalAlloc program could. However,
>> this wouldn't be an issue if the BSS memory
>> could be pieced together from separate chunks
>> via the memory manager at load time. Does it
>> work that way?
>
>PE sections must be contiguous. PE files themselves must be contiguous in
>virtual memory, though under certain circumstances you can create gaps
>between sections.
>
>> Or is there *any* reason to prefer GlobalAlloc
>> for memory that is used for the life of the program?
>
>None whatsoever. It is already highly unlikely that a PE file would be
>unable to load in 2 GB of address space. Collisions are solved by using
>relocation data (/FIXED:NO switch to the MS linker).
>
>For that matter, I wouldn't use GlobalAlloc at all. GlobalAlloc(x) is nearly
>synonymous with HeapAlloc(GetProcessHeap(), 0, x), the difference being that
>the former has extra overhead and headache. It is purely a carry-over from
>the Win16 days, and the moveable/discardable flags are completely ignored by
>the virtual memory system.
>
>-Matt
>

Mat, thanks for your reply. But I'm still unclear on this.
I understand that PE sections are contiguous in virtual
memory, but I am wondering about physical memory.
If physical memory is fragmented (for whatever reason),
can the OS piece together multiple fragments and make
them appear as contiguous virtual memory at load time?

My thought was that if the OS required contiguous physical
memory at load time, then there might be a benefit to
making the load image smaller and later allocating
memory that had no restrictions on it physical location
with GlobalAlloc.

If the OS can use fragmented phsical memory at load
time to make a contiguous image, then I understand
that there would be no point to later GlobalAllocs.

Thanks!

Bob Masta
dqatechATdaqartaDOTcom
 
            D A Q A R T A
Data AcQuisition And Real-Time Analysis
           www.daqarta.com



Relevant Pages

  • Re: HANDLE vs. POINTER
    ... As I understand it GlobalHandle gets the Handle of a block of memory ... with API level memory allocation ... Memory objects allocated by GlobalAlloc and LocalAlloc are in private, ... Applications requiring shared memory for other purposes must use ...
    (microsoft.public.vb.winapi)
  • Re: virtual memory question
    ... User Accessible Memory obtained with VirtualAlloc and/ot GlobalAlloc ... > The API hooks were the problem and not the WH_ message hooks. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Static memory allocation
    ... > section versus Windows GlobalAlloc. ... > understanding of protected mode memory management ... When a process is created, at least on NT systems, a file mapping object is ... BSS cannot load, then surely it GlobalAlloc will simply result in a runtime ...
    (comp.lang.asm.x86)
  • <char*> data with process-to-process messages
    ... SendMessage(HWND_BROADCAST, MYMESSAGE, (WPARAM)buf, NULL); ... the message data (I suppose this is due to memory beeing virtually adressed ... I've tried to alloc the memory globally (via GlobalAlloc) and pass the ... data as an atom as pass the atom handle. ...
    (microsoft.public.vc.language)
  • Re: Memory limit reached with Windows Mobile
    ... That would explain the memory problem - you just can't do that. ... You have to load stuff when it's ... All native DLLs get loaded ...
    (microsoft.public.pocketpc.developer)