Recent

Author Topic: Rallocates  (Read 844 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 391
Rallocates
« on: October 24, 2025, 11:36:14 pm »
Is it possible to say how many bytes can grow a pointer before it will be reallocated?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

jamie

  • Hero Member
  • *****
  • Posts: 7407
Re: Rallocates
« Reply #1 on: October 25, 2025, 12:41:19 am »
Whenever it cannot return a contiguous chunk starting at the same location.

You should always plan on this happening, and it depends on the minimum chuck of allocation.

Last time I checked it was around 1024 but that could be wrong.

P.S.
  that is, only if there isn't any memory following of course.


Jamie


« Last Edit: October 25, 2025, 12:42:58 am by jamie »
The only true wisdom is knowing you know nothing

BrunoK

  • Hero Member
  • *****
  • Posts: 756
  • Retired programmer
Re: Rallocates
« Reply #2 on: October 25, 2025, 07:18:36 am »
MemSize(p) where p la pointer

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: Rallocates
« Reply #3 on: October 25, 2025, 07:36:30 am »
The smallest allocation is 16 -real alloc -. 12 will be reported as used, length is stored. Depends on memory manager, though.
E.g. cmem reports 1.
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$endif}
  2. //uses cmem;
  3. var
  4.   mem1,mem2:pointer;
  5. begin
  6.   mem1 := allocmem(1);
  7.   writeln(memsize(mem1));
  8. end.
So: no, you can't really predict re-allocations, because they are internals.
What you can do is adapt a memory manager, like cmem - that is easy- and log the reallocations to analyze how your program behaves. You just write to screen or log from the reallocmem function.

Also note that the heap manager is recently completely rewritten and above is tested with trunk from today.
« Last Edit: October 25, 2025, 07:56:32 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

LV

  • Sr. Member
  • ****
  • Posts: 380
Re: Rallocates
« Reply #4 on: October 25, 2025, 08:05:27 am »

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: Rallocates
« Reply #5 on: October 25, 2025, 11:06:49 am »
Suggested mm:
Code: Pascal  [Select][+][-]
  1. unit mymem;
  2. // works with heaptrc and default memorymanager.
  3. // probably not for others.
  4. interface
  5.  
  6. implementation
  7. const
  8.   signalled = 'realloc:';
  9.  
  10. var
  11.   OldMem,
  12.   NewMem:TMemoryManager;
  13.  
  14.   function MyReAllocMem(var p:pointer;Size:ptruint):Pointer;
  15.   begin
  16.     MyReAllocMem := OldMem.ReAllocmem(p,size);
  17.     writeln(signalled,size,'  ',ptruint(p),' ',memsize(p));
  18.   end;
  19.  
  20. initialization
  21.   GetMemoryManager(OldMem);
  22.   NewMem:=OldMem;
  23.   NewMem.ReAllocMem := @MyReAllocMem;
  24.   SetMemoryManager(NewMem);
  25. finalization;
  26.   SetMemoryManager(OldMem);
  27. end.
Redirect writeln in gui apps to log instead.
Helps you analyze impact of reallocations
« Last Edit: October 25, 2025, 11:12:07 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

runewalsh

  • Full Member
  • ***
  • Posts: 106
Re: Rallocates
« Reply #6 on: October 25, 2025, 07:22:28 pm »
You don’t have any guarantees, any ReallocMem might decide to move. In trunk, if you have a situation like
Code: [Select]
[free 5,000][A 1,000][free 5,000]then any ReallocMem(A, N) will move A to a better place, be it N = 900, or N = 2,000, or even N = 1,000. If ReallocMem(A, 1000) does not decide to move A to a completely different place, it will turn it into
Code: [Select]
[A 1,000][free 10,000]
« Last Edit: October 25, 2025, 07:29:13 pm by runewalsh »

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: Rallocates
« Reply #7 on: October 25, 2025, 07:27:20 pm »
Yes, to my surprise, in trunk even smaller re-allocations to smaller size might trigger a move.
That is usually bad, unless the move is cheaper than alternatives.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018