Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-10

Code conversion question - alloc and dealloc memory

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

I'm am having problems allocating memory and then populating that memory with a TFileStream.Read() call. So I thought, maybe my GetMem() call is incorrect. I then looked at the original code I am porting from, which had these methods instead. If I get this memory allocation correct, I'll see if it is needed to ask a question abotu the TFileStream.Read() issue I have.

Do I need these methods, or does the standard GetMem() & FreeMem() in FPC handle all this for me. What if I don't pass in a size to FreeMem(), will it know how much to free?

Please note that this code DOES NOT compile on my 64bit system, as there is no "pLong" type defined. I tried replacing pLong with PtrInt & longint with Integer (for 64bit support), but then the compiler complains and says the dereference statements are invalid.

Looking at the code I understand this as follows - please correct me if I am wrong. When it allocates memory, it actually allocates the size as Size + sizeof(Size) bigger and then uses the first area in memory to store the "size" allocated, so that it can be retrieved later when deallocating the memmory and can find out exactly by how much it must deallocate.

------------------------
function AllocateMemory( const Size: longint ): pointer;
begin
  GetMem( Result, size + sizeof( Size ) );
  pLong( Result )^ := Size;
  inc( Result, sizeof( Size ) );
end;
procedure DeallocateMemory( Var P: pointer );
var
  Size: longint;
begin
  if P = nil then
    exit;
  dec( P, sizeof( Size ) );
  Size := pLong( P )^;
  FreeMem( P, Size + sizeof( Size ) );
  P := nil;
end;

------------------------

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/
Graeme Geldenhuys
to Graeme Geldenhuys: I'm am having problems allocating memory and then populating that memory with a…
Graeme Geldenhuys wrote 5 lines
------------------------ function AllocateMemory( const Size: longint ): pointer; procedure DeallocateMemory( Var P: pointer );

OK, I managed to convert those two functions to the following.. it now compiles under my 64bit FPC.

-----------------------------
function AllocateMemory( const Size: ValUInt ): pointer;
begin
  GetMem( Result, size + sizeof( Size ) );
  PtrUInt(Result^) := Size;
  inc( Result, sizeof( Size ) );
end;
procedure DeallocateMemory( Var P: pointer );
var
  Size: ValUInt;
begin
  if P = nil then
    exit;
  dec( P, sizeof( size ) );
  Size := ValUInt(P^);
  FreeMem( P, Size + sizeof( Size ) );
  P := nil;
end;
-----------------------------

But based on Florian's reply, I assume I don't need to bother using these methods, because FreeMem() will automatically know much much memory to deallocate.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/
Graeme Geldenhuys
to Florian Klaempfl: Yes (as long as you don't mess with internal heap structures ;)).
Florian Klaempfl wrote
Yes (as long as you don't mess with internal heap structures ;)).

OK, thanks. So that means I don't need to use the custom AllocateMemory() and DeallocateMemory() functions - no need to track the original size of memory allocated.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/
Michael Van Canneyt
to Graeme Geldenhuys: OK, I managed to convert those two functions to the following.. it now compiles under my…
Graeme Geldenhuys wrote 35 lines
On 15/10/2009, Graeme Geldenhuys wrote: > > ------------------------ > function AllocateMemory( const Size: longint ): pointer; > > procedure DeallocateMemory( Var P: pointer ); OK, I managed to convert those two functions to the following.. it now compiles under my 64bit FPC. ----------------------------- function AllocateMemory( const Size: ValUInt ): pointer; begin GetMem( Result, size + sizeof( Size ) ); PtrUInt(Result^) := Size; inc( Result, sizeof( Size ) ); end; procedure DeallocateMemory( Var P: pointer ); var Size: ValUInt; begin if P = nil then exit; dec( P, sizeof( size ) ); Size := ValUInt(P^); FreeMem( P, Size + sizeof( Size ) ); P := nil; end; ----------------------------- But based on Florian's reply, I assume I don't need to bother using these methods, because FreeMem() will automatically know much much memory to deallocate.

Yes.

Michael.