Missing memory in TBitmap

Writing a program showing large bitmaps I
found that bitmaps larger than 64k allocate
twice as much memory than needed, which in
my case is about 500k.
The problem is that in the graphics unit
memory is allocated using MemAlloc function:

function MemAlloc(Size: Longint): Pointer;
var
  Handle: THandle;
begin
  if Size < 65535 then
    GetMem(Result, Size)
  else
  begin
    Handle := GlobalAlloc(HeapAllocFlags, Size);
    Result := GlobalLock(Handle);
  end;
end;

For size larger than 64k MemAlloc uses WinAPI
functions GlobalAlloc and GlobalLock. So far
everything is Ok, but memory allocated with
this function is deallocated with a Delphi
function FreeMem! (in ReadWin3DIB procedure,
to say one)

.....
BitsMem := MemAlloc(ImageSize);
.....
FreeMem(BitsMem, ImageSize);

Definitely, FreeMem cannot deallocate memory
allcated with GlobalAlloc and does nothing
(though it would be nice if it could raise
an exception).

Fixing this problem takes just several lines,
for example one can add function FreeMem in
the implementation section of the graphics
unit, which looks like its counterpart MemAlloc
and forces all the methods in this unit to
use your FreeMem rather than one from
the system unit:

procedure FreeMem(var P; Size: Longint);
begin
 if Size < 65535 then
    System.FreeMem(Pointer(P), Size)
  else
   GlobalFreePtr(Pointer(P));
end;

Note that Size is now of the type longint,
not word.

Sincerely,

Dmitry Streblechenko (dmit...@phyast.la.asu.edu)