22 February 2020, 22:27 | #1 |
titan sucks!
Join Date: Dec 2012
Location: munich/germany
Posts: 54
|
Custom stack size for own application
Hi,
at the moment I'm working on a demo production and I got some places where I use the stack a bit more. The engine is quite complex, so yeah, I'm using more stack than the default stack size. I don't want to rely on the stack size set by the user or the default is 4096 which is really too small. I'm working with bebbos GCC (v6) compiler and libnix which has its own libstack library with stack-checking and -expanding. The variable __stack can only be used if you link against libstack and enable the extensions. But this adds overhead I don't want in my engine. I know there's min_stack but that only works in newer OS versions (and 3.1.4 afaik). So is there some way to *enforce* a stack size for your own application? Thanks. |
22 February 2020, 22:59 | #2 |
Registered User
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
|
Why not allocate as much as you want to use and then set A7 to it?
|
22 February 2020, 23:04 | #3 |
titan sucks!
Join Date: Dec 2012
Location: munich/germany
Posts: 54
|
Yeah but I thought there might be some "standardized" way of doing that ...
|
22 February 2020, 23:04 | #4 |
Registered User
Join Date: Mar 2012
Location: Norfolk, UK
Posts: 1,153
|
If you're working in C, something like this should do what you need: (Needs at least Kickstart 2.04)
Code:
/*************************************************************************** Example code demonstrating use of the exec.library StackSwap() routine from C. Compiled and tested with VBCC. This code will do nothing if the stack is already large enough. ***************************************************************************/ #include <stdio.h> #include <exec/types.h> #include <exec/tasks.h> #include <exec/memory.h> #include <inline/exec_protos.h> /* Important: Do *not* use the Amiga.lib stub for StackSwap()! */ #define STACKSIZE 8192 extern void *SysBase; struct StackSwapStruct MyStackSwap; /* Must be global... */ int MainReturn; /* ...as must this! */ int main2(); int main() { struct Task *me=FindTask(NULL); ULONG currentstack=(ULONG)me->tc_SPUpper-(ULONG)me->tc_SPLower; printf("Current stack: %lu\n",currentstack); if(currentstack<STACKSIZE) { ULONG *MyStack; if(MyStack=AllocVec(STACKSIZE,MEMF_PUBLIC)) { MyStackSwap.stk_Lower=MyStack; MyStackSwap.stk_Upper=(ULONG)MyStack+STACKSIZE; MyStackSwap.stk_Pointer=(void *)(MyStackSwap.stk_Upper-16); StackSwap(&MyStackSwap); /* From here until the second StackSwap() call, the local variables within this function are off-limits (unless your compiler accesses them through a register other than A7 with a stack-frame) */ MainReturn=main2(); StackSwap(&MyStackSwap); /* Now locals can be accessed again */ FreeVec(MyStack); } else printf("Error: Can't allocate new stack!\n"); return(MainReturn); } else return(main2()); } int main2() { struct Task *me=FindTask(NULL); ULONG newstack=(ULONG)me->tc_SPUpper-(ULONG)me->tc_SPLower; printf("New stack: %lu\n",newstack); return(0); } |
22 February 2020, 23:09 | #5 |
move.l #$c0ff33,throat
Join Date: Dec 2005
Location: Berlin/Joymoney
Posts: 6,863
|
You could run your demo as own task (Exec, AddTask() ), this would allow you to set the stack size yourself (tc_SPLower and tc_SPUpper and in the task structure).
|
22 February 2020, 23:13 | #6 |
titan sucks!
Join Date: Dec 2012
Location: munich/germany
Posts: 54
|
Nice suggestions, guys. Thanks I'll try them
|
22 February 2020, 23:31 | #7 |
Registered User
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
|
When you modify the task stack variables, shouldn't you disable multitasking and interrupts? Or is the supervisor stack used when a taskswitch or interrupt occurs?
|
23 February 2020, 00:34 | #8 |
Registered User
Join Date: Mar 2012
Location: Norfolk, UK
Posts: 1,153
|
They should be disabled while the actual swap takes place (and while its documentation doesn't explicitly say so, I'd be astonished if the OS StackSwap() function doesn't take care of that), but once the deed is done the system can continue as normal. Even if the system does write to the user stack, it will only write downwards - it won't read or manipulate anything that was written to the stack previously by the program. As long as the original stack is restored before control flow leaves the function that called StackSwap() it should all work fine.
|
23 February 2020, 19:41 | #9 |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,216
|
The SwapStack exec function is fine and takes care of race conditions. It is in general simpler just to use a $STACK cookie in the binary, or set a STACK entry in the icon of the program. AddTask() is typically not a smart idea is it makes the created task only a task, and as such, it cannot use many dos.library function. CreateNewProc() may be used, but is generally overshooting.
|
23 February 2020, 22:54 | #10 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
|
14 October 2022, 00:37 | #11 | |
Registered User
Join Date: Jan 2005
Location: Umeå
Age: 43
Posts: 922
|
Quote:
|
|
14 October 2022, 01:49 | #12 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,039
|
Maybe in case a compiler is not aware of stack swapping, and it tries to do something funny with locals (3 variables + return address = (3+1)*4=16) behind the scenes, it won't mess it up?
edit: Motors won't turn off :P, maybe not return address but a C's beloved link instruction which allocates 4b on the stack. Too used to asm, didn't think of that right away. Last edited by a/b; 14 October 2022 at 02:21. |
14 October 2022, 02:10 | #13 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,039
|
OK, the reply went thought (I was getting 502 bad gateway for ~20min). This made me curious, so I google it in the meanwhile. Yeah, something to that tune: the AmigaOS StackSwap function isn't designed to be reliably used from C code.
|
14 October 2022, 04:02 | #14 |
Registered User
Join Date: May 2021
Location: Los Angeles / USA
Posts: 135
|
I'm with Thomas on this, set in Icon info and be done with it.
Chris |
14 October 2022, 17:47 | #15 | |
Registered User
Join Date: Jan 2005
Location: Umeå
Age: 43
Posts: 922
|
Quote:
Need it to work for CLI commands from 2.04+. |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Supervisor stack size? | alpine9000 | Coders. System | 71 | 09 June 2019 01:36 |
Stack position and size | hop | Coders. Asm / Hardware | 2 | 26 May 2019 18:20 |
display a picture with custom size? | fstarred | Coders. Asm / Hardware | 12 | 02 May 2018 18:07 |
I'm looking for an application | Narf the Mouse | request.Apps | 33 | 27 May 2011 20:05 |
WHDLoad and its application... | MethodGit | support.Apps | 42 | 26 August 2001 04:50 |
|
|