Recent

Author Topic: [SOLVED] What is the name of the default memory allocator in Free Pascal?  (Read 2148 times)

furious programming

  • Hero Member
  • *****
  • Posts: 863
Free Pascal has a default memory allocator (or memory manager if you prefer) and its implementation is in the heaph.inc file. What kind of allocator is this? Any known algorithm, and if so, what is it called? I checked the documentation and even the sources, but unfortunately I did not find such information.
« Last Edit: May 22, 2024, 01:13:24 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

jamie

  • Hero Member
  • *****
  • Posts: 6310
Re: What is the name of the default memory allocator in Free Pascal?
« Reply #1 on: May 20, 2024, 02:28:04 am »
GetmemoryManager ?

It populates a TmemoryManager record you supply.
The only true wisdom is knowing you know nothing

furious programming

  • Hero Member
  • *****
  • Posts: 863
Re: What is the name of the default memory allocator in Free Pascal?
« Reply #2 on: May 20, 2024, 03:41:10 am »
No, that's not the point. The heaph.inc file contains the following functions:

Code: Pascal  [Select][+][-]
  1. Function  SysGetmem(Size:ptruint):Pointer;
  2. Function  SysFreemem(p:pointer):ptruint;
  3. Function  SysFreememSize(p:pointer;Size:ptruint):ptruint;
  4. Function  SysMemSize(p:pointer):ptruint;
  5. Function  SysAllocMem(size:ptruint):Pointer;
  6. function  SysTryResizeMem(var p:pointer;size:ptruint):Boolean;
  7. Function  SysReAllocMem(var p:pointer;size:ptruint):Pointer;
  8. function  SysGetHeapStatus:THeapStatus;
  9. function  SysGetFPCHeapStatus:TFPCHeapStatus;

They are set as default in the memory manager:

Code: Pascal  [Select][+][-]
  1. const
  2.   MemoryManager: TMemoryManager = (
  3.     NeedLock: false;  // Obsolete
  4.     GetMem: {$ifndef FPC_NO_DEFAULT_HEAP}@SysGetMem{$else}nil{$endif};
  5.     FreeMem: {$ifndef FPC_NO_DEFAULT_HEAP}@SysFreeMem{$else}nil{$endif};
  6.     FreeMemSize: {$ifndef FPC_NO_DEFAULT_HEAP}@SysFreeMemSize{$else}nil{$endif};
  7.     AllocMem: {$ifndef FPC_NO_DEFAULT_HEAP}@SysAllocMem{$else}nil{$endif};
  8.     ReAllocMem: {$ifndef FPC_NO_DEFAULT_HEAP}@SysReAllocMem{$else}nil{$endif};
  9.     MemSize: {$ifndef FPC_NO_DEFAULT_HEAP}@SysMemSize{$else}nil{$endif};
  10.     InitThread: nil;
  11.     DoneThread: nil;
  12.     RelocateHeap: nil;
  13.     GetHeapStatus: {$ifndef FPC_NO_DEFAULT_HEAP}@SysGetHeapStatus{$else}nil{$endif};
  14.     GetFPCHeapStatus: {$ifndef FPC_NO_DEFAULT_HEAP}@SysGetFPCHeapStatus{$else}nil{$endif};
  15.   );

They bodies are in the heap.inc file and contain application-side memory management code to minimize the use of the Windows HeapAlloc and HeapFree functions to increase allocation and deallocation efficiency. Is the code of these functions a port of some well-known allocator that has its own name and is described somewhere (such as mimalloc), or was it invented entirely by Free Pascal developers?
« Last Edit: May 20, 2024, 03:45:43 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Thaddy

  • Hero Member
  • *****
  • Posts: 14849
  • Censorship about opinions does not belong here.
Re: What is the name of the default memory allocator in Free Pascal?
« Reply #3 on: May 20, 2024, 08:05:59 am »
It was written from scratch by the FPC developers, but to a known, common pattern: bucketlist based allocators. The same pattern is e.g. used in Delphi too, with a completely different implementation based on FastMM. I have also seen implementations for C and C++, but in these languages the default is a simple heap based allocator. we can use that too: the implementation is in cmem.pas.
Bucket based allocators are less prone to heap memory fragmentation in the average use case.

In compiler shoot-outs, languages that have bucket based allocators usually have more efficient use of memory sizes, whereas heap based memory allocators favor speed over memory size.
a note of warning, though: the choice of a memory manager may depend on a specific use case.
« Last Edit: May 20, 2024, 08:22:18 am by Thaddy »
Remember the Medway disaster..

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11600
  • FPC developer.
Re: What is the name of the default memory allocator in Free Pascal?
« Reply #4 on: May 20, 2024, 12:02:23 pm »
Afaik the default FPC allocator also has per thread allocation/deallocation to avoid unnecessary locks.  That also has advantages and disadvantages, the positive is that allocating small amounts in a thread requires less locks, But it uses a bit more memory , and cross thread (allocate in one, deallocate in another) becomes more expensive.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6930
Re: What is the name of the default memory allocator in Free Pascal?
« Reply #5 on: May 20, 2024, 06:07:36 pm »
Afaik the default FPC allocator also has per thread allocation/deallocation to avoid unnecessary locks.  That also has advantages and disadvantages, the positive is that allocating small amounts in a thread requires less locks, But it uses a bit more memory , and cross thread (allocate in one, deallocate in another) becomes more expensive.

I believe you are correct, I remember discussing this with Jonas.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

furious programming

  • Hero Member
  • *****
  • Posts: 863
Re: What is the name of the default memory allocator in Free Pascal?
« Reply #6 on: May 22, 2024, 01:13:10 am »
Ok, thanks for the answers. Is it described somewhere so that I can learn how exactly this implementation works?
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5544
  • Compiler Developer
Re: What is the name of the default memory allocator in Free Pascal?
« Reply #7 on: May 22, 2024, 09:37:24 pm »
Is it described somewhere so that I can learn how exactly this implementation works?

If you want to learn how it works, look at the source code.

furious programming

  • Hero Member
  • *****
  • Posts: 863
What else is left if there is no documentation. Thanks.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Thaddy

  • Hero Member
  • *****
  • Posts: 14849
  • Censorship about opinions does not belong here.
Well, the internals, read the sourcecode, the design pattern and theory: google on bucket based memory allocation.
What else is not documented? Bloody irritating since I already explained it.
Remember the Medway disaster..

 

TinyPortal © 2005-2018