Forum > General

Malloca, does FPC has something like this?

(1/2) > >>

jamie:
Like the title states, does fpc have a stack allocating feature for runtime usages?

440bx:

--- Quote from: jamie on May 31, 2022, 11:42:33 pm ---Like the title states, does fpc have a stack allocating feature for runtime usages?

--- End quote ---
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.

jamie:
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.

440bx:

--- Quote from: jamie on June 01, 2022, 12:27:34 am --- well it's nothing more than moving the stack pointer down the path and returning its starting address.

--- End quote ---
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.)

Thaddy:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version