English Amiga Board


Go Back   English Amiga Board > Coders > Coders. System

 
 
Thread Tools
Old 22 February 2020, 22:27   #1
neoman
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.
neoman is offline  
Old 22 February 2020, 22:59   #2
sparhawk
Registered User
 
sparhawk's Avatar
 
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?
sparhawk is offline  
Old 22 February 2020, 23:04   #3
neoman
titan sucks!
 
Join Date: Dec 2012
Location: munich/germany
Posts: 54
Yeah but I thought there might be some "standardized" way of doing that ...
neoman is offline  
Old 22 February 2020, 23:04   #4
robinsonb5
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);
}
robinsonb5 is offline  
Old 22 February 2020, 23:09   #5
StingRay
move.l #$c0ff33,throat
 
StingRay's Avatar
 
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).
StingRay is offline  
Old 22 February 2020, 23:13   #6
neoman
titan sucks!
 
Join Date: Dec 2012
Location: munich/germany
Posts: 54
Nice suggestions, guys. Thanks I'll try them
neoman is offline  
Old 22 February 2020, 23:31   #7
sparhawk
Registered User
 
sparhawk's Avatar
 
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?
sparhawk is offline  
Old 23 February 2020, 00:34   #8
robinsonb5
Registered User
 
Join Date: Mar 2012
Location: Norfolk, UK
Posts: 1,153
Quote:
Originally Posted by sparhawk View Post
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?
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.
robinsonb5 is offline  
Old 23 February 2020, 19:41   #9
Thomas Richter
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.
Thomas Richter is online now  
Old 23 February 2020, 22:54   #10
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Untested.

https://groups.google.com/d/msg/comp...s/PVXxvKyaDzoJ
alkis is offline  
Old 14 October 2022, 00:37   #11
patrik
Registered User
 
patrik's Avatar
 
Join Date: Jan 2005
Location: Umeå
Age: 43
Posts: 922
Quote:
Originally Posted by robinsonb5 View Post
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);
}
May I ask why 16 is subtracted from stk_Upper when setting the initial stk_Pointer?
patrik is offline  
Old 14 October 2022, 01:49   #12
a/b
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.
a/b is online now  
Old 14 October 2022, 02:10   #13
a/b
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.
a/b is online now  
Old 14 October 2022, 04:02   #14
QuikSanz
Registered User
 
QuikSanz's Avatar
 
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
QuikSanz is offline  
Old 14 October 2022, 17:47   #15
patrik
Registered User
 
patrik's Avatar
 
Join Date: Jan 2005
Location: Umeå
Age: 43
Posts: 922
Quote:
Originally Posted by a/b View Post
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.
The code above generously shared by @robinsonb5 works very well, have not had any issues with it. Would be interesting to know if there is an unsafe aspect of it even if used in this way. Anyone have any info on this?

Quote:
Originally Posted by QuikSanz View Post
I'm with Thomas on this, set in Icon info and be done with it.
Need it to work for CLI commands from 2.04+.
patrik is offline  
 


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 16:21.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.16027 seconds with 15 queries