[comp.sys.amiga.tech] stack setting?

ric@hpspdra.HP.COM (Ric Peregrino) (07/03/90)

I have ported a wave editor program to the amiga and found that the 
default stack of 4000 is too small. I tried using a system call in
the program to automatically set the stack, but it seems to fail.
Anybody know why this is, or how it can be done? 

#include <stdio.h>
#include <stdlib.h>

void main(void) {

printf("setting stack to 8192\n");
system("stack 8192");
system("stack");
}

the second call to stack with no argument returns the current stack
size: 4000. 

Thanks for any help,
ric@hpspd

kevin@cbmvax.commodore.com (Kevin Klop) (07/03/90)

In article <13580002@hpspdra.HP.COM> ric@hpspdra.HP.COM (Ric Peregrino) writes:
>I have ported a wave editor program to the amiga and found that the 
>default stack of 4000 is too small. I tried using a system call in
>the program to automatically set the stack, but it seems to fail.
>Anybody know why this is, or how it can be done? 
>
>#include <stdio.h>
>#include <stdlib.h>
>
>void main(void) {
>
>printf("setting stack to 8192\n");
>system("stack 8192");
>system("stack");
>}
>
>the second call to stack with no argument returns the current stack
>size: 4000. 
>
>Thanks for any help,
>ric@hpspd

the ``stack'' command basically means:

set the stack size for this process and all children processes to x.

the system() call means:

start a new process and have it execute the following command.

Thus, system("stack 8192"); means

start a new process, change that process' stack size to 8192, then
exit that process.

Note that this does NOT affect the stack size of the process ISSUING the
system() call.

You are pretty much limited to setting the stack size to whatever
you wish BEFORE you run the program, OR setting up your own private
stack inside the program (basically, allocating a large chunk of memory,
pointing A7 at it, and then moving A7 back to its original position before
exiting).

				-- Kevin --


Kevin Klop		{uunet|rutgers|amiga}!cbmvax!kevin
Commodore-Amiga, Inc.

	``Be excellent to each other.''
		- Bill and Ted's most excellent adventure

Disclaimer: _I_ don't know what I said, much less my employer.

peterk@cbmger.UUCP (Peter Kittel GERMANY) (07/03/90)

In article <13580002@hpspdra.HP.COM> ric@hpspdra.HP.COM (Ric Peregrino) writes:
>I have ported a wave editor program to the amiga and found that the 
>default stack of 4000 is too small. I tried using a system call in
>the program to automatically set the stack, but it seems to fail.
>Anybody know why this is, or how it can be done? 
>
>#include <stdio.h>
>#include <stdlib.h>
>
>void main(void) {
>
>printf("setting stack to 8192\n");
>system("stack 8192");
>system("stack");
>}
>
>the second call to stack with no argument returns the current stack
>size: 4000. 

Sorry, I only can tell you why this failed (ran into similar things
myself): Invoking system makes up a new CLI for this very command and
there the stack size is in fact increased. But then this one is closed
and the effect is gone before the next statement (that opens a CLI for
itself again).

The solution I use for similar situations is to start applications
only through script files (plus icon and IconX for convenience) that
contain only two lines:
    newcli con:... from file.s
    endcli
(or newshell), where file.s contains the stack command and the call
for your application. This way you have _two_ additional windows on
the screen at least for short time, but that's not so bad (you can
give them identical dimensions and positions, so you hardly notice).


-- 
Best regards, Dr. Peter Kittel      //     E-Mail to 
Commodore Frankfurt, Germany      \X/      rutgers!cbmvax!cbmger!peterk

nfs1675@dsacg3.dsac.dla.mil ( Michael S Figg) (07/04/90)

In article <13580002@hpspdra.HP.COM>, ric@hpspdra.HP.COM (Ric Peregrino) writes:
> I have ported a wave editor program to the amiga and found that the 
> default stack of 4000 is too small. I tried using a system call in
> the program to automatically set the stack, but it seems to fail.
> Anybody know why this is, or how it can be done? 
> 
> printf("setting stack to 8192\n");
> system("stack 8192");
> system("stack");
> 
> ric@hpspd

My experience is alittle limited but I've been studying this lately. I believe
that 'stack' only sets the stack for the current process and each of your
'system' calls is a seperate process besides your program itself. You could
set it first from the shell or have a script driving your wave editor which
would set the stack first. The stack is only memory for your program to use so
you should also be able to do a 'malloc' but I'm not sure how you would 
allocate this just for the stack. 

Am I making any sense here? Could someone explain this better so I understand
it also!

---Mike


-- 
A man said to the Universe "Sir, I exist!" | Michael Figg  DSAC-FSD
"However," replied the Universe,           | DLA Systems Automation Center
"The fact has not created in me a          | Columbus, Ohio
sense of obligation."- Stephen Crane       | mfigg@dsac.dla.mil  CIS: 73777,360

jbickers@templar.actrix.co.nz (John Bickers) (07/04/90)

Quoted from - peterk@cbmger.UUCP (Peter Kittel GERMANY):
> In article <13580002@hpspdra.HP.COM> ric@hpspdra.HP.COM (Ric Peregrino) writes:
> >I have ported a wave editor program to the amiga and found that the 
> >default stack of 4000 is too small. I tried using a system call in
> >the program to automatically set the stack, but it seems to fail.
> >Anybody know why this is, or how it can be done? 
                                    [...]
> Sorry, I only can tell you why this failed (ran into similar things
> myself): Invoking system makes up a new CLI for this very command and

    One Bryce Nesbitt wrote some code a while back to do this sort of
    thing. The stack size can be found by examining your task structure.

    So one possible way to treat this is to detect that the stack is too
    small, and error out, with something like:

        "Sorry bud(ette), your stack is too small."

    Or you can reset your stack yourself. He wrote a little function to do
    this, that I have adjusted a little to work with Lattice C and keep
    stack checking.

    Hm. Decisions decisions. 127 lines. What the heck...
                -------------------- 8< --------------------
;**************************************************************************
;***                              stkset.a                (JJB TEMPLAR) ***
;*** Date begun: 3/7/89.                                                ***
;*** Last modified: 3/7/89.                                             ***
;**************************************************************************
;*** This module is cut directly from AmigaLine7 (Bryce Nesbitt). See   ***
;*** his documentation below for particulars. However, I have added     ***
;*** some code to also 'tickle' the __base value used by Lattice stack  ***
;*** checking.                                                          ***
;*** Problem: should allocation of the new stack memory be done here    ***
;*** instead of in the caller?                                          ***
;**************************************************************************

LATTICE     EQU     1       ; Comment out if don't want __base involved

;---------------------------------------------------------------------------
;NAME
;    SetStack -- use a different process stack
;    V1.0 Tuesday 19-Jan-88 04:14:15 By Bryce Nesbitt
;
;SYNOPSIS
;    SetStack(newstack,size)
;         a1       d0
;
;    char *newstack;
;    long size;
;
;FUNCTION
;    This function points the current process' stack to a new memory
;    location.   The active part of the old stack is duplicated, so no
;    context is lost.  SetStack() also tickles the proper fields in the task
;    structure to tell the system about the change.  At this time it does
;    not deallocate the old stack.
;
;USAGE
;    Typically this is used by a process that needs a larger stack than it
;    might have been started with.  The stack size can be found with:
;
;    #include "libraries/dosextens.h"
;    ...
;    stack=*(ULONG *)( ((struct Process *)FindTask(0L))->pr_ReturnAddr );
;
;    Or in English, the long word at the address pointed to by pr_ReturnAddr.
;
;WARNINGS
;    A future revision of exec may do stack blowout checking every time a
;    process/task is switched out.  This protective feature will not be
;    available to programs that use funky methods of swapping stacks.
;
;    This only works for processes, tasks are not eligible.
;
;    Don't skimp on stack... you'll get burned.  Any call to the dos.library,
;    for example, takes just under 1600 bytes *minimum*.
;
;INPUTS
;    newstack - Pointer to memory block for stack.  Must be longword aligned.
;    size -   The size of the block.   Must be an even multiple of 4 bytes.
;
;SEE ALSO
;    AddTask()

;
;SetStack()
;V1.0 Tuesday 19-Jan-88 04:14:15 (C)1988 Bryce Nesbitt
;1712 Marin Ave.  Berkeley, Ca  94707-2206 415-524-2110
;ucbvax!hoser!bryce  -or-  bryce@hoser.berkeley.EDU
;
;Structure of stack:
;
;    -------
;    -4 size
;    -8 finalRTS
;     ...
;     -size blowoutdata     ;For checking stack blowouts
;**************************************************************************
            csect   text

            XDEF    _SetStack
            XDEF    SetStack

        IFD     LATTICE
            XREF    __base              ; offset for stack checking base
                                        ; stack is checked with:
                                        ;   cmpa.l  __base(a4),a7
                                        ;   bcs.w   __xcovf
        ENDC

_SetStack:  move.l  4(a7),a1
            move.l  8(a7),d0
SetStack:   movem.l d2/a2-a4/a6,-(a7)   ; Assembler entry (params = reg)
                                        ; NOT @function compatible

        IFD     LATTICE
            move.l  a1,__base(a4)       ; set __base before a4 used
        ENDC

            move.l  d0,a0
            adda.l  a1,a0               ;a1+size=a0

            move.l  4,a6                ;exec library
            move.l  $114(a6),a3         ;Internal "magic";FindTask(0L);
            lea.l   $b0(a3),a2          ;&pr_ReturnAddr

            move.l  (a2),a3             ;Get pr_ReturnAddr
            move.l  (a3)+,d1            ;Get old stack size. a3+...
            move.l  a3,d2               ;...now points to old stack top
            move.l  a0,a4
copystack:  move.w  -(a3),-(a4)
            cmp.l   a3,a7
            bne.s   copystack

            move.l  d0,-4(a0)           ;Set new stack size value
            move.l  d2,a3
            sub.l   d1,a3
            move.l  (a3),(a1)           ;Copy bottom long

            addq.b  #1,$127(a6)         ;Internal "magic"
            move.l  a0,(a2)
            subq.l  #4,(a2)             ;pr_ReturnAddr is 4 under top...
            sub.l   a7,d2               ;Get old stack offset
            sub.l   d2,a0               ;index into new stack
            move.l  a0,a7
            jsr     -$138(a6)           ;Public Permit();

            movem.l (a7)+,d2/a2-a4/a6
            rts
            END
                -------------------- 8< --------------------

    Assembles ok with Lattice's asm program.

> Best regards, Dr. Peter Kittel      //     E-Mail to 

    Adios.
--
*** John Bickers, TAP, NZAmigaUG.         jbickers@templar.actrix.co.nz ***
***           "Man, Machine -> Super Human Being" - Kraftwerk.          ***

jesup@cbmvax.commodore.com (Randell Jesup) (07/05/90)

In article <13580002@hpspdra.HP.COM> ric@hpspdra.HP.COM (Ric Peregrino) writes:
>I have ported a wave editor program to the amiga and found that the 
>default stack of 4000 is too small. I tried using a system call in
>the program to automatically set the stack, but it seems to fail.
>Anybody know why this is, or how it can be done? 

	Simple.  Calls to system() (or Execute()) cause a program to be
run on another process.  So they set the stacksize for that other process,
not yours.

	Changing your own stack is a bit more complicated.  It's pretty easy
in assembler, but in C you have to be more careful, especially with
exit() and stack-checking.

-- 
Randell Jesup, Keeper of AmigaDos, Commodore Engineering.
{uunet|rutgers}!cbmvax!jesup, jesup@cbmvax.cbm.commodore.com  BIX: rjesup  
Common phrase heard at Amiga Devcon '89: "It's in there!"

BAXTER_A@wehi.dn.mu.oz (07/07/90)

In article <13009@cbmvax.commodore.com>, kevin@cbmvax.commodore.com (Kevin Klop) writes:
> In article <13580002@hpspdra.HP.COM> ric@hpspdra.HP.COM (Ric Peregrino) writes:
>>I have ported a wave editor program to the amiga and found that the 
>>default stack of 4000 is too small. I tried using a system call in
>>the program to automatically set the stack, but it seems to fail.
>>Anybody know why this is, or how it can be done? 
>>
>>#include <stdio.h>
>>#include <stdlib.h>
>>
>>void main(void) {
>>
>>printf("setting stack to 8192\n");
> start a new process and have it execute the following command.
> 
> Thus, system("stack 8192"); means
> 
> start a new process, change that process' stack size to 8192, then
> exit that process.
> 
> Note that this does NOT affect the stack size of the process ISSUING the
> system() call.


Note however that you CAN use the Lattice functio Execute like this:

Execute("stack 8192\n programname");

(with the other arguments, which escape my mind at the moment)

Regards Alan