I wrote this a few days ago to get around an OS3.x problem.  It allocates a
stack before calling your actual function.  You'd use this by doing long l =
stack_hack(your_intended_function());

I've used this only on CW generated code on OS 3.x.  See if it works on 2.0
and email me directly for comments.  Comments appreciated.

There is also another hack which involves doing a setjmp to get and set the
stack pointer and although that is "hack elegant" I couldn't use it because
my app doesn't have access to globals.

Have fun.


// stackfix.c
#include <PalmOS.h>
#include "stackfix.h"
#include "Util.h"


static asm UInt32 hacko_stacko(UInt8* buff, FN fn);

static asm UInt32 hacko_stacko(UInt8* buff, FN fn)
{
    /*
                      |<--- 4 bytes ----->|
    sp or a7 ----->   [    return address ]        ^
                      [  buff             ]        |  growth direction
                      [  fn               ]
    */
    
    // push a6
    suba.l %4,sp 
    move.l a6,(sp)   // +4
    
    // push a2
    suba.l %4, sp
    move.l a2,(sp)   // + 8


    move.l sp,a6           // save the old stack pointer
    // a6 is the frame pointer, called routine better guarantee
    // that it sets it back to what it was before we called it
    // is this true for gcc generated code?

    movea.l 16(sp), a2     // get target
    movea.l 12(sp),sp     // change stack pointer
    adda.l %STACK_SIZE-4,sp      // YOU ARE AN IDIOT!  the stack grows
downward
    
    jsr (a2)              // call target (jsr reachable?)
    
    move.l a6,sp // restore stack pointer
    
    // pop a2
    move.l (sp),a2
    adda.l %4,sp 

    // pop a6
    move.l (sp),a6 
    adda.l %4,sp 
    rts // bye

    // note: that the return of the called function is already in d0
    // don't touch it so orig caller can get it.
    // is this true for gcc?
}


extern UInt32 stack_hack(FN fn)
{
    UInt32 ret;
    UInt8* buff = MemPtrNew(STACK_SIZE);
    if (!buff)
    {
        show_message(5, "out of heap");
        return -1;
    }
    ret = hacko_stacko(buff, fn);
    MemPtrFree(buff);
    return ret;
}


/// stackfix.h
#ifndef __STACKFIX_H__
#define __STACKFIX_H__


// wrap's function so it can have a big stack size

// FIXME: called function cannot accept parameters
// FIXME: stack size is fixed to STACK_SIZE

// caveat: stack space allocated from the heap
#define STACK_SIZE 6000

// the function prototype
typedef UInt32 (*FN)();

// this is the wrapper, returns the return of fn
extern UInt32 stack_hack(FN fn);

#endif



on 12/7/00 11:51 AM, Chris DiPierro at [EMAIL PROTECTED] arranged
bits to form:

> I'm getting a little annoyed at CW.
> 
> I've got a complex function that does a lot of MathLib double
> manipulation. The function works great on modern OS's (v3)
> but is causing nothing but headaches on OS/2.0 due to
> stack overflows.
> 
> I've tried by the #pragma stack_cleanup on thing as well
> as the app preference, but neither seem to help. CW just leaks
> stack with temporary calculations even if they're not stored to
> local vars.
> 
> Can someone post a way to create my own stack when I enter
> a function using MemPtrNew? I assume this would fix everything.
> 
> 
> 
> 
> 


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to