Recent

Author Topic: Malloca, does FPC has something like this?  (Read 543 times)

jamie

  • Hero Member
  • *****
  • Posts: 4763
Malloca, does FPC has something like this?
« on: May 31, 2022, 11:42:33 pm »
Like the title states, does fpc have a stack allocating feature for runtime usages?

The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 3028
Re: Malloca, does FPC has something like this?
« Reply #1 on: June 01, 2022, 12:07:09 am »
Like the title states, does fpc have a stack allocating feature for runtime usages?
TTBOMK, it doesn't but, under Windows and, using just a few lines of Assembler, it can be implemented with a moderate amount of care using ntdll's __chkstk (64bit) and _chkstk (32bit)  Internally, __chkstk is named "alloca_probe". MS' C/C++ compiler uses it to ensure there is enough space for the locals in a function.

Unfortunately, it does require the programmer to do the necessary manual adjustments to the stack pointer but, using some Assembly- free "tricks" (dirty tricks!) that can be avoided if the function/procedure does _not_ call any other function (except the "trick" function" upon entry.), IOW, a leaf function.

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 4763
Re: Malloca, does FPC has something like this?
« Reply #2 on: June 01, 2022, 12:27:34 am »
humm.

 well it's nothing more than moving the stack pointer down the path and returning its starting address.

 upon exit, the stack pointer is simply reset to proper value. No memory handling overhead and it makes for a faster working program where this
code is getting called at high rates.

   I have done ASM in the block to reset the stack pointer and there is a significant speed increase with the code I have but it would be nice to have an intrinsic call to I wouldn't have to get out the ASM manual for each target.
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 3028
Re: Malloca, does FPC has something like this?
« Reply #3 on: June 01, 2022, 12:45:45 am »
well it's nothing more than moving the stack pointer down the path and returning its starting address.
It's not that simple because if the stack pointer is moved down too far, more than 4K, it will skip the guard page and accesses below the guard page will cause a memory violation.

The only time, moving the stack pointer is "good enough" is if the allocated size is less than 4K (Windows' page size), if the stack pointer is moved down further a memory violation is likely to happen (depends on what addresses are accessed first.)
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 11933
Re: Malloca, does FPC has something like this?
« Reply #4 on: June 01, 2022, 06:33:01 am »
Look for class on stack here somewhere. I linked in (sic) C's _Alloca for that many moons ago.
There is also another example that uses xmm64 code by someone else.
It is also possible in pure Pascal btw. The system unit has some helpful provisions for that, though not directly.
« Last Edit: June 01, 2022, 06:34:50 am by Thaddy »
Black themes should be banned.

PascalDragon

  • Hero Member
  • *****
  • Posts: 4569
  • Compiler Developer
Re: Malloca, does FPC has something like this?
« Reply #5 on: June 01, 2022, 09:03:14 am »
Like the title states, does fpc have a stack allocating feature for runtime usages?

First of the title says “malloca” which is a mixture of “malloc” and “alloca”, so the title alone is not necessarily clear :P

Second there was a recent discussion about this on fpc-pascal. To sum it up: no, FPC does not support that, especially not in a cross platform way.

 

TinyPortal © 2005-2018